Mads
Multi-Agent Distributed System
Loading...
Searching...
No Matches
zmqpp_compat.hpp
Go to the documentation of this file.
1/*
2 _____ __ __ ___ ____ ____ ____ _
3 |__ /| \/ |/ _ \| _ \| _ \ / ___|___ _ __ ___ _ __ __ _| |_
4 / / | |\/| | | | | |_) | |_) | | | / _ \| '_ ` _ \| '_ \ / _` | __|
5 / /_ | | | | |_| | __/| __/ | |__| (_) | | | | | | |_) | (_| | |_
6 /____||_| |_|\__\_\_| |_| \____\___/|_| |_| |_| .__/ \__,_|\__|
7 |_|
8DEPRECATED source-compatibility shim for pre-cppzmq downstream agents
9Copyright (C) 2026 Paolo Bosetti
10*/
11
58#pragma once
59#ifdef _WIN32
60#ifndef NOMINMAX
61#define NOMINMAX
62#endif
63#ifndef WIN32_LEAN_AND_MEAN
64#define WIN32_LEAN_AND_MEAN
65#endif
66#include <winsock2.h>
67#endif
68
69#include <cstring>
70#include <string>
71#include <utility>
72
73#include <zmq.hpp>
74#include <zmq_addon.hpp>
75
76#include "zap_auth.hpp"
77
78#if defined(MADS_NO_ZMQPP_COMPAT_WARNING)
79#define MADS_ZMQPP_DEPRECATED(msg)
80#else
81#define MADS_ZMQPP_DEPRECATED(msg) [[deprecated(msg)]]
82#endif
83
91namespace zmqpp {
92
94using socket_type = zmq::socket_type;
95
97using exception = zmq::error_t;
99using zmq_internal_exception = zmq::error_t;
100
108namespace socket_option {
109inline constexpr auto linger = zmq::sockopt::linger;
110inline constexpr auto receive_timeout = zmq::sockopt::rcvtimeo;
111inline constexpr auto send_timeout = zmq::sockopt::sndtimeo;
112inline constexpr auto conflate = zmq::sockopt::conflate;
113inline constexpr auto receive_high_water_mark = zmq::sockopt::rcvhwm;
114inline constexpr auto send_high_water_mark = zmq::sockopt::sndhwm;
115inline constexpr auto identity = zmq::sockopt::routing_id;
116inline constexpr auto curve_server = zmq::sockopt::curve_server;
117inline constexpr auto curve_public_key = zmq::sockopt::curve_publickey;
118inline constexpr auto curve_secret_key = zmq::sockopt::curve_secretkey;
119inline constexpr auto curve_server_key = zmq::sockopt::curve_serverkey;
120} // namespace socket_option
121
125class MADS_ZMQPP_DEPRECATED("use zmq::context_t") context
126 : public zmq::context_t {
127public:
128 using zmq::context_t::context_t;
129
131 void terminate() { close(); }
132};
133
141class MADS_ZMQPP_DEPRECATED("use zmq::multipart_t") message
142 : public zmq::multipart_t {
143public:
144 using zmq::multipart_t::multipart_t;
145
146 message() = default;
147 message(zmq::multipart_t &&other) noexcept
148 : zmq::multipart_t(std::move(other)) {}
149
151 size_t parts() const { return size(); }
152
154 std::string get(size_t n) const { return at(n).to_string(); }
155
157 template <typename T> T get(size_t n) const {
158 T value{};
159 const auto &frame = at(n);
160 std::memcpy(&value, frame.data(),
161 frame.size() < sizeof(T) ? frame.size() : sizeof(T));
162 return value;
163 }
164
166 const void *raw_data(size_t n) const { return at(n).data(); }
167
169 size_t size(size_t n) const { return at(n).size(); }
170
172 size_t size() const { return zmq::multipart_t::size(); }
173
175 void add_raw(const void *data, size_t len) { addmem(data, len); }
176
178 message copy() const { return message(clone()); }
179
181 message &operator<<(const std::string &part) {
182 addstr(part);
183 return *this;
184 }
185 message &operator<<(const char *part) {
186 addstr(std::string(part));
187 return *this;
188 }
189
191 message &operator>>(std::string &part) {
192 part = popstr();
193 return *this;
194 }
195};
196
203class MADS_ZMQPP_DEPRECATED("use zmq::socket_t") socket
204 : public zmq::socket_t {
205public:
206 using zmq::socket_t::socket_t;
207
208 socket(zmq::context_t &ctx, socket_type type) : zmq::socket_t(ctx, type) {}
209
211 void subscribe(const std::string &topic) {
212 set(zmq::sockopt::subscribe, topic);
213 }
214
216 void unsubscribe(const std::string &topic) {
217 set(zmq::sockopt::unsubscribe, topic);
218 }
219
221 bool send(zmq::multipart_t &msg, bool dont_block = false) {
222 return msg.send(*this, dont_block ? ZMQ_DONTWAIT : 0);
223 }
224
226 bool send(const std::string &payload, bool dont_block = false) {
227 return zmq::socket_t::send(zmq::buffer(payload),
228 dont_block ? zmq::send_flags::dontwait
229 : zmq::send_flags::none)
230 .has_value();
231 }
232
234 bool receive(zmq::multipart_t &msg, bool dont_block = false) {
235 return msg.recv(*this, dont_block ? ZMQ_DONTWAIT : 0);
236 }
237
239 template <typename Opt, typename T> void get(Opt opt, T &out) const {
240 out = zmq::socket_t::get(opt);
241 }
242
243 using zmq::socket_t::get;
244};
245
247namespace curve {
248
251
254
255} // namespace curve
256
257} // namespace zmqpp
CurveKeypair generate_keypair()
Generate a fresh CURVE keypair.
keypair generate_keypair()
Generate a CURVE keypair.
constexpr auto curve_server_key
constexpr auto curve_public_key
constexpr auto receive_high_water_mark
constexpr auto conflate
constexpr auto send_timeout
constexpr auto curve_server
constexpr auto identity
constexpr auto send_high_water_mark
constexpr auto linger
constexpr auto receive_timeout
constexpr auto curve_secret_key
Minimal stand-in for the parts of zmqpp that MADS used to expose.
zmq::error_t zmq_internal_exception
zmqpp raised this for libzmq-level failures; cppzmq has one type.
zmq::socket_type socket_type
Socket types, spelled as in zmqpp (socket_type::pub, ...).
zmq::error_t exception
zmqpp's single exception type maps onto cppzmq's.
A CURVE keypair, both keys Z85-encoded (40 characters each).
Definition zap_auth.hpp:166
#define MADS_ZMQPP_DEPRECATED(msg)