Implementation of the reactor pattern. More...
#include <reactor.hpp>
Public Types | |
using | handler_ptr = std::shared_ptr< handler > |
using | t_handler_type = std::pair< handler_ptr, event_type > |
using | t_handler_list = std::list< t_handler_type > |
Public Member Functions | |
reactor () | |
void | register_handler (const handler_ptr &h, event_type type) |
void | unregister_handler (const handler_ptr &h, event_type type) |
void | schedule_timer (const std::shared_ptr< handler > &h, time_t offset, time_t interval) |
void | cancel_timer (const std::shared_ptr< handler > &h) |
void | run () |
void | handle_events () |
void | shutdown () |
bool | is_running () const |
select_fdsets | fdsets () const |
void | mark_handler_for_delete (const std::shared_ptr< handler > &h) |
void | activate_handler (const handler_ptr &h, event_type ev) |
void | deactivate_handler (const handler_ptr &h, event_type ev) |
t_handler_list::iterator | find_handler_type (const handler_ptr &h) |
void | interrupt () |
Implementation of the reactor pattern.
This class implements the reactor pattern. It is used to dispatch network connection based on file descriptors.
Each connection is represented and handled by an instance of a class based on class handler.
A handler must be registered within the reactor for a specific event type. The types are
The type can be changed while the reactor is running.
There is also an acceptor class to handle incoming connections and a connector class to execute actively to a remote service.
It's also possible to handle timeouts on a regularly base.
using matador::reactor::handler_ptr = std::shared_ptr<handler> |
Shortcut for shared pointer to handler
using matador::reactor::t_handler_list = std::list<t_handler_type> |
Shortcut for handler type list
using matador::reactor::t_handler_type = std::pair<handler_ptr, event_type> |
Shortcut for handler event type pair
matador::reactor::reactor | ( | ) |
Default constructor
void matador::reactor::activate_handler | ( | const handler_ptr & | h, |
event_type | ev | ||
) |
Activates the given handler for given event type.
h | Handler to be used for activation |
ev | Event type to be activated |
void matador::reactor::cancel_timer | ( | const std::shared_ptr< handler > & | h | ) |
Cancels all timer for a given handler.
h | Handler to cancel the timer for |
void matador::reactor::deactivate_handler | ( | const handler_ptr & | h, |
event_type | ev | ||
) |
Deactivates the given handler for given event type.
h | Handler to be used for deactivation |
ev | Event type to be deactivated |
select_fdsets matador::reactor::fdsets | ( | ) | const |
Returns the current internal fd sets for reading, writing and exceptions.
t_handler_list::iterator matador::reactor::find_handler_type | ( | const handler_ptr & | h | ) |
Finds a handler type pair for the given handler.
h | Handler key to be searched for |
void matador::reactor::handle_events | ( | ) |
Handle events.
void matador::reactor::interrupt | ( | ) |
Interrupts the reactor.
bool matador::reactor::is_running | ( | ) | const |
Return true if reactor was started and run was called.
void matador::reactor::mark_handler_for_delete | ( | const std::shared_ptr< handler > & | h | ) |
Marks the given handler to be deleted from the reactor. That means the handler was closed.
h | Handler to be marked for deletion |
void matador::reactor::register_handler | ( | const handler_ptr & | h, |
event_type | type | ||
) |
Registers a new handler with the reactor for the given event type.
h | Handler to register |
type | Event type which the handler is used for |
void matador::reactor::run | ( | ) |
Starts the dispatching process of the reactor. The call to this method only terminates if the handler list is empty and there are no timers scheduled or shutdown ist called.
void matador::reactor::schedule_timer | ( | const std::shared_ptr< handler > & | h, |
time_t | offset, | ||
time_t | interval | ||
) |
Schedules a timer handler within the reactor. The handler is registered with a given offset and a given interval. If the interval is zero the timer is only schedules once.
h | Handler to schedule. |
offset | Offset for first schedule in seconds. |
interval | Interval of the schedule. |
void matador::reactor::shutdown | ( | ) |
Shutdown the dispatching process of the reactor.
void matador::reactor::unregister_handler | ( | const handler_ptr & | h, |
event_type | type | ||
) |
Unregisters a handler from the reactor for the given event mask. If the mask id set to NONE and the handle isn't scheduled for a timeout the handler is removed.
h | Handler to unregister. |
type | Event mask for which the handler is to be removed |