26 #include <sys/types.h>
36 typedef FutureEvent::Error Error ;
37 typedef FutureEvent::handle_type handle_type ;
45 unsigned int receive() ;
48 static bool send( handle_type ,
unsigned int ) g__noexcept ;
51 handle_type handle()
const ;
57 static int init(
int ) ;
58 virtual void readEvent() ;
59 virtual void onException( std::exception & ) ;
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()
const
103 return static_cast<handle_type
>(m_fd_write) ;
106 unsigned int GNet::FutureEventImp::receive()
109 ssize_t rc = ::recv( m_fd_read , &c , 1 , 0 ) ;
111 throw Error(
"recv") ;
112 return static_cast<unsigned int>(c) ;
115 bool GNet::FutureEventImp::send( handle_type handle ,
unsigned int payload ) g__noexcept
117 int fd =
static_cast<int>(handle) ;
118 char c =
static_cast<char>(payload) ;
120 const bool ok = rc == 1 ;
124 void GNet::FutureEventImp::readEvent()
126 m_handler.onFutureEvent( receive() ) ;
129 void GNet::FutureEventImp::onException( std::exception & e )
131 m_handler.onException( e ) ;
147 return FutureEventImp::send( handle , payload ) ;
152 return m_imp->handle() ;
A callback interface for GNet::FutureEvent.
handle_type handle() const
Returns a handle that can be passed between threads and used in send().
static ssize_t send(int, const void *, size_t, int, int fd_to_send=-1)
A send() replacement using sendmsg().
virtual void dropRead(Descriptor fd)=0
Removes the given event source descriptor from the list of read sources.
~FutureEvent()
Destructor.
A class that encapsulates a network file descriptor and hides knowledge of its o/s-spefific error val...
virtual void addRead(Descriptor fd, EventHandler &handler)=0
Adds the given event source descriptor and associated handler to the read list.
FutureEvent(FutureEventHandler &)
Constructor.
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 exists()
Returns true if an instance exists.
static bool send(handle_type handle, unsigned int payload) g__noexcept
Pokes the event payload into the main event loop so that the callback is called once the stack is unw...
static EventLoop & instance()
Returns a reference to an instance of the class, if any.