coral
A C++ library for distributed co-simulation
|
An implementation of the reactor pattern. More...
#include "coral/net/reactor.hpp"
Public Member Functions | |
void | AddSocket (zmq::socket_t &socket, SocketHandler handler) |
Adds a handler for the given socket. | |
void | RemoveSocket (zmq::socket_t &socket) CORAL_NOEXCEPT |
Removes all handlers for the given socket. More... | |
void | AddNativeSocket (NativeSocket socket, NativeSocketHandler handler) |
Adds a handler for the given native socket. | |
void | RemoveNativeSocket (NativeSocket socket) CORAL_NOEXCEPT |
Removes all handlers for the given native socket. More... | |
int | AddTimer (std::chrono::milliseconds interval, int count, TimerHandler handler) |
Adds a timer. More... | |
void | RemoveTimer (int id) |
Removes a timer. More... | |
void | RestartTimerInterval (int id) |
Resets the time to the next event for a timer. More... | |
void | Run () |
Runs the messaging loop. More... | |
void | Stop () |
Stops the messaging loop. More... | |
Static Public Attributes | |
static const int | invalidTimerID |
A number which will never be returned by AddTimer(). | |
An implementation of the reactor pattern.
This class polls a number of sockets, and when a socket has incoming messages, it dispatches to the registered handler function(s) for that socket. If multiple sockets have incoming messages, or there are multiple handlers for one socket, the functions are called in the order they were added.
It also supports timed events, where a handler function is called a certain number of times (or indefinitely) with a fixed time interval. Timers are only active when the messaging loop is running, i.e. between Run() and Stop().
int coral::net::Reactor::AddTimer | ( | std::chrono::milliseconds | interval, |
int | count, | ||
TimerHandler | handler | ||
) |
Adds a timer.
If the messaging loop is running, the first event will be triggered at interval
after this function is called. Otherwise, the first event will be triggered interval
after Run() is called.
[in] | interval | The time between events. |
[in] | count | The total number of events. If negative, the timer runs indefinitely. |
[in] | handler | The event handler. |
std::invalid_argument | if count is zero or interval is negative. |
void coral::net::Reactor::RemoveNativeSocket | ( | NativeSocket | socket | ) |
Removes all handlers for the given native socket.
If this function is called by a socket handler, no more handlers will be called for the removed socket, even if the last poll indicated that it has incoming messages.
If the given socket was never registered with AddNativeSocket(), this function simply returns without doing anything.
void coral::net::Reactor::RemoveSocket | ( | zmq::socket_t & | socket | ) |
Removes all handlers for the given socket.
If this function is called by a socket handler, no more handlers will be called for the removed socket, even if the last poll indicated that it has incoming messages.
If the given socket was never registered with AddSocket(), this function simply returns without doing anything.
void coral::net::Reactor::RemoveTimer | ( | int | id | ) |
Removes a timer.
std::invalid_argument | if id is not a valid timer ID. |
void coral::net::Reactor::RestartTimerInterval | ( | int | id | ) |
Resets the time to the next event for a timer.
This function sets the elapsed time for the current iteration of a timer to zero. It does not change the number of remaining events.
std::invalid_argument | if id is not a valid timer ID. |
void coral::net::Reactor::Run | ( | ) |
Runs the messaging loop.
This function does not return before Stop() is called (by one of the socket/timer handlers) or an error occurs.
If a socket/timer handler throws an exception, the messaging loop will stop and the exception will propagate out of Run().
zmq::error_t | if ZMQ reports an error. |
void coral::net::Reactor::Stop | ( | ) |
Stops the messaging loop.
This method may be called by a socket/timer handler, and will exit the messaging loop once all handlers for the current event(s) have been called.