31 #include <sys/types.h>
52 int fdmax(
int = 0 )
const ;
53 fd_set * operator()() ;
58 fd_set m_set_internal ;
59 fd_set m_set_external ;
69 G_EXCEPTION( Error ,
"select() error" ) ;
72 virtual std::string run()
override ;
73 virtual bool running()
const override ;
74 virtual void quit( std::string )
override ;
79 virtual void dropRead(
Descriptor fd )
override ;
80 virtual void dropWrite(
Descriptor fd )
override ;
81 virtual void dropOther(
Descriptor fd )
override ;
82 virtual std::string report()
const override ;
93 std::string m_quit_reason ;
105 GNet::FdSet::FdSet() :
111 fd_set * GNet::FdSet::operator()()
113 return &m_set_external ;
116 void GNet::FdSet::invalidate()
121 void GNet::FdSet::init(
const EventHandlerList & list )
134 FD_ZERO( &m_set_internal ) ;
135 const EventHandlerList::Iterator end = list.end() ;
136 for( EventHandlerList::Iterator p = list.begin() ; p != end ; ++p )
138 Descriptor fd = p.fd() ;
139 G_ASSERT( fd.valid() && fd.fd() >= 0 ) ;
140 if( fd.fd() < 0 )
continue ;
141 FD_SET( fd.fd() , &m_set_internal ) ;
142 if( (fd.fd()+1) > m_fdmax )
143 m_fdmax = (fd.fd()+1) ;
147 m_set_external = m_set_internal ;
150 int GNet::FdSet::fdmax(
int n )
const
152 return n > m_fdmax ? n : m_fdmax ;
155 void GNet::FdSet::raiseEvents( EventHandlerList & list ,
void (EventHandler::*method)() )
157 EventHandlerList::Lock lock( list ) ;
158 const EventHandlerList::Iterator end = list.end() ;
159 for( EventHandlerList::Iterator p = list.begin() ; p != end ; ++p )
161 Descriptor fd = p.fd() ;
162 if( fd.fd() >= 0 && FD_ISSET( fd.fd() , &m_set_external ) )
164 p.raiseEvent( method ) ;
169 void GNet::FdSet::raiseEvents( EventHandlerList & list ,
void (EventHandler::*method)(EventHandler::Reason) , EventHandler::Reason reason )
171 EventHandlerList::Lock lock( list ) ;
172 const EventHandlerList::Iterator end = list.end() ;
173 for( EventHandlerList::Iterator p = list.begin() ; p != end ; ++p )
175 Descriptor fd = p.fd() ;
176 if( fd.fd() >= 0 && FD_ISSET( fd.fd() , &m_set_external ) )
178 p.raiseEvent( method , reason ) ;
187 return new EventLoopImp ;
192 GNet::EventLoopImp::EventLoopImp() :
195 m_read_list(
"read") ,
196 m_write_list(
"write") ,
197 m_other_list(
"other")
201 GNet::EventLoopImp::~EventLoopImp()
205 std::string GNet::EventLoopImp::run()
207 EventLoop::Running running( m_running ) ;
212 std::string quit_reason = m_quit_reason ;
213 m_quit_reason.clear() ;
218 bool GNet::EventLoopImp::running()
const
223 void GNet::EventLoopImp::quit( std::string reason )
226 m_quit_reason = reason ;
234 void GNet::EventLoopImp::runOnce()
238 m_read_set.init( m_read_list ) ;
239 m_write_set.init( m_write_list ) ;
240 m_other_set.init( m_other_list ) ;
241 int n = m_read_set.fdmax( m_write_set.fdmax(m_other_set.fdmax()) ) ;
245 typedef struct timeval Timeval ;
247 Timeval * timeout_p = nullptr ;
248 bool timeout_immediate = false ;
251 bool timeout_infinite = false ;
253 timeout_immediate = !timeout_infinite && interval.s == 0 && interval.us == 0U ;
254 timeout.tv_sec = interval.s ;
255 timeout.tv_usec = interval.us ;
256 timeout_p = timeout_infinite ?
nullptr : &timeout ;
263 if( timeout_p ==
nullptr || timeout.tv_sec > 0 )
266 timeout.tv_usec = 999999 ;
268 timeout_p = &timeout ;
273 int rc = ::select( n , m_read_set() , m_write_set() , m_other_set() , timeout_p ) ;
283 if( rc == 0 || timeout_immediate )
301 Timeval timeout_slow ;
302 timeout_slow.tv_sec = 0 ;
303 timeout_slow.tv_usec = 100000 ;
304 ::select( 0 ,
nullptr ,
nullptr ,
nullptr , &timeout_slow ) ;
308 void GNet::EventLoopImp::addRead( Descriptor fd , EventHandler & handler , ExceptionHandler & eh )
310 m_read_list.add( fd , &handler , &eh ) ;
311 m_read_set.invalidate() ;
314 void GNet::EventLoopImp::addWrite( Descriptor fd , EventHandler & handler , ExceptionHandler & eh )
316 m_write_list.add( fd , &handler , &eh ) ;
317 m_write_set.invalidate() ;
320 void GNet::EventLoopImp::addOther( Descriptor fd , EventHandler & handler , ExceptionHandler & eh )
322 m_other_list.add( fd , &handler , &eh ) ;
323 m_other_set.invalidate() ;
326 void GNet::EventLoopImp::dropRead( Descriptor fd )
328 m_read_list.remove( fd ) ;
329 m_read_set.invalidate() ;
332 void GNet::EventLoopImp::dropWrite( Descriptor fd )
334 m_write_list.remove( fd ) ;
335 m_write_set.invalidate() ;
338 void GNet::EventLoopImp::dropOther( Descriptor fd )
340 m_other_list.remove( fd ) ;
341 m_other_set.invalidate() ;
344 void GNet::EventLoopImp::disarm( ExceptionHandler * p )
346 m_read_list.disarm( p ) ;
347 m_write_list.disarm( p ) ;
348 m_other_list.disarm( p ) ;
356 std::string GNet::EventLoopImp::report()
const
358 return std::string() ;
A subsecond-resolution timestamp based on a time_t.
An empty structure that is used to indicate a signal-safe, reentrant implementation.
An abstract base class for a singleton that keeps track of open sockets and their associated handlers...
virtual void readEvent()
Called for a read event.
A class that encapsulates a network socket file descriptor and an associated windows event handle...
static std::string fromInt(int i)
Converts int 'i' to a string.
A concrete implementation of GNet::EventLoop using select() in its implementation.
An abstract interface for handling exceptions thrown out of event-loop callbacks (socket events...
static EventLoop * create()
A factory method which creates an instance of a derived class on the heap.
static TimerList & instance()
Singleton access. Throws an exception if none.
An "fd_set" wrapper class used by GNet::EventLoopImp.
G::EpochTime interval(bool &infinite) const
Returns the interval to the first timer expiry.
virtual void writeEvent()
Called for a write event.
static bool enabled()
Returns true if test features are enabled.
void doTimeouts()
Triggers the timeout callbacks of any expired timers.
A base class for classes that handle asynchronous events from the event loop.
static bool remove(const Path &path, const NoThrow &)
Deletes the file or directory. Returns false on error.
An overload discriminator class for File methods.
virtual void otherEvent(Reason)
Called for a socket-exception event or a socket-close event on windows.
A class that maps from a file descriptor to an event handler, used in the implemention of classes der...