matador::reactor Class Reference

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 ()
 

Detailed Description

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

  • NONE do nothing
  • READ handle incoming data
  • WRITE handle outgoing data
  • EXCEPT handle exceptional data
  • ACCEPT handle incoming connections

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.

Member Typedef Documentation

◆ handler_ptr

using matador::reactor::handler_ptr = std::shared_ptr<handler>

Shortcut for shared pointer to handler

◆ t_handler_list

Shortcut for handler type list

◆ t_handler_type

Shortcut for handler event type pair

Constructor & Destructor Documentation

◆ reactor()

matador::reactor::reactor ( )

Default constructor

Member Function Documentation

◆ activate_handler()

void matador::reactor::activate_handler ( const handler_ptr h,
event_type  ev 
)

Activates the given handler for given event type.

Parameters
hHandler to be used for activation
evEvent type to be activated

◆ cancel_timer()

void matador::reactor::cancel_timer ( const std::shared_ptr< handler > &  h)

Cancels all timer for a given handler.

Parameters
hHandler to cancel the timer for

◆ deactivate_handler()

void matador::reactor::deactivate_handler ( const handler_ptr h,
event_type  ev 
)

Deactivates the given handler for given event type.

Parameters
hHandler to be used for deactivation
evEvent type to be deactivated

◆ fdsets()

select_fdsets matador::reactor::fdsets ( ) const

Returns the current internal fd sets for reading, writing and exceptions.

Returns
The internal select fd sets

◆ find_handler_type()

t_handler_list::iterator matador::reactor::find_handler_type ( const handler_ptr h)

Finds a handler type pair for the given handler.

Parameters
hHandler key to be searched for
Returns
The found pair or end iterator

◆ handle_events()

void matador::reactor::handle_events ( )

Handle events.

◆ interrupt()

void matador::reactor::interrupt ( )

Interrupts the reactor.

◆ is_running()

bool matador::reactor::is_running ( ) const

Return true if reactor was started and run was called.

Returns
True if reactor is running

◆ mark_handler_for_delete()

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.

Parameters
hHandler to be marked for deletion

◆ register_handler()

void matador::reactor::register_handler ( const handler_ptr h,
event_type  type 
)

Registers a new handler with the reactor for the given event type.

Parameters
hHandler to register
typeEvent type which the handler is used for

◆ run()

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.

◆ schedule_timer()

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.

Parameters
hHandler to schedule.
offsetOffset for first schedule in seconds.
intervalInterval of the schedule.

◆ shutdown()

void matador::reactor::shutdown ( )

Shutdown the dispatching process of the reactor.

◆ unregister_handler()

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.

Parameters
hHandler to unregister.
typeEvent mask for which the handler is to be removed

The documentation for this class was generated from the following file:
  • matador/net/reactor.hpp