|
CppZmqZoltanExt 0.0.1
|
Event loop for managing socket and timer events. More...
#include <loop.h>
Public Member Functions | |
| void | add (zmq::socket_ref socket, fn_socket_handler_t fn) |
| Register a socket with an I/O handler. | |
| timer_id_t | add_timer (std::chrono::milliseconds timeout, std::size_t occurences, fn_timer_handler_t fn) |
| Register a timer with an expiration handler. | |
| void | remove (zmq::socket_ref socket) |
| Unregister a socket from the event loop. | |
| void | remove_timer (timer_id_t timer_id) |
| Unregister a timer from the event loop. | |
| void | run (bool interruptible=true, std::chrono::milliseconds interruptCheckInterval=std::chrono::milliseconds{-1}) |
| Run the event loop. | |
| bool | terminated () const noexcept |
| Check if the event loop has been terminated by interrupt signal or context termination. | |
Event loop for managing socket and timer events.
The loop_t class provides a reactive event loop that monitors multiple sockets for I/O readiness and manages scheduled timers. It uses a poller_t internally to efficiently monitor multiple sockets simultaneously, and maintains a collection of timers with expiration tracking.
The event loop integrates with the interrupt handling system, allowing graceful shutdown in response to signals like SIGINT or SIGTERM.
| void zmqzext::loop_t::add | ( | zmq::socket_ref | socket, |
| fn_socket_handler_t | fn | ||
| ) |
Register a socket with an I/O handler.
Adds a socket to the event loop with an associated callback function. The callback is invoked whenever the socket becomes ready for receiving (data available to receive).
| socket | The ZMQ socket to register |
| fn | Callback function to invoke when socket is ready |
| std::invalid_argument | if the socket is invalid or already added |
| timer_id_t zmqzext::loop_t::add_timer | ( | std::chrono::milliseconds | timeout, |
| std::size_t | occurences, | ||
| fn_timer_handler_t | fn | ||
| ) |
Register a timer with an expiration handler.
Adds a timer to the event loop that will expire at regular intervals. The callback is invoked when the timer expires. Timers can be one-shot (occurences=1) or recurring (occurences > 1 or 0 for infinite).
| timeout | Duration between timer expirations in milliseconds |
| occurences | Number of times the timer should fire (0 for infinite) |
| fn | Callback function to invoke when timer expires |
| std::runtime_error | if no unique timer ID can be generated |
| void zmqzext::loop_t::remove | ( | zmq::socket_ref | socket | ) |
Unregister a socket from the event loop.
Removes a socket from event monitoring. The socket's handler callback will no longer be invoked.
| socket | The ZMQ socket to remove |
| void zmqzext::loop_t::remove_timer | ( | timer_id_t | timer_id | ) |
Unregister a timer from the event loop.
Removes a timer from the event loop. The timer's handler callback will no longer be invoked.
| timer_id | The unique identifier of the timer to remove |
| void zmqzext::loop_t::run | ( | bool | interruptible = true, |
| std::chrono::milliseconds | interruptCheckInterval = std::chrono::milliseconds{-1} |
||
| ) |
Run the event loop.
Starts the event loop, which continuously monitors sockets and timers, invoking their respective callbacks when events occur. The loop blocks until terminated via signal interrupt, the termination of the context associated with any socket, callback return value is false, becomes empty (no sockets or timers registered anymore).
| interruptible | Whether to check for interrupt signals during loop execution (default is true) to finish the loop. When false, the loop will ignore any interrupt signals and will continue running (useful in actors orchestrated by a main application). See class documentation notes on interruptible behavior. |
| interruptCheckInterval | Duration between interrupt checks (when enabled) in milliseconds (default: -1 for checking only when interrupted by a signal, socket ready, or timer expiry). See class documentation notes on interrupt checking. |
|
inlinenoexcept |