31 #include <sys/types.h>
54 int fdmax(
int = 0 )
const ;
59 fd_set m_set_internal ;
60 fd_set m_set_external ;
74 virtual std::string
run() ;
76 virtual void quit( std::string ) ;
90 std::string m_quit_reason ;
97 FdSet m_exception_set ;
113 void operator=(
const Lock & ) ;
139 return &m_set_external ;
153 FD_ZERO( &m_set_internal ) ;
158 FD_SET( fd.
fd() , &m_set_internal ) ;
159 if( (fd.
fd()+1) > m_fdmax )
160 m_fdmax = (fd.
fd()+1) ;
164 m_set_external = m_set_internal ;
169 return n > m_fdmax ? n : m_fdmax ;
181 if( FD_ISSET( fd.
fd() , &m_set_external ) )
185 raiseEvent( h , method ) ;
196 catch( std::exception & e )
215 m_read_list(
"read") ,
216 m_write_list(
"write") ,
217 m_exception_list(
"exception")
237 std::string quit_reason = m_quit_reason ;
238 m_quit_reason.clear() ;
251 m_quit_reason = reason ;
254 void GNet::Select::runOnce()
258 m_read_set.init( m_read_list ) ;
259 m_write_set.init( m_write_list ) ;
260 m_exception_set.init( m_exception_list ) ;
261 int n = m_read_set.fdmax( m_write_set.fdmax(m_exception_set.fdmax()) ) ;
269 bool infinite = false ;
271 timeout.tv_usec = 0 ;
272 timeout_p = infinite ? NULL : &timeout ;
277 int rc = ::select( n , m_read_set() , m_write_set() , m_exception_set() , timeout_p ) ;
283 if( rc == 0 || ( timeout_p != NULL && timeout_p->tv_sec == 0 ) )
285 G_DEBUG(
"GNet::Select::runOnce: select() timeout" ) ;
290 G_DEBUG(
"GNet::Select::runOnce: detected event(s) on " << rc <<
" fd(s)" ) ;
299 timeout_slow.tv_sec = 0 ;
300 timeout_slow.tv_usec = 100000 ;
301 ::select( 0 , NULL , NULL , NULL , &timeout_slow ) ;
307 m_read_list.add( fd , & handler ) ;
308 m_read_set.invalidate() ;
313 m_write_list.add( fd , & handler ) ;
314 m_write_set.invalidate() ;
319 m_exception_list.add( fd , & handler ) ;
320 m_exception_set.invalidate() ;
325 m_read_list.remove( fd ) ;
326 m_read_set.invalidate() ;
331 m_write_list.remove( fd ) ;
332 m_write_set.invalidate() ;
337 m_exception_list.remove( fd ) ;
338 m_exception_set.invalidate() ;
A noncopyable base class (a la boost).
A concrete implementation of GNet::EventLoop using select() in the implementation.
EventHandlerList & m_list
Iterator begin() const
Returns a forward iterator.
An abstract base class for a singleton that keeps track of open sockets and their associated handlers...
void raiseEvents(EventHandlerList &, void(EventHandler::*method)())
virtual void readEvent()
Called for a read event.
Overload discriminator class for TimerList.
A network file descriptor.
A class to manage a boolean flag while in scope.
unsigned int interval(bool &infinite) const
Returns the interval to the next timer expiry.
void lock()
Called at the start of an iteration which might change the list.
void init(const EventHandlerList &)
static TimerList & instance()
Singleton access. Throws an exception if none.
An "fd_set" wrapper type.
SOCKET fd() const
Returns the low-level descriptor.
virtual void onException(std::exception &)=0
Called when an exception is thrown out of readEvent(), writeEvent() or exceptionEvent().
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.
Map::const_iterator Iterator
static Descriptor fd(Iterator i)
Returns the iterator's file descriptor.
virtual void quit(std::string)
Causes run() to return (once the call stack has unwound).
A base class for classes that handle asynchronous socket events.
static EventHandler * handler(Iterator i)
Returns the iterator's handler.
virtual void dropWrite(Descriptor fd)
Removes the given event source descriptor from the list of write sources.
#define G_EXCEPTION(class_name, description)
define as a function rather than a type if optimising for size
Iterator end() const
Returns an end iterator.
virtual void addWrite(Descriptor fd, EventHandler &handler)
Adds the given event source descriptor and associated handler to the write list.
virtual bool init()
Initialises the object.
virtual bool running() const
Returns true if called from within run().
Lock(EventHandlerList &list)
static EventLoop * create()
A factory method which creates an instance of a derived class on the heap.
A private implementation class used by GNet::Select to lock data structures in the face of reentrancy...
virtual std::string run()
Runs the main event loop.
virtual void addException(Descriptor fd, EventHandler &handler)
Adds the given event source descriptor and associated handler to the exception list.
virtual void dropRead(Descriptor fd)
Removes the given event source descriptor from the list of read sources.
virtual void addRead(Descriptor fd, EventHandler &handler)
Adds the given event source descriptor and associated handler to the read list.
void raiseEvent(EventHandler *, void(EventHandler::*method)())
virtual void dropException(Descriptor fd)
Removes the given event source descriptor from the list of exception sources.
virtual void exceptionEvent()
Called for an exception event.
A class which can be used in the implemention of classes derived from GNet::EventLoop.