36 using handle_type = FutureEvent::handle_type ;
44 static bool send( handle_type ,
bool ) noexcept ;
50 handle_type handle() noexcept ;
60 void readEvent()
override ;
63 static int init(
int ) ;
69 ~Fd() {
if(fd!=-1) ::close(fd) ; }
70 Fd & operator=(
int fd_ ) { fd = fd_ ;
return *this ; }
72 Fd(
const Fd & ) = delete ;
73 Fd( Fd && ) = delete ;
74 void operator=(
const Fd & ) = delete ;
75 void operator=( Fd && ) = delete ;
89 std::array<int,2U> fds {{ -1 , -1 }} ;
90 int rc = ::socketpair( AF_UNIX , SOCK_DGRAM , 0 , &fds[0] ) ;
92 throw FutureEvent::Error(
"socketpair" ) ;
93 m_read = init( fds[0] ) ;
94 m_write = init( fds[1] ) ;
98int GNet::FutureEventImp::init(
int fd )
100 GDEF_IGNORE_RETURN ::fcntl( fd , F_SETFL , ::fcntl(fd,F_GETFL) | O_NONBLOCK ) ;
104GNet::FutureEventImp::~FutureEventImp()
113GNet::FutureEventImp::handle_type GNet::FutureEventImp::handle() noexcept
116 std::swap( m_write.fd , fd ) ;
117 return static_cast<handle_type
>(fd) ;
120void GNet::FutureEventImp::receive()
123 GDEF_IGNORE_RETURN ::recv( m_read.fd , &c , 1 , 0 ) ;
126bool GNet::FutureEventImp::send( handle_type handle ,
bool close )
noexcept
128 int fd =
static_cast<int>(handle) ;
133 const bool ok = rc == 1 ;
137void GNet::FutureEventImp::readEvent()
143 m_handler.onFutureEvent() ;
159 return FutureEventImp::send( handle , close ) ;
164 return m_imp->handle() ;
A base class for classes that handle asynchronous events from the event loop.
virtual void dropRead(Descriptor fd) noexcept=0
Removes the given event source descriptor from the list of read sources.
static bool exists()
Returns true if an instance exists.
static EventLoop & instance()
Returns a reference to an instance of the class, if any.
virtual void addRead(Descriptor fd, EventHandler &, ExceptionSink)=0
Adds the given event source descriptor and associated handler to the read list.
A tuple containing an ExceptionHandler interface pointer and a bound 'exception source' pointer.
A callback interface for GNet::FutureEvent.
A pimple-pattern implementation class used by GNet::FutureEvent.
FutureEvent(FutureEventHandler &, ExceptionSink)
Constructor. Installs itself in the event loop.
handle_type handle() noexcept
Extracts a handle that can be passed between threads and used in send().
static bool send(handle_type handle, bool close=true) noexcept
Pokes an event into the main event loop so that the FutureEventHandler callback is called asynchronou...
~FutureEvent()
Destructor.
static ssize_t send(SOCKET, const void *, std::size_t, int flags) noexcept
A send() replacement using sendmsg().