CppZmqZoltanExt 0.0.1
Loading...
Searching...
No Matches
poller.cpp
Go to the documentation of this file.
1/*
2MIT License
3
4Copyright (c) 2025 Luan Young
5
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22SOFTWARE.
23*/
37
38#include <algorithm>
39#include <stdexcept>
40
42
43namespace zmqzext {
44
45void poller_t::add(zmq::socket_ref socket) {
46 if (!socket) {
47 throw std::invalid_argument("Cannot add null socket to poller");
48 }
49
50 if (has_socket(socket.handle())) {
51 throw std::invalid_argument("Socket already exists in poller");
52 }
53
54 _poll_items.push_back({socket.handle(), 0, ZMQ_POLLIN, 0});
55}
56
57void poller_t::remove(zmq::socket_ref socket) {
58 auto handle = socket.handle();
59 _poll_items.erase(std::remove_if(_poll_items.begin(), _poll_items.end(),
60 [handle](zmq::pollitem_t const& item) { return item.socket == handle; }),
61 _poll_items.end());
62}
63
64zmq::socket_ref poller_t::wait(std::chrono::milliseconds timeout /*= std::chrono::milliseconds{-1}*/) {
66 _terminated = true;
67 return zmq::socket_ref{};
68 }
69 _terminated = false;
70 try {
71 auto const n_items = zmq::poll(_poll_items, timeout);
72 // interrupt may have happened between is_interrupted() and poll() calls
73 // in that case, the poll does not throw with EINTR
74 // then, we check if interrupted before processing results
76 _terminated = true;
77 return zmq::socket_ref{};
78 }
79 if (n_items > 0) {
80 for (std::size_t i = 0; i < _poll_items.size(); ++i) {
81 if (_poll_items[i].revents == ZMQ_POLLIN) {
82 return zmq::socket_ref{zmq::from_handle, _poll_items[i].socket};
83 }
84 }
85 }
86 } catch (zmq::error_t const& e) {
87 auto const error = e.num();
88 if (error == EINTR) {
89 if (is_interruptible()) {
90 _terminated = true;
91 }
92 } else if (error == ETERM) {
93 _terminated = true;
94 } else {
95 throw;
96 }
97 }
98 return zmq::socket_ref{};
99}
100
101std::vector<zmq::socket_ref> poller_t::wait_all(std::chrono::milliseconds timeout /*= std::chrono::milliseconds{-1}*/) {
102 std::vector<zmq::socket_ref> result{};
103 if (is_interrupted() && is_interruptible()) {
104 _terminated = true;
105 return result;
106 }
107 _terminated = false;
108 try {
109 auto const n_items = zmq::poll(_poll_items, timeout);
110 // interrupt may have happened between is_interrupted() and poll() calls
111 // in that case, the poll does not throw with EINTR
112 // then, we check if interrupted before processing results
113 if (is_interrupted() && is_interruptible()) {
114 _terminated = true;
115 return result;
116 }
117 if (n_items > 0) {
118 result.reserve(n_items);
119 for (std::size_t i = 0; i < _poll_items.size(); ++i) {
120 if (_poll_items[i].revents == ZMQ_POLLIN) {
121 result.emplace_back(zmq::socket_ref{zmq::from_handle, _poll_items[i].socket});
122 }
123 }
124 }
125 } catch (zmq::error_t const& e) {
126 auto const error = e.num();
127 if (error == EINTR) {
128 if (is_interruptible()) {
129 _terminated = true;
130 }
131 } else if (error == ETERM) {
132 _terminated = true;
133 } else {
134 throw;
135 }
136 }
137 return result;
138}
139
140bool poller_t::has_socket(void* socket_handle) const {
141 return std::any_of(_poll_items.begin(), _poll_items.end(),
142 [socket_handle](const zmq::pollitem_t& item) { return item.socket == socket_handle; });
143}
144
145} // namespace zmqzext
void add(zmq::socket_ref socket)
Add a socket to the polling set.
Definition: poller.cpp:45
zmq::socket_ref wait(std::chrono::milliseconds timeout=std::chrono::milliseconds{-1})
Wait for any socket to become ready for receiving.
Definition: poller.cpp:64
bool is_interruptible() const noexcept
Check if polling is interruptible.
Definition: poller.h:150
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.
Definition: poller.cpp:101
void remove(zmq::socket_ref socket)
Remove a socket from the polling set.
Definition: poller.cpp:57
Signal interrupt handling for graceful application shutdown.
CZZE_EXPORT bool is_interrupted() noexcept
Check if a program interrupt was received.
Definition: interrupt.cpp:118
Event polling for monitoring multiple ZMQ sockets.