Thread-safe event loop supporting I/O multiplexing and Timers. More...

#include <cxxtools/eventloop.h>

Inheritance diagram for cxxtools::EventLoop:
cxxtools::EventLoopBase cxxtools::SelectorBase cxxtools::EventSink cxxtools::Connectable cxxtools::NonCopyable

Public Member Functions

 EventLoop ()
 Constructs the EventLoop.
virtual ~EventLoop ()
 Destructs the EventLoop.
- Public Member Functions inherited from cxxtools::EventLoopBase
virtual ~EventLoopBase ()
 Destructs the EventLoop.
void run ()
 Starts the event loop.
void processEvents ()
 Processes all events which are currently in the event queue.
void exit ()
 Stops the EventLoop.
void setIdleTimeout (Milliseconds msecs)
 Sets the idle timeout.
Milliseconds idleTimeout () const
 Returns the idle timeout.
- Public Member Functions inherited from cxxtools::SelectorBase
virtual ~SelectorBase ()
 Destructor.
void add (Selectable &s)
 Adds an IOResult.
void remove (Selectable &s)
 Cancel an IOResult.
void add (Timer &timer)
 Adds a Timer.
void remove (Timer &timer)
 Removes a Timer.
bool wait (Milliseconds msecs=WaitInfinite)
 Wait for activity.
bool waitUntil (Timespan t)
 Wait for activity.
void wake ()
 Wakes the selctor from waiting.
Timespan waitTimer ()
 Wait for activity.
- Public Member Functions inherited from cxxtools::Connectable
 Connectable ()
 Default constructor.
virtual ~Connectable ()
 Closes all connections.
virtual void onConnectionOpen (const Connection &c)
 Registers a Connection with the Connectable.
virtual void onConnectionClose (const Connection &c)
 Unregisters a Connection from the Connectable.
size_t connectionCount () const
- Public Member Functions inherited from cxxtools::EventSink
 EventSink ()
virtual ~EventSink ()
void queueEvent (const Event &event)
 Puts a event in the queue but does not wake the loop.
void commitEvent (const Event &event)
 Puts a event in the queue and wakes the loop so that the event is processed.
void queuePriorityEvent (const Event &event)
 Puts a priority event in the queue and wakes the loop so that the event is processed.
void commitPriorityEvent (const Event &event)
 Puts a priority event in the queue and wakes the loop so that the event is processed.

Protected Member Functions

virtual void onAdd (Selectable &s)
 A Selectable is added to this Selector.
virtual void onRemove (Selectable &s)
 A Selectable is removed from this Selector.
virtual void onReinit (Selectable &s)
 A Selectable is reinitialised and needs to be updated.
virtual void onChanged (Selectable &s)
 A Selectable in this Selector has changed.
virtual void onRun ()
virtual bool onWaitUntil (Timespan timeout)
virtual void onWake ()
virtual void onExit ()
virtual void onQueueEvent (const Event &event, bool priority)
virtual void onCommitEvent (const Event &event, bool priority)
virtual void onProcessEvents ()
- Protected Member Functions inherited from cxxtools::EventLoopBase
 EventLoopBase ()
 Constructs the EventLoop.
- Protected Member Functions inherited from cxxtools::SelectorBase
 SelectorBase ()
 Default constructor.
void onAddTimer (Timer &timer)
void onRemoveTimer (Timer &timer)
void onTimerChanged (Timer &timer)
- Protected Member Functions inherited from cxxtools::Connectable
 Connectable (const Connectable &)
 Copy constructor.
Connectableoperator= (const Connectable &)
 Assignment operator.
const std::list< Connection > & connections () const
 Returns a list of all current connections.
std::list< Connection > & connections ()
 Returns a list of all current connections.
void clear ()

Additional Inherited Members

- Public Attributes inherited from cxxtools::EventLoopBase
Signal timeout
 Notifies about wait timeouts This signal is send when the timeout given to a wait call of the selector expires and no activity occured.
Signal idle
 Notifies that all events are processed and the event loop starts waiting.
Signal< const Event & > event
 Reports all events.
Signal exited
 Emited when the eventloop is exited.
- Static Public Attributes inherited from cxxtools::SelectorBase
static const int WaitInfinite = -1
- Protected Attributes inherited from cxxtools::Connectable
std::list< Connection_connections
 A list of all current connections.

Detailed Description

Thread-safe event loop supporting I/O multiplexing and Timers.

The System EventLoop can be used as the central entity of a thread or process to dispatch application events and wait on multiple IODevices or Timers for activity.

Events can be added to the internal event queue, even from other threads using the method EventLoop::commitEvent or EventLoop::queueEvent. The first method will add the event to the internal queue and wake the event loop, the latter allows queing multiple events and it is up to the caller to wake the event loop by calling EventLoop::wake when all events are added. When the event loop processes its event, the signal "event" is send for each processed event. Events are processes in the order they were added.

To start the EventLoop the method EventLoop::run must be executed. It blocks until the event loop is stopped. To stop the EventLoop, EventLoop::exit must be called. The delivery of the events occurs inside the thread that started the execution of the event loop.

IODevices and Timers can be added to an EventLoop just as to Selector. In fact a Selector is used internally to implement the EventLoop.

Since the EventLoop is a Runnable, it can be easily assigned to a Thread to give it its own event loop.

Constructor & Destructor Documentation

cxxtools::EventLoop::EventLoop ( )

Constructs the EventLoop.

virtual cxxtools::EventLoop::~EventLoop ( )
virtual

Destructs the EventLoop.

Member Function Documentation

virtual void cxxtools::EventLoop::onAdd ( Selectable )
protectedvirtual

A Selectable is added to this Selector.

Do not throw exceptions.

Implements cxxtools::SelectorBase.

virtual void cxxtools::EventLoop::onChanged ( Selectable s)
protectedvirtual

A Selectable in this Selector has changed.

Do not throw exceptions.

Implements cxxtools::SelectorBase.

virtual void cxxtools::EventLoop::onCommitEvent ( const Event event,
bool  priority 
)
protectedvirtual

Implements cxxtools::EventSink.

virtual void cxxtools::EventLoop::onExit ( )
protectedvirtual
virtual void cxxtools::EventLoop::onProcessEvents ( )
protectedvirtual
virtual void cxxtools::EventLoop::onQueueEvent ( const Event event,
bool  priority 
)
protectedvirtual

Implements cxxtools::EventSink.

virtual void cxxtools::EventLoop::onReinit ( Selectable )
protectedvirtual

A Selectable is reinitialised and needs to be updated.

Do not throw exceptions.

Implements cxxtools::SelectorBase.

virtual void cxxtools::EventLoop::onRemove ( Selectable )
protectedvirtual

A Selectable is removed from this Selector.

Do not throw exceptions.

Implements cxxtools::SelectorBase.

virtual void cxxtools::EventLoop::onRun ( )
protectedvirtual
virtual bool cxxtools::EventLoop::onWaitUntil ( Timespan  timeout)
protectedvirtual
virtual void cxxtools::EventLoop::onWake ( )
protectedvirtual

The documentation for this class was generated from the following file: