25 #include <sys/types.h>
35 typedef FutureEvent::Error Error ;
36 typedef FutureEvent::handle_type handle_type ;
44 static bool send( handle_type ) g__noexcept ;
50 handle_type handle() ;
56 static int init(
int ) ;
57 virtual void readEvent() ;
73 int rc = ::socketpair( AF_UNIX , SOCK_DGRAM , 0 , fds ) ;
75 throw Error(
"socketpair" ) ;
76 m_fd_read = init( fds[0] ) ;
77 m_fd_write = init( fds[1] ) ;
81 int GNet::FutureEventImp::init(
int fd )
83 int rc = ::fcntl( fd , F_SETFL , ::fcntl(fd,F_GETFL) | O_NONBLOCK ) ; G_IGNORE_VARIABLE(rc) ;
87 GNet::FutureEventImp::~FutureEventImp()
93 ::close( m_fd_read ) ;
97 ::close( m_fd_write ) ;
101 GNet::FutureEventImp::handle_type GNet::FutureEventImp::handle()
103 int fd = m_fd_write ;
105 return static_cast<handle_type
>(fd) ;
108 void GNet::FutureEventImp::receive()
111 ssize_t rc = ::recv( m_fd_read , &c , 1 , 0 ) ;
112 G_IGNORE_VARIABLE( rc ) ;
115 bool GNet::FutureEventImp::send( handle_type handle ) g__noexcept
117 int fd =
static_cast<int>(handle) ;
121 const bool ok = rc == 1 ;
125 void GNet::FutureEventImp::readEvent()
131 m_handler.onFutureEvent() ;
148 return FutureEventImp::send( handle ) ;
153 return m_imp->handle() ;
A callback interface for GNet::FutureEvent.
virtual void dropRead(Descriptor fd)=0
Removes the given event source descriptor from the list of read sources.
static ssize_t send(SOCKET, const void *, size_t, int, int fd_to_send=-1)
A send() replacement using sendmsg().
~FutureEvent()
Destructor.
A class that encapsulates a network socket file descriptor and an associated windows event handle...
virtual void addRead(Descriptor fd, EventHandler &, ExceptionHandler &)=0
Adds the given event source descriptor and associated handler to the read list.
An abstract interface for handling exceptions thrown out of event-loop callbacks (socket events...
FutureEvent(FutureEventHandler &, ExceptionHandler &)
Constructor. Installs itself in the event loop.
A base class for classes that handle asynchronous events from the event loop.
virtual ~FutureEventHandler()
Destructor.
A pimple-pattern implementation class used by GNet::FutureEvent.
static bool send(handle_type handle) g__noexcept
Pokes an event into the main event loop so that the FutureEventHandler callback is called asynchronou...
handle_type handle()
Returns a handle that can be passed between threads and used in send().
static bool exists()
Returns true if an instance exists.
static EventLoop & instance()
Returns a reference to an instance of the class, if any.