Created
April 17, 2025 15:59
-
-
Save cacharle/907a2617e3ea258393c72f4130b3dd53 to your computer and use it in GitHub Desktop.
ZMQ timers draft
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <chrono> | |
#include <optional> | |
#include <zmq.h> | |
#include <zmq.hpp> | |
namespace zmq | |
{ | |
using timer_id_t = int; | |
class timers_t | |
{ | |
public: | |
timers_t() | |
: _timers(zmq_timers_new()) | |
{ | |
if (_timers == nullptr) | |
throw error_t(); | |
} | |
timers_t(timers_t &other) = delete; | |
~timers_t() { assert(zmq_timers_destroy(&_timers) == 0); } | |
timer_id_t add(std::chrono::milliseconds interval, zmq_timer_fn handler, void *arg) | |
{ | |
int timer_id = zmq_timers_add(_timers, interval.count(), handler, arg); | |
if (timer_id == -1) | |
throw zmq::error_t(); | |
return timer_id; | |
} | |
void cancel(timer_id_t timer_id) | |
{ | |
int rc = zmq_timers_cancel(_timers, timer_id); | |
if (rc == -1) | |
throw zmq::error_t(); | |
} | |
void set_interval(timer_id_t timer_id, std::chrono::milliseconds interval) | |
{ | |
int rc = zmq_timers_set_interval(_timers, timer_id, interval.count()); | |
if (rc == -1) | |
throw zmq::error_t(); | |
} | |
void reset(timer_id_t timer_id) | |
{ | |
int rc = zmq_timers_reset(_timers, timer_id); | |
if (rc == -1) | |
throw zmq::error_t(); | |
} | |
std::optional<std::chrono::milliseconds> timeout() const | |
{ | |
int timeout = zmq_timers_timeout(_timers); | |
if (timeout == -1) | |
return std::nullopt; | |
return std::chrono::milliseconds{timeout}; | |
} | |
void execute() | |
{ | |
int rc = zmq_timers_execute(_timers); | |
if (rc == -1) | |
throw zmq::error_t(); | |
} | |
private: | |
void *_timers; | |
}; | |
} // namespace zmq |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment