34class GNet::FutureEventImp :
public EventHandler
37 FutureEventImp( FutureEventHandler & , ExceptionSink ) ;
40 ~FutureEventImp()
override ;
43 static bool send( HANDLE ,
bool ) noexcept ;
49 HANDLE handle() noexcept ;
53 FutureEventImp( const FutureEventImp & ) = delete ;
54 FutureEventImp( FutureEventImp && ) = delete ;
55 FutureEventImp & operator=( const FutureEventImp & ) = delete ;
56 FutureEventImp & operator=( FutureEventImp && ) = delete ;
59 void readEvent() override ;
62 static
int init(
int ) ;
68 ~Fd() {
if(fd!=-1) ::close(fd) ; }
69 Fd & operator=(
int fd_ ) { fd = fd_ ;
return *this ; }
71 Fd(
const Fd & ) = delete ;
72 Fd( Fd && ) = delete ;
73 Fd & operator=(
const Fd & ) = delete ;
74 Fd & operator=( Fd && ) = delete ;
78 FutureEventHandler & m_handler ;
84GNet::FutureEventImp::FutureEventImp( FutureEventHandler & handler , ExceptionSink es ) :
88 std::array<int,2U> fds {{ -1 , -1 }} ;
89 int rc = ::socketpair( AF_UNIX , SOCK_DGRAM , 0 , &fds[0] ) ;
95 m_read = init( fds[0] ) ;
96 m_write = init( fds[1] ) ;
97 EventLoop::instance().addRead( Descriptor(m_read.fd) , *
this , es ) ;
100int GNet::FutureEventImp::init(
int fd )
102 GDEF_IGNORE_RETURN ::fcntl( fd , F_SETFL , ::fcntl(fd,F_GETFL) | O_NONBLOCK ) ;
106GNet::FutureEventImp::~FutureEventImp()
110 if( EventLoop::exists() )
111 EventLoop::instance().dropRead( Descriptor(m_read.fd) ) ;
115HANDLE GNet::FutureEventImp::handle() noexcept
118 std::swap( m_write.fd , fd ) ;
119 return static_cast<HANDLE
>(fd) ;
122void GNet::FutureEventImp::receive()
125 GDEF_IGNORE_RETURN ::recv( m_read.fd , &c , 1 , 0 ) ;
128bool GNet::FutureEventImp::send( HANDLE handle ,
bool close )
noexcept
130 int fd =
static_cast<int>(handle) ;
135 const bool ok = rc == 1 ;
145 m_handler.onFutureEvent() ;
152 m_imp(
std::make_unique<FutureEventImp>(handler,es))
161 return FutureEventImp::send( handle , close ) ;
166 return m_imp->handle() ;
virtual void readEvent()
Called for a read event.
A tuple containing an ExceptionHandler interface pointer and a bound 'exception source' pointer.
A callback interface for GNet::FutureEvent.
FutureEvent(FutureEventHandler &, ExceptionSink)
Constructor. Installs itself in the event loop.
static bool send(HANDLE handle, bool close=true) noexcept
Pokes an event into the main event loop so that the FutureEventHandler callback is called asynchronou...
HANDLE handle() noexcept
Extracts a handle that can be passed between threads and used in send().
~FutureEvent()
Destructor.
static ssize_t send(SOCKET, const void *, std::size_t, int flags) noexcept
A send() wrapper.
static std::string strerror(int errno_)
Translates an 'errno' value into a meaningful diagnostic string.
static int errno_(const SignalSafe &=G::SignalSafe()) noexcept
Returns the process's current 'errno' value.