26 #ifndef __ZMQ_HPP_INCLUDED__
27 #define __ZMQ_HPP_INCLUDED__
38 #if defined(_MSVC_LANG)
39 #define CPPZMQ_LANG _MSVC_LANG
41 #define CPPZMQ_LANG __cplusplus
44 #if defined(_HAS_CXX14) && _HAS_CXX14 && CPPZMQ_LANG < 201402L
46 #define CPPZMQ_LANG 201402L
48 #if defined(_HAS_CXX17) && _HAS_CXX17 && CPPZMQ_LANG < 201703L
50 #define CPPZMQ_LANG 201703L
54 #if CPPZMQ_LANG >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900)
57 #if CPPZMQ_LANG >= 201402L
60 #if CPPZMQ_LANG >= 201703L
64 #if defined(ZMQ_CPP14) && !defined(_MSC_VER)
65 #define ZMQ_DEPRECATED(msg) [[deprecated(msg)]]
66 #elif defined(_MSC_VER)
67 #define ZMQ_DEPRECATED(msg) __declspec(deprecated(msg))
68 #elif defined(__GNUC__)
69 #define ZMQ_DEPRECATED(msg) __attribute__((deprecated(msg)))
72 #if defined(ZMQ_CPP17)
73 #define ZMQ_NODISCARD [[nodiscard]]
78 #if defined(ZMQ_CPP11)
79 #define ZMQ_NOTHROW noexcept
80 #define ZMQ_EXPLICIT explicit
81 #define ZMQ_OVERRIDE override
82 #define ZMQ_NULLPTR nullptr
83 #define ZMQ_CONSTEXPR_FN constexpr
84 #define ZMQ_CONSTEXPR_VAR constexpr
85 #define ZMQ_CPP11_DEPRECATED(msg) ZMQ_DEPRECATED(msg)
87 #define ZMQ_NOTHROW throw()
91 #define ZMQ_CONSTEXPR_FN
92 #define ZMQ_CONSTEXPR_VAR const
93 #define ZMQ_CPP11_DEPRECATED(msg)
95 #if defined(ZMQ_CPP14) && (!defined(_MSC_VER) || _MSC_VER > 1900)
96 #define ZMQ_EXTENDED_CONSTEXPR
98 #if defined(ZMQ_CPP17)
99 #define ZMQ_INLINE_VAR inline
101 #define ZMQ_INLINE_VAR
120 #if defined(__has_include) && defined(ZMQ_CPP17)
121 #define CPPZMQ_HAS_INCLUDE_CPP17(X) __has_include(X)
123 #define CPPZMQ_HAS_INCLUDE_CPP17(X) 0
126 #if CPPZMQ_HAS_INCLUDE_CPP17(<optional>) && !defined(CPPZMQ_HAS_OPTIONAL)
127 #define CPPZMQ_HAS_OPTIONAL 1
129 #ifndef CPPZMQ_HAS_OPTIONAL
130 #define CPPZMQ_HAS_OPTIONAL 0
131 #elif CPPZMQ_HAS_OPTIONAL
135 #if CPPZMQ_HAS_INCLUDE_CPP17(<string_view>) && !defined(CPPZMQ_HAS_STRING_VIEW)
136 #define CPPZMQ_HAS_STRING_VIEW 1
138 #ifndef CPPZMQ_HAS_STRING_VIEW
139 #define CPPZMQ_HAS_STRING_VIEW 0
140 #elif CPPZMQ_HAS_STRING_VIEW
141 #include <string_view>
145 #define CPPZMQ_VERSION_MAJOR 4
146 #define CPPZMQ_VERSION_MINOR 7
147 #define CPPZMQ_VERSION_PATCH 1
149 #define CPPZMQ_VERSION \
150 ZMQ_MAKE_VERSION(CPPZMQ_VERSION_MAJOR, CPPZMQ_VERSION_MINOR, CPPZMQ_VERSION_PATCH)
153 #if(defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 2)) && \
154 defined(__GXX_EXPERIMENTAL_CXX0X__))
155 #define ZMQ_HAS_RVALUE_REFS
156 #define ZMQ_DELETED_FUNCTION = delete
157 #elif defined(__clang__)
158 #if __has_feature(cxx_rvalue_references)
159 #define ZMQ_HAS_RVALUE_REFS
162 #if __has_feature(cxx_deleted_functions)
163 #define ZMQ_DELETED_FUNCTION = delete
165 #define ZMQ_DELETED_FUNCTION
167 #elif defined(_MSC_VER) && (_MSC_VER >= 1900)
168 #define ZMQ_HAS_RVALUE_REFS
169 #define ZMQ_DELETED_FUNCTION = delete
170 #elif defined(_MSC_VER) && (_MSC_VER >= 1600)
171 #define ZMQ_HAS_RVALUE_REFS
172 #define ZMQ_DELETED_FUNCTION
174 #define ZMQ_DELETED_FUNCTION
177 #if defined(ZMQ_CPP11) && !defined(__llvm__) && !defined(__INTEL_COMPILER) && \
178 defined(__GNUC__) && __GNUC__ < 5
179 #define ZMQ_CPP11_PARTIAL
180 #elif defined(__GLIBCXX__) && __GLIBCXX__ < 20160805
183 #define ZMQ_CPP11_PARTIAL
187 #ifdef ZMQ_CPP11_PARTIAL
188 #define ZMQ_IS_TRIVIALLY_COPYABLE(T) __has_trivial_copy(T)
190 #include <type_traits>
191 #define ZMQ_IS_TRIVIALLY_COPYABLE(T) std::is_trivially_copyable<T>::value
195 #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(3, 3, 0)
196 #define ZMQ_NEW_MONITOR_EVENT_LAYOUT
199 #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 1, 0)
200 #define ZMQ_HAS_PROXY_STEERABLE
210 #if ZMQ_VERSION < ZMQ_MAKE_VERSION(3, 2, 0)
211 #define zmq_msg_recv(msg, socket, flags) zmq_recvmsg(socket, msg, flags)
217 #define ZMQ_ASSERT(expression) assert(expression)
219 #define ZMQ_ASSERT(expression) (void)(expression)
232 auto begin(T&& r) -> decltype(
begin(std::forward<T>(r)))
234 return begin(std::forward<T>(r));
237 auto end(T&& r) -> decltype(
end(std::forward<T>(r)))
239 return end(std::forward<T>(r));
246 template <
class Iter>
247 using iter_value_t =
typename std::iterator_traits<Iter>::value_type;
249 template <
class Range>
253 template <
class Range>
256 template <
class T,
class =
void>
257 struct is_range : std::false_type
264 std::declval<typename std::remove_reference<T>::type&>()) ==
266 std::declval<typename std::remove_reference<T>::type&>()))>>
277 class error_t :
public std::exception
286 return zmq_strerror(
errnum);
297 inline int poll(zmq_pollitem_t* items_,
size_t nitems_,
long timeout_ = -1)
299 int rc = zmq_poll(items_,
static_cast<int>(nitems_), timeout_);
306 inline
int poll(zmq_pollitem_t const* items_,
size_t nitems_,
long timeout_ = -1)
308 return poll(
const_cast<zmq_pollitem_t*
>(items_), nitems_, timeout_);
313 inline
int poll(zmq_pollitem_t const* items,
size_t nitems,
314 std::chrono::milliseconds timeout)
316 return poll(
const_cast<zmq_pollitem_t*
>(items), nitems,
317 static_cast<long>(timeout.count()));
321 inline
int poll(
std::vector<zmq_pollitem_t> const& items,
322 std::chrono::milliseconds timeout)
324 return poll(
const_cast<zmq_pollitem_t*
>(items.data()), items.size(),
325 static_cast<long>(timeout.count()));
329 inline
int poll(
std::vector<zmq_pollitem_t> const& items,
long timeout_ = -1)
331 return poll(
const_cast<zmq_pollitem_t*
>(items.data()), items.size(), timeout_);
334 inline int poll(zmq_pollitem_t* items,
size_t nitems, std::chrono::milliseconds timeout)
336 return poll(items, nitems,
static_cast<long>(timeout.count()));
339 inline int poll(std::vector<zmq_pollitem_t>& items, std::chrono::milliseconds timeout)
341 return poll(items.data(), items.size(),
static_cast<long>(timeout.count()));
344 ZMQ_DEPRECATED(
"from 4.3.1, use poll taking std::chrono instead of long")
345 inline
int poll(
std::vector<zmq_pollitem_t>& items,
long timeout_ = -1)
347 return poll(items.data(), items.size(), timeout_);
350 template <std::
size_t SIZE>
351 inline int poll(std::array<zmq_pollitem_t, SIZE>& items,
352 std::chrono::milliseconds timeout)
354 return poll(items.data(), items.size(),
static_cast<long>(timeout.count()));
358 inline void version(
int* major_,
int* minor_,
int* patch_)
360 zmq_version(major_, minor_, patch_);
364 inline std::tuple<int, int, int>
version()
366 std::tuple<int, int, int> v;
367 zmq_version(&std::get<0>(v), &std::get<1>(v), &std::get<2>(v));
371 #if !defined(ZMQ_CPP11_PARTIAL)
378 static constexpr
bool value =
392 int rc = zmq_msg_init(&
msg);
398 int rc = zmq_msg_init_size(&
msg, size_);
403 template <
class ForwardIter>
406 typedef typename std::iterator_traits<ForwardIter>::value_type
value_t;
408 assert(std::distance(first, last) >= 0);
410 static_cast<size_t>(std::distance(first, last)) *
sizeof(
value_t);
411 int const rc = zmq_msg_init_size(&
msg, size_);
414 std::copy(first, last, data<value_t>());
419 int rc = zmq_msg_init_size(&
msg, size_);
426 memcpy(
data(), data_, size_);
432 int rc = zmq_msg_init_data(&
msg, data_, size_, ffn_, hint_);
438 #if defined(ZMQ_CPP11) && !defined(ZMQ_CPP11_PARTIAL)
443 template <
class Char,
size_t N,
445 ZMQ_DEPRECATED(
"from 4.7.0, use constructors taking iterators, (pointer, size) "
446 "or strings instead")
454 !detail::is_char_type<detail::range_value_t<Range>>::value &&
463 #if CPPZMQ_HAS_STRING_VIEW
470 #ifdef ZMQ_HAS_RVALUE_REFS
473 int rc = zmq_msg_init(&rhs.msg);
486 int rc = zmq_msg_close(&
msg);
492 int rc = zmq_msg_close(&
msg);
495 rc = zmq_msg_init(&
msg);
501 int rc = zmq_msg_close(&
msg);
504 rc = zmq_msg_init_size(&
msg, size_);
511 int rc = zmq_msg_close(&
msg);
514 rc = zmq_msg_init_size(&
msg, size_);
517 memcpy(
data(), data_, size_);
522 int rc = zmq_msg_close(&
msg);
525 rc = zmq_msg_init_data(&
msg, data_, size_, ffn_, hint_);
530 ZMQ_DEPRECATED(
"from 4.3.1, use move taking non-const reference instead")
533 int rc = zmq_msg_move(&
msg,
const_cast<zmq_msg_t*
>(msg_->handle()));
540 int rc = zmq_msg_move(&
msg, msg_.handle());
545 ZMQ_DEPRECATED(
"from 4.3.1, use copy taking non-const reference instead")
548 int rc = zmq_msg_copy(&
msg,
const_cast<zmq_msg_t*
>(msg_->handle()));
555 int rc = zmq_msg_copy(&
msg, msg_.handle());
562 int rc = zmq_msg_more(
const_cast<zmq_msg_t*
>(&
msg));
568 return zmq_msg_data(&
msg);
573 return zmq_msg_data(
const_cast<zmq_msg_t*
>(&
msg));
578 return zmq_msg_size(
const_cast<zmq_msg_t*
>(&
msg));
586 template <
typename T>
589 return static_cast<T*
>(
data());
592 template <
typename T>
595 return static_cast<T const*
>(
data());
601 return *
this == *other;
606 const size_t my_size =
size();
607 return my_size == other.size() && 0 == memcmp(
data(), other.data(), my_size);
612 return !(*
this == other);
615 #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(3, 2, 0)
618 int value = zmq_msg_get(&
msg, property_);
625 #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 1, 0)
626 const char*
gets(
const char* property_)
628 const char* value = zmq_msg_gets(&
msg, property_);
635 #if defined(ZMQ_BUILD_DRAFT_API) && ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 0)
636 uint32_t routing_id()
const
638 return zmq_msg_routing_id(
const_cast<zmq_msg_t*
>(&
msg));
641 void set_routing_id(uint32_t routing_id)
643 int rc = zmq_msg_set_routing_id(&
msg, routing_id);
648 const char* group()
const
650 return zmq_msg_group(
const_cast<zmq_msg_t*
>(&
msg));
653 void set_group(
const char* group)
655 int rc = zmq_msg_set_group(&
msg, group);
664 return std::string(
static_cast<const char*
>(
data()),
size());
666 #if CPPZMQ_HAS_STRING_VIEW
683 std::stringstream os;
685 const unsigned char* msg_data = this->data<unsigned char>();
690 os <<
"zmq::message_t [size " << std::dec << std::setw(3) << std::setfill(
'0') <<
size
695 os <<
"... too big to print)";
703 is_ascii[1] = (
byte >= 32 &&
byte < 127);
714 <<
static_cast<short>(
byte);
759 #ifdef ZMQ_IO_THREADS
760 io_threads = ZMQ_IO_THREADS,
762 #ifdef ZMQ_THREAD_SCHED_POLICY
763 thread_sched_policy = ZMQ_THREAD_SCHED_POLICY,
765 #ifdef ZMQ_THREAD_PRIORITY
766 thread_priority = ZMQ_THREAD_PRIORITY,
768 #ifdef ZMQ_THREAD_AFFINITY_CPU_ADD
769 thread_affinity_cpu_add = ZMQ_THREAD_AFFINITY_CPU_ADD,
771 #ifdef ZMQ_THREAD_AFFINITY_CPU_REMOVE
772 thread_affinity_cpu_remove = ZMQ_THREAD_AFFINITY_CPU_REMOVE,
774 #ifdef ZMQ_THREAD_NAME_PREFIX
775 thread_name_prefix = ZMQ_THREAD_NAME_PREFIX,
778 max_msgsz = ZMQ_MAX_MSGSZ,
780 #ifdef ZMQ_ZERO_COPY_RECV
781 zero_copy_recv = ZMQ_ZERO_COPY_RECV,
783 #ifdef ZMQ_MAX_SOCKETS
784 max_sockets = ZMQ_MAX_SOCKETS,
786 #ifdef ZMQ_SOCKET_LIMIT
787 socket_limit = ZMQ_SOCKET_LIMIT,
792 #ifdef ZMQ_MSG_T_SIZE
793 msg_t_size = ZMQ_MSG_T_SIZE
808 explicit context_t(
int io_threads_,
int max_sockets_ = ZMQ_MAX_SOCKETS_DFLT)
814 int rc = zmq_ctx_set(
ptr, ZMQ_IO_THREADS, io_threads_);
817 rc = zmq_ctx_set(
ptr, ZMQ_MAX_SOCKETS, max_sockets_);
821 #ifdef ZMQ_HAS_RVALUE_REFS
842 int rc = zmq_ctx_set(
ptr, option_, optval_);
850 return zmq_ctx_get(
ptr, option_);
854 void set(ctxopt option,
int optval)
856 int rc = zmq_ctx_set(
ptr,
static_cast<int>(option), optval);
863 int rc = zmq_ctx_get(
ptr,
static_cast<int>(option));
882 rc = zmq_ctx_destroy(
ptr);
883 }
while(rc == -1 && errno == EINTR);
896 int rc = zmq_ctx_shutdown(
ptr);
943 struct recv_buffer_size
946 size_t untruncated_size;
950 return size != untruncated_size;
954 #if CPPZMQ_HAS_OPTIONAL
956 using send_result_t = std::optional<size_t>;
957 using recv_result_t = std::optional<size_t>;
958 using recv_buffer_result_t = std::optional<recv_buffer_size>;
967 class trivial_optional
971 using value_type = T;
973 trivial_optional() =
default;
974 trivial_optional(T value) noexcept : _value(value), _has_value(
true)
977 const T* operator->() const noexcept
982 T* operator->() noexcept
988 const T& operator*() const noexcept
993 T& operator*() noexcept
1002 throw std::exception();
1005 const T&
value()
const
1008 throw std::exception();
1012 explicit operator bool() const noexcept
1016 bool has_value() const noexcept
1023 bool _has_value{
false };
1027 using send_result_t = detail::trivial_optional<size_t>;
1028 using recv_result_t = detail::trivial_optional<size_t>;
1029 using recv_buffer_result_t = detail::trivial_optional<recv_buffer_size>;
1036 constexpr T enum_bit_or(T a, T b) noexcept
1039 using U =
typename std::underlying_type<T>::type;
1040 return static_cast<T
>(
static_cast<U
>(a) |
static_cast<U
>(b));
1043 constexpr T enum_bit_and(T a, T b) noexcept
1046 using U =
typename std::underlying_type<T>::type;
1047 return static_cast<T
>(
static_cast<U
>(a) &
static_cast<U
>(b));
1050 constexpr T enum_bit_xor(T a, T b) noexcept
1053 using U =
typename std::underlying_type<T>::type;
1054 return static_cast<T
>(
static_cast<U
>(a) ^
static_cast<U
>(b));
1057 constexpr T enum_bit_not(T a) noexcept
1060 using U =
typename std::underlying_type<T>::type;
1061 return static_cast<T
>(~static_cast<U>(a));
1066 enum class send_flags : int
1069 dontwait = ZMQ_DONTWAIT,
1070 sndmore = ZMQ_SNDMORE
1073 constexpr send_flags
operator|(send_flags a, send_flags b) noexcept
1075 return detail::enum_bit_or(a, b);
1077 constexpr send_flags
operator&(send_flags a, send_flags b) noexcept
1079 return detail::enum_bit_and(a, b);
1081 constexpr send_flags
operator^(send_flags a, send_flags b) noexcept
1083 return detail::enum_bit_xor(a, b);
1085 constexpr send_flags
operator~(send_flags a) noexcept
1087 return detail::enum_bit_not(a);
1091 enum class recv_flags : int
1094 dontwait = ZMQ_DONTWAIT
1097 constexpr recv_flags
operator|(recv_flags a, recv_flags b) noexcept
1099 return detail::enum_bit_or(a, b);
1101 constexpr recv_flags
operator&(recv_flags a, recv_flags b) noexcept
1103 return detail::enum_bit_and(a, b);
1105 constexpr recv_flags
operator^(recv_flags a, recv_flags b) noexcept
1107 return detail::enum_bit_xor(a, b);
1109 constexpr recv_flags
operator~(recv_flags a) noexcept
1111 return detail::enum_bit_not(a);
1118 class mutable_buffer
1121 constexpr mutable_buffer() noexcept : _data(
nullptr), _size(0)
1123 constexpr mutable_buffer(
void*
p,
size_t n) noexcept : _data(
p), _size(
n)
1125 #ifdef ZMQ_EXTENDED_CONSTEXPR
1126 assert(
p !=
nullptr ||
n == 0);
1130 constexpr
void* data() const noexcept
1134 constexpr
size_t size() const noexcept
1138 mutable_buffer& operator+=(
size_t n) noexcept
1141 const auto shift = (std::min)(
n, _size);
1142 _data =
static_cast<char*
>(_data) + shift;
1152 inline mutable_buffer
operator+(
const mutable_buffer& mb,
size_t n) noexcept
1154 return mutable_buffer(
static_cast<char*
>(mb.data()) + (std::min)(
n, mb.size()),
1155 mb.size() - (std::min)(
n, mb.size()));
1157 inline mutable_buffer
operator+(
size_t n,
const mutable_buffer& mb) noexcept
1165 constexpr const_buffer() noexcept : _data(
nullptr), _size(0)
1167 constexpr const_buffer(
const void*
p,
size_t n) noexcept : _data(
p), _size(
n)
1169 #ifdef ZMQ_EXTENDED_CONSTEXPR
1170 assert(
p !=
nullptr ||
n == 0);
1173 constexpr const_buffer(
const mutable_buffer& mb) noexcept
1174 : _data(mb.data()), _size(mb.size())
1177 constexpr
const void* data() const noexcept
1181 constexpr
size_t size() const noexcept
1185 const_buffer& operator+=(
size_t n) noexcept
1187 const auto shift = (std::min)(
n, _size);
1188 _data =
static_cast<const char*
>(_data) + shift;
1198 inline const_buffer
operator+(
const const_buffer& cb,
size_t n) noexcept
1200 return const_buffer(
static_cast<const char*
>(cb.data()) + (std::min)(
n, cb.size()),
1201 cb.size() - (std::min)(
n, cb.size()));
1203 inline const_buffer
operator+(
size_t n,
const const_buffer& cb) noexcept
1210 constexpr mutable_buffer
buffer(
void*
p,
size_t n) noexcept
1212 return mutable_buffer(
p,
n);
1214 constexpr const_buffer
buffer(
const void*
p,
size_t n) noexcept
1216 return const_buffer(
p,
n);
1218 constexpr mutable_buffer
buffer(
const mutable_buffer& mb) noexcept
1222 inline mutable_buffer
buffer(
const mutable_buffer& mb,
size_t n) noexcept
1224 return mutable_buffer(mb.data(), (std::min)(mb.size(),
n));
1226 constexpr const_buffer
buffer(
const const_buffer& cb) noexcept
1230 inline const_buffer
buffer(
const const_buffer& cb,
size_t n) noexcept
1232 return const_buffer(cb.data(), (std::min)(cb.size(),
n));
1240 static constexpr
bool value =
1251 static constexpr
bool value =
1256 constexpr
auto seq_size(
const C& c) noexcept -> decltype(c.size())
1260 template <
class T,
size_t N>
1261 constexpr
size_t seq_size(
const T (& )[N]) noexcept
1266 template <
class Seq>
1267 auto buffer_contiguous_sequence(Seq&& seq) noexcept
1270 using T =
typename std::remove_cv<
1271 typename std::remove_reference<decltype(*
std::begin(seq))>::type>::type;
1274 const auto size = seq_size(seq);
1278 template <
class Seq>
1279 auto buffer_contiguous_sequence(Seq&& seq,
size_t n_bytes) noexcept
1280 -> decltype(buffer_contiguous_sequence(seq))
1282 using T =
typename std::remove_cv<
1283 typename std::remove_reference<decltype(*
std::begin(seq))>::type>::type;
1286 const auto size = seq_size(seq);
1288 (std::min)(
size *
sizeof(T), n_bytes));
1294 template <
class T,
size_t N>
1295 mutable_buffer
buffer(T (&data)[N]) noexcept
1297 return detail::buffer_contiguous_sequence(data);
1299 template <
class T,
size_t N>
1300 mutable_buffer
buffer(T (&data)[N],
size_t n_bytes) noexcept
1302 return detail::buffer_contiguous_sequence(data, n_bytes);
1304 template <
class T,
size_t N>
1305 const_buffer
buffer(
const T (&data)[N]) noexcept
1307 return detail::buffer_contiguous_sequence(data);
1309 template <
class T,
size_t N>
1310 const_buffer
buffer(
const T (&data)[N],
size_t n_bytes) noexcept
1312 return detail::buffer_contiguous_sequence(data, n_bytes);
1315 template <
class T,
size_t N>
1316 mutable_buffer
buffer(std::array<T, N>& data) noexcept
1318 return detail::buffer_contiguous_sequence(data);
1320 template <
class T,
size_t N>
1321 mutable_buffer
buffer(std::array<T, N>& data,
size_t n_bytes) noexcept
1323 return detail::buffer_contiguous_sequence(data, n_bytes);
1325 template <
class T,
size_t N>
1326 const_buffer
buffer(std::array<const T, N>& data) noexcept
1328 return detail::buffer_contiguous_sequence(data);
1330 template <
class T,
size_t N>
1331 const_buffer
buffer(std::array<const T, N>& data,
size_t n_bytes) noexcept
1333 return detail::buffer_contiguous_sequence(data, n_bytes);
1335 template <
class T,
size_t N>
1336 const_buffer
buffer(
const std::array<T, N>& data) noexcept
1338 return detail::buffer_contiguous_sequence(data);
1340 template <
class T,
size_t N>
1341 const_buffer
buffer(
const std::array<T, N>& data,
size_t n_bytes) noexcept
1343 return detail::buffer_contiguous_sequence(data, n_bytes);
1346 template <
class T,
class Allocator>
1347 mutable_buffer
buffer(std::vector<T, Allocator>& data) noexcept
1349 return detail::buffer_contiguous_sequence(data);
1351 template <
class T,
class Allocator>
1352 mutable_buffer
buffer(std::vector<T, Allocator>& data,
size_t n_bytes) noexcept
1354 return detail::buffer_contiguous_sequence(data, n_bytes);
1356 template <
class T,
class Allocator>
1357 const_buffer
buffer(
const std::vector<T, Allocator>& data) noexcept
1359 return detail::buffer_contiguous_sequence(data);
1361 template <
class T,
class Allocator>
1362 const_buffer
buffer(
const std::vector<T, Allocator>& data,
size_t n_bytes) noexcept
1364 return detail::buffer_contiguous_sequence(data, n_bytes);
1367 template <
class T,
class Traits,
class Allocator>
1368 mutable_buffer
buffer(std::basic_string<T, Traits, Allocator>& data) noexcept
1370 return detail::buffer_contiguous_sequence(data);
1372 template <
class T,
class Traits,
class Allocator>
1373 mutable_buffer
buffer(std::basic_string<T, Traits, Allocator>& data,
1374 size_t n_bytes) noexcept
1376 return detail::buffer_contiguous_sequence(data, n_bytes);
1378 template <
class T,
class Traits,
class Allocator>
1379 const_buffer
buffer(
const std::basic_string<T, Traits, Allocator>& data) noexcept
1381 return detail::buffer_contiguous_sequence(data);
1383 template <
class T,
class Traits,
class Allocator>
1384 const_buffer
buffer(
const std::basic_string<T, Traits, Allocator>& data,
1385 size_t n_bytes) noexcept
1387 return detail::buffer_contiguous_sequence(data, n_bytes);
1390 #if CPPZMQ_HAS_STRING_VIEW
1392 template <
class T,
class Traits>
1393 const_buffer
buffer(std::basic_string_view<T, Traits> data) noexcept
1395 return detail::buffer_contiguous_sequence(data);
1397 template <
class T,
class Traits>
1398 const_buffer
buffer(std::basic_string_view<T, Traits> data,
size_t n_bytes) noexcept
1400 return detail::buffer_contiguous_sequence(data, n_bytes);
1407 template <
class Char,
size_t N>
1408 constexpr const_buffer str_buffer(
const Char (&data)[N]) noexcept
1411 #ifdef ZMQ_EXTENDED_CONSTEXPR
1412 assert(data[N - 1] == Char{ 0 });
1414 return const_buffer(
static_cast<const Char*
>(data), (N - 1) *
sizeof(Char));
1419 constexpr const_buffer
operator"" _zbuf(
const char* str,
size_t len) noexcept
1421 return const_buffer(str, len *
sizeof(
char));
1423 constexpr const_buffer
operator"" _zbuf(
const wchar_t* str,
size_t len) noexcept
1425 return const_buffer(str, len *
sizeof(
wchar_t));
1427 constexpr const_buffer
operator"" _zbuf(
const char16_t* str,
size_t len) noexcept
1429 return const_buffer(str, len *
sizeof(char16_t));
1431 constexpr const_buffer
operator"" _zbuf(
const char32_t* str,
size_t len) noexcept
1433 return const_buffer(str, len *
sizeof(char32_t));
1447 template <
int Opt,
class T,
bool BoolUnit = false>
1448 struct integral_option
1456 template <
int Opt,
int NullTerm = 1>
1461 #define ZMQ_DEFINE_INTEGRAL_OPT(OPT, NAME, TYPE) \
1462 using NAME##_t = integral_option<OPT, TYPE, false>; \
1463 ZMQ_INLINE_VAR ZMQ_CONSTEXPR_VAR NAME##_t NAME \
1465 #define ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(OPT, NAME, TYPE) \
1466 using NAME##_t = integral_option<OPT, TYPE, true>; \
1467 ZMQ_INLINE_VAR ZMQ_CONSTEXPR_VAR NAME##_t NAME \
1469 #define ZMQ_DEFINE_ARRAY_OPT(OPT, NAME) \
1470 using NAME##_t = array_option<OPT>; \
1471 ZMQ_INLINE_VAR ZMQ_CONSTEXPR_VAR NAME##_t NAME \
1473 #define ZMQ_DEFINE_ARRAY_OPT_BINARY(OPT, NAME) \
1474 using NAME##_t = array_option<OPT, 0>; \
1475 ZMQ_INLINE_VAR ZMQ_CONSTEXPR_VAR NAME##_t NAME \
1477 #define ZMQ_DEFINE_ARRAY_OPT_BIN_OR_Z85(OPT, NAME) \
1478 using NAME##_t = array_option<OPT, 2>; \
1479 ZMQ_INLINE_VAR ZMQ_CONSTEXPR_VAR NAME##_t NAME \
1485 typedef unsigned __int64 cppzmq_fd_t;
1487 typedef unsigned int cppzmq_fd_t;
1490 typedef int cppzmq_fd_t;
1494 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_AFFINITY, affinity, uint64_t);
1497 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_BACKLOG, backlog,
int);
1499 #ifdef ZMQ_BINDTODEVICE
1500 ZMQ_DEFINE_ARRAY_OPT_BINARY(ZMQ_BINDTODEVICE, bindtodevice);
1503 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_CONFLATE, conflate,
int);
1505 #ifdef ZMQ_CONNECT_ROUTING_ID
1506 ZMQ_DEFINE_ARRAY_OPT(ZMQ_CONNECT_ROUTING_ID, connect_routing_id);
1508 #ifdef ZMQ_CONNECT_TIMEOUT
1509 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_CONNECT_TIMEOUT, connect_timeout,
int);
1511 #ifdef ZMQ_CURVE_PUBLICKEY
1512 ZMQ_DEFINE_ARRAY_OPT_BIN_OR_Z85(ZMQ_CURVE_PUBLICKEY, curve_publickey);
1514 #ifdef ZMQ_CURVE_SECRETKEY
1515 ZMQ_DEFINE_ARRAY_OPT_BIN_OR_Z85(ZMQ_CURVE_SECRETKEY, curve_secretkey);
1517 #ifdef ZMQ_CURVE_SERVER
1518 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_CURVE_SERVER, curve_server,
int);
1520 #ifdef ZMQ_CURVE_SERVERKEY
1521 ZMQ_DEFINE_ARRAY_OPT_BIN_OR_Z85(ZMQ_CURVE_SERVERKEY, curve_serverkey);
1524 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_EVENTS, events,
int);
1527 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_FD, fd, cppzmq_fd_t);
1529 #ifdef ZMQ_GSSAPI_PLAINTEXT
1530 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_GSSAPI_PLAINTEXT, gssapi_plaintext,
int);
1532 #ifdef ZMQ_GSSAPI_SERVER
1533 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_GSSAPI_SERVER, gssapi_server,
int);
1535 #ifdef ZMQ_GSSAPI_SERVICE_PRINCIPAL
1536 ZMQ_DEFINE_ARRAY_OPT(ZMQ_GSSAPI_SERVICE_PRINCIPAL, gssapi_service_principal);
1538 #ifdef ZMQ_GSSAPI_SERVICE_PRINCIPAL_NAMETYPE
1539 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_GSSAPI_SERVICE_PRINCIPAL_NAMETYPE,
1540 gssapi_service_principal_nametype,
int);
1542 #ifdef ZMQ_GSSAPI_PRINCIPAL
1543 ZMQ_DEFINE_ARRAY_OPT(ZMQ_GSSAPI_PRINCIPAL, gssapi_principal);
1545 #ifdef ZMQ_GSSAPI_PRINCIPAL_NAMETYPE
1546 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_GSSAPI_PRINCIPAL_NAMETYPE, gssapi_principal_nametype,
int);
1548 #ifdef ZMQ_HANDSHAKE_IVL
1549 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_HANDSHAKE_IVL, handshake_ivl,
int);
1551 #ifdef ZMQ_HEARTBEAT_IVL
1552 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_HEARTBEAT_IVL, heartbeat_ivl,
int);
1554 #ifdef ZMQ_HEARTBEAT_TIMEOUT
1555 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_HEARTBEAT_TIMEOUT, heartbeat_timeout,
int);
1557 #ifdef ZMQ_HEARTBEAT_TTL
1558 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_HEARTBEAT_TTL, heartbeat_ttl,
int);
1560 #ifdef ZMQ_IMMEDIATE
1561 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_IMMEDIATE, immediate,
int);
1563 #ifdef ZMQ_INVERT_MATCHING
1564 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_INVERT_MATCHING, invert_matching,
int);
1567 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_IPV6, ipv6,
int);
1569 #ifdef ZMQ_LAST_ENDPOINT
1570 ZMQ_DEFINE_ARRAY_OPT(ZMQ_LAST_ENDPOINT, last_endpoint);
1573 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_LINGER, linger,
int);
1575 #ifdef ZMQ_MAXMSGSIZE
1576 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_MAXMSGSIZE, maxmsgsize, int64_t);
1578 #ifdef ZMQ_MECHANISM
1579 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_MECHANISM, mechanism,
int);
1582 ZMQ_DEFINE_ARRAY_OPT(ZMQ_METADATA, metadata);
1584 #ifdef ZMQ_MULTICAST_HOPS
1585 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_MULTICAST_HOPS, multicast_hops,
int);
1587 #ifdef ZMQ_MULTICAST_LOOP
1588 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_MULTICAST_LOOP, multicast_loop,
int);
1590 #ifdef ZMQ_MULTICAST_MAXTPDU
1591 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_MULTICAST_MAXTPDU, multicast_maxtpdu,
int);
1593 #ifdef ZMQ_PLAIN_SERVER
1594 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_PLAIN_SERVER, plain_server,
int);
1596 #ifdef ZMQ_PLAIN_PASSWORD
1597 ZMQ_DEFINE_ARRAY_OPT(ZMQ_PLAIN_PASSWORD, plain_password);
1599 #ifdef ZMQ_PLAIN_USERNAME
1600 ZMQ_DEFINE_ARRAY_OPT(ZMQ_PLAIN_USERNAME, plain_username);
1603 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_USE_FD, use_fd,
int);
1605 #ifdef ZMQ_PROBE_ROUTER
1606 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_PROBE_ROUTER, probe_router,
int);
1609 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_RATE, rate,
int);
1612 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_RCVBUF, rcvbuf,
int);
1615 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_RCVHWM, rcvhwm,
int);
1618 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_RCVMORE, rcvmore,
int);
1621 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_RCVTIMEO, rcvtimeo,
int);
1623 #ifdef ZMQ_RECONNECT_IVL
1624 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_RECONNECT_IVL, reconnect_ivl,
int);
1626 #ifdef ZMQ_RECONNECT_IVL_MAX
1627 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_RECONNECT_IVL_MAX, reconnect_ivl_max,
int);
1629 #ifdef ZMQ_RECOVERY_IVL
1630 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_RECOVERY_IVL, recovery_ivl,
int);
1632 #ifdef ZMQ_REQ_CORRELATE
1633 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_REQ_CORRELATE, req_correlate,
int);
1635 #ifdef ZMQ_REQ_RELAXED
1636 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_REQ_RELAXED, req_relaxed,
int);
1638 #ifdef ZMQ_ROUTER_HANDOVER
1639 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_ROUTER_HANDOVER, router_handover,
int);
1641 #ifdef ZMQ_ROUTER_MANDATORY
1642 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_ROUTER_MANDATORY, router_mandatory,
int);
1644 #ifdef ZMQ_ROUTER_NOTIFY
1645 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_ROUTER_NOTIFY, router_notify,
int);
1647 #ifdef ZMQ_ROUTING_ID
1648 ZMQ_DEFINE_ARRAY_OPT_BINARY(ZMQ_ROUTING_ID, routing_id);
1651 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_SNDBUF, sndbuf,
int);
1654 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_SNDHWM, sndhwm,
int);
1657 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_SNDTIMEO, sndtimeo,
int);
1659 #ifdef ZMQ_SOCKS_PROXY
1660 ZMQ_DEFINE_ARRAY_OPT(ZMQ_SOCKS_PROXY, socks_proxy);
1662 #ifdef ZMQ_STREAM_NOTIFY
1663 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_STREAM_NOTIFY, stream_notify,
int);
1665 #ifdef ZMQ_SUBSCRIBE
1666 ZMQ_DEFINE_ARRAY_OPT(ZMQ_SUBSCRIBE, subscribe);
1668 #ifdef ZMQ_TCP_KEEPALIVE
1669 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TCP_KEEPALIVE, tcp_keepalive,
int);
1671 #ifdef ZMQ_TCP_KEEPALIVE_CNT
1672 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TCP_KEEPALIVE_CNT, tcp_keepalive_cnt,
int);
1674 #ifdef ZMQ_TCP_KEEPALIVE_IDLE
1675 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TCP_KEEPALIVE_IDLE, tcp_keepalive_idle,
int);
1677 #ifdef ZMQ_TCP_KEEPALIVE_INTVL
1678 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TCP_KEEPALIVE_INTVL, tcp_keepalive_intvl,
int);
1680 #ifdef ZMQ_TCP_MAXRT
1681 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TCP_MAXRT, tcp_maxrt,
int);
1683 #ifdef ZMQ_THREAD_SAFE
1684 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_THREAD_SAFE, thread_safe,
int);
1687 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TOS, tos,
int);
1690 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TYPE, type,
int);
1692 #ifdef ZMQ_UNSUBSCRIBE
1693 ZMQ_DEFINE_ARRAY_OPT(ZMQ_UNSUBSCRIBE, unsubscribe);
1695 #ifdef ZMQ_VMCI_BUFFER_SIZE
1696 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_VMCI_BUFFER_SIZE, vmci_buffer_size, uint64_t);
1698 #ifdef ZMQ_VMCI_BUFFER_MIN_SIZE
1699 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_VMCI_BUFFER_MIN_SIZE, vmci_buffer_min_size, uint64_t);
1701 #ifdef ZMQ_VMCI_BUFFER_MAX_SIZE
1702 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_VMCI_BUFFER_MAX_SIZE, vmci_buffer_max_size, uint64_t);
1704 #ifdef ZMQ_VMCI_CONNECT_TIMEOUT
1705 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_VMCI_CONNECT_TIMEOUT, vmci_connect_timeout,
int);
1707 #ifdef ZMQ_XPUB_VERBOSE
1708 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_XPUB_VERBOSE, xpub_verbose,
int);
1710 #ifdef ZMQ_XPUB_VERBOSER
1711 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_XPUB_VERBOSER, xpub_verboser,
int);
1713 #ifdef ZMQ_XPUB_MANUAL
1714 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_XPUB_MANUAL, xpub_manual,
int);
1716 #ifdef ZMQ_XPUB_NODROP
1717 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_XPUB_NODROP, xpub_nodrop,
int);
1719 #ifdef ZMQ_XPUB_WELCOME_MSG
1720 ZMQ_DEFINE_ARRAY_OPT(ZMQ_XPUB_WELCOME_MSG, xpub_welcome_msg);
1722 #ifdef ZMQ_ZAP_ENFORCE_DOMAIN
1723 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_ZAP_ENFORCE_DOMAIN, zap_enforce_domain,
int);
1725 #ifdef ZMQ_ZAP_DOMAIN
1726 ZMQ_DEFINE_ARRAY_OPT(ZMQ_ZAP_DOMAIN, zap_domain);
1742 template <
typename T>
1752 int rc = zmq_setsockopt(_handle, option_, optval_, optvallen_);
1760 int rc = zmq_getsockopt(_handle, option_, optval_, optvallen_);
1765 template <
typename T>
1770 size_t optlen =
sizeof(T);
1778 template <
int Opt,
class T,
bool BoolUnit>
1779 void set(sockopt::integral_option<Opt, T, BoolUnit>,
const T& val)
1787 template <
int Opt,
class T>
1788 void set(sockopt::integral_option<Opt, T, true>,
bool val)
1797 template <
int Opt,
int NullTerm>
1798 void set(sockopt::array_option<Opt, NullTerm>,
const char* buf)
1805 template <
int Opt,
int NullTerm>
1806 void set(sockopt::array_option<Opt, NullTerm>, const_buffer buf)
1813 template <
int Opt,
int NullTerm>
1814 void set(sockopt::array_option<Opt, NullTerm>,
const std::string& buf)
1819 #if CPPZMQ_HAS_STRING_VIEW
1822 template <
int Opt,
int NullTerm>
1831 template <
int Opt,
class T,
bool BoolUnit>
1836 size_t size =
sizeof val;
1838 assert(
size ==
sizeof val);
1844 template <
int Opt,
int NullTerm>
1845 ZMQ_NODISCARD size_t get(sockopt::array_option<Opt, NullTerm>, mutable_buffer buf)
const
1847 size_t size = buf.size();
1856 template <
int Opt,
int NullTerm>
1858 size_t init_size = 1024)
const
1860 if(NullTerm == 2 && init_size == 1024)
1864 std::string str(init_size,
'\0');
1865 size_t size =
get(sockopt::array_option<Opt>{},
buffer(str));
1870 assert(str[
size - 1] ==
'\0');
1874 else if(NullTerm == 2)
1879 assert(str[
size - 1] ==
'\0');
1895 int rc = zmq_bind(_handle, addr_);
1907 int rc = zmq_unbind(_handle, addr_);
1919 int rc = zmq_connect(_handle, addr_);
1931 int rc = zmq_disconnect(_handle, addr_);
1944 int nbytes = zmq_send(_handle, buf_, len_,
flags_);
1946 return static_cast<size_t>(nbytes);
1947 if(zmq_errno() == EAGAIN)
1956 int nbytes = zmq_msg_send(msg_.handle(), _handle,
flags_);
1959 if(zmq_errno() == EAGAIN)
1964 template <
typename T>
1966 "ranges), and send_flags")
1970 int nbytes = zmq_msg_send(msg.handle(), _handle,
flags_);
1973 if(zmq_errno() == EAGAIN)
1978 #ifdef ZMQ_HAS_RVALUE_REFS
1984 return send(msg_,
static_cast<send_flags
>(
flags_)).has_value();
1992 send_result_t
send(const_buffer buf, send_flags flags = send_flags::none)
1994 const int nbytes = zmq_send(_handle, buf.data(), buf.size(),
static_cast<int>(flags));
1996 return static_cast<size_t>(nbytes);
1997 if(zmq_errno() == EAGAIN)
2002 send_result_t
send(message_t& msg, send_flags flags)
2004 int nbytes = zmq_msg_send(msg.handle(), _handle,
static_cast<int>(flags));
2006 return static_cast<size_t>(nbytes);
2007 if(zmq_errno() == EAGAIN)
2012 send_result_t
send(message_t&& msg, send_flags flags)
2014 return send(msg, flags);
2019 size_t recv(
void* buf_,
size_t len_,
int flags_ = 0)
2021 int nbytes = zmq_recv(_handle, buf_, len_,
flags_);
2023 return static_cast<size_t>(nbytes);
2024 if(zmq_errno() == EAGAIN)
2031 bool recv(message_t* msg_,
int flags_ = 0)
2033 int nbytes = zmq_msg_recv(msg_->handle(), _handle,
flags_);
2036 if(zmq_errno() == EAGAIN)
2043 recv_buffer_result_t recv(mutable_buffer buf, recv_flags flags = recv_flags::none)
2045 const int nbytes = zmq_recv(_handle, buf.data(), buf.size(),
static_cast<int>(flags));
2048 return recv_buffer_size{ (std::min)(
static_cast<size_t>(nbytes), buf.size()),
2049 static_cast<size_t>(nbytes) };
2051 if(zmq_errno() == EAGAIN)
2057 recv_result_t recv(message_t& msg, recv_flags flags = recv_flags::none)
2059 const int nbytes = zmq_msg_recv(msg.handle(), _handle,
static_cast<int>(flags));
2062 assert(msg.size() ==
static_cast<size_t>(nbytes));
2063 return static_cast<size_t>(nbytes);
2065 if(zmq_errno() == EAGAIN)
2071 #if defined(ZMQ_BUILD_DRAFT_API) && ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 0)
2072 void join(
const char* group)
2074 int rc = zmq_join(_handle, group);
2079 void leave(
const char* group)
2081 int rc = zmq_leave(_handle, group);
2111 void set_option(
int option_,
const void* optval_,
size_t optvallen_)
2113 int rc = zmq_setsockopt(_handle, option_, optval_, optvallen_);
2118 void get_option(
int option_,
void* optval_,
size_t* optvallen_)
const
2120 int rc = zmq_getsockopt(_handle, option_, optval_, optvallen_);
2128 enum class socket_type : int
2132 dealer = ZMQ_DEALER,
2133 router = ZMQ_ROUTER,
2140 #if defined(ZMQ_BUILD_DRAFT_API) && ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 0)
2141 server = ZMQ_SERVER,
2142 client = ZMQ_CLIENT,
2146 #if ZMQ_VERSION_MAJOR >= 4
2147 stream = ZMQ_STREAM,
2153 struct from_handle_t
2166 class socket_ref :
public detail::socket_base
2182 return sr.handle() ==
nullptr;
2186 return sr.handle() ==
nullptr;
2190 return !(sr ==
nullptr);
2194 return !(sr ==
nullptr);
2200 return std::equal_to<void*>()(a.handle(), b.handle());
2208 return std::less<void*>()(a.handle(), b.handle());
2233 return hash<void*>()(sr.handle());
2241 class socket_t :
public detail::socket_base
2258 :
socket_t(context_, static_cast<int>(type_))
2262 #ifdef ZMQ_HAS_RVALUE_REFS
2298 int rc = zmq_close(_handle);
2338 inline
void proxy(
void* frontend,
void* backend,
void*
capture)
2340 int rc = zmq_proxy(frontend, backend,
capture);
2345 inline void proxy(socket_ref frontend, socket_ref backend,
2346 socket_ref
capture = socket_ref())
2348 int rc = zmq_proxy(frontend.handle(), backend.handle(),
capture.handle());
2353 #ifdef ZMQ_HAS_PROXY_STEERABLE
2354 ZMQ_DEPRECATED(
"from 4.3.1, use proxy_steerable taking socket_t objects")
2357 int rc = zmq_proxy_steerable(frontend, backend,
capture,
control);
2365 int rc = zmq_proxy_steerable(frontend.handle(), backend.handle(),
capture.handle(),
2383 #ifdef ZMQ_HAS_RVALUE_REFS
2402 monitor(socket, addr.c_str(), events);
2407 init(socket, addr_, events);
2414 void init(
socket_t& socket, std::string
const& addr,
int events = ZMQ_EVENT_ALL)
2416 init(socket, addr.c_str(), events);
2421 int rc = zmq_socket_monitor(socket.handle(), addr_, events);
2437 zmq_msg_init(&eventMsg);
2445 if(items[0].revents & ZMQ_POLLIN)
2448 if(rc == -1 && zmq_errno() == ETERM)
2454 zmq_msg_close(&eventMsg);
2458 #if ZMQ_VERSION_MAJOR >= 4
2459 const char* data =
static_cast<const char*
>(zmq_msg_data(&eventMsg));
2461 memcpy(&msgEvent.
event, data,
sizeof(uint16_t));
2462 data +=
sizeof(uint16_t);
2463 memcpy(&msgEvent.
value, data,
sizeof(int32_t));
2469 #ifdef ZMQ_NEW_MONITOR_EVENT_LAYOUT
2471 zmq_msg_init(&addrMsg);
2473 if(rc == -1 && zmq_errno() == ETERM)
2475 zmq_msg_close(&eventMsg);
2480 const char* str =
static_cast<const char*
>(zmq_msg_data(&addrMsg));
2481 std::string address(str, str + zmq_msg_size(&addrMsg));
2482 zmq_msg_close(&addrMsg);
2485 std::string address =
event->data.connected.addr;
2488 #ifdef ZMQ_EVENT_MONITOR_STOPPED
2489 if(event->event == ZMQ_EVENT_MONITOR_STOPPED)
2491 zmq_msg_close(&eventMsg);
2497 switch(event->event)
2499 case ZMQ_EVENT_CONNECTED:
2502 case ZMQ_EVENT_CONNECT_DELAYED:
2505 case ZMQ_EVENT_CONNECT_RETRIED:
2508 case ZMQ_EVENT_LISTENING:
2511 case ZMQ_EVENT_BIND_FAILED:
2514 case ZMQ_EVENT_ACCEPTED:
2517 case ZMQ_EVENT_ACCEPT_FAILED:
2520 case ZMQ_EVENT_CLOSED:
2523 case ZMQ_EVENT_CLOSE_FAILED:
2526 case ZMQ_EVENT_DISCONNECTED:
2529 #ifdef ZMQ_BUILD_DRAFT_API
2530 #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 3)
2531 case ZMQ_EVENT_HANDSHAKE_FAILED_NO_DETAIL:
2534 case ZMQ_EVENT_HANDSHAKE_FAILED_PROTOCOL:
2537 case ZMQ_EVENT_HANDSHAKE_FAILED_AUTH:
2540 case ZMQ_EVENT_HANDSHAKE_SUCCEEDED:
2543 #elif ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 1)
2544 case ZMQ_EVENT_HANDSHAKE_FAILED:
2545 on_event_handshake_failed(*event, address.c_str());
2547 case ZMQ_EVENT_HANDSHAKE_SUCCEED:
2548 on_event_handshake_succeed(*event, address.c_str());
2556 zmq_msg_close(&eventMsg);
2561 #ifdef ZMQ_EVENT_MONITOR_STOPPED
2622 #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 3)
2646 #elif ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 1)
2647 virtual void on_event_handshake_failed(
const zmq_event_t& event_,
const char* addr_)
2652 virtual void on_event_handshake_succeed(
const zmq_event_t& event_,
const char* addr_)
2679 #if defined(ZMQ_BUILD_DRAFT_API) && defined(ZMQ_CPP11) && defined(ZMQ_HAVE_POLLER)
2682 enum class event_flags : short
2685 pollin = ZMQ_POLLIN,
2686 pollout = ZMQ_POLLOUT,
2687 pollerr = ZMQ_POLLERR,
2688 pollpri = ZMQ_POLLPRI
2691 constexpr event_flags
operator|(event_flags a, event_flags b) noexcept
2693 return detail::enum_bit_or(a, b);
2695 constexpr event_flags
operator&(event_flags a, event_flags b) noexcept
2697 return detail::enum_bit_and(a, b);
2699 constexpr event_flags
operator^(event_flags a, event_flags b) noexcept
2701 return detail::enum_bit_xor(a, b);
2703 constexpr event_flags
operator~(event_flags a) noexcept
2705 return detail::enum_bit_not(a);
2708 struct no_user_data;
2711 template <
class T = no_user_data>
2724 template <
typename T = no_user_data>
2728 using event_type = poller_event<T>;
2730 poller_t() : poller_ptr(zmq_poller_new())
2736 template <
typename Dummy =
void,
2741 add_impl(socket, events, user_data);
2746 add_impl(socket, events,
nullptr);
2751 if(0 != zmq_poller_remove(poller_ptr.get(), socket.handle()))
2760 zmq_poller_modify(poller_ptr.get(), socket.handle(),
static_cast<short>(events)))
2766 size_t wait_all(std::vector<event_type>& poller_events,
2767 const std::chrono::milliseconds timeout)
2769 int rc = zmq_poller_wait_all(
2770 poller_ptr.get(),
reinterpret_cast<zmq_poller_event_t*
>(poller_events.data()),
2771 static_cast<int>(poller_events.size()),
static_cast<long>(timeout.count()));
2773 return static_cast<size_t>(rc);
2775 #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 3)
2776 if(zmq_errno() == EAGAIN)
2778 if(zmq_errno() == ETIMEDOUT)
2786 struct destroy_poller_t
2788 void operator()(
void* ptr) noexcept
2790 int rc = zmq_poller_destroy(&ptr);
2795 std::unique_ptr<void, destroy_poller_t> poller_ptr;
2797 void add_impl(
zmq::socket_ref socket, event_flags events, T* user_data)
2799 if(0 != zmq_poller_add(poller_ptr.get(), socket.handle(), user_data,
2800 static_cast<short>(events)))
2806 #endif // defined(ZMQ_BUILD_DRAFT_API) && defined(ZMQ_CPP11) && defined(ZMQ_HAVE_POLLER)
2808 inline std::ostream&
operator<<(std::ostream& os,
const message_t& msg)
2810 return os << msg.str();
2815 #endif // __ZMQ_HPP_INCLUDED__