CppZmqZoltanExt 0.0.1
Loading...
Searching...
No Matches
loop.h
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*/
61#pragma once
62
63#include <chrono>
64#include <cstddef>
65#include <functional>
66#include <list>
67#include <map>
68#include <zmq.hpp>
69
70#include "cppzmqzoltanext/czze_export.h"
71#include "poller.h"
72
73namespace zmqzext {
74
75class loop_t;
76
78using timer_id_t = std::size_t;
79
91using fn_socket_handler_t = std::function<bool(loop_t&, zmq::socket_ref)>;
92
104using fn_timer_handler_t = std::function<bool(loop_t&, timer_id_t)>;
105
134class CZZE_EXPORT loop_t {
135private:
136 using time_point_t = std::chrono::time_point<std::chrono::steady_clock>;
137 using time_milliseconds_t = std::chrono::milliseconds;
138
142 struct timer_t {
143 timer_id_t id;
144 std::chrono::milliseconds timeout;
145 std::size_t occurences;
146 time_point_t next_occurence;
147 fn_timer_handler_t handler;
148 bool removed;
149 };
150
151public:
164 void add(zmq::socket_ref socket, fn_socket_handler_t fn);
165
181 timer_id_t add_timer(std::chrono::milliseconds timeout, std::size_t occurences, fn_timer_handler_t fn);
182
194 void remove(zmq::socket_ref socket);
195
207 void remove_timer(timer_id_t timer_id);
208
232 void run(bool interruptible = true,
233 std::chrono::milliseconds interruptCheckInterval = std::chrono::milliseconds{-1});
234
241 bool terminated() const noexcept { return _poller.terminated(); }
242
243private:
248 time_point_t now();
249
259 time_milliseconds_t find_next_timeout(time_point_t const& actual_time);
260
267 void removeFlagedTimers();
268
277 timer_id_t generate_unique_timer_id();
278
279private:
280 poller_t _poller;
281 std::map<zmq::socket_ref, fn_socket_handler_t> _socket_handlers;
282 std::list<timer_t> _timer_handlers;
283 timer_id_t _last_timer_id{0};
284 bool _timer_id_has_overflowed{false};
285 time_milliseconds_t _interruptCheckInterval{-1};
286};
287
288} // namespace zmqzext
Event loop for managing socket and timer events.
Definition: loop.h:134
bool terminated() const noexcept
Check if the event loop has been terminated by interrupt signal or context termination.
Definition: loop.h:241
Class for efficient polling of multiple ZMQ sockets.
Definition: poller.h:91
std::function< bool(loop_t &, timer_id_t)> fn_timer_handler_t
Timer event handler callback type.
Definition: loop.h:104
std::function< bool(loop_t &, zmq::socket_ref)> fn_socket_handler_t
Socket event handler callback type.
Definition: loop.h:91
std::size_t timer_id_t
Unique identifier for timer instances.
Definition: loop.h:78
Event polling for monitoring multiple ZMQ sockets.