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 8
147 #define CPPZMQ_VERSION_PATCH 0
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>
263 T,
void_t<decltype(ranges::begin(
264 std::declval<typename std::remove_reference<T>::type&>()) ==
266 std::declval<typename std::remove_reference<T>::type&>()))>>
288 return zmq_strerror(
errnum);
299 inline int poll(zmq_pollitem_t* items_,
size_t nitems_,
long timeout_ = -1)
301 int rc = zmq_poll(items_,
static_cast<int>(nitems_), timeout_);
307 ZMQ_DEPRECATED(
"from 4.3.1, use poll taking non-const items")
308 inline
int poll(zmq_pollitem_t const* items_,
size_t nitems_,
long timeout_ = -1)
310 return poll(
const_cast<zmq_pollitem_t*
>(items_), nitems_, timeout_);
314 ZMQ_DEPRECATED(
"from 4.3.1, use poll taking non-const items")
315 inline
int poll(zmq_pollitem_t const* items,
size_t nitems,
316 std::chrono::milliseconds timeout)
318 return poll(
const_cast<zmq_pollitem_t*
>(items), nitems,
319 static_cast<long>(timeout.count()));
322 ZMQ_DEPRECATED(
"from 4.3.1, use poll taking non-const items")
323 inline
int poll(
std::vector<zmq_pollitem_t> const& items,
324 std::chrono::milliseconds timeout)
326 return poll(
const_cast<zmq_pollitem_t*
>(items.data()), items.size(),
327 static_cast<long>(timeout.count()));
330 ZMQ_DEPRECATED(
"from 4.3.1, use poll taking non-const items")
331 inline
int poll(
std::vector<zmq_pollitem_t> const& items,
long timeout_ = -1)
333 return poll(
const_cast<zmq_pollitem_t*
>(items.data()), items.size(), timeout_);
336 inline int poll(zmq_pollitem_t* items,
size_t nitems, std::chrono::milliseconds timeout)
338 return poll(items, nitems,
static_cast<long>(timeout.count()));
341 inline int poll(std::vector<zmq_pollitem_t>& items, std::chrono::milliseconds timeout)
343 return poll(items.data(), items.size(),
static_cast<long>(timeout.count()));
346 ZMQ_DEPRECATED(
"from 4.3.1, use poll taking std::chrono instead of long")
347 inline
int poll(
std::vector<zmq_pollitem_t>& items,
long timeout_ = -1)
349 return poll(items.data(), items.size(), timeout_);
352 template <std::
size_t SIZE>
353 inline int poll(std::array<zmq_pollitem_t, SIZE>& items,
354 std::chrono::milliseconds timeout)
356 return poll(items.data(), items.size(),
static_cast<long>(timeout.count()));
360 inline void version(
int* major_,
int* minor_,
int* patch_)
362 zmq_version(major_, minor_, patch_);
366 inline std::tuple<int, int, int>
version()
368 std::tuple<int, int, int> v;
369 zmq_version(&std::get<0>(v), &std::get<1>(v), &std::get<2>(v));
373 #if !defined(ZMQ_CPP11_PARTIAL)
380 static constexpr
bool value =
381 std::is_same<T, char>::value || std::is_same<T, wchar_t>::value ||
382 std::is_same<T, char16_t>::value || std::is_same<T, char32_t>::value;
394 int rc = zmq_msg_init(&
msg);
400 int rc = zmq_msg_init_size(&
msg, size_);
405 template <
class ForwardIter>
408 typedef typename std::iterator_traits<ForwardIter>::value_type
value_t;
412 static_cast<size_t>(std::distance(
first, last)) *
sizeof(
value_t);
413 int const rc = zmq_msg_init_size(&
msg, size_);
421 int rc = zmq_msg_init_size(&
msg, size_);
428 memcpy(
data(), data_, size_);
434 int rc = zmq_msg_init_data(&
msg, data_, size_, ffn_, hint_);
440 #if defined(ZMQ_CPP11) && !defined(ZMQ_CPP11_PARTIAL)
445 template <
class Char,
size_t N,
446 typename =
typename std::enable_if<detail::is_char_type<Char>::value>::type>
447 ZMQ_DEPRECATED(
"from 4.7.0, use constructors taking iterators, (pointer, size) "
448 "or strings instead")
454 template <
class Range,
typename =
typename std::enable_if<
455 detail::is_range<Range>::value &&
456 ZMQ_IS_TRIVIALLY_COPYABLE(detail::range_value_t<Range>) &&
457 !detail::is_char_type<detail::range_value_t<Range>>::value &&
458 !std::is_same<Range, message_t>::value>::type>
468 #if CPPZMQ_HAS_STRING_VIEW
476 #ifdef ZMQ_HAS_RVALUE_REFS
479 int rc = zmq_msg_init(&rhs.msg);
492 int rc = zmq_msg_close(&
msg);
498 int rc = zmq_msg_close(&
msg);
501 rc = zmq_msg_init(&
msg);
507 int rc = zmq_msg_close(&
msg);
510 rc = zmq_msg_init_size(&
msg, size_);
517 int rc = zmq_msg_close(&
msg);
520 rc = zmq_msg_init_size(&
msg, size_);
523 memcpy(
data(), data_, size_);
528 int rc = zmq_msg_close(&
msg);
531 rc = zmq_msg_init_data(&
msg, data_, size_, ffn_, hint_);
536 ZMQ_DEPRECATED(
"from 4.3.1, use move taking non-const reference instead")
539 int rc = zmq_msg_move(&
msg,
const_cast<zmq_msg_t*
>(msg_->handle()));
546 int rc = zmq_msg_move(&
msg, msg_.
handle());
551 ZMQ_DEPRECATED(
"from 4.3.1, use copy taking non-const reference instead")
554 int rc = zmq_msg_copy(&
msg,
const_cast<zmq_msg_t*
>(msg_->handle()));
561 int rc = zmq_msg_copy(&
msg, msg_.
handle());
568 int rc = zmq_msg_more(
const_cast<zmq_msg_t*
>(&
msg));
574 return zmq_msg_data(&
msg);
579 return zmq_msg_data(
const_cast<zmq_msg_t*
>(&
msg));
584 return zmq_msg_size(
const_cast<zmq_msg_t*
>(&
msg));
592 template <
typename T>
595 return static_cast<T*
>(
data());
598 template <
typename T>
601 return static_cast<T const*
>(
data());
604 ZMQ_DEPRECATED(
"from 4.3.0, use operator== instead")
607 return *
this == *other;
612 const size_t my_size =
size();
613 return my_size == other.size() && 0 == memcmp(
data(), other.data(), my_size);
618 return !(*
this == other);
621 #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(3, 2, 0)
624 int value = zmq_msg_get(&
msg, property_);
631 #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 1, 0)
632 const char*
gets(
const char* property_)
634 const char* value = zmq_msg_gets(&
msg, property_);
641 #if defined(ZMQ_BUILD_DRAFT_API) && ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 0)
642 uint32_t routing_id()
const
644 return zmq_msg_routing_id(
const_cast<zmq_msg_t*
>(&
msg));
647 void set_routing_id(uint32_t routing_id)
649 int rc = zmq_msg_set_routing_id(&
msg, routing_id);
654 const char* group()
const
656 return zmq_msg_group(
const_cast<zmq_msg_t*
>(&
msg));
659 void set_group(
const char* group)
661 int rc = zmq_msg_set_group(&
msg, group);
670 return std::string(
static_cast<const char*
>(
data()),
size());
672 #if CPPZMQ_HAS_STRING_VIEW
689 std::stringstream os;
691 const unsigned char* msg_data = this->data<unsigned char>();
694 int is_ascii[2] = { 0, 0 };
696 os <<
"zmq::message_t [size " << std::dec << std::setw(3) << std::setfill(
'0') <<
size
701 os <<
"... too big to print)";
709 is_ascii[1] = (
byte >= 32 &&
byte < 127);
710 if (is_ascii[1] != is_ascii[0])
719 os << std::hex << std::uppercase << std::setw(2) << std::setfill(
'0')
720 <<
static_cast<short>(byte);
722 is_ascii[0] = is_ascii[1];
765 #ifdef ZMQ_IO_THREADS
766 io_threads = ZMQ_IO_THREADS,
768 #ifdef ZMQ_THREAD_SCHED_POLICY
769 thread_sched_policy = ZMQ_THREAD_SCHED_POLICY,
771 #ifdef ZMQ_THREAD_PRIORITY
772 thread_priority = ZMQ_THREAD_PRIORITY,
774 #ifdef ZMQ_THREAD_AFFINITY_CPU_ADD
775 thread_affinity_cpu_add = ZMQ_THREAD_AFFINITY_CPU_ADD,
777 #ifdef ZMQ_THREAD_AFFINITY_CPU_REMOVE
778 thread_affinity_cpu_remove = ZMQ_THREAD_AFFINITY_CPU_REMOVE,
780 #ifdef ZMQ_THREAD_NAME_PREFIX
781 thread_name_prefix = ZMQ_THREAD_NAME_PREFIX,
784 max_msgsz = ZMQ_MAX_MSGSZ,
786 #ifdef ZMQ_ZERO_COPY_RECV
787 zero_copy_recv = ZMQ_ZERO_COPY_RECV,
789 #ifdef ZMQ_MAX_SOCKETS
790 max_sockets = ZMQ_MAX_SOCKETS,
792 #ifdef ZMQ_SOCKET_LIMIT
793 socket_limit = ZMQ_SOCKET_LIMIT,
798 #ifdef ZMQ_MSG_T_SIZE
799 msg_t_size = ZMQ_MSG_T_SIZE
814 explicit context_t(
int io_threads_,
int max_sockets_ = ZMQ_MAX_SOCKETS_DFLT)
820 int rc = zmq_ctx_set(
ptr, ZMQ_IO_THREADS, io_threads_);
823 rc = zmq_ctx_set(
ptr, ZMQ_MAX_SOCKETS, max_sockets_);
827 #ifdef ZMQ_HAS_RVALUE_REFS
848 int rc = zmq_ctx_set(
ptr, option_, optval_);
856 return zmq_ctx_get(
ptr, option_);
860 void set(ctxopt option,
int optval)
862 int rc = zmq_ctx_set(
ptr,
static_cast<int>(option), optval);
869 int rc = zmq_ctx_get(
ptr,
static_cast<int>(option));
888 rc = zmq_ctx_destroy(
ptr);
889 }
while (rc == -1 && errno == EINTR);
902 int rc = zmq_ctx_shutdown(
ptr);
924 ZMQ_DEPRECATED(
"from 4.7.0, use handle() != nullptr instead")
949 struct recv_buffer_size
952 size_t untruncated_size;
956 return size != untruncated_size;
960 #if CPPZMQ_HAS_OPTIONAL
962 using send_result_t = std::optional<size_t>;
963 using recv_result_t = std::optional<size_t>;
964 using recv_buffer_result_t = std::optional<recv_buffer_size>;
973 class trivial_optional
976 static_assert(std::is_trivial<T>::value,
"T must be trivial");
977 using value_type = T;
979 trivial_optional() =
default;
980 trivial_optional(T value) noexcept : _value(value), _has_value(
true)
984 const T* operator->() const noexcept
989 T* operator->() noexcept
1009 throw std::exception();
1012 const T&
value()
const
1015 throw std::exception();
1019 explicit operator bool() const noexcept
1023 bool has_value() const noexcept
1030 bool _has_value{
false };
1034 using send_result_t = detail::trivial_optional<size_t>;
1035 using recv_result_t = detail::trivial_optional<size_t>;
1036 using recv_buffer_result_t = detail::trivial_optional<recv_buffer_size>;
1043 constexpr T enum_bit_or(T a, T b) noexcept
1045 static_assert(std::is_enum<T>::value,
"must be enum");
1047 return static_cast<T
>(
static_cast<U
>(a) |
static_cast<U
>(b));
1050 constexpr T enum_bit_and(T a, T b) noexcept
1052 static_assert(std::is_enum<T>::value,
"must be enum");
1054 return static_cast<T
>(
static_cast<U
>(a) &
static_cast<U
>(b));
1057 constexpr T enum_bit_xor(T a, T b) noexcept
1059 static_assert(std::is_enum<T>::value,
"must be enum");
1061 return static_cast<T
>(
static_cast<U
>(a) ^
static_cast<U
>(b));
1064 constexpr T enum_bit_not(T a) noexcept
1066 static_assert(std::is_enum<T>::value,
"must be enum");
1068 return static_cast<T
>(~static_cast<U>(a));
1073 enum class send_flags :
int
1076 dontwait = ZMQ_DONTWAIT,
1077 sndmore = ZMQ_SNDMORE
1080 constexpr send_flags
operator|(send_flags a, send_flags b) noexcept
1082 return detail::enum_bit_or(a, b);
1084 constexpr send_flags
operator&(send_flags a, send_flags b) noexcept
1086 return detail::enum_bit_and(a, b);
1088 constexpr send_flags operator^(send_flags a, send_flags b) noexcept
1090 return detail::enum_bit_xor(a, b);
1092 constexpr send_flags operator~(send_flags a) noexcept
1094 return detail::enum_bit_not(a);
1098 enum class recv_flags :
int
1101 dontwait = ZMQ_DONTWAIT
1104 constexpr recv_flags
operator|(recv_flags a, recv_flags b) noexcept
1106 return detail::enum_bit_or(a, b);
1108 constexpr recv_flags
operator&(recv_flags a, recv_flags b) noexcept
1110 return detail::enum_bit_and(a, b);
1112 constexpr recv_flags operator^(recv_flags a, recv_flags b) noexcept
1114 return detail::enum_bit_xor(a, b);
1116 constexpr recv_flags operator~(recv_flags a) noexcept
1118 return detail::enum_bit_not(a);
1125 class mutable_buffer
1128 constexpr mutable_buffer() noexcept : _data(
nullptr), _size(0)
1131 constexpr mutable_buffer(
void* p,
size_t n) noexcept : _data(p), _size(n)
1133 #ifdef ZMQ_EXTENDED_CONSTEXPR
1134 assert(p !=
nullptr || n == 0);
1138 constexpr
void*
data() const noexcept
1142 constexpr
size_t size() const noexcept
1146 mutable_buffer& operator+=(
size_t n) noexcept
1149 const auto shift = (std::min)(n, _size);
1150 _data =
static_cast<char*
>(_data) + shift;
1160 inline mutable_buffer
operator+(
const mutable_buffer& mb,
size_t n) noexcept
1162 return mutable_buffer(
static_cast<char*
>(mb.data()) + (std::min)(n, mb.size()),
1163 mb.size() - (std::min)(n, mb.size()));
1165 inline mutable_buffer
operator+(
size_t n,
const mutable_buffer& mb) noexcept
1173 constexpr const_buffer() noexcept : _data(
nullptr), _size(0)
1176 constexpr const_buffer(
const void* p,
size_t n) noexcept : _data(p), _size(n)
1178 #ifdef ZMQ_EXTENDED_CONSTEXPR
1179 assert(p !=
nullptr || n == 0);
1182 constexpr const_buffer(
const mutable_buffer& mb) noexcept
1183 : _data(mb.data()), _size(mb.size())
1187 constexpr
const void*
data() const noexcept
1191 constexpr
size_t size() const noexcept
1195 const_buffer& operator+=(
size_t n) noexcept
1197 const auto shift = (std::min)(n, _size);
1198 _data =
static_cast<const char*
>(_data) + shift;
1208 inline const_buffer
operator+(
const const_buffer& cb,
size_t n) noexcept
1210 return const_buffer(
static_cast<const char*
>(cb.data()) + (std::min)(n, cb.size()),
1211 cb.size() - (std::min)(n, cb.size()));
1213 inline const_buffer
operator+(
size_t n,
const const_buffer& cb) noexcept
1220 constexpr mutable_buffer buffer(
void* p,
size_t n) noexcept
1222 return mutable_buffer(p, n);
1224 constexpr const_buffer buffer(
const void* p,
size_t n) noexcept
1226 return const_buffer(p, n);
1228 constexpr mutable_buffer buffer(
const mutable_buffer& mb) noexcept
1232 inline mutable_buffer buffer(
const mutable_buffer& mb,
size_t n) noexcept
1234 return mutable_buffer(mb.data(), (std::min)(mb.size(), n));
1236 constexpr const_buffer buffer(
const const_buffer& cb) noexcept
1240 inline const_buffer buffer(
const const_buffer& cb,
size_t n) noexcept
1242 return const_buffer(cb.data(), (std::min)(cb.size(), n));
1250 static constexpr
bool value =
1251 std::is_same<T, const_buffer>::value || std::is_same<T, mutable_buffer>::value;
1261 static constexpr
bool value =
1262 ZMQ_IS_TRIVIALLY_COPYABLE(T) && std::is_standard_layout<T>::value;
1266 constexpr
auto seq_size(
const C& c) noexcept -> decltype(c.size())
1270 template <
class T,
size_t N>
1271 constexpr
size_t seq_size(
const T (& )[N]) noexcept
1276 template <
class Seq>
1277 auto buffer_contiguous_sequence(Seq&& seq) noexcept
1278 -> decltype(buffer(std::addressof(*std::begin(seq)),
size_t{}))
1280 using T =
typename std::remove_cv<
1281 typename std::remove_reference<decltype(*std::begin(seq))>::type>::type;
1282 static_assert(detail::is_pod_like<T>::value,
"T must be POD");
1284 const auto size = seq_size(seq);
1285 return buffer(
size != 0u ? std::addressof(*std::begin(seq)) :
nullptr,
1288 template <
class Seq>
1289 auto buffer_contiguous_sequence(Seq&& seq,
size_t n_bytes) noexcept
1290 -> decltype(buffer_contiguous_sequence(seq))
1292 using T =
typename std::remove_cv<
1293 typename std::remove_reference<decltype(*std::begin(seq))>::type>::type;
1294 static_assert(detail::is_pod_like<T>::value,
"T must be POD");
1296 const auto size = seq_size(seq);
1297 return buffer(
size != 0u ? std::addressof(*std::begin(seq)) :
nullptr,
1298 (std::min)(
size *
sizeof(T), n_bytes));
1304 template <
class T,
size_t N>
1305 mutable_buffer buffer(T (&
data)[N]) noexcept
1307 return detail::buffer_contiguous_sequence(
data);
1309 template <
class T,
size_t N>
1310 mutable_buffer buffer(T (&
data)[N],
size_t n_bytes) noexcept
1312 return detail::buffer_contiguous_sequence(
data, n_bytes);
1314 template <
class T,
size_t N>
1315 const_buffer buffer(
const T (&
data)[N]) noexcept
1317 return detail::buffer_contiguous_sequence(
data);
1319 template <
class T,
size_t N>
1320 const_buffer buffer(
const T (&
data)[N],
size_t n_bytes) noexcept
1322 return detail::buffer_contiguous_sequence(
data, n_bytes);
1325 template <
class T,
size_t N>
1326 mutable_buffer buffer(std::array<T, N>&
data) noexcept
1328 return detail::buffer_contiguous_sequence(
data);
1330 template <
class T,
size_t N>
1331 mutable_buffer buffer(std::array<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(std::array<const T, N>&
data) noexcept
1338 return detail::buffer_contiguous_sequence(
data);
1340 template <
class T,
size_t N>
1341 const_buffer buffer(std::array<const T, N>&
data,
size_t n_bytes) noexcept
1343 return detail::buffer_contiguous_sequence(
data, n_bytes);
1345 template <
class T,
size_t N>
1346 const_buffer buffer(
const std::array<T, N>&
data) noexcept
1348 return detail::buffer_contiguous_sequence(
data);
1350 template <
class T,
size_t N>
1351 const_buffer buffer(
const std::array<T, N>&
data,
size_t n_bytes) noexcept
1353 return detail::buffer_contiguous_sequence(
data, n_bytes);
1356 template <
class T,
class Allocator>
1357 mutable_buffer buffer(std::vector<T, Allocator>&
data) noexcept
1359 return detail::buffer_contiguous_sequence(
data);
1361 template <
class T,
class Allocator>
1362 mutable_buffer buffer(std::vector<T, Allocator>&
data,
size_t n_bytes) noexcept
1364 return detail::buffer_contiguous_sequence(
data, n_bytes);
1366 template <
class T,
class Allocator>
1367 const_buffer buffer(
const std::vector<T, Allocator>&
data) noexcept
1369 return detail::buffer_contiguous_sequence(
data);
1371 template <
class T,
class Allocator>
1372 const_buffer buffer(
const std::vector<T, Allocator>&
data,
size_t n_bytes) noexcept
1374 return detail::buffer_contiguous_sequence(
data, n_bytes);
1377 template <
class T,
class Traits,
class Allocator>
1378 mutable_buffer buffer(std::basic_string<T, Traits, Allocator>&
data) noexcept
1380 return detail::buffer_contiguous_sequence(
data);
1382 template <
class T,
class Traits,
class Allocator>
1383 mutable_buffer buffer(std::basic_string<T, Traits, Allocator>&
data,
1384 size_t n_bytes) noexcept
1386 return detail::buffer_contiguous_sequence(
data, n_bytes);
1388 template <
class T,
class Traits,
class Allocator>
1389 const_buffer buffer(
const std::basic_string<T, Traits, Allocator>&
data) noexcept
1391 return detail::buffer_contiguous_sequence(
data);
1393 template <
class T,
class Traits,
class Allocator>
1394 const_buffer buffer(
const std::basic_string<T, Traits, Allocator>&
data,
1395 size_t n_bytes) noexcept
1397 return detail::buffer_contiguous_sequence(
data, n_bytes);
1400 #if CPPZMQ_HAS_STRING_VIEW
1402 template <
class T,
class Traits>
1403 const_buffer buffer(std::basic_string_view<T, Traits>
data) noexcept
1405 return detail::buffer_contiguous_sequence(
data);
1407 template <
class T,
class Traits>
1408 const_buffer buffer(std::basic_string_view<T, Traits>
data,
size_t n_bytes) noexcept
1410 return detail::buffer_contiguous_sequence(
data, n_bytes);
1417 template <
class Char,
size_t N>
1418 constexpr const_buffer str_buffer(
const Char (&
data)[N]) noexcept
1420 static_assert(detail::is_pod_like<Char>::value,
"Char must be POD");
1421 #ifdef ZMQ_EXTENDED_CONSTEXPR
1424 return const_buffer(
static_cast<const Char*
>(
data), (N - 1) *
sizeof(Char));
1429 constexpr const_buffer
operator"" _zbuf(
const char* str,
size_t len) noexcept
1431 return const_buffer(str, len *
sizeof(
char));
1433 constexpr const_buffer
operator"" _zbuf(
const wchar_t* str,
size_t len) noexcept
1435 return const_buffer(str, len *
sizeof(
wchar_t));
1437 constexpr const_buffer
operator"" _zbuf(
const char16_t* str,
size_t len) noexcept
1439 return const_buffer(str, len *
sizeof(char16_t));
1441 constexpr const_buffer
operator"" _zbuf(
const char32_t* str,
size_t len) noexcept
1443 return const_buffer(str, len *
sizeof(char32_t));
1457 template <
int Opt,
class T,
bool BoolUnit = false>
1458 struct integral_option
1466 template <
int Opt,
int NullTerm = 1>
1471 #define ZMQ_DEFINE_INTEGRAL_OPT(OPT, NAME, TYPE) \
1472 using NAME##_t = integral_option<OPT, TYPE, false>; \
1473 ZMQ_INLINE_VAR ZMQ_CONSTEXPR_VAR NAME##_t NAME \
1476 #define ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(OPT, NAME, TYPE) \
1477 using NAME##_t = integral_option<OPT, TYPE, true>; \
1478 ZMQ_INLINE_VAR ZMQ_CONSTEXPR_VAR NAME##_t NAME \
1481 #define ZMQ_DEFINE_ARRAY_OPT(OPT, NAME) \
1482 using NAME##_t = array_option<OPT>; \
1483 ZMQ_INLINE_VAR ZMQ_CONSTEXPR_VAR NAME##_t NAME \
1486 #define ZMQ_DEFINE_ARRAY_OPT_BINARY(OPT, NAME) \
1487 using NAME##_t = array_option<OPT, 0>; \
1488 ZMQ_INLINE_VAR ZMQ_CONSTEXPR_VAR NAME##_t NAME \
1491 #define ZMQ_DEFINE_ARRAY_OPT_BIN_OR_Z85(OPT, NAME) \
1492 using NAME##_t = array_option<OPT, 2>; \
1493 ZMQ_INLINE_VAR ZMQ_CONSTEXPR_VAR NAME##_t NAME \
1500 typedef unsigned __int64 cppzmq_fd_t;
1502 typedef unsigned int cppzmq_fd_t;
1505 typedef int cppzmq_fd_t;
1509 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_AFFINITY, affinity, uint64_t);
1512 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_BACKLOG, backlog,
int);
1514 #ifdef ZMQ_BINDTODEVICE
1515 ZMQ_DEFINE_ARRAY_OPT_BINARY(ZMQ_BINDTODEVICE, bindtodevice);
1518 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_CONFLATE, conflate,
int);
1520 #ifdef ZMQ_CONNECT_ROUTING_ID
1521 ZMQ_DEFINE_ARRAY_OPT(ZMQ_CONNECT_ROUTING_ID, connect_routing_id);
1523 #ifdef ZMQ_CONNECT_TIMEOUT
1524 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_CONNECT_TIMEOUT, connect_timeout,
int);
1526 #ifdef ZMQ_CURVE_PUBLICKEY
1527 ZMQ_DEFINE_ARRAY_OPT_BIN_OR_Z85(ZMQ_CURVE_PUBLICKEY, curve_publickey);
1529 #ifdef ZMQ_CURVE_SECRETKEY
1530 ZMQ_DEFINE_ARRAY_OPT_BIN_OR_Z85(ZMQ_CURVE_SECRETKEY, curve_secretkey);
1532 #ifdef ZMQ_CURVE_SERVER
1533 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_CURVE_SERVER, curve_server,
int);
1535 #ifdef ZMQ_CURVE_SERVERKEY
1536 ZMQ_DEFINE_ARRAY_OPT_BIN_OR_Z85(ZMQ_CURVE_SERVERKEY, curve_serverkey);
1539 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_EVENTS, events,
int);
1542 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_FD, fd, cppzmq_fd_t);
1544 #ifdef ZMQ_GSSAPI_PLAINTEXT
1545 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_GSSAPI_PLAINTEXT, gssapi_plaintext,
int);
1547 #ifdef ZMQ_GSSAPI_SERVER
1548 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_GSSAPI_SERVER, gssapi_server,
int);
1550 #ifdef ZMQ_GSSAPI_SERVICE_PRINCIPAL
1551 ZMQ_DEFINE_ARRAY_OPT(ZMQ_GSSAPI_SERVICE_PRINCIPAL, gssapi_service_principal);
1553 #ifdef ZMQ_GSSAPI_SERVICE_PRINCIPAL_NAMETYPE
1554 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_GSSAPI_SERVICE_PRINCIPAL_NAMETYPE,
1555 gssapi_service_principal_nametype,
int);
1557 #ifdef ZMQ_GSSAPI_PRINCIPAL
1558 ZMQ_DEFINE_ARRAY_OPT(ZMQ_GSSAPI_PRINCIPAL, gssapi_principal);
1560 #ifdef ZMQ_GSSAPI_PRINCIPAL_NAMETYPE
1561 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_GSSAPI_PRINCIPAL_NAMETYPE, gssapi_principal_nametype,
int);
1563 #ifdef ZMQ_HANDSHAKE_IVL
1564 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_HANDSHAKE_IVL, handshake_ivl,
int);
1566 #ifdef ZMQ_HEARTBEAT_IVL
1567 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_HEARTBEAT_IVL, heartbeat_ivl,
int);
1569 #ifdef ZMQ_HEARTBEAT_TIMEOUT
1570 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_HEARTBEAT_TIMEOUT, heartbeat_timeout,
int);
1572 #ifdef ZMQ_HEARTBEAT_TTL
1573 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_HEARTBEAT_TTL, heartbeat_ttl,
int);
1575 #ifdef ZMQ_IMMEDIATE
1576 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_IMMEDIATE, immediate,
int);
1578 #ifdef ZMQ_INVERT_MATCHING
1579 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_INVERT_MATCHING, invert_matching,
int);
1582 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_IPV6, ipv6,
int);
1584 #ifdef ZMQ_LAST_ENDPOINT
1585 ZMQ_DEFINE_ARRAY_OPT(ZMQ_LAST_ENDPOINT, last_endpoint);
1588 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_LINGER, linger,
int);
1590 #ifdef ZMQ_MAXMSGSIZE
1591 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_MAXMSGSIZE, maxmsgsize, int64_t);
1593 #ifdef ZMQ_MECHANISM
1594 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_MECHANISM, mechanism,
int);
1597 ZMQ_DEFINE_ARRAY_OPT(ZMQ_METADATA, metadata);
1599 #ifdef ZMQ_MULTICAST_HOPS
1600 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_MULTICAST_HOPS, multicast_hops,
int);
1602 #ifdef ZMQ_MULTICAST_LOOP
1603 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_MULTICAST_LOOP, multicast_loop,
int);
1605 #ifdef ZMQ_MULTICAST_MAXTPDU
1606 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_MULTICAST_MAXTPDU, multicast_maxtpdu,
int);
1608 #ifdef ZMQ_PLAIN_SERVER
1609 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_PLAIN_SERVER, plain_server,
int);
1611 #ifdef ZMQ_PLAIN_PASSWORD
1612 ZMQ_DEFINE_ARRAY_OPT(ZMQ_PLAIN_PASSWORD, plain_password);
1614 #ifdef ZMQ_PLAIN_USERNAME
1615 ZMQ_DEFINE_ARRAY_OPT(ZMQ_PLAIN_USERNAME, plain_username);
1618 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_USE_FD, use_fd,
int);
1620 #ifdef ZMQ_PROBE_ROUTER
1621 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_PROBE_ROUTER, probe_router,
int);
1624 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_RATE, rate,
int);
1627 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_RCVBUF, rcvbuf,
int);
1630 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_RCVHWM, rcvhwm,
int);
1633 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_RCVMORE, rcvmore,
int);
1636 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_RCVTIMEO, rcvtimeo,
int);
1638 #ifdef ZMQ_RECONNECT_IVL
1639 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_RECONNECT_IVL, reconnect_ivl,
int);
1641 #ifdef ZMQ_RECONNECT_IVL_MAX
1642 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_RECONNECT_IVL_MAX, reconnect_ivl_max,
int);
1644 #ifdef ZMQ_RECOVERY_IVL
1645 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_RECOVERY_IVL, recovery_ivl,
int);
1647 #ifdef ZMQ_REQ_CORRELATE
1648 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_REQ_CORRELATE, req_correlate,
int);
1650 #ifdef ZMQ_REQ_RELAXED
1651 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_REQ_RELAXED, req_relaxed,
int);
1653 #ifdef ZMQ_ROUTER_HANDOVER
1654 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_ROUTER_HANDOVER, router_handover,
int);
1656 #ifdef ZMQ_ROUTER_MANDATORY
1657 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_ROUTER_MANDATORY, router_mandatory,
int);
1659 #ifdef ZMQ_ROUTER_NOTIFY
1660 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_ROUTER_NOTIFY, router_notify,
int);
1662 #ifdef ZMQ_ROUTING_ID
1663 ZMQ_DEFINE_ARRAY_OPT_BINARY(ZMQ_ROUTING_ID, routing_id);
1666 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_SNDBUF, sndbuf,
int);
1669 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_SNDHWM, sndhwm,
int);
1672 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_SNDTIMEO, sndtimeo,
int);
1674 #ifdef ZMQ_SOCKS_PROXY
1675 ZMQ_DEFINE_ARRAY_OPT(ZMQ_SOCKS_PROXY, socks_proxy);
1677 #ifdef ZMQ_STREAM_NOTIFY
1678 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_STREAM_NOTIFY, stream_notify,
int);
1680 #ifdef ZMQ_SUBSCRIBE
1681 ZMQ_DEFINE_ARRAY_OPT(ZMQ_SUBSCRIBE, subscribe);
1683 #ifdef ZMQ_TCP_KEEPALIVE
1684 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TCP_KEEPALIVE, tcp_keepalive,
int);
1686 #ifdef ZMQ_TCP_KEEPALIVE_CNT
1687 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TCP_KEEPALIVE_CNT, tcp_keepalive_cnt,
int);
1689 #ifdef ZMQ_TCP_KEEPALIVE_IDLE
1690 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TCP_KEEPALIVE_IDLE, tcp_keepalive_idle,
int);
1692 #ifdef ZMQ_TCP_KEEPALIVE_INTVL
1693 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TCP_KEEPALIVE_INTVL, tcp_keepalive_intvl,
int);
1695 #ifdef ZMQ_TCP_MAXRT
1696 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TCP_MAXRT, tcp_maxrt,
int);
1698 #ifdef ZMQ_THREAD_SAFE
1699 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_THREAD_SAFE, thread_safe,
int);
1702 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TOS, tos,
int);
1705 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TYPE, type,
int);
1707 #ifdef ZMQ_UNSUBSCRIBE
1708 ZMQ_DEFINE_ARRAY_OPT(ZMQ_UNSUBSCRIBE, unsubscribe);
1710 #ifdef ZMQ_VMCI_BUFFER_SIZE
1711 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_VMCI_BUFFER_SIZE, vmci_buffer_size, uint64_t);
1713 #ifdef ZMQ_VMCI_BUFFER_MIN_SIZE
1714 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_VMCI_BUFFER_MIN_SIZE, vmci_buffer_min_size, uint64_t);
1716 #ifdef ZMQ_VMCI_BUFFER_MAX_SIZE
1717 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_VMCI_BUFFER_MAX_SIZE, vmci_buffer_max_size, uint64_t);
1719 #ifdef ZMQ_VMCI_CONNECT_TIMEOUT
1720 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_VMCI_CONNECT_TIMEOUT, vmci_connect_timeout,
int);
1722 #ifdef ZMQ_XPUB_VERBOSE
1723 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_XPUB_VERBOSE, xpub_verbose,
int);
1725 #ifdef ZMQ_XPUB_VERBOSER
1726 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_XPUB_VERBOSER, xpub_verboser,
int);
1728 #ifdef ZMQ_XPUB_MANUAL
1729 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_XPUB_MANUAL, xpub_manual,
int);
1731 #ifdef ZMQ_XPUB_NODROP
1732 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_XPUB_NODROP, xpub_nodrop,
int);
1734 #ifdef ZMQ_XPUB_WELCOME_MSG
1735 ZMQ_DEFINE_ARRAY_OPT(ZMQ_XPUB_WELCOME_MSG, xpub_welcome_msg);
1737 #ifdef ZMQ_ZAP_ENFORCE_DOMAIN
1738 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_ZAP_ENFORCE_DOMAIN, zap_enforce_domain,
int);
1740 #ifdef ZMQ_ZAP_DOMAIN
1741 ZMQ_DEFINE_ARRAY_OPT(ZMQ_ZAP_DOMAIN, zap_domain);
1759 template <
typename T>
1769 int rc = zmq_setsockopt(_handle, option_, optval_, optvallen_);
1777 int rc = zmq_getsockopt(_handle, option_, optval_, optvallen_);
1782 template <
typename T>
1787 size_t optlen =
sizeof(T);
1795 template <
int Opt,
class T,
bool BoolUnit>
1796 void set(sockopt::integral_option<Opt, T, BoolUnit>,
const T& val)
1798 static_assert(std::is_integral<T>::value,
"T must be integral");
1804 template <
int Opt,
class T>
1805 void set(sockopt::integral_option<Opt, T, true>,
bool val)
1807 static_assert(std::is_integral<T>::value,
"T must be integral");
1814 template <
int Opt,
int NullTerm>
1815 void set(sockopt::array_option<Opt, NullTerm>,
const char* buf)
1822 template <
int Opt,
int NullTerm>
1823 void set(sockopt::array_option<Opt, NullTerm>, const_buffer buf)
1830 template <
int Opt,
int NullTerm>
1831 void set(sockopt::array_option<Opt, NullTerm>,
const std::string& buf)
1836 #if CPPZMQ_HAS_STRING_VIEW
1839 template <
int Opt,
int NullTerm>
1848 template <
int Opt,
class T,
bool BoolUnit>
1851 static_assert(std::is_integral<T>::value,
"T must be integral");
1853 size_t size =
sizeof val;
1861 template <
int Opt,
int NullTerm>
1862 ZMQ_NODISCARD size_t get(sockopt::array_option<Opt, NullTerm>, mutable_buffer buf)
const
1864 size_t size = buf.size();
1873 template <
int Opt,
int NullTerm>
1875 size_t init_size = 1024)
const
1877 if (NullTerm == 2 && init_size == 1024)
1881 std::string str(init_size,
'\0');
1882 size_t size =
get(sockopt::array_option<Opt>{}, buffer(str));
1891 else if (NullTerm == 2)
1912 int rc = zmq_bind(_handle, addr_);
1924 int rc = zmq_unbind(_handle, addr_);
1936 int rc = zmq_connect(_handle, addr_);
1948 int rc = zmq_disconnect(_handle, addr_);
1961 int nbytes = zmq_send(_handle, buf_, len_,
flags_);
1963 return static_cast<size_t>(nbytes);
1964 if (zmq_errno() == EAGAIN)
1973 int nbytes = zmq_msg_send(msg_.handle(), _handle,
flags_);
1976 if (zmq_errno() == EAGAIN)
1981 template <
typename T>
1983 "ranges), and send_flags")
1987 int nbytes = zmq_msg_send(
msg.handle(), _handle,
flags_);
1990 if (zmq_errno() == EAGAIN)
1995 #ifdef ZMQ_HAS_RVALUE_REFS
2001 return send(msg_,
static_cast<send_flags
>(
flags_)).has_value();
2009 send_result_t
send(const_buffer buf, send_flags flags = send_flags::none)
2011 const int nbytes = zmq_send(_handle, buf.data(), buf.size(),
static_cast<int>(flags));
2013 return static_cast<size_t>(nbytes);
2014 if (zmq_errno() == EAGAIN)
2019 send_result_t
send(message_t&
msg, send_flags flags)
2021 int nbytes = zmq_msg_send(
msg.handle(), _handle,
static_cast<int>(flags));
2023 return static_cast<size_t>(nbytes);
2024 if (zmq_errno() == EAGAIN)
2029 send_result_t
send(message_t&&
msg, send_flags flags)
2038 int nbytes = zmq_recv(_handle, buf_, len_,
flags_);
2040 return static_cast<size_t>(nbytes);
2041 if (zmq_errno() == EAGAIN)
2048 bool recv(message_t* msg_,
int flags_ = 0)
2050 int nbytes = zmq_msg_recv(msg_->handle(), _handle,
flags_);
2053 if (zmq_errno() == EAGAIN)
2060 recv_buffer_result_t recv(mutable_buffer buf, recv_flags flags = recv_flags::none)
2062 const int nbytes = zmq_recv(_handle, buf.data(), buf.size(),
static_cast<int>(flags));
2065 return recv_buffer_size{ (std::min)(
static_cast<size_t>(nbytes), buf.size()),
2066 static_cast<size_t>(nbytes) };
2068 if (zmq_errno() == EAGAIN)
2074 recv_result_t recv(message_t&
msg, recv_flags flags = recv_flags::none)
2076 const int nbytes = zmq_msg_recv(
msg.handle(), _handle,
static_cast<int>(flags));
2079 assert(
msg.size() ==
static_cast<size_t>(nbytes));
2080 return static_cast<size_t>(nbytes);
2082 if (zmq_errno() == EAGAIN)
2088 #if defined(ZMQ_BUILD_DRAFT_API) && ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 0)
2089 void join(
const char* group)
2091 int rc = zmq_join(_handle, group);
2096 void leave(
const char* group)
2098 int rc = zmq_leave(_handle, group);
2128 void set_option(
int option_,
const void* optval_,
size_t optvallen_)
2130 int rc = zmq_setsockopt(_handle, option_, optval_, optvallen_);
2135 void get_option(
int option_,
void* optval_,
size_t* optvallen_)
const
2137 int rc = zmq_getsockopt(_handle, option_, optval_, optvallen_);
2149 dealer = ZMQ_DEALER,
2150 router = ZMQ_ROUTER,
2157 #if defined(ZMQ_BUILD_DRAFT_API) && ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 0)
2158 server = ZMQ_SERVER,
2163 #if ZMQ_VERSION_MAJOR >= 4
2164 stream = ZMQ_STREAM,
2203 return sr.handle() ==
nullptr;
2207 return sr.handle() ==
nullptr;
2211 return !(sr ==
nullptr);
2215 return !(sr ==
nullptr);
2221 return std::equal_to<void*>()(a.handle(), b.handle());
2229 return std::less<void*>()(a.handle(), b.handle());
2254 return hash<void*>()(sr.handle());
2285 #ifdef ZMQ_HAS_RVALUE_REFS
2321 int rc = zmq_close(_handle);
2360 ZMQ_DEPRECATED(
"from 4.3.1, use proxy taking socket_t objects")
2361 inline
void proxy(
void* frontend,
void* backend,
void* capture)
2363 int rc = zmq_proxy(frontend, backend, capture);
2371 int rc = zmq_proxy(frontend.handle(), backend.handle(), capture.handle());
2376 #ifdef ZMQ_HAS_PROXY_STEERABLE
2377 ZMQ_DEPRECATED(
"from 4.3.1, use proxy_steerable taking socket_t objects")
2380 int rc = zmq_proxy_steerable(frontend, backend, capture, control);
2388 int rc = zmq_proxy_steerable(frontend.handle(), backend.handle(), capture.handle(),
2407 #ifdef ZMQ_HAS_RVALUE_REFS
2426 monitor(socket, addr.c_str(), events);
2431 init(socket, addr_, events);
2438 void init(
socket_t& socket, std::string
const& addr,
int events = ZMQ_EVENT_ALL)
2440 init(socket, addr.c_str(), events);
2445 int rc = zmq_socket_monitor(socket.handle(), addr_, events);
2461 zmq_msg_init(&eventMsg);
2469 if (items[0].revents & ZMQ_POLLIN)
2472 if (rc == -1 && zmq_errno() == ETERM)
2478 zmq_msg_close(&eventMsg);
2482 #if ZMQ_VERSION_MAJOR >= 4
2483 const char*
data =
static_cast<const char*
>(zmq_msg_data(&eventMsg));
2485 memcpy(&msgEvent.
event,
data,
sizeof(uint16_t));
2486 data +=
sizeof(uint16_t);
2487 memcpy(&msgEvent.
value,
data,
sizeof(int32_t));
2493 #ifdef ZMQ_NEW_MONITOR_EVENT_LAYOUT
2495 zmq_msg_init(&addrMsg);
2497 if (rc == -1 && zmq_errno() == ETERM)
2499 zmq_msg_close(&eventMsg);
2504 const char* str =
static_cast<const char*
>(zmq_msg_data(&addrMsg));
2505 std::string address(str, str + zmq_msg_size(&addrMsg));
2506 zmq_msg_close(&addrMsg);
2510 std::string address =
event->data.connected.addr;
2513 #ifdef ZMQ_EVENT_MONITOR_STOPPED
2514 if (event->event == ZMQ_EVENT_MONITOR_STOPPED)
2516 zmq_msg_close(&eventMsg);
2522 switch (event->event)
2524 case ZMQ_EVENT_CONNECTED:
2527 case ZMQ_EVENT_CONNECT_DELAYED:
2530 case ZMQ_EVENT_CONNECT_RETRIED:
2533 case ZMQ_EVENT_LISTENING:
2536 case ZMQ_EVENT_BIND_FAILED:
2539 case ZMQ_EVENT_ACCEPTED:
2542 case ZMQ_EVENT_ACCEPT_FAILED:
2545 case ZMQ_EVENT_CLOSED:
2548 case ZMQ_EVENT_CLOSE_FAILED:
2551 case ZMQ_EVENT_DISCONNECTED:
2554 #ifdef ZMQ_BUILD_DRAFT_API
2555 #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 3)
2556 case ZMQ_EVENT_HANDSHAKE_FAILED_NO_DETAIL:
2559 case ZMQ_EVENT_HANDSHAKE_FAILED_PROTOCOL:
2562 case ZMQ_EVENT_HANDSHAKE_FAILED_AUTH:
2565 case ZMQ_EVENT_HANDSHAKE_SUCCEEDED:
2568 #elif ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 1)
2569 case ZMQ_EVENT_HANDSHAKE_FAILED:
2570 on_event_handshake_failed(*event, address.c_str());
2572 case ZMQ_EVENT_HANDSHAKE_SUCCEED:
2573 on_event_handshake_succeed(*event, address.c_str());
2581 zmq_msg_close(&eventMsg);
2586 #ifdef ZMQ_EVENT_MONITOR_STOPPED
2648 #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 3)
2672 #elif ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 1)
2673 virtual void on_event_handshake_failed(
const zmq_event_t& event_,
const char* addr_)
2678 virtual void on_event_handshake_succeed(
const zmq_event_t& event_,
const char* addr_)
2705 #if defined(ZMQ_BUILD_DRAFT_API) && defined(ZMQ_CPP11) && defined(ZMQ_HAVE_POLLER)
2708 enum class event_flags : short
2711 pollin = ZMQ_POLLIN,
2712 pollout = ZMQ_POLLOUT,
2713 pollerr = ZMQ_POLLERR,
2714 pollpri = ZMQ_POLLPRI
2717 constexpr event_flags
operator|(event_flags a, event_flags b) noexcept
2719 return detail::enum_bit_or(a, b);
2721 constexpr event_flags
operator&(event_flags a, event_flags b) noexcept
2723 return detail::enum_bit_and(a, b);
2725 constexpr event_flags operator^(event_flags a, event_flags b) noexcept
2727 return detail::enum_bit_xor(a, b);
2729 constexpr event_flags operator~(event_flags a) noexcept
2731 return detail::enum_bit_not(a);
2734 struct no_user_data;
2737 template <
class T = no_user_data>
2750 template <
typename T = no_user_data>
2754 using event_type = poller_event<T>;
2756 poller_t() : poller_ptr(zmq_poller_new())
2762 template <
typename Dummy =
void,
2763 typename =
typename std::enable_if<!std::is_same<T, no_user_data>::value,
2767 add_impl(socket, events, user_data);
2772 add_impl(socket, events,
nullptr);
2777 if (0 != zmq_poller_remove(poller_ptr.get(), socket.handle()))
2786 zmq_poller_modify(poller_ptr.get(), socket.handle(),
static_cast<short>(events)))
2792 size_t wait_all(std::vector<event_type>& poller_events,
2793 const std::chrono::milliseconds timeout)
2795 int rc = zmq_poller_wait_all(
2796 poller_ptr.get(),
reinterpret_cast<zmq_poller_event_t*
>(poller_events.data()),
2797 static_cast<int>(poller_events.size()),
static_cast<long>(timeout.count()));
2799 return static_cast<size_t>(rc);
2801 #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 3)
2802 if (zmq_errno() == EAGAIN)
2804 if (zmq_errno() == ETIMEDOUT)
2812 struct destroy_poller_t
2814 void operator()(
void*
ptr) noexcept
2816 int rc = zmq_poller_destroy(&
ptr);
2821 std::unique_ptr<void, destroy_poller_t> poller_ptr;
2823 void add_impl(
zmq::socket_ref socket, event_flags events, T* user_data)
2825 if (0 != zmq_poller_add(poller_ptr.get(), socket.handle(), user_data,
2826 static_cast<short>(events)))
2832 #endif // defined(ZMQ_BUILD_DRAFT_API) && defined(ZMQ_CPP11) && defined(ZMQ_HAVE_POLLER)
2836 return os <<
msg.str();
2841 #endif // __ZMQ_HPP_INCLUDED__