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, \ 151 CPPZMQ_VERSION_PATCH) 154 #if (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 2)) \ 155 && defined(__GXX_EXPERIMENTAL_CXX0X__)) 156 #define ZMQ_HAS_RVALUE_REFS 157 #define ZMQ_DELETED_FUNCTION = delete 158 #elif defined(__clang__) 159 #if __has_feature(cxx_rvalue_references) 160 #define ZMQ_HAS_RVALUE_REFS 163 #if __has_feature(cxx_deleted_functions) 164 #define ZMQ_DELETED_FUNCTION = delete 166 #define ZMQ_DELETED_FUNCTION 168 #elif defined(_MSC_VER) && (_MSC_VER >= 1900) 169 #define ZMQ_HAS_RVALUE_REFS 170 #define ZMQ_DELETED_FUNCTION = delete 171 #elif defined(_MSC_VER) && (_MSC_VER >= 1600) 172 #define ZMQ_HAS_RVALUE_REFS 173 #define ZMQ_DELETED_FUNCTION 175 #define ZMQ_DELETED_FUNCTION 178 #if defined(ZMQ_CPP11) && !defined(__llvm__) && !defined(__INTEL_COMPILER) \ 179 && defined(__GNUC__) && __GNUC__ < 5 180 #define ZMQ_CPP11_PARTIAL 181 #elif defined(__GLIBCXX__) && __GLIBCXX__ < 20160805 184 #define ZMQ_CPP11_PARTIAL 188 #ifdef ZMQ_CPP11_PARTIAL 189 #define ZMQ_IS_TRIVIALLY_COPYABLE(T) __has_trivial_copy(T) 191 #include <type_traits> 192 #define ZMQ_IS_TRIVIALLY_COPYABLE(T) std::is_trivially_copyable<T>::value 196 #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(3, 3, 0) 197 #define ZMQ_NEW_MONITOR_EVENT_LAYOUT 200 #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 1, 0) 201 #define ZMQ_HAS_PROXY_STEERABLE 211 #if ZMQ_VERSION < ZMQ_MAKE_VERSION(3, 2, 0) 212 #define zmq_msg_recv(msg, socket, flags) zmq_recvmsg(socket, msg, flags) 219 #define ZMQ_ASSERT(expression) assert(expression) 221 #define ZMQ_ASSERT(expression) (void) (expression) 233 template<
class T>
auto begin(T &&r) -> decltype(begin(std::forward<T>(r)))
235 return begin(std::forward<T>(r));
237 template<
class T>
auto end(T &&r) -> decltype(end(std::forward<T>(r)))
239 return end(std::forward<T>(r));
243 template<
class T>
using void_t = void;
246 using iter_value_t =
typename std::iterator_traits<Iter>::value_type;
248 template<
class Range>
249 using range_iter_t = decltype(
250 ranges::begin(std::declval<
typename std::remove_reference<Range>::type &>()));
252 template<
class Range>
using range_value_t = iter_value_t<range_iter_t<Range>>;
254 template<
class T,
class =
void>
struct is_range : std::false_type
262 ranges::begin(std::declval<typename std::remove_reference<T>::type &>())
263 == ranges::end(std::declval<typename std::remove_reference<T>::type &>()))>>
274 class error_t :
public std::exception
281 return zmq_strerror(errnum);
289 inline int poll(zmq_pollitem_t *items_,
size_t nitems_,
long timeout_ = -1)
291 int rc = zmq_poll(items_, static_cast<int>(nitems_), timeout_);
297 ZMQ_DEPRECATED(
"from 4.3.1, use poll taking non-const items")
298 inline int poll(zmq_pollitem_t
const *items_,
size_t nitems_,
long timeout_ = -1)
300 return poll(const_cast<zmq_pollitem_t *>(items_), nitems_, timeout_);
304 ZMQ_DEPRECATED(
"from 4.3.1, use poll taking non-const items")
306 poll(zmq_pollitem_t
const *items,
size_t nitems, std::chrono::milliseconds timeout)
308 return poll(const_cast<zmq_pollitem_t *>(items), nitems,
309 static_cast<long>(timeout.count()));
312 ZMQ_DEPRECATED(
"from 4.3.1, use poll taking non-const items")
313 inline int poll(std::vector<zmq_pollitem_t>
const &items,
314 std::chrono::milliseconds timeout)
316 return poll(const_cast<zmq_pollitem_t *>(items.data()), items.size(),
317 static_cast<long>(timeout.count()));
320 ZMQ_DEPRECATED(
"from 4.3.1, use poll taking non-const items")
321 inline int poll(std::vector<zmq_pollitem_t>
const &items,
long timeout_ = -1)
323 return poll(const_cast<zmq_pollitem_t *>(items.data()), items.size(), timeout_);
327 poll(zmq_pollitem_t *items,
size_t nitems, std::chrono::milliseconds timeout)
329 return poll(items, nitems, static_cast<long>(timeout.count()));
332 inline int poll(std::vector<zmq_pollitem_t> &items,
333 std::chrono::milliseconds timeout)
335 return poll(items.data(), items.size(),
static_cast<long>(timeout.count()));
338 ZMQ_DEPRECATED(
"from 4.3.1, use poll taking std::chrono instead of long")
339 inline int poll(std::vector<zmq_pollitem_t> &items,
long timeout_ = -1)
341 return poll(items.data(), items.size(), timeout_);
344 template<std::
size_t SIZE>
345 inline int poll(std::array<zmq_pollitem_t, SIZE> &items,
346 std::chrono::milliseconds timeout)
348 return poll(items.data(), items.size(),
static_cast<long>(timeout.count()));
353 inline void version(
int *major_,
int *minor_,
int *patch_)
355 zmq_version(major_, minor_, patch_);
359 inline std::tuple<int, int, int>
version()
361 std::tuple<int, int, int> v;
362 zmq_version(&std::get<0>(v), &std::get<1>(v), &std::get<2>(v));
366 #if !defined(ZMQ_CPP11_PARTIAL) 369 template<
class T>
struct is_char_type
372 static constexpr
bool value =
373 std::is_same<T, char>::value || std::is_same<T, wchar_t>::value
374 || std::is_same<T, char16_t>::value || std::is_same<T, char32_t>::value;
386 int rc = zmq_msg_init(&msg);
392 int rc = zmq_msg_init_size(&msg, size_);
397 template<
class ForwardIter>
message_t(ForwardIter first, ForwardIter last)
399 typedef typename std::iterator_traits<ForwardIter>::value_type value_t;
401 assert(std::distance(first, last) >= 0);
403 static_cast<size_t>(std::distance(first, last)) *
sizeof(value_t);
404 int const rc = zmq_msg_init_size(&msg, size_);
407 std::copy(first, last, data<value_t>());
412 int rc = zmq_msg_init_size(&msg, size_);
418 memcpy(
data(), data_, size_);
424 int rc = zmq_msg_init_data(&msg, data_, size_, ffn_, hint_);
430 #if defined(ZMQ_CPP11) && !defined(ZMQ_CPP11_PARTIAL) 438 typename =
typename std::enable_if<detail::is_char_type<Char>::value>::type>
439 ZMQ_DEPRECATED(
"from 4.7.0, use constructors taking iterators, (pointer, size) " 440 "or strings instead")
442 message_t(detail::ranges::begin(
data), detail::ranges::end(
data))
446 template<
class Range,
448 detail::is_range<Range>::value
449 && ZMQ_IS_TRIVIALLY_COPYABLE(detail::range_value_t<Range>)
450 && !detail::is_char_type<detail::range_value_t<Range>>::value
451 && !std::is_same<Range, message_t>::value>::type>
452 explicit message_t(
const Range &rng) :
453 message_t(detail::ranges::begin(rng), detail::ranges::end(rng))
457 explicit message_t(
const std::string &str) : message_t(str.data(), str.size()) {}
459 #if CPPZMQ_HAS_STRING_VIEW 460 explicit message_t(std::string_view str) : message_t(str.data(), str.size()) {}
465 #ifdef ZMQ_HAS_RVALUE_REFS 466 message_t(message_t &&rhs)
ZMQ_NOTHROW : msg(rhs.msg)
468 int rc = zmq_msg_init(&rhs.msg);
481 int rc = zmq_msg_close(&msg);
487 int rc = zmq_msg_close(&msg);
490 rc = zmq_msg_init(&msg);
496 int rc = zmq_msg_close(&msg);
499 rc = zmq_msg_init_size(&msg, size_);
506 int rc = zmq_msg_close(&msg);
509 rc = zmq_msg_init_size(&msg, size_);
512 memcpy(
data(), data_, size_);
517 int rc = zmq_msg_close(&msg);
520 rc = zmq_msg_init_data(&msg, data_, size_, ffn_, hint_);
525 ZMQ_DEPRECATED(
"from 4.3.1, use move taking non-const reference instead")
526 void move(message_t const *msg_)
528 int rc = zmq_msg_move(&msg, const_cast<zmq_msg_t *>(msg_->handle()));
535 int rc = zmq_msg_move(&msg, msg_.
handle());
540 ZMQ_DEPRECATED(
"from 4.3.1, use copy taking non-const reference instead")
543 int rc = zmq_msg_copy(&msg, const_cast<zmq_msg_t *>(msg_->handle()));
550 int rc = zmq_msg_copy(&msg, msg_.
handle());
557 int rc = zmq_msg_more(const_cast<zmq_msg_t *>(&msg));
565 return zmq_msg_data(const_cast<zmq_msg_t *>(&msg));
570 return zmq_msg_size(const_cast<zmq_msg_t *>(&msg));
579 return static_cast<T
const *
>(
data());
582 ZMQ_DEPRECATED(
"from 4.3.0, use operator== instead")
587 const size_t my_size = size();
588 return my_size == other.size() && 0 == memcmp(
data(), other.data(), my_size);
593 return !(*
this == other);
596 #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(3, 2, 0) 597 int get(
int property_)
599 int value = zmq_msg_get(&msg, property_);
606 #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 1, 0) 607 const char *
gets(
const char *property_)
609 const char *value = zmq_msg_gets(&msg, property_);
616 #if defined(ZMQ_BUILD_DRAFT_API) && ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 0) 617 uint32_t routing_id()
const 619 return zmq_msg_routing_id(const_cast<zmq_msg_t *>(&msg));
622 void set_routing_id(uint32_t routing_id)
624 int rc = zmq_msg_set_routing_id(&msg, routing_id);
629 const char *group()
const 631 return zmq_msg_group(const_cast<zmq_msg_t *>(&msg));
634 void set_group(
const char *group)
636 int rc = zmq_msg_set_group(&msg, group);
645 return std::string(static_cast<const char *>(
data()), size());
647 #if CPPZMQ_HAS_STRING_VIEW 651 return std::string_view(static_cast<const char *>(
data()), size());
664 std::stringstream os;
666 const unsigned char *msg_data = this->data<unsigned char>();
668 size_t size = this->size();
669 int is_ascii[2] = {0, 0};
671 os <<
"zmq::message_t [size " << std::dec << std::setw(3)
672 << std::setfill(
'0') << size <<
"] (";
675 os <<
"... too big to print)";
680 is_ascii[1] = (byte >= 32 && byte < 127);
681 if (is_ascii[1] != is_ascii[0])
687 os << std::hex << std::uppercase << std::setw(2)
688 << std::setfill(
'0') <<
static_cast<short>(byte);
690 is_ascii[0] = is_ascii[1];
727 #ifdef ZMQ_IO_THREADS 728 io_threads = ZMQ_IO_THREADS,
730 #ifdef ZMQ_THREAD_SCHED_POLICY 731 thread_sched_policy = ZMQ_THREAD_SCHED_POLICY,
733 #ifdef ZMQ_THREAD_PRIORITY 734 thread_priority = ZMQ_THREAD_PRIORITY,
736 #ifdef ZMQ_THREAD_AFFINITY_CPU_ADD 737 thread_affinity_cpu_add = ZMQ_THREAD_AFFINITY_CPU_ADD,
739 #ifdef ZMQ_THREAD_AFFINITY_CPU_REMOVE 740 thread_affinity_cpu_remove = ZMQ_THREAD_AFFINITY_CPU_REMOVE,
742 #ifdef ZMQ_THREAD_NAME_PREFIX 743 thread_name_prefix = ZMQ_THREAD_NAME_PREFIX,
746 max_msgsz = ZMQ_MAX_MSGSZ,
748 #ifdef ZMQ_ZERO_COPY_RECV 749 zero_copy_recv = ZMQ_ZERO_COPY_RECV,
751 #ifdef ZMQ_MAX_SOCKETS 752 max_sockets = ZMQ_MAX_SOCKETS,
754 #ifdef ZMQ_SOCKET_LIMIT 755 socket_limit = ZMQ_SOCKET_LIMIT,
760 #ifdef ZMQ_MSG_T_SIZE 761 msg_t_size = ZMQ_MSG_T_SIZE
777 explicit context_t(
int io_threads_,
int max_sockets_ = ZMQ_MAX_SOCKETS_DFLT)
783 int rc = zmq_ctx_set(ptr, ZMQ_IO_THREADS, io_threads_);
786 rc = zmq_ctx_set(ptr, ZMQ_MAX_SOCKETS, max_sockets_);
790 #ifdef ZMQ_HAS_RVALUE_REFS 803 int setctxopt(
int option_,
int optval_)
805 int rc = zmq_ctx_set(ptr, option_, optval_);
811 int getctxopt(
int option_) {
return zmq_ctx_get(ptr, option_); }
814 void set(ctxopt option,
int optval)
816 int rc = zmq_ctx_set(ptr, static_cast<int>(option), optval);
823 int rc = zmq_ctx_get(ptr, static_cast<int>(option));
841 rc = zmq_ctx_destroy(ptr);
842 }
while (rc == -1 && errno == EINTR);
855 int rc = zmq_ctx_shutdown(ptr);
868 ZMQ_DEPRECATED(
"from 4.7.0, use handle() != nullptr instead")
887 struct recv_buffer_size
890 size_t untruncated_size;
894 return size != untruncated_size;
898 #if CPPZMQ_HAS_OPTIONAL 900 using send_result_t = std::optional<size_t>;
901 using recv_result_t = std::optional<size_t>;
902 using recv_buffer_result_t = std::optional<recv_buffer_size>;
910 template<
class T>
class trivial_optional
913 static_assert(std::is_trivial<T>::value,
"T must be trivial");
914 using value_type = T;
916 trivial_optional() =
default;
917 trivial_optional(T value) noexcept : _value(value), _has_value(
true) {}
919 const T *operator->()
const noexcept
924 T *operator->() noexcept
930 const T &operator*()
const noexcept
935 T &operator*() noexcept
944 throw std::exception();
947 const T &value()
const 950 throw std::exception();
954 explicit operator bool()
const noexcept {
return _has_value; }
955 bool has_value()
const noexcept {
return _has_value; }
959 bool _has_value{
false};
963 using send_result_t = detail::trivial_optional<size_t>;
964 using recv_result_t = detail::trivial_optional<size_t>;
965 using recv_buffer_result_t = detail::trivial_optional<recv_buffer_size>;
971 template<
class T> constexpr T enum_bit_or(T a, T b) noexcept
973 static_assert(std::is_enum<T>::value,
"must be enum");
974 using U =
typename std::underlying_type<T>::type;
975 return static_cast<T
>(
static_cast<U
>(a) | static_cast<U>(b));
977 template<
class T> constexpr T enum_bit_and(T a, T b) noexcept
979 static_assert(std::is_enum<T>::value,
"must be enum");
980 using U =
typename std::underlying_type<T>::type;
981 return static_cast<T
>(
static_cast<U
>(a) & static_cast<U>(b));
983 template<
class T> constexpr T enum_bit_xor(T a, T b) noexcept
985 static_assert(std::is_enum<T>::value,
"must be enum");
986 using U =
typename std::underlying_type<T>::type;
987 return static_cast<T
>(
static_cast<U
>(a) ^ static_cast<U>(b));
989 template<
class T> constexpr T enum_bit_not(T a) noexcept
991 static_assert(std::is_enum<T>::value,
"must be enum");
992 using U =
typename std::underlying_type<T>::type;
993 return static_cast<T
>(~static_cast<U>(a));
998 enum class send_flags : int
1001 dontwait = ZMQ_DONTWAIT,
1002 sndmore = ZMQ_SNDMORE
1005 constexpr send_flags operator|(send_flags a, send_flags b) noexcept
1007 return detail::enum_bit_or(a, b);
1009 constexpr send_flags operator&(send_flags a, send_flags b) noexcept
1011 return detail::enum_bit_and(a, b);
1013 constexpr send_flags operator^(send_flags a, send_flags b) noexcept
1015 return detail::enum_bit_xor(a, b);
1017 constexpr send_flags operator~(send_flags a) noexcept
1019 return detail::enum_bit_not(a);
1023 enum class recv_flags : int
1026 dontwait = ZMQ_DONTWAIT
1029 constexpr recv_flags operator|(recv_flags a, recv_flags b) noexcept
1031 return detail::enum_bit_or(a, b);
1033 constexpr recv_flags operator&(recv_flags a, recv_flags b) noexcept
1035 return detail::enum_bit_and(a, b);
1037 constexpr recv_flags operator^(recv_flags a, recv_flags b) noexcept
1039 return detail::enum_bit_xor(a, b);
1041 constexpr recv_flags operator~(recv_flags a) noexcept
1043 return detail::enum_bit_not(a);
1051 class mutable_buffer
1054 constexpr mutable_buffer() noexcept : _data(
nullptr), _size(0) {}
1055 constexpr mutable_buffer(
void *p,
size_t n) noexcept : _data(p), _size(n)
1057 #ifdef ZMQ_EXTENDED_CONSTEXPR 1058 assert(p !=
nullptr || n == 0);
1062 constexpr
void *
data()
const noexcept {
return _data; }
1063 constexpr
size_t size()
const noexcept {
return _size; }
1064 mutable_buffer &operator+=(
size_t n) noexcept
1067 const auto shift = (std::min)(n, _size);
1068 _data =
static_cast<char *
>(_data) + shift;
1078 inline mutable_buffer operator+(
const mutable_buffer &mb,
size_t n) noexcept
1080 return mutable_buffer(static_cast<char *>(mb.data()) + (std::min)(n, mb.size()),
1081 mb.size() - (std::min)(n, mb.size()));
1083 inline mutable_buffer operator+(
size_t n,
const mutable_buffer &mb) noexcept
1091 constexpr const_buffer() noexcept : _data(
nullptr), _size(0) {}
1092 constexpr const_buffer(
const void *p,
size_t n) noexcept : _data(p), _size(n)
1094 #ifdef ZMQ_EXTENDED_CONSTEXPR 1095 assert(p !=
nullptr || n == 0);
1098 constexpr const_buffer(
const mutable_buffer &mb) noexcept :
1104 constexpr
const void *
data()
const noexcept {
return _data; }
1105 constexpr
size_t size()
const noexcept {
return _size; }
1106 const_buffer &operator+=(
size_t n) noexcept
1108 const auto shift = (std::min)(n, _size);
1109 _data =
static_cast<const char *
>(_data) + shift;
1119 inline const_buffer operator+(
const const_buffer &cb,
size_t n) noexcept
1121 return const_buffer(static_cast<const char *>(cb.data())
1122 + (std::min)(n, cb.size()),
1123 cb.size() - (std::min)(n, cb.size()));
1125 inline const_buffer operator+(
size_t n,
const const_buffer &cb) noexcept
1132 constexpr mutable_buffer
buffer(
void *p,
size_t n) noexcept
1134 return mutable_buffer(p, n);
1136 constexpr const_buffer
buffer(
const void *p,
size_t n) noexcept
1138 return const_buffer(p, n);
1140 constexpr mutable_buffer
buffer(
const mutable_buffer &mb) noexcept
1144 inline mutable_buffer
buffer(
const mutable_buffer &mb,
size_t n) noexcept
1146 return mutable_buffer(mb.data(), (std::min)(mb.size(), n));
1148 constexpr const_buffer
buffer(
const const_buffer &cb) noexcept
1152 inline const_buffer
buffer(
const const_buffer &cb,
size_t n) noexcept
1154 return const_buffer(cb.data(), (std::min)(cb.size(), n));
1159 template<
class T>
struct is_buffer
1161 static constexpr
bool value =
1162 std::is_same<T, const_buffer>::value || std::is_same<T, mutable_buffer>::value;
1165 template<
class T>
struct is_pod_like
1171 static constexpr
bool value =
1172 ZMQ_IS_TRIVIALLY_COPYABLE(T) && std::is_standard_layout<T>::value;
1175 template<
class C> constexpr
auto seq_size(
const C &c) noexcept -> decltype(c.size())
1179 template<
class T,
size_t N>
1180 constexpr
size_t seq_size(
const T (&)[N]) noexcept
1186 auto buffer_contiguous_sequence(Seq &&seq) noexcept
1187 -> decltype(
buffer(std::addressof(*std::begin(seq)),
size_t{}))
1189 using T =
typename std::remove_cv<
1190 typename std::remove_reference<decltype(*std::begin(seq))>::type>::type;
1191 static_assert(detail::is_pod_like<T>::value,
"T must be POD");
1193 const auto size = seq_size(seq);
1194 return buffer(size != 0u ? std::addressof(*std::begin(seq)) :
nullptr,
1198 auto buffer_contiguous_sequence(Seq &&seq,
size_t n_bytes) noexcept
1199 -> decltype(buffer_contiguous_sequence(seq))
1201 using T =
typename std::remove_cv<
1202 typename std::remove_reference<decltype(*std::begin(seq))>::type>::type;
1203 static_assert(detail::is_pod_like<T>::value,
"T must be POD");
1205 const auto size = seq_size(seq);
1206 return buffer(size != 0u ? std::addressof(*std::begin(seq)) :
nullptr,
1207 (std::min)(size *
sizeof(T), n_bytes));
1213 template<
class T,
size_t N> mutable_buffer
buffer(T (&
data)[N]) noexcept
1215 return detail::buffer_contiguous_sequence(
data);
1217 template<
class T,
size_t N>
1218 mutable_buffer
buffer(T (&
data)[N],
size_t n_bytes) noexcept
1220 return detail::buffer_contiguous_sequence(
data, n_bytes);
1222 template<
class T,
size_t N> const_buffer
buffer(
const T (&
data)[N]) noexcept
1224 return detail::buffer_contiguous_sequence(
data);
1226 template<
class T,
size_t N>
1227 const_buffer
buffer(
const T (&
data)[N],
size_t n_bytes) noexcept
1229 return detail::buffer_contiguous_sequence(
data, n_bytes);
1232 template<
class T,
size_t N> mutable_buffer
buffer(std::array<T, N> &
data) noexcept
1234 return detail::buffer_contiguous_sequence(
data);
1236 template<
class T,
size_t N>
1237 mutable_buffer
buffer(std::array<T, N> &
data,
size_t n_bytes) noexcept
1239 return detail::buffer_contiguous_sequence(
data, n_bytes);
1241 template<
class T,
size_t N>
1242 const_buffer
buffer(std::array<const T, N> &
data) noexcept
1244 return detail::buffer_contiguous_sequence(
data);
1246 template<
class T,
size_t N>
1247 const_buffer
buffer(std::array<const T, N> &
data,
size_t n_bytes) noexcept
1249 return detail::buffer_contiguous_sequence(
data, n_bytes);
1251 template<
class T,
size_t N>
1252 const_buffer
buffer(
const std::array<T, N> &
data) noexcept
1254 return detail::buffer_contiguous_sequence(
data);
1256 template<
class T,
size_t N>
1257 const_buffer
buffer(
const std::array<T, N> &
data,
size_t n_bytes) noexcept
1259 return detail::buffer_contiguous_sequence(
data, n_bytes);
1262 template<
class T,
class Allocator>
1263 mutable_buffer
buffer(std::vector<T, Allocator> &
data) noexcept
1265 return detail::buffer_contiguous_sequence(
data);
1267 template<
class T,
class Allocator>
1268 mutable_buffer
buffer(std::vector<T, Allocator> &
data,
size_t n_bytes) noexcept
1270 return detail::buffer_contiguous_sequence(
data, n_bytes);
1272 template<
class T,
class Allocator>
1273 const_buffer
buffer(
const std::vector<T, Allocator> &
data) noexcept
1275 return detail::buffer_contiguous_sequence(
data);
1277 template<
class T,
class Allocator>
1278 const_buffer
buffer(
const std::vector<T, Allocator> &
data,
size_t n_bytes) noexcept
1280 return detail::buffer_contiguous_sequence(
data, n_bytes);
1283 template<
class T,
class Traits,
class Allocator>
1284 mutable_buffer
buffer(std::basic_string<T, Traits, Allocator> &
data) noexcept
1286 return detail::buffer_contiguous_sequence(
data);
1288 template<
class T,
class Traits,
class Allocator>
1289 mutable_buffer
buffer(std::basic_string<T, Traits, Allocator> &
data,
1290 size_t n_bytes) noexcept
1292 return detail::buffer_contiguous_sequence(
data, n_bytes);
1294 template<
class T,
class Traits,
class Allocator>
1295 const_buffer
buffer(
const std::basic_string<T, Traits, Allocator> &
data) noexcept
1297 return detail::buffer_contiguous_sequence(
data);
1299 template<
class T,
class Traits,
class Allocator>
1300 const_buffer
buffer(
const std::basic_string<T, Traits, Allocator> &
data,
1301 size_t n_bytes) noexcept
1303 return detail::buffer_contiguous_sequence(
data, n_bytes);
1306 #if CPPZMQ_HAS_STRING_VIEW 1308 template<
class T,
class Traits>
1309 const_buffer
buffer(std::basic_string_view<T, Traits>
data) noexcept
1311 return detail::buffer_contiguous_sequence(
data);
1313 template<
class T,
class Traits>
1314 const_buffer
buffer(std::basic_string_view<T, Traits>
data,
size_t n_bytes) noexcept
1316 return detail::buffer_contiguous_sequence(
data, n_bytes);
1323 template<
class Char,
size_t N>
1324 constexpr const_buffer str_buffer(
const Char (&
data)[N]) noexcept
1326 static_assert(detail::is_pod_like<Char>::value,
"Char must be POD");
1327 #ifdef ZMQ_EXTENDED_CONSTEXPR 1328 assert(
data[N - 1] == Char{0});
1330 return const_buffer(static_cast<const Char *>(
data), (N - 1) *
sizeof(Char));
1335 constexpr const_buffer
operator"" _zbuf(
const char *str,
size_t len) noexcept
1337 return const_buffer(str, len *
sizeof(
char));
1339 constexpr const_buffer
operator"" _zbuf(
const wchar_t *str,
size_t len) noexcept
1341 return const_buffer(str, len *
sizeof(
wchar_t));
1343 constexpr const_buffer
operator"" _zbuf(
const char16_t *str,
size_t len) noexcept
1345 return const_buffer(str, len *
sizeof(char16_t));
1347 constexpr const_buffer
operator"" _zbuf(
const char32_t *str,
size_t len) noexcept
1349 return const_buffer(str, len *
sizeof(char32_t));
1364 template<
int Opt,
class T,
bool BoolUnit = false>
struct integral_option
1372 template<
int Opt,
int NullTerm = 1>
struct array_option
1376 #define ZMQ_DEFINE_INTEGRAL_OPT(OPT, NAME, TYPE) \ 1377 using NAME##_t = integral_option<OPT, TYPE, false>; \ 1378 ZMQ_INLINE_VAR ZMQ_CONSTEXPR_VAR NAME##_t NAME {} 1379 #define ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(OPT, NAME, TYPE) \ 1380 using NAME##_t = integral_option<OPT, TYPE, true>; \ 1381 ZMQ_INLINE_VAR ZMQ_CONSTEXPR_VAR NAME##_t NAME {} 1382 #define ZMQ_DEFINE_ARRAY_OPT(OPT, NAME) \ 1383 using NAME##_t = array_option<OPT>; \ 1384 ZMQ_INLINE_VAR ZMQ_CONSTEXPR_VAR NAME##_t NAME {} 1385 #define ZMQ_DEFINE_ARRAY_OPT_BINARY(OPT, NAME) \ 1386 using NAME##_t = array_option<OPT, 0>; \ 1387 ZMQ_INLINE_VAR ZMQ_CONSTEXPR_VAR NAME##_t NAME {} 1388 #define ZMQ_DEFINE_ARRAY_OPT_BIN_OR_Z85(OPT, NAME) \ 1389 using NAME##_t = array_option<OPT, 2>; \ 1390 ZMQ_INLINE_VAR ZMQ_CONSTEXPR_VAR NAME##_t NAME {} 1395 typedef unsigned __int64 cppzmq_fd_t;
1397 typedef unsigned int cppzmq_fd_t;
1400 typedef int cppzmq_fd_t;
1404 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_AFFINITY, affinity, uint64_t);
1407 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_BACKLOG, backlog,
int);
1409 #ifdef ZMQ_BINDTODEVICE 1410 ZMQ_DEFINE_ARRAY_OPT_BINARY(ZMQ_BINDTODEVICE, bindtodevice);
1413 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_CONFLATE, conflate,
int);
1415 #ifdef ZMQ_CONNECT_ROUTING_ID 1416 ZMQ_DEFINE_ARRAY_OPT(ZMQ_CONNECT_ROUTING_ID, connect_routing_id);
1418 #ifdef ZMQ_CONNECT_TIMEOUT 1419 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_CONNECT_TIMEOUT, connect_timeout,
int);
1421 #ifdef ZMQ_CURVE_PUBLICKEY 1422 ZMQ_DEFINE_ARRAY_OPT_BIN_OR_Z85(ZMQ_CURVE_PUBLICKEY, curve_publickey);
1424 #ifdef ZMQ_CURVE_SECRETKEY 1425 ZMQ_DEFINE_ARRAY_OPT_BIN_OR_Z85(ZMQ_CURVE_SECRETKEY, curve_secretkey);
1427 #ifdef ZMQ_CURVE_SERVER 1428 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_CURVE_SERVER, curve_server,
int);
1430 #ifdef ZMQ_CURVE_SERVERKEY 1431 ZMQ_DEFINE_ARRAY_OPT_BIN_OR_Z85(ZMQ_CURVE_SERVERKEY, curve_serverkey);
1434 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_EVENTS, events,
int);
1437 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_FD, fd, cppzmq_fd_t);
1439 #ifdef ZMQ_GSSAPI_PLAINTEXT 1440 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_GSSAPI_PLAINTEXT, gssapi_plaintext,
int);
1442 #ifdef ZMQ_GSSAPI_SERVER 1443 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_GSSAPI_SERVER, gssapi_server,
int);
1445 #ifdef ZMQ_GSSAPI_SERVICE_PRINCIPAL 1446 ZMQ_DEFINE_ARRAY_OPT(ZMQ_GSSAPI_SERVICE_PRINCIPAL, gssapi_service_principal);
1448 #ifdef ZMQ_GSSAPI_SERVICE_PRINCIPAL_NAMETYPE 1449 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_GSSAPI_SERVICE_PRINCIPAL_NAMETYPE,
1450 gssapi_service_principal_nametype,
1453 #ifdef ZMQ_GSSAPI_PRINCIPAL 1454 ZMQ_DEFINE_ARRAY_OPT(ZMQ_GSSAPI_PRINCIPAL, gssapi_principal);
1456 #ifdef ZMQ_GSSAPI_PRINCIPAL_NAMETYPE 1457 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_GSSAPI_PRINCIPAL_NAMETYPE,
1458 gssapi_principal_nametype,
1461 #ifdef ZMQ_HANDSHAKE_IVL 1462 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_HANDSHAKE_IVL, handshake_ivl,
int);
1464 #ifdef ZMQ_HEARTBEAT_IVL 1465 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_HEARTBEAT_IVL, heartbeat_ivl,
int);
1467 #ifdef ZMQ_HEARTBEAT_TIMEOUT 1468 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_HEARTBEAT_TIMEOUT, heartbeat_timeout,
int);
1470 #ifdef ZMQ_HEARTBEAT_TTL 1471 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_HEARTBEAT_TTL, heartbeat_ttl,
int);
1473 #ifdef ZMQ_IMMEDIATE 1474 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_IMMEDIATE, immediate,
int);
1476 #ifdef ZMQ_INVERT_MATCHING 1477 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_INVERT_MATCHING, invert_matching,
int);
1480 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_IPV6, ipv6,
int);
1482 #ifdef ZMQ_LAST_ENDPOINT 1483 ZMQ_DEFINE_ARRAY_OPT(ZMQ_LAST_ENDPOINT, last_endpoint);
1486 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_LINGER, linger,
int);
1488 #ifdef ZMQ_MAXMSGSIZE 1489 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_MAXMSGSIZE, maxmsgsize, int64_t);
1491 #ifdef ZMQ_MECHANISM 1492 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_MECHANISM, mechanism,
int);
1495 ZMQ_DEFINE_ARRAY_OPT(ZMQ_METADATA, metadata);
1497 #ifdef ZMQ_MULTICAST_HOPS 1498 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_MULTICAST_HOPS, multicast_hops,
int);
1500 #ifdef ZMQ_MULTICAST_LOOP 1501 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_MULTICAST_LOOP, multicast_loop,
int);
1503 #ifdef ZMQ_MULTICAST_MAXTPDU 1504 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_MULTICAST_MAXTPDU, multicast_maxtpdu,
int);
1506 #ifdef ZMQ_PLAIN_SERVER 1507 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_PLAIN_SERVER, plain_server,
int);
1509 #ifdef ZMQ_PLAIN_PASSWORD 1510 ZMQ_DEFINE_ARRAY_OPT(ZMQ_PLAIN_PASSWORD, plain_password);
1512 #ifdef ZMQ_PLAIN_USERNAME 1513 ZMQ_DEFINE_ARRAY_OPT(ZMQ_PLAIN_USERNAME, plain_username);
1516 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_USE_FD, use_fd,
int);
1518 #ifdef ZMQ_PROBE_ROUTER 1519 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_PROBE_ROUTER, probe_router,
int);
1522 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_RATE, rate,
int);
1525 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_RCVBUF, rcvbuf,
int);
1528 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_RCVHWM, rcvhwm,
int);
1531 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_RCVMORE, rcvmore,
int);
1534 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_RCVTIMEO, rcvtimeo,
int);
1536 #ifdef ZMQ_RECONNECT_IVL 1537 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_RECONNECT_IVL, reconnect_ivl,
int);
1539 #ifdef ZMQ_RECONNECT_IVL_MAX 1540 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_RECONNECT_IVL_MAX, reconnect_ivl_max,
int);
1542 #ifdef ZMQ_RECOVERY_IVL 1543 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_RECOVERY_IVL, recovery_ivl,
int);
1545 #ifdef ZMQ_REQ_CORRELATE 1546 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_REQ_CORRELATE, req_correlate,
int);
1548 #ifdef ZMQ_REQ_RELAXED 1549 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_REQ_RELAXED, req_relaxed,
int);
1551 #ifdef ZMQ_ROUTER_HANDOVER 1552 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_ROUTER_HANDOVER, router_handover,
int);
1554 #ifdef ZMQ_ROUTER_MANDATORY 1555 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_ROUTER_MANDATORY, router_mandatory,
int);
1557 #ifdef ZMQ_ROUTER_NOTIFY 1558 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_ROUTER_NOTIFY, router_notify,
int);
1560 #ifdef ZMQ_ROUTING_ID 1561 ZMQ_DEFINE_ARRAY_OPT_BINARY(ZMQ_ROUTING_ID, routing_id);
1564 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_SNDBUF, sndbuf,
int);
1567 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_SNDHWM, sndhwm,
int);
1570 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_SNDTIMEO, sndtimeo,
int);
1572 #ifdef ZMQ_SOCKS_PROXY 1573 ZMQ_DEFINE_ARRAY_OPT(ZMQ_SOCKS_PROXY, socks_proxy);
1575 #ifdef ZMQ_STREAM_NOTIFY 1576 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_STREAM_NOTIFY, stream_notify,
int);
1578 #ifdef ZMQ_SUBSCRIBE 1579 ZMQ_DEFINE_ARRAY_OPT(ZMQ_SUBSCRIBE, subscribe);
1581 #ifdef ZMQ_TCP_KEEPALIVE 1582 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TCP_KEEPALIVE, tcp_keepalive,
int);
1584 #ifdef ZMQ_TCP_KEEPALIVE_CNT 1585 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TCP_KEEPALIVE_CNT, tcp_keepalive_cnt,
int);
1587 #ifdef ZMQ_TCP_KEEPALIVE_IDLE 1588 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TCP_KEEPALIVE_IDLE, tcp_keepalive_idle,
int);
1590 #ifdef ZMQ_TCP_KEEPALIVE_INTVL 1591 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TCP_KEEPALIVE_INTVL, tcp_keepalive_intvl,
int);
1593 #ifdef ZMQ_TCP_MAXRT 1594 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TCP_MAXRT, tcp_maxrt,
int);
1596 #ifdef ZMQ_THREAD_SAFE 1597 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_THREAD_SAFE, thread_safe,
int);
1600 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TOS, tos,
int);
1603 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TYPE, type,
int);
1605 #ifdef ZMQ_UNSUBSCRIBE 1606 ZMQ_DEFINE_ARRAY_OPT(ZMQ_UNSUBSCRIBE, unsubscribe);
1608 #ifdef ZMQ_VMCI_BUFFER_SIZE 1609 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_VMCI_BUFFER_SIZE, vmci_buffer_size, uint64_t);
1611 #ifdef ZMQ_VMCI_BUFFER_MIN_SIZE 1612 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_VMCI_BUFFER_MIN_SIZE, vmci_buffer_min_size, uint64_t);
1614 #ifdef ZMQ_VMCI_BUFFER_MAX_SIZE 1615 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_VMCI_BUFFER_MAX_SIZE, vmci_buffer_max_size, uint64_t);
1617 #ifdef ZMQ_VMCI_CONNECT_TIMEOUT 1618 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_VMCI_CONNECT_TIMEOUT, vmci_connect_timeout,
int);
1620 #ifdef ZMQ_XPUB_VERBOSE 1621 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_XPUB_VERBOSE, xpub_verbose,
int);
1623 #ifdef ZMQ_XPUB_VERBOSER 1624 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_XPUB_VERBOSER, xpub_verboser,
int);
1626 #ifdef ZMQ_XPUB_MANUAL 1627 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_XPUB_MANUAL, xpub_manual,
int);
1629 #ifdef ZMQ_XPUB_NODROP 1630 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_XPUB_NODROP, xpub_nodrop,
int);
1632 #ifdef ZMQ_XPUB_WELCOME_MSG 1633 ZMQ_DEFINE_ARRAY_OPT(ZMQ_XPUB_WELCOME_MSG, xpub_welcome_msg);
1635 #ifdef ZMQ_ZAP_ENFORCE_DOMAIN 1636 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_ZAP_ENFORCE_DOMAIN, zap_enforce_domain,
int);
1638 #ifdef ZMQ_ZAP_DOMAIN 1639 ZMQ_DEFINE_ARRAY_OPT(ZMQ_ZAP_DOMAIN, zap_domain);
1654 template<
typename T>
1656 void setsockopt(
int option_, T const &optval)
1658 setsockopt(option_, &optval,
sizeof(T));
1662 void setsockopt(
int option_, const
void *optval_,
size_t optvallen_)
1664 int rc = zmq_setsockopt(_handle, option_, optval_, optvallen_);
1670 void getsockopt(
int option_,
void *optval_,
size_t *optvallen_)
const 1672 int rc = zmq_getsockopt(_handle, option_, optval_, optvallen_);
1677 template<
typename T>
1679 T getsockopt(
int option_)
const 1682 size_t optlen =
sizeof(T);
1683 getsockopt(option_, &optval, &optlen);
1690 template<
int Opt,
class T,
bool BoolUnit>
1691 void set(sockopt::integral_option<Opt, T, BoolUnit>,
const T &val)
1693 static_assert(std::is_integral<T>::value,
"T must be integral");
1694 set_option(Opt, &val,
sizeof val);
1699 template<
int Opt,
class T>
1700 void set(sockopt::integral_option<Opt, T, true>,
bool val)
1702 static_assert(std::is_integral<T>::value,
"T must be integral");
1704 set_option(Opt, &rep_val,
sizeof rep_val);
1709 template<
int Opt,
int NullTerm>
1710 void set(sockopt::array_option<Opt, NullTerm>,
const char *buf)
1712 set_option(Opt, buf, std::strlen(buf));
1717 template<
int Opt,
int NullTerm>
1718 void set(sockopt::array_option<Opt, NullTerm>, const_buffer buf)
1720 set_option(Opt, buf.data(), buf.size());
1725 template<
int Opt,
int NullTerm>
1726 void set(sockopt::array_option<Opt, NullTerm>,
const std::string &buf)
1728 set_option(Opt, buf.data(), buf.size());
1731 #if CPPZMQ_HAS_STRING_VIEW 1734 template<
int Opt,
int NullTerm>
1735 void set(sockopt::array_option<Opt, NullTerm>, std::string_view buf)
1737 set_option(Opt, buf.data(), buf.size());
1743 template<
int Opt,
class T,
bool BoolUnit>
1744 ZMQ_NODISCARD T
get(sockopt::integral_option<Opt, T, BoolUnit>)
const 1746 static_assert(std::is_integral<T>::value,
"T must be integral");
1748 size_t size =
sizeof val;
1749 get_option(Opt, &val, &size);
1750 assert(size ==
sizeof val);
1756 template<
int Opt,
int NullTerm>
1757 ZMQ_NODISCARD size_t get(sockopt::array_option<Opt, NullTerm>,
1758 mutable_buffer buf)
const 1760 size_t size = buf.size();
1761 get_option(Opt, buf.data(), &size);
1769 template<
int Opt,
int NullTerm>
1770 ZMQ_NODISCARD std::string
get(sockopt::array_option<Opt, NullTerm>,
1771 size_t init_size = 1024)
const 1773 if (NullTerm == 2 && init_size == 1024) {
1776 std::string str(init_size,
'\0');
1777 size_t size =
get(sockopt::array_option<Opt>{},
buffer(str));
1778 if (NullTerm == 1) {
1780 assert(str[size - 1] ==
'\0');
1783 }
else if (NullTerm == 2) {
1784 assert(size == 32 || size == 41);
1786 assert(str[size - 1] ==
'\0');
1795 void bind(std::string
const &addr) {
bind(addr.c_str()); }
1799 int rc = zmq_bind(_handle, addr_);
1808 int rc = zmq_unbind(_handle, addr_);
1817 int rc = zmq_connect(_handle, addr_);
1826 int rc = zmq_disconnect(_handle, addr_);
1834 size_t send(const
void *buf_,
size_t len_,
int flags_ = 0)
1836 int nbytes = zmq_send(_handle, buf_, len_, flags_);
1838 return static_cast<size_t>(nbytes);
1839 if (zmq_errno() == EAGAIN)
1848 int nbytes = zmq_msg_send(msg_.handle(), _handle, flags_);
1851 if (zmq_errno() == EAGAIN)
1856 template<
typename T>
1858 "from 4.4.1, use send taking message_t or buffer (for contiguous " 1859 "ranges), and send_flags")
1860 bool send(T first, T last,
int flags_ = 0)
1863 int nbytes = zmq_msg_send(msg.
handle(), _handle, flags_);
1866 if (zmq_errno() == EAGAIN)
1871 #ifdef ZMQ_HAS_RVALUE_REFS 1877 return send(msg_, static_cast<send_flags>(flags_)).has_value();
1879 return send(msg_, flags_);
1885 send_result_t send(const_buffer buf, send_flags flags = send_flags::none)
1888 zmq_send(_handle, buf.data(), buf.size(),
static_cast<int>(flags));
1890 return static_cast<size_t>(nbytes);
1891 if (zmq_errno() == EAGAIN)
1896 send_result_t send(
message_t &msg, send_flags flags)
1898 int nbytes = zmq_msg_send(msg.
handle(), _handle,
static_cast<int>(flags));
1900 return static_cast<size_t>(nbytes);
1901 if (zmq_errno() == EAGAIN)
1906 send_result_t send(
message_t &&msg, send_flags flags)
1908 return send(msg, flags);
1913 "from 4.3.1, use recv taking a mutable_buffer and recv_flags")
1914 size_t recv(
void *buf_,
size_t len_,
int flags_ = 0)
1916 int nbytes = zmq_recv(_handle, buf_, len_, flags_);
1918 return static_cast<size_t>(nbytes);
1919 if (zmq_errno() == EAGAIN)
1925 "from 4.3.1, use recv taking a reference to message_t and recv_flags")
1926 bool recv(
message_t *msg_,
int flags_ = 0)
1928 int nbytes = zmq_msg_recv(msg_->
handle(), _handle, flags_);
1931 if (zmq_errno() == EAGAIN)
1938 recv_buffer_result_t recv(mutable_buffer buf,
1939 recv_flags flags = recv_flags::none)
1942 zmq_recv(_handle, buf.data(), buf.size(),
static_cast<int>(flags));
1944 return recv_buffer_size{
1945 (std::min)(static_cast<size_t>(nbytes), buf.size()),
1946 static_cast<size_t>(nbytes)};
1948 if (zmq_errno() == EAGAIN)
1954 recv_result_t recv(
message_t &msg, recv_flags flags = recv_flags::none)
1957 zmq_msg_recv(msg.
handle(), _handle,
static_cast<int>(flags));
1959 assert(msg.
size() ==
static_cast<size_t>(nbytes));
1960 return static_cast<size_t>(nbytes);
1962 if (zmq_errno() == EAGAIN)
1968 #if defined(ZMQ_BUILD_DRAFT_API) && ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 0) 1969 void join(
const char *group)
1971 int rc = zmq_join(_handle, group);
1976 void leave(
const char *group)
1978 int rc = zmq_leave(_handle, group);
1996 void set_option(
int option_,
const void *optval_,
size_t optvallen_)
1998 int rc = zmq_setsockopt(_handle, option_, optval_, optvallen_);
2003 void get_option(
int option_,
void *optval_,
size_t *optvallen_)
const 2005 int rc = zmq_getsockopt(_handle, option_, optval_, optvallen_);
2013 enum class socket_type : int
2017 dealer = ZMQ_DEALER,
2018 router = ZMQ_ROUTER,
2025 #if defined(ZMQ_BUILD_DRAFT_API) && ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 0) 2026 server = ZMQ_SERVER,
2027 client = ZMQ_CLIENT,
2031 #if ZMQ_VERSION_MAJOR >= 4 2032 stream = ZMQ_STREAM,
2067 return sr.handle() ==
nullptr;
2071 return sr.handle() ==
nullptr;
2075 return !(sr ==
nullptr);
2079 return !(sr ==
nullptr);
2085 return std::equal_to<void *>()(a.handle(), b.handle());
2093 return std::less<void *>()(a.handle(), b.handle());
2117 return hash<void *>()(sr.handle());
2133 detail::socket_base(zmq_socket(context_.handle(), type_)),
2134 ctxptr(context_.handle())
2142 socket_t(context_, static_cast<int>(type_))
2147 #ifdef ZMQ_HAS_RVALUE_REFS 2174 int rc = zmq_close(_handle);
2196 detail::socket_base(zmq_socket(context_, type_)),
2211 ZMQ_DEPRECATED(
"from 4.3.1, use proxy taking socket_t objects")
2212 inline void proxy(
void *frontend,
void *backend,
void *capture)
2214 int rc = zmq_proxy(frontend, backend, capture);
2222 int rc = zmq_proxy(frontend.handle(), backend.handle(), capture.handle());
2227 #ifdef ZMQ_HAS_PROXY_STEERABLE 2228 ZMQ_DEPRECATED(
"from 4.3.1, use proxy_steerable taking socket_t objects")
2230 proxy_steerable(
void *frontend,
void *backend,
void *capture,
void *control)
2232 int rc = zmq_proxy_steerable(frontend, backend, capture, control);
2242 int rc = zmq_proxy_steerable(frontend.handle(), backend.handle(),
2243 capture.handle(), control.handle());
2256 #ifdef ZMQ_HAS_RVALUE_REFS 2260 std::swap(_monitor_socket, rhs._monitor_socket);
2268 std::swap(_monitor_socket, rhs._monitor_socket);
2277 monitor(socket, addr.c_str(), events);
2282 init(socket, addr_, events);
2288 void init(
socket_t &socket, std::string
const &addr,
int events = ZMQ_EVENT_ALL)
2290 init(socket, addr.c_str(), events);
2295 int rc = zmq_socket_monitor(socket.handle(), addr_, events);
2301 _monitor_socket.connect(addr_);
2303 on_monitor_started();
2308 assert(_monitor_socket);
2311 zmq_msg_init(&eventMsg);
2314 {_monitor_socket.handle(), 0, ZMQ_POLLIN, 0},
2319 if (items[0].revents & ZMQ_POLLIN) {
2320 int rc = zmq_msg_recv(&eventMsg, _monitor_socket.handle(), 0);
2321 if (rc == -1 && zmq_errno() == ETERM)
2326 zmq_msg_close(&eventMsg);
2330 #if ZMQ_VERSION_MAJOR >= 4 2331 const char *
data =
static_cast<const char *
>(zmq_msg_data(&eventMsg));
2333 memcpy(&msgEvent.
event, data,
sizeof(uint16_t));
2334 data +=
sizeof(uint16_t);
2335 memcpy(&msgEvent.
value, data,
sizeof(int32_t));
2341 #ifdef ZMQ_NEW_MONITOR_EVENT_LAYOUT 2343 zmq_msg_init(&addrMsg);
2344 int rc = zmq_msg_recv(&addrMsg, _monitor_socket.handle(), 0);
2345 if (rc == -1 && zmq_errno() == ETERM) {
2346 zmq_msg_close(&eventMsg);
2351 const char *str =
static_cast<const char *
>(zmq_msg_data(&addrMsg));
2352 std::string address(str, str + zmq_msg_size(&addrMsg));
2353 zmq_msg_close(&addrMsg);
2356 std::string address =
event->data.connected.addr;
2359 #ifdef ZMQ_EVENT_MONITOR_STOPPED 2360 if (event->event == ZMQ_EVENT_MONITOR_STOPPED) {
2361 zmq_msg_close(&eventMsg);
2367 switch (event->event) {
2368 case ZMQ_EVENT_CONNECTED:
2369 on_event_connected(*event, address.c_str());
2371 case ZMQ_EVENT_CONNECT_DELAYED:
2372 on_event_connect_delayed(*event, address.c_str());
2374 case ZMQ_EVENT_CONNECT_RETRIED:
2375 on_event_connect_retried(*event, address.c_str());
2377 case ZMQ_EVENT_LISTENING:
2378 on_event_listening(*event, address.c_str());
2380 case ZMQ_EVENT_BIND_FAILED:
2381 on_event_bind_failed(*event, address.c_str());
2383 case ZMQ_EVENT_ACCEPTED:
2384 on_event_accepted(*event, address.c_str());
2386 case ZMQ_EVENT_ACCEPT_FAILED:
2387 on_event_accept_failed(*event, address.c_str());
2389 case ZMQ_EVENT_CLOSED:
2390 on_event_closed(*event, address.c_str());
2392 case ZMQ_EVENT_CLOSE_FAILED:
2393 on_event_close_failed(*event, address.c_str());
2395 case ZMQ_EVENT_DISCONNECTED:
2396 on_event_disconnected(*event, address.c_str());
2398 #ifdef ZMQ_BUILD_DRAFT_API 2399 #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 3) 2400 case ZMQ_EVENT_HANDSHAKE_FAILED_NO_DETAIL:
2401 on_event_handshake_failed_no_detail(*event, address.c_str());
2403 case ZMQ_EVENT_HANDSHAKE_FAILED_PROTOCOL:
2404 on_event_handshake_failed_protocol(*event, address.c_str());
2406 case ZMQ_EVENT_HANDSHAKE_FAILED_AUTH:
2407 on_event_handshake_failed_auth(*event, address.c_str());
2409 case ZMQ_EVENT_HANDSHAKE_SUCCEEDED:
2410 on_event_handshake_succeeded(*event, address.c_str());
2412 #elif ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 1) 2413 case ZMQ_EVENT_HANDSHAKE_FAILED:
2414 on_event_handshake_failed(*event, address.c_str());
2416 case ZMQ_EVENT_HANDSHAKE_SUCCEED:
2417 on_event_handshake_succeed(*event, address.c_str());
2422 on_event_unknown(*event, address.c_str());
2425 zmq_msg_close(&eventMsg);
2430 #ifdef ZMQ_EVENT_MONITOR_STOPPED 2434 zmq_socket_monitor(_socket.handle(),
ZMQ_NULLPTR, 0);
2492 #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 3) 2517 #elif ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 1) 2518 virtual void on_event_handshake_failed(
const zmq_event_t &event_,
2524 virtual void on_event_handshake_succeed(
const zmq_event_t &event_,
2547 zmq_socket_monitor(_socket.handle(),
ZMQ_NULLPTR, 0);
2548 _monitor_socket.
close();
2552 #if defined(ZMQ_BUILD_DRAFT_API) && defined(ZMQ_CPP11) && defined(ZMQ_HAVE_POLLER) 2555 enum class event_flags : short
2558 pollin = ZMQ_POLLIN,
2559 pollout = ZMQ_POLLOUT,
2560 pollerr = ZMQ_POLLERR,
2561 pollpri = ZMQ_POLLPRI
2564 constexpr event_flags operator|(event_flags a, event_flags b) noexcept
2566 return detail::enum_bit_or(a, b);
2568 constexpr event_flags operator&(event_flags a, event_flags b) noexcept
2570 return detail::enum_bit_and(a, b);
2572 constexpr event_flags operator^(event_flags a, event_flags b) noexcept
2574 return detail::enum_bit_xor(a, b);
2576 constexpr event_flags operator~(event_flags a) noexcept
2578 return detail::enum_bit_not(a);
2581 struct no_user_data;
2584 template<
class T = no_user_data>
struct poller_event
2596 template<
typename T = no_user_data>
class poller_t
2599 using event_type = poller_event<T>;
2601 poller_t() : poller_ptr(zmq_poller_new())
2608 typename Dummy = void,
2610 typename std::enable_if<!std::is_same<T, no_user_data>::value, Dummy>::type>
2613 add_impl(socket, events, user_data);
2618 add_impl(socket, events,
nullptr);
2623 if (0 != zmq_poller_remove(poller_ptr.get(), socket.handle())) {
2631 != zmq_poller_modify(poller_ptr.get(), socket.handle(),
2632 static_cast<short>(events))) {
2637 size_t wait_all(std::vector<event_type> &poller_events,
2638 const std::chrono::milliseconds timeout)
2640 int rc = zmq_poller_wait_all(
2642 reinterpret_cast<zmq_poller_event_t *
>(poller_events.data()),
2643 static_cast<int>(poller_events.size()),
2644 static_cast<long>(timeout.count()));
2646 return static_cast<size_t>(rc);
2648 #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 3) 2649 if (zmq_errno() == EAGAIN)
2651 if (zmq_errno() == ETIMEDOUT)
2659 struct destroy_poller_t
2661 void operator()(
void *ptr) noexcept
2663 int rc = zmq_poller_destroy(&ptr);
2668 std::unique_ptr<void, destroy_poller_t> poller_ptr;
2670 void add_impl(
zmq::socket_ref socket, event_flags events, T *user_data)
2673 != zmq_poller_add(poller_ptr.get(), socket.handle(), user_data,
2674 static_cast<short>(events))) {
2679 #endif // defined(ZMQ_BUILD_DRAFT_API) && defined(ZMQ_CPP11) && defined(ZMQ_HAVE_POLLER) 2683 return os << msg.
str();
2688 #endif // __ZMQ_HPP_INCLUDED__ void monitor(socket_t &socket, const char *addr_, int events=ZMQ_EVENT_ALL)
void swap(context_t &other) ZMQ_NOTHROW
const T * data(const std::vector< T, Alloc > &v)
context_t(int io_threads_, int max_sockets_=ZMQ_MAX_SOCKETS_DFLT)
ZMQ_CONSTEXPR_FN ZMQ_EXPLICIT from_handle_t(_private) ZMQ_NOTHROW
socket_t(void *context_, int type_)
virtual const char * what() const ZMQ_NOTHROW ZMQ_OVERRIDE
ZMQ_NODISCARD const zmq_msg_t * handle() const ZMQ_NOTHROW
bool operator<(const detail::socket_base &a, const detail::socket_base &b) ZMQ_NOTHROW
void monitor(socket_t &socket, std::string const &addr, int events=ZMQ_EVENT_ALL)
virtual void on_event_handshake_failed_protocol(const zmq_event_t &event_, const char *addr_)
zmq_pollitem_t pollitem_t
virtual void on_event_connect_delayed(const zmq_event_t &event_, const char *addr_)
static raw_event_t * buffer
bool connected() const ZMQ_NOTHROW
std::ostream & operator<<(std::ostream &os, const message_t &msg)
const char * gets(const char *property_)
void swap(socket_t &other) ZMQ_NOTHROW
void bind(const char *addr_)
void get_option(int option_, void *optval_, size_t *optvallen_) const
virtual void on_event_closed(const zmq_event_t &event_, const char *addr_)
virtual void on_event_disconnected(const zmq_event_t &event_, const char *addr_)
virtual void on_event_accepted(const zmq_event_t &event_, const char *addr_)
void rebuild(const void *data_, size_t size_)
message_t(const void *data_, size_t size_)
void swap(linb::any &lhs, linb::any &rhs) noexcept
ZMQ_NODISCARD void * handle() ZMQ_NOTHROW
ZMQ_NODISCARD bool empty() const ZMQ_NOTHROW
basic_string_view< CharT, Traits > to_string_view(std::basic_string< CharT, Traits, Allocator > const &s)
void unbind(std::string const &addr)
void disconnect(const char *addr_)
bool operator>=(const detail::socket_base &a, const detail::socket_base &b) ZMQ_NOTHROW
virtual void on_event_handshake_succeeded(const zmq_event_t &event_, const char *addr_)
ZMQ_NODISCARD zmq_msg_t * handle() ZMQ_NOTHROW
T const * data() const ZMQ_NOTHROW
ZMQ_EXPLICIT socket_base(void *handle) ZMQ_NOTHROW
void proxy_steerable(void *frontend, void *backend, void *capture, void *control)
void * data() ZMQ_NOTHROW
int poll(zmq_pollitem_t *items_, size_t nitems_, long timeout_=-1)
void proxy(void *frontend, void *backend, void *capture)
virtual void on_event_handshake_failed_auth(const zmq_event_t &event_, const char *addr_)
void move(message_t &msg_)
const void * data() const ZMQ_NOTHROW
void set_option(int option_, const void *optval_, size_t optvallen_)
void copy(message_t &msg_)
#define ZMQ_ASSERT(expression)
void init(socket_t &socket, const char *addr_, int events=ZMQ_EVENT_ALL)
bool operator>(const detail::socket_base &a, const detail::socket_base &b) ZMQ_NOTHROW
#define ZMQ_DELETED_FUNCTION
bool check_event(int timeout=0)
message_t(ForwardIter first, ForwardIter last)
virtual void on_event_close_failed(const zmq_event_t &event_, const char *addr_)
virtual void on_event_handshake_failed_no_detail(const zmq_event_t &event_, const char *addr_)
int num() const ZMQ_NOTHROW
message_t(void *data_, size_t size_, free_fn *ffn_, void *hint_=ZMQ_NULLPTR)
socket_base() ZMQ_NOTHROW
#define ZMQ_CPP11_DEPRECATED(msg)
virtual void on_event_unknown(const zmq_event_t &event_, const char *addr_)
socket_t(context_t &context_, int type_)
void rebuild(size_t size_)
bool operator<=(const detail::socket_base &a, const detail::socket_base &b) ZMQ_NOTHROW
typename std::enable_if< Predicate::value >::type * enable_if
bool operator==(const message_t &other) const ZMQ_NOTHROW
virtual void on_event_listening(const zmq_event_t &event_, const char *addr_)
void version(int *major_, int *minor_, int *patch_)
virtual void on_event_connected(const zmq_event_t &event_, const char *addr_)
void connect(std::string const &addr)
bool operator==(const detail::socket_base &a, const detail::socket_base &b) ZMQ_NOTHROW
socket_ref(from_handle_t, void *handle) ZMQ_NOTHROW
bool operator!=(const message_t &other) const ZMQ_NOTHROW
void connect(const char *addr_)
void swap(message_t &a, message_t &b) ZMQ_NOTHROW
void shutdown() ZMQ_NOTHROW
size_t size() const ZMQ_NOTHROW
void bind(std::string const &addr)
void rebuild(void *data_, size_t size_, free_fn *ffn_, void *hint_=ZMQ_NULLPTR)
bool operator!=(const detail::socket_base &a, const detail::socket_base &b) ZMQ_NOTHROW
virtual void on_monitor_started()
virtual void on_event_bind_failed(const zmq_event_t &event_, const char *addr_)
#define ZMQ_CONSTEXPR_VAR
void init(socket_t &socket, std::string const &addr, int events=ZMQ_EVENT_ALL)
virtual void on_event_accept_failed(const zmq_event_t &event_, const char *addr_)
void disconnect(std::string const &addr)
void swap(message_t &other) ZMQ_NOTHROW
void unbind(const char *addr_)
std::string to_string() const
virtual void on_event_connect_retried(const zmq_event_t &event_, const char *addr_)
error_t(int err) ZMQ_NOTHROW
ZMQ_CONSTEXPR_VAR from_handle_t from_handle
bool more() const ZMQ_NOTHROW