8#include <framework/pcore/zmq/connections/ZMQConnection.h>
23 const auto emptyFunction = []() {};
27bool ZMQConnection::poll(
const std::map<const ZMQConnection*, ZMQConnection::ReactorFunction>& connectionList,
int timeout)
29 std::vector<const ReactorFunction*> socketMapping;
30 std::vector<zmq::pollitem_t> pollItems;
33 for (
const auto& [connection, function] : connectionList) {
34 auto sockets = connection->getSockets();
35 for (zmq::socket_t* socket : sockets) {
36 zmq::pollitem_t pollItem;
37 pollItem.socket =
static_cast<void*
>(*socket);
38 pollItem.events = ZMQ_POLLIN;
40 pollItems.push_back(std::move(pollItem));
43 socketMapping.push_back(&function);
47 if (pollItems.empty()) {
52 zmq::poll(&pollItems[0], pollItems.size(), timeout);
54 bool anySocket =
false;
55 unsigned int counter = 0;
56 for (
const auto& pollItem : pollItems) {
57 if (pollItem.revents & ZMQ_POLLIN) {
59 const auto* functionPtr = socketMapping.at(counter);
60 const auto function = *functionPtr;
67 }
catch (zmq::error_t& error) {
68 if (error.num() == EINTR) {
89 std::string endpoint{
""};
91 endpoint =
m_socket->get(zmq::sockopt::last_endpoint);
ZMQConnectionOverSocket(const std::shared_ptr< ZMQParent > &parent)
Create a new instance passing the shared ZMQParent.
std::string getEndPoint() const
Return the connection string for this socket.
std::unique_ptr< zmq::socket_t > m_socket
The memory of the socket. Needs to be initialized in a derived class.
std::vector< zmq::socket_t * > getSockets() const final
The socket used for polling is just the stored socket.
Base class for every connection with virtual functions to be implemented:
virtual bool isReady() const
Return true of this connection is able to send messages right now. Can be overloaded in derived class...
static bool hasMessage(const ZMQConnection *connection)
Check if the given connection as an incoming message (right now, no waiting).
static bool poll(const std::map< const ZMQConnection *, ReactorFunction > &connectionList, int timeout)
Poll on the given connections and call the attached function if a messages comes in.
Abstract base class for different kinds of events.