Skip to content

Commit 4a5fc4d

Browse files
committed
Add zmq_timers support
1 parent 7cb78a8 commit 4a5fc4d

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

zmq.hpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2794,6 +2794,70 @@ inline std::ostream &operator<<(std::ostream &os, const message_t &msg)
27942794
return os << msg.str();
27952795
}
27962796

2797+
using timer_id_t = int;
2798+
2799+
class timers_t
2800+
{
2801+
public:
2802+
timers_t() : _timers(zmq_timers_new())
2803+
{
2804+
if (_timers == nullptr)
2805+
throw error_t();
2806+
}
2807+
2808+
timers_t(timers_t &other) = delete;
2809+
2810+
~timers_t() { ZMQ_ASSERT(zmq_timers_destroy(&_timers) == 0); }
2811+
2812+
timer_id_t
2813+
add(std::chrono::milliseconds interval, zmq_timer_fn handler, void *arg)
2814+
{
2815+
int timer_id = zmq_timers_add(_timers, interval.count(), handler, arg);
2816+
if (timer_id == -1)
2817+
throw zmq::error_t();
2818+
return timer_id;
2819+
}
2820+
2821+
void cancel(timer_id_t timer_id)
2822+
{
2823+
int rc = zmq_timers_cancel(_timers, timer_id);
2824+
if (rc == -1)
2825+
throw zmq::error_t();
2826+
}
2827+
2828+
void set_interval(timer_id_t timer_id, std::chrono::milliseconds interval)
2829+
{
2830+
int rc = zmq_timers_set_interval(_timers, timer_id, interval.count());
2831+
if (rc == -1)
2832+
throw zmq::error_t();
2833+
}
2834+
2835+
void reset(timer_id_t timer_id)
2836+
{
2837+
int rc = zmq_timers_reset(_timers, timer_id);
2838+
if (rc == -1)
2839+
throw zmq::error_t();
2840+
}
2841+
2842+
std::optional<std::chrono::milliseconds> timeout() const
2843+
{
2844+
int timeout = zmq_timers_timeout(_timers);
2845+
if (timeout == -1)
2846+
return std::nullopt;
2847+
return std::chrono::milliseconds{timeout};
2848+
}
2849+
2850+
void execute()
2851+
{
2852+
int rc = zmq_timers_execute(_timers);
2853+
if (rc == -1)
2854+
throw zmq::error_t();
2855+
}
2856+
2857+
private:
2858+
void *_timers;
2859+
};
2860+
27972861
} // namespace zmq
27982862

27992863
#endif // __ZMQ_HPP_INCLUDED__

0 commit comments

Comments
 (0)