CppZmqZoltanExt 0.0.1
Loading...
Searching...
No Matches
zmqzext::poller_t Class Reference

Class for efficient polling of multiple ZMQ sockets. More...

#include <poller.h>

Public Member Functions

void add (zmq::socket_ref socket)
 Add a socket to the polling set.
 
void remove (zmq::socket_ref socket)
 Remove a socket from the polling set.
 
void set_interruptible (bool interruptible) noexcept
 Set whether polling should be interruptible.
 
bool is_interruptible () const noexcept
 Check if polling is interruptible.
 
std::size_t size () const noexcept
 Get the number of sockets in the polling set.
 
bool terminated () const noexcept
 Check if the poller has been terminated during the last wait operation.
 
zmq::socket_ref wait (std::chrono::milliseconds timeout=std::chrono::milliseconds{-1})
 Wait for any socket to become ready for receiving.
 
std::vector< zmq::socket_ref > wait_all (std::chrono::milliseconds timeout=std::chrono::milliseconds{-1})
 Wait for at least one socket to become ready for receiving and return all ready.
 

Detailed Description

Class for efficient polling of multiple ZMQ sockets.

The poller_t class provides a convenient wrapper around ZMQ's polling mechanism. It allows applications to monitor multiple sockets simultaneously and wait for data availability on any or all of them.

The poller supports adding and removing sockets to be monitored at any time.

When used in conjunction with the interrupt handling module and the application receives a SIINT or SIGTERM signal, the poller will return early from wait operations, allowing the application to handle the interrupt by checking if the poller was terminated.

The set_interruptible() method can be used to enable or disable interrupt checking. When set to false (the default is true), the poller will still return early on interrupt signals, but the terminated() method will always return false. This behavior may be desirable on actors when they should continue processing all events before receiving a stop request from the main application. So the main application can perform a graceful shutdown without the actors loosing any messages that are already in their queues.

Note
This class is not thread-safe.
On Windows, the waiting calls to ZMQ functions do not return early on signals, no matter if the signal handlers are installed or not. Still, the interrupt flag is set and can be checked by the poller. Then, it is very important to call the wait methods with an appropriate timeout to allow the poller to detect the interrupt within a reasonable time.

Definition at line 91 of file poller.h.

Member Function Documentation

◆ add()

void zmqzext::poller_t::add ( zmq::socket_ref  socket)

Add a socket to the polling set.

Registers a socket with the poller for monitoring. The socket will be polled in subsequent wait operations to detect readiness for receive operation.

Parameters
socketThe ZMQ socket reference to add
Exceptions
std::invalid_argumentif the socket is invalid or already added
See also
remove()

Definition at line 45 of file poller.cpp.

◆ is_interruptible()

bool zmqzext::poller_t::is_interruptible ( ) const
inlinenoexcept

Check if polling is interruptible.

Returns
true if the poller will check for interrupt signals, false otherwise
See also
set_interruptible()

Definition at line 150 of file poller.h.

◆ remove()

void zmqzext::poller_t::remove ( zmq::socket_ref  socket)

Remove a socket from the polling set.

Unregisters a socket from the poller. The socket will no longer be monitored in wait operations.

Parameters
socketThe ZMQ socket reference to remove
Note
Removing a socket that was not added is a no-op
See also
add()

Definition at line 57 of file poller.cpp.

◆ set_interruptible()

void zmqzext::poller_t::set_interruptible ( bool  interruptible)
inlinenoexcept

Set whether polling should be interruptible.

Controls whether the poller will check for interrupt signals during wait operations.

When enabled (default), the poller wait operations will return immediately if an interrupt signal was already received, or return early if an interrupt signal is received during the wait operations. The is_terminated() will then return true.

When interruptible is disabled, the wait operations are allowed to wait for incoming messages in the monitored sockets even if an interrupt signal was already received. Still, the wait operations will return early if an interrupt signal is received during the wait. The is_terminated() method will always return false, even if an interrupt signal was received. This behavior may be desirable on actors when they should continue processing all events before receiving a stop request from the main application. So the main application can perform a graceful shutdown without the actors loosing any messages that are already in their queues.

Parameters
interruptibletrue to enable interrupt checking, false otherwise
Note
Interrupt checking requires install_interrupt_handler() to be called
Default is true
Setting to false does not disable early return on interrupts
See also
is_interruptible()
install_interrupt_handler()

Definition at line 142 of file poller.h.

◆ size()

std::size_t zmqzext::poller_t::size ( ) const
inlinenoexcept

Get the number of sockets in the polling set.

Returns
The count of registered sockets

Definition at line 157 of file poller.h.

◆ terminated()

bool zmqzext::poller_t::terminated ( ) const
inlinenoexcept

Check if the poller has been terminated during the last wait operation.

Returns whether a termination condition has been detected during the last wait operation. The termination condition occurs when an interrupt signal is received and the poller is interruptible or when the context associated with any of the monitored sockets is terminated.

The terminated state is reset on each wait operation so a new wait can be performed after receiving an interrupt signal when the interruptible mode is disabled.

Returns
true if the poller is in a terminated state, false otherwise
See also
set_interruptible()

Definition at line 174 of file poller.h.

◆ wait()

zmq::socket_ref zmqzext::poller_t::wait ( std::chrono::milliseconds  timeout = std::chrono::milliseconds{-1})

Wait for any socket to become ready for receiving.

Blocks until at least one socket becomes ready for receiving, the timeout expires, an interrupt signal is received or the context associated with any of the monitored sockets is terminated. Returns the first ready socket found.

Sockets are checked in the order they were added to the poller. If multiple sockets are ready, the first one is returned. If the same socket is always ready, it may starve other sockets. For fairness, consider using wait_all() instead.

Parameters
timeoutMaximum wait duration in milliseconds (default: -1 for infinite timeout)
Returns
A reference to the first socket that is ready for I/O
Exceptions
zmq::error_tif a ZMQ error occurs
Note
When interrupted, it returns early, no matter the interruptible setting
See also
wait_all()

Definition at line 64 of file poller.cpp.

◆ wait_all()

std::vector< zmq::socket_ref > zmqzext::poller_t::wait_all ( std::chrono::milliseconds  timeout = std::chrono::milliseconds{-1})

Wait for at least one socket to become ready for receiving and return all ready.

Blocks until at least one socket becomes ready for receiving, the timeout expires, an interrupt signal is received or the context associated with any of the monitored sockets is terminated. Returns all ready sockets at the time of the check.

Sockets are checked in the order they were added to the poller. If multiple sockets are ready, all of them are returned in the order they were added.

Parameters
timeoutMaximum wait duration in milliseconds (default: -1 for infinite timeout)
Returns
A vector of references to all ready sockets
Exceptions
zmq::error_tif a ZMQ error occurs
Note
When interrupted, it returns early, no matter the interruptible setting
See also
wait()

Definition at line 101 of file poller.cpp.


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