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, \ 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));
246 using iter_value_t =
typename std::iterator_traits<Iter>::value_type;
248 template<
class Range>
249 using range_iter_t = decltype(
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 &>()))>>
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 =
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) 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,
447 typename =
typename std::enable_if<
449 && ZMQ_IS_TRIVIALLY_COPYABLE(detail::range_value_t<Range>)
450 && !detail::is_char_type<detail::range_value_t<Range>>::value
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")
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 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];
713 void operator=(const
message_t &) ZMQ_DELETED_FUNCTION;
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")
877 void operator=(const
context_t &) ZMQ_DELETED_FUNCTION;
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
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
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
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
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
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
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 :
1099 _data(mb.data()), _size(mb.size())
1103 constexpr
const void *
data()
const noexcept {
return _data; }
1104 constexpr
size_t size()
const noexcept {
return _size; }
1105 const_buffer &operator+=(
size_t n) noexcept
1107 const auto shift = (
std::min)(n, _size);
1108 _data =
static_cast<const char *
>(_data) + shift;
1118 inline const_buffer
operator+(
const const_buffer &cb,
size_t n) noexcept
1120 return const_buffer(static_cast<const char *>(cb.data())
1122 cb.size() - (
std::min)(n, cb.size()));
1124 inline const_buffer
operator+(
size_t n,
const const_buffer &cb) noexcept
1131 constexpr mutable_buffer buffer(
void *p,
size_t n) noexcept
1133 return mutable_buffer(p, n);
1135 constexpr const_buffer buffer(
const void *p,
size_t n) noexcept
1137 return const_buffer(p, n);
1139 constexpr mutable_buffer buffer(
const mutable_buffer &mb) noexcept
1143 inline mutable_buffer buffer(
const mutable_buffer &mb,
size_t n) noexcept
1145 return mutable_buffer(mb.data(), (
std::min)(mb.size(), n));
1147 constexpr const_buffer buffer(
const const_buffer &cb) noexcept
1151 inline const_buffer buffer(
const const_buffer &cb,
size_t n) noexcept
1153 return const_buffer(cb.data(), (
std::min)(cb.size(), n));
1158 template<
class T>
struct is_buffer
1160 static constexpr
bool value =
1164 template<
class T>
struct is_pod_like
1170 static constexpr
bool value =
1174 template<
class C> constexpr
auto seq_size(
const C &
c) noexcept -> decltype(c.size())
1178 template<
class T,
size_t N>
1179 constexpr
size_t seq_size(
const T (&)[N]) noexcept
1185 auto buffer_contiguous_sequence(Seq &&seq) noexcept
1186 -> decltype(buffer(std::addressof(*std::begin(seq)),
size_t{}))
1188 using T =
typename std::remove_cv<
1192 const auto size = seq_size(seq);
1193 return buffer(size != 0u ? std::addressof(*std::begin(seq)) :
nullptr,
1197 auto buffer_contiguous_sequence(Seq &&seq,
size_t n_bytes) noexcept
1198 -> decltype(buffer_contiguous_sequence(seq))
1200 using T =
typename std::remove_cv<
1204 const auto size = seq_size(seq);
1205 return buffer(size != 0u ? std::addressof(*std::begin(seq)) :
nullptr,
1206 (
std::min)(size *
sizeof(T), n_bytes));
1212 template<
class T,
size_t N> mutable_buffer buffer(T (&
data)[N]) noexcept
1214 return detail::buffer_contiguous_sequence(
data);
1216 template<
class T,
size_t N>
1217 mutable_buffer buffer(T (&
data)[N],
size_t n_bytes) noexcept
1219 return detail::buffer_contiguous_sequence(
data, n_bytes);
1221 template<
class T,
size_t N> const_buffer buffer(
const T (&
data)[N]) noexcept
1223 return detail::buffer_contiguous_sequence(
data);
1225 template<
class T,
size_t N>
1226 const_buffer buffer(
const T (&
data)[N],
size_t n_bytes) noexcept
1228 return detail::buffer_contiguous_sequence(
data, n_bytes);
1231 template<
class T,
size_t N> mutable_buffer buffer(std::array<T, N> &
data) noexcept
1233 return detail::buffer_contiguous_sequence(
data);
1235 template<
class T,
size_t N>
1236 mutable_buffer buffer(std::array<T, N> &
data,
size_t n_bytes) noexcept
1238 return detail::buffer_contiguous_sequence(
data, n_bytes);
1240 template<
class T,
size_t N>
1241 const_buffer buffer(std::array<const T, N> &
data) noexcept
1243 return detail::buffer_contiguous_sequence(
data);
1245 template<
class T,
size_t N>
1246 const_buffer buffer(std::array<const T, N> &
data,
size_t n_bytes) noexcept
1248 return detail::buffer_contiguous_sequence(
data, n_bytes);
1250 template<
class T,
size_t N>
1251 const_buffer buffer(
const std::array<T, N> &
data) noexcept
1253 return detail::buffer_contiguous_sequence(
data);
1255 template<
class T,
size_t N>
1256 const_buffer buffer(
const std::array<T, N> &
data,
size_t n_bytes) noexcept
1258 return detail::buffer_contiguous_sequence(
data, n_bytes);
1261 template<
class T,
class Allocator>
1262 mutable_buffer buffer(std::vector<T, Allocator> &
data) noexcept
1264 return detail::buffer_contiguous_sequence(
data);
1266 template<
class T,
class Allocator>
1267 mutable_buffer buffer(std::vector<T, Allocator> &
data,
size_t n_bytes) noexcept
1269 return detail::buffer_contiguous_sequence(
data, n_bytes);
1271 template<
class T,
class Allocator>
1272 const_buffer buffer(
const std::vector<T, Allocator> &
data) noexcept
1274 return detail::buffer_contiguous_sequence(
data);
1276 template<
class T,
class Allocator>
1277 const_buffer buffer(
const std::vector<T, Allocator> &
data,
size_t n_bytes) noexcept
1279 return detail::buffer_contiguous_sequence(
data, n_bytes);
1282 template<
class T,
class Traits,
class Allocator>
1283 mutable_buffer buffer(std::basic_string<T, Traits, Allocator> &
data) noexcept
1285 return detail::buffer_contiguous_sequence(
data);
1287 template<
class T,
class Traits,
class Allocator>
1288 mutable_buffer buffer(std::basic_string<T, Traits, Allocator> &
data,
1289 size_t n_bytes) noexcept
1291 return detail::buffer_contiguous_sequence(
data, n_bytes);
1293 template<
class T,
class Traits,
class Allocator>
1294 const_buffer buffer(
const std::basic_string<T, Traits, Allocator> &
data) noexcept
1296 return detail::buffer_contiguous_sequence(
data);
1298 template<
class T,
class Traits,
class Allocator>
1299 const_buffer buffer(
const std::basic_string<T, Traits, Allocator> &
data,
1300 size_t n_bytes) noexcept
1302 return detail::buffer_contiguous_sequence(
data, n_bytes);
1305 #if CPPZMQ_HAS_STRING_VIEW 1307 template<
class T,
class Traits>
1308 const_buffer buffer(std::basic_string_view<T, Traits>
data) noexcept
1310 return detail::buffer_contiguous_sequence(
data);
1312 template<
class T,
class Traits>
1313 const_buffer buffer(std::basic_string_view<T, Traits>
data,
size_t n_bytes) noexcept
1315 return detail::buffer_contiguous_sequence(
data, n_bytes);
1322 template<
class Char,
size_t N>
1323 constexpr const_buffer str_buffer(
const Char (&
data)[N]) noexcept
1326 #ifdef ZMQ_EXTENDED_CONSTEXPR 1329 return const_buffer(static_cast<const Char *>(
data), (N - 1) *
sizeof(Char));
1334 constexpr const_buffer
operator"" _zbuf(
const char *str,
size_t len) noexcept
1336 return const_buffer(str, len *
sizeof(
char));
1338 constexpr const_buffer
operator"" _zbuf(
const wchar_t *str,
size_t len) noexcept
1340 return const_buffer(str, len *
sizeof(
wchar_t));
1342 constexpr const_buffer
operator"" _zbuf(
const char16_t *str,
size_t len) noexcept
1344 return const_buffer(str, len *
sizeof(char16_t));
1346 constexpr const_buffer
operator"" _zbuf(
const char32_t *str,
size_t len) noexcept
1348 return const_buffer(str, len *
sizeof(char32_t));
1363 template<
int Opt,
class T,
bool BoolUnit = false>
struct integral_option
1371 template<
int Opt,
int NullTerm = 1>
struct array_option
1375 #define ZMQ_DEFINE_INTEGRAL_OPT(OPT, NAME, TYPE) \ 1376 using NAME##_t = integral_option<OPT, TYPE, false>; \ 1377 ZMQ_INLINE_VAR ZMQ_CONSTEXPR_VAR NAME##_t NAME {} 1378 #define ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(OPT, NAME, TYPE) \ 1379 using NAME##_t = integral_option<OPT, TYPE, true>; \ 1380 ZMQ_INLINE_VAR ZMQ_CONSTEXPR_VAR NAME##_t NAME {} 1381 #define ZMQ_DEFINE_ARRAY_OPT(OPT, NAME) \ 1382 using NAME##_t = array_option<OPT>; \ 1383 ZMQ_INLINE_VAR ZMQ_CONSTEXPR_VAR NAME##_t NAME {} 1384 #define ZMQ_DEFINE_ARRAY_OPT_BINARY(OPT, NAME) \ 1385 using NAME##_t = array_option<OPT, 0>; \ 1386 ZMQ_INLINE_VAR ZMQ_CONSTEXPR_VAR NAME##_t NAME {} 1387 #define ZMQ_DEFINE_ARRAY_OPT_BIN_OR_Z85(OPT, NAME) \ 1388 using NAME##_t = array_option<OPT, 2>; \ 1389 ZMQ_INLINE_VAR ZMQ_CONSTEXPR_VAR NAME##_t NAME {} 1394 typedef unsigned __int64 cppzmq_fd_t;
1396 typedef unsigned int cppzmq_fd_t;
1399 typedef int cppzmq_fd_t;
1403 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_AFFINITY, affinity, uint64_t);
1406 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_BACKLOG, backlog,
int);
1408 #ifdef ZMQ_BINDTODEVICE 1409 ZMQ_DEFINE_ARRAY_OPT_BINARY(ZMQ_BINDTODEVICE, bindtodevice);
1412 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_CONFLATE, conflate,
int);
1414 #ifdef ZMQ_CONNECT_ROUTING_ID 1415 ZMQ_DEFINE_ARRAY_OPT(ZMQ_CONNECT_ROUTING_ID, connect_routing_id);
1417 #ifdef ZMQ_CONNECT_TIMEOUT 1418 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_CONNECT_TIMEOUT, connect_timeout,
int);
1420 #ifdef ZMQ_CURVE_PUBLICKEY 1421 ZMQ_DEFINE_ARRAY_OPT_BIN_OR_Z85(ZMQ_CURVE_PUBLICKEY, curve_publickey);
1423 #ifdef ZMQ_CURVE_SECRETKEY 1424 ZMQ_DEFINE_ARRAY_OPT_BIN_OR_Z85(ZMQ_CURVE_SECRETKEY, curve_secretkey);
1426 #ifdef ZMQ_CURVE_SERVER 1427 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_CURVE_SERVER, curve_server,
int);
1429 #ifdef ZMQ_CURVE_SERVERKEY 1430 ZMQ_DEFINE_ARRAY_OPT_BIN_OR_Z85(ZMQ_CURVE_SERVERKEY, curve_serverkey);
1433 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_EVENTS, events,
int);
1436 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_FD, fd, cppzmq_fd_t);
1438 #ifdef ZMQ_GSSAPI_PLAINTEXT 1439 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_GSSAPI_PLAINTEXT, gssapi_plaintext,
int);
1441 #ifdef ZMQ_GSSAPI_SERVER 1442 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_GSSAPI_SERVER, gssapi_server,
int);
1444 #ifdef ZMQ_GSSAPI_SERVICE_PRINCIPAL 1445 ZMQ_DEFINE_ARRAY_OPT(ZMQ_GSSAPI_SERVICE_PRINCIPAL, gssapi_service_principal);
1447 #ifdef ZMQ_GSSAPI_SERVICE_PRINCIPAL_NAMETYPE 1448 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_GSSAPI_SERVICE_PRINCIPAL_NAMETYPE,
1449 gssapi_service_principal_nametype,
1452 #ifdef ZMQ_GSSAPI_PRINCIPAL 1453 ZMQ_DEFINE_ARRAY_OPT(ZMQ_GSSAPI_PRINCIPAL, gssapi_principal);
1455 #ifdef ZMQ_GSSAPI_PRINCIPAL_NAMETYPE 1456 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_GSSAPI_PRINCIPAL_NAMETYPE,
1457 gssapi_principal_nametype,
1460 #ifdef ZMQ_HANDSHAKE_IVL 1461 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_HANDSHAKE_IVL, handshake_ivl,
int);
1463 #ifdef ZMQ_HEARTBEAT_IVL 1464 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_HEARTBEAT_IVL, heartbeat_ivl,
int);
1466 #ifdef ZMQ_HEARTBEAT_TIMEOUT 1467 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_HEARTBEAT_TIMEOUT, heartbeat_timeout,
int);
1469 #ifdef ZMQ_HEARTBEAT_TTL 1470 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_HEARTBEAT_TTL, heartbeat_ttl,
int);
1472 #ifdef ZMQ_IMMEDIATE 1473 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_IMMEDIATE, immediate,
int);
1475 #ifdef ZMQ_INVERT_MATCHING 1476 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_INVERT_MATCHING, invert_matching,
int);
1479 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_IPV6, ipv6,
int);
1481 #ifdef ZMQ_LAST_ENDPOINT 1482 ZMQ_DEFINE_ARRAY_OPT(ZMQ_LAST_ENDPOINT, last_endpoint);
1485 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_LINGER, linger,
int);
1487 #ifdef ZMQ_MAXMSGSIZE 1488 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_MAXMSGSIZE, maxmsgsize, int64_t);
1490 #ifdef ZMQ_MECHANISM 1491 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_MECHANISM, mechanism,
int);
1494 ZMQ_DEFINE_ARRAY_OPT(ZMQ_METADATA, metadata);
1496 #ifdef ZMQ_MULTICAST_HOPS 1497 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_MULTICAST_HOPS, multicast_hops,
int);
1499 #ifdef ZMQ_MULTICAST_LOOP 1500 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_MULTICAST_LOOP, multicast_loop,
int);
1502 #ifdef ZMQ_MULTICAST_MAXTPDU 1503 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_MULTICAST_MAXTPDU, multicast_maxtpdu,
int);
1505 #ifdef ZMQ_PLAIN_SERVER 1506 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_PLAIN_SERVER, plain_server,
int);
1508 #ifdef ZMQ_PLAIN_PASSWORD 1509 ZMQ_DEFINE_ARRAY_OPT(ZMQ_PLAIN_PASSWORD, plain_password);
1511 #ifdef ZMQ_PLAIN_USERNAME 1512 ZMQ_DEFINE_ARRAY_OPT(ZMQ_PLAIN_USERNAME, plain_username);
1515 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_USE_FD, use_fd,
int);
1517 #ifdef ZMQ_PROBE_ROUTER 1518 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_PROBE_ROUTER, probe_router,
int);
1521 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_RATE, rate,
int);
1524 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_RCVBUF, rcvbuf,
int);
1527 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_RCVHWM, rcvhwm,
int);
1530 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_RCVMORE, rcvmore,
int);
1533 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_RCVTIMEO, rcvtimeo,
int);
1535 #ifdef ZMQ_RECONNECT_IVL 1536 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_RECONNECT_IVL, reconnect_ivl,
int);
1538 #ifdef ZMQ_RECONNECT_IVL_MAX 1539 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_RECONNECT_IVL_MAX, reconnect_ivl_max,
int);
1541 #ifdef ZMQ_RECOVERY_IVL 1542 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_RECOVERY_IVL, recovery_ivl,
int);
1544 #ifdef ZMQ_REQ_CORRELATE 1545 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_REQ_CORRELATE, req_correlate,
int);
1547 #ifdef ZMQ_REQ_RELAXED 1548 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_REQ_RELAXED, req_relaxed,
int);
1550 #ifdef ZMQ_ROUTER_HANDOVER 1551 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_ROUTER_HANDOVER, router_handover,
int);
1553 #ifdef ZMQ_ROUTER_MANDATORY 1554 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_ROUTER_MANDATORY, router_mandatory,
int);
1556 #ifdef ZMQ_ROUTER_NOTIFY 1557 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_ROUTER_NOTIFY, router_notify,
int);
1559 #ifdef ZMQ_ROUTING_ID 1560 ZMQ_DEFINE_ARRAY_OPT_BINARY(ZMQ_ROUTING_ID, routing_id);
1563 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_SNDBUF, sndbuf,
int);
1566 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_SNDHWM, sndhwm,
int);
1569 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_SNDTIMEO, sndtimeo,
int);
1571 #ifdef ZMQ_SOCKS_PROXY 1572 ZMQ_DEFINE_ARRAY_OPT(ZMQ_SOCKS_PROXY, socks_proxy);
1574 #ifdef ZMQ_STREAM_NOTIFY 1575 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_STREAM_NOTIFY, stream_notify,
int);
1577 #ifdef ZMQ_SUBSCRIBE 1578 ZMQ_DEFINE_ARRAY_OPT(ZMQ_SUBSCRIBE, subscribe);
1580 #ifdef ZMQ_TCP_KEEPALIVE 1581 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TCP_KEEPALIVE, tcp_keepalive,
int);
1583 #ifdef ZMQ_TCP_KEEPALIVE_CNT 1584 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TCP_KEEPALIVE_CNT, tcp_keepalive_cnt,
int);
1586 #ifdef ZMQ_TCP_KEEPALIVE_IDLE 1587 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TCP_KEEPALIVE_IDLE, tcp_keepalive_idle,
int);
1589 #ifdef ZMQ_TCP_KEEPALIVE_INTVL 1590 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TCP_KEEPALIVE_INTVL, tcp_keepalive_intvl,
int);
1592 #ifdef ZMQ_TCP_MAXRT 1593 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TCP_MAXRT, tcp_maxrt,
int);
1595 #ifdef ZMQ_THREAD_SAFE 1596 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_THREAD_SAFE, thread_safe,
int);
1599 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TOS, tos,
int);
1602 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TYPE, type,
int);
1604 #ifdef ZMQ_UNSUBSCRIBE 1605 ZMQ_DEFINE_ARRAY_OPT(ZMQ_UNSUBSCRIBE, unsubscribe);
1607 #ifdef ZMQ_VMCI_BUFFER_SIZE 1608 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_VMCI_BUFFER_SIZE, vmci_buffer_size, uint64_t);
1610 #ifdef ZMQ_VMCI_BUFFER_MIN_SIZE 1611 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_VMCI_BUFFER_MIN_SIZE, vmci_buffer_min_size, uint64_t);
1613 #ifdef ZMQ_VMCI_BUFFER_MAX_SIZE 1614 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_VMCI_BUFFER_MAX_SIZE, vmci_buffer_max_size, uint64_t);
1616 #ifdef ZMQ_VMCI_CONNECT_TIMEOUT 1617 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_VMCI_CONNECT_TIMEOUT, vmci_connect_timeout,
int);
1619 #ifdef ZMQ_XPUB_VERBOSE 1620 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_XPUB_VERBOSE, xpub_verbose,
int);
1622 #ifdef ZMQ_XPUB_VERBOSER 1623 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_XPUB_VERBOSER, xpub_verboser,
int);
1625 #ifdef ZMQ_XPUB_MANUAL 1626 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_XPUB_MANUAL, xpub_manual,
int);
1628 #ifdef ZMQ_XPUB_NODROP 1629 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_XPUB_NODROP, xpub_nodrop,
int);
1631 #ifdef ZMQ_XPUB_WELCOME_MSG 1632 ZMQ_DEFINE_ARRAY_OPT(ZMQ_XPUB_WELCOME_MSG, xpub_welcome_msg);
1634 #ifdef ZMQ_ZAP_ENFORCE_DOMAIN 1635 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_ZAP_ENFORCE_DOMAIN, zap_enforce_domain,
int);
1637 #ifdef ZMQ_ZAP_DOMAIN 1638 ZMQ_DEFINE_ARRAY_OPT(ZMQ_ZAP_DOMAIN, zap_domain);
1653 template<
typename T>
1655 void setsockopt(
int option_, T const &optval)
1657 setsockopt(option_, &optval,
sizeof(T));
1661 void setsockopt(
int option_, const
void *optval_,
size_t optvallen_)
1663 int rc = zmq_setsockopt(_handle, option_, optval_, optvallen_);
1669 void getsockopt(
int option_,
void *optval_,
size_t *optvallen_)
const 1671 int rc = zmq_getsockopt(_handle, option_, optval_, optvallen_);
1676 template<
typename T>
1678 T getsockopt(
int option_)
const 1681 size_t optlen =
sizeof(T);
1682 getsockopt(option_, &optval, &optlen);
1689 template<
int Opt,
class T,
bool BoolUnit>
1690 void set(sockopt::integral_option<Opt, T, BoolUnit>,
const T &val)
1693 set_option(Opt, &val,
sizeof val);
1698 template<
int Opt,
class T>
1699 void set(sockopt::integral_option<Opt, T, true>,
bool val)
1703 set_option(Opt, &rep_val,
sizeof rep_val);
1708 template<
int Opt,
int NullTerm>
1709 void set(sockopt::array_option<Opt, NullTerm>,
const char *buf)
1711 set_option(Opt, buf, std::strlen(buf));
1716 template<
int Opt,
int NullTerm>
1717 void set(sockopt::array_option<Opt, NullTerm>, const_buffer buf)
1719 set_option(Opt, buf.data(), buf.size());
1724 template<
int Opt,
int NullTerm>
1725 void set(sockopt::array_option<Opt, NullTerm>,
const std::string &buf)
1727 set_option(Opt, buf.data(), buf.size());
1730 #if CPPZMQ_HAS_STRING_VIEW 1733 template<
int Opt,
int NullTerm>
1736 set_option(Opt, buf.data(), buf.size());
1742 template<
int Opt,
class T,
bool BoolUnit>
1743 ZMQ_NODISCARD T
get(sockopt::integral_option<Opt, T, BoolUnit>)
const 1747 size_t size =
sizeof val;
1748 get_option(Opt, &val, &size);
1749 assert(size ==
sizeof val);
1755 template<
int Opt,
int NullTerm>
1756 ZMQ_NODISCARD size_t get(sockopt::array_option<Opt, NullTerm>,
1757 mutable_buffer buf)
const 1759 size_t size = buf.size();
1760 get_option(Opt, buf.data(), &size);
1768 template<
int Opt,
int NullTerm>
1769 ZMQ_NODISCARD std::string
get(sockopt::array_option<Opt, NullTerm>,
1770 size_t init_size = 1024)
const 1772 if (NullTerm == 2 && init_size == 1024) {
1775 std::string str(init_size,
'\0');
1776 size_t size =
get(sockopt::array_option<Opt>{}, buffer(str));
1777 if (NullTerm == 1) {
1779 assert(str[size - 1] ==
'\0');
1782 }
else if (NullTerm == 2) {
1783 assert(size == 32 || size == 41);
1785 assert(str[size - 1] ==
'\0');
1794 void bind(std::string
const &addr) {
bind(addr.c_str()); }
1798 int rc = zmq_bind(_handle, addr_);
1807 int rc = zmq_unbind(_handle, addr_);
1816 int rc = zmq_connect(_handle, addr_);
1825 int rc = zmq_disconnect(_handle, addr_);
1833 size_t send(const
void *buf_,
size_t len_,
int flags_ = 0)
1835 int nbytes = zmq_send(_handle, buf_, len_, flags_);
1837 return static_cast<size_t>(nbytes);
1838 if (zmq_errno() == EAGAIN)
1847 int nbytes = zmq_msg_send(msg_.handle(), _handle, flags_);
1850 if (zmq_errno() == EAGAIN)
1855 template<
typename T>
1857 "from 4.4.1, use send taking message_t or buffer (for contiguous " 1858 "ranges), and send_flags")
1859 bool send(T first, T last,
int flags_ = 0)
1862 int nbytes = zmq_msg_send(msg.
handle(), _handle, flags_);
1865 if (zmq_errno() == EAGAIN)
1870 #ifdef ZMQ_HAS_RVALUE_REFS 1876 return send(msg_, static_cast<send_flags>(flags_)).has_value();
1878 return send(msg_, flags_);
1887 zmq_send(_handle, buf.data(), buf.size(),
static_cast<int>(flags));
1889 return static_cast<size_t>(nbytes);
1890 if (zmq_errno() == EAGAIN)
1895 send_result_t send(
message_t &msg, send_flags flags)
1897 int nbytes = zmq_msg_send(msg.
handle(), _handle,
static_cast<int>(flags));
1899 return static_cast<size_t>(nbytes);
1900 if (zmq_errno() == EAGAIN)
1905 send_result_t send(
message_t &&msg, send_flags flags)
1907 return send(msg, flags);
1912 "from 4.3.1, use recv taking a mutable_buffer and recv_flags")
1913 size_t recv(
void *buf_,
size_t len_,
int flags_ = 0)
1915 int nbytes = zmq_recv(_handle, buf_, len_, flags_);
1917 return static_cast<size_t>(nbytes);
1918 if (zmq_errno() == EAGAIN)
1924 "from 4.3.1, use recv taking a reference to message_t and recv_flags")
1925 bool recv(
message_t *msg_,
int flags_ = 0)
1927 int nbytes = zmq_msg_recv(msg_->
handle(), _handle, flags_);
1930 if (zmq_errno() == EAGAIN)
1937 recv_buffer_result_t recv(mutable_buffer buf,
1941 zmq_recv(_handle, buf.data(), buf.size(),
static_cast<int>(flags));
1943 return recv_buffer_size{
1944 (
std::min)(static_cast<size_t>(nbytes), buf.size()),
1945 static_cast<size_t>(nbytes)};
1947 if (zmq_errno() == EAGAIN)
1956 zmq_msg_recv(msg.
handle(), _handle,
static_cast<int>(flags));
1958 assert(msg.
size() ==
static_cast<size_t>(nbytes));
1959 return static_cast<size_t>(nbytes);
1961 if (zmq_errno() == EAGAIN)
1967 #if defined(ZMQ_BUILD_DRAFT_API) && ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 0) 1968 void join(
const char *group)
1970 int rc = zmq_join(_handle, group);
1975 void leave(
const char *group)
1977 int rc = zmq_leave(_handle, group);
1995 void set_option(
int option_,
const void *optval_,
size_t optvallen_)
1997 int rc = zmq_setsockopt(_handle, option_, optval_, optvallen_);
2002 void get_option(
int option_,
void *optval_,
size_t *optvallen_)
const 2004 int rc = zmq_getsockopt(_handle, option_, optval_, optvallen_);
2012 enum class socket_type : int
2016 dealer = ZMQ_DEALER,
2017 router = ZMQ_ROUTER,
2024 #if defined(ZMQ_BUILD_DRAFT_API) && ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 0) 2025 server = ZMQ_SERVER,
2030 #if ZMQ_VERSION_MAJOR >= 4 2031 stream = ZMQ_STREAM,
2066 return sr.handle() ==
nullptr;
2070 return sr.handle() ==
nullptr;
2074 return !(sr ==
nullptr);
2078 return !(sr ==
nullptr);
2084 return std::equal_to<void *>()(a.handle(), b.handle());
2092 return std::less<void *>()(a.handle(), b.handle());
2116 return hash<void *>()(sr.handle());
2132 detail::socket_base(zmq_socket(context_.handle(), type_)),
2133 ctxptr(context_.handle())
2141 socket_t(context_, static_cast<int>(type_))
2146 #ifdef ZMQ_HAS_RVALUE_REFS 2173 int rc = zmq_close(_handle);
2191 void operator=(const
socket_t &) ZMQ_DELETED_FUNCTION;
2195 detail::socket_base(zmq_socket(context_, type_)), ctxptr(context_)
2209 ZMQ_DEPRECATED(
"from 4.3.1, use proxy taking socket_t objects")
2210 inline
void proxy(
void *frontend,
void *backend,
void *capture)
2212 int rc = zmq_proxy(frontend, backend, capture);
2220 int rc = zmq_proxy(frontend.handle(), backend.handle(), capture.handle());
2225 #ifdef ZMQ_HAS_PROXY_STEERABLE 2226 ZMQ_DEPRECATED(
"from 4.3.1, use proxy_steerable taking socket_t objects")
2230 int rc = zmq_proxy_steerable(frontend, backend, capture, control);
2240 int rc = zmq_proxy_steerable(frontend.handle(), backend.handle(),
2241 capture.handle(), control.handle());
2254 #ifdef ZMQ_HAS_RVALUE_REFS 2258 std::swap(_monitor_socket, rhs._monitor_socket);
2266 std::swap(_monitor_socket, rhs._monitor_socket);
2275 monitor(socket, addr.c_str(), events);
2280 init(socket, addr_, events);
2288 init(socket, addr.c_str(), events);
2293 int rc = zmq_socket_monitor(socket.handle(), addr_, events);
2299 _monitor_socket.connect(addr_);
2301 on_monitor_started();
2309 zmq_msg_init(&eventMsg);
2312 {_monitor_socket.handle(), 0, ZMQ_POLLIN, 0},
2317 if (items[0].revents & ZMQ_POLLIN) {
2318 int rc = zmq_msg_recv(&eventMsg, _monitor_socket.handle(), 0);
2319 if (rc == -1 && zmq_errno() == ETERM)
2324 zmq_msg_close(&eventMsg);
2328 #if ZMQ_VERSION_MAJOR >= 4 2329 const char *
data =
static_cast<const char *
>(zmq_msg_data(&eventMsg));
2331 memcpy(&msgEvent.
event, data,
sizeof(uint16_t));
2332 data +=
sizeof(uint16_t);
2333 memcpy(&msgEvent.
value, data,
sizeof(int32_t));
2339 #ifdef ZMQ_NEW_MONITOR_EVENT_LAYOUT 2341 zmq_msg_init(&addrMsg);
2342 int rc = zmq_msg_recv(&addrMsg, _monitor_socket.handle(), 0);
2343 if (rc == -1 && zmq_errno() == ETERM) {
2344 zmq_msg_close(&eventMsg);
2349 const char *str =
static_cast<const char *
>(zmq_msg_data(&addrMsg));
2350 std::string address(str, str + zmq_msg_size(&addrMsg));
2351 zmq_msg_close(&addrMsg);
2354 std::string address =
event->data.connected.addr;
2357 #ifdef ZMQ_EVENT_MONITOR_STOPPED 2358 if (event->event == ZMQ_EVENT_MONITOR_STOPPED) {
2359 zmq_msg_close(&eventMsg);
2365 switch (event->event) {
2366 case ZMQ_EVENT_CONNECTED:
2367 on_event_connected(*event, address.c_str());
2369 case ZMQ_EVENT_CONNECT_DELAYED:
2370 on_event_connect_delayed(*event, address.c_str());
2372 case ZMQ_EVENT_CONNECT_RETRIED:
2373 on_event_connect_retried(*event, address.c_str());
2375 case ZMQ_EVENT_LISTENING:
2376 on_event_listening(*event, address.c_str());
2378 case ZMQ_EVENT_BIND_FAILED:
2379 on_event_bind_failed(*event, address.c_str());
2381 case ZMQ_EVENT_ACCEPTED:
2382 on_event_accepted(*event, address.c_str());
2384 case ZMQ_EVENT_ACCEPT_FAILED:
2385 on_event_accept_failed(*event, address.c_str());
2387 case ZMQ_EVENT_CLOSED:
2388 on_event_closed(*event, address.c_str());
2390 case ZMQ_EVENT_CLOSE_FAILED:
2391 on_event_close_failed(*event, address.c_str());
2393 case ZMQ_EVENT_DISCONNECTED:
2394 on_event_disconnected(*event, address.c_str());
2396 #ifdef ZMQ_BUILD_DRAFT_API 2397 #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 3) 2398 case ZMQ_EVENT_HANDSHAKE_FAILED_NO_DETAIL:
2399 on_event_handshake_failed_no_detail(*event, address.c_str());
2401 case ZMQ_EVENT_HANDSHAKE_FAILED_PROTOCOL:
2402 on_event_handshake_failed_protocol(*event, address.c_str());
2404 case ZMQ_EVENT_HANDSHAKE_FAILED_AUTH:
2405 on_event_handshake_failed_auth(*event, address.c_str());
2407 case ZMQ_EVENT_HANDSHAKE_SUCCEEDED:
2408 on_event_handshake_succeeded(*event, address.c_str());
2410 #elif ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 1) 2411 case ZMQ_EVENT_HANDSHAKE_FAILED:
2412 on_event_handshake_failed(*event, address.c_str());
2414 case ZMQ_EVENT_HANDSHAKE_SUCCEED:
2415 on_event_handshake_succeed(*event, address.c_str());
2420 on_event_unknown(*event, address.c_str());
2423 zmq_msg_close(&eventMsg);
2428 #ifdef ZMQ_EVENT_MONITOR_STOPPED 2432 zmq_socket_monitor(_socket.handle(),
ZMQ_NULLPTR, 0);
2490 #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 3) 2515 #elif ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 1) 2516 virtual void on_event_handshake_failed(
const zmq_event_t &event_,
2522 virtual void on_event_handshake_succeed(
const zmq_event_t &event_,
2545 zmq_socket_monitor(_socket.handle(),
ZMQ_NULLPTR, 0);
2546 _monitor_socket.
close();
2550 #if defined(ZMQ_BUILD_DRAFT_API) && defined(ZMQ_CPP11) && defined(ZMQ_HAVE_POLLER) 2553 enum class event_flags : short
2556 pollin = ZMQ_POLLIN,
2557 pollout = ZMQ_POLLOUT,
2558 pollerr = ZMQ_POLLERR,
2559 pollpri = ZMQ_POLLPRI
2562 constexpr event_flags operator|(event_flags a, event_flags b) noexcept
2564 return detail::enum_bit_or(a, b);
2566 constexpr event_flags operator&(event_flags a, event_flags b) noexcept
2568 return detail::enum_bit_and(a, b);
2570 constexpr event_flags operator^(event_flags a, event_flags b) noexcept
2572 return detail::enum_bit_xor(a, b);
2574 constexpr event_flags operator~(event_flags a) noexcept
2576 return detail::enum_bit_not(a);
2579 struct no_user_data;
2582 template<
class T = no_user_data>
struct poller_event
2594 template<
typename T = no_user_data>
class poller_t
2597 using event_type = poller_event<T>;
2599 poller_t() : poller_ptr(zmq_poller_new())
2606 typename Dummy =
void,
2611 add_impl(socket, events, user_data);
2616 add_impl(socket, events,
nullptr);
2621 if (0 != zmq_poller_remove(poller_ptr.get(), socket.handle())) {
2629 != zmq_poller_modify(poller_ptr.get(), socket.handle(),
2630 static_cast<short>(events))) {
2635 size_t wait_all(std::vector<event_type> &poller_events,
2636 const std::chrono::milliseconds timeout)
2638 int rc = zmq_poller_wait_all(
2640 reinterpret_cast<zmq_poller_event_t *
>(poller_events.data()),
2641 static_cast<int>(poller_events.size()),
2642 static_cast<long>(timeout.count()));
2644 return static_cast<size_t>(
rc);
2646 #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 3) 2647 if (zmq_errno() == EAGAIN)
2649 if (zmq_errno() == ETIMEDOUT)
2657 struct destroy_poller_t
2659 void operator()(
void *
ptr) noexcept
2661 int rc = zmq_poller_destroy(&ptr);
2666 std::unique_ptr<void, destroy_poller_t> poller_ptr;
2668 void add_impl(
zmq::socket_ref socket, event_flags events, T *user_data)
2671 != zmq_poller_add(poller_ptr.get(), socket.handle(), user_data,
2672 static_cast<short>(events))) {
2677 #endif // defined(ZMQ_BUILD_DRAFT_API) && defined(ZMQ_CPP11) && defined(ZMQ_HAVE_POLLER) 2681 return os << msg.
str();
2686 #endif // __ZMQ_HPP_INCLUDED__ void monitor(socket_t &socket, const char *addr_, int events=ZMQ_EVENT_ALL)
void swap(context_t &other) ZMQ_NOTHROW
context_t(int io_threads_, int max_sockets_=ZMQ_MAX_SOCKETS_DFLT)
enum MQTTPropertyCodes value
bool operator>=(socket_ref a, socket_ref b) ZMQ_NOTHROW
ZMQ_CONSTEXPR_FN ZMQ_EXPLICIT from_handle_t(_private) ZMQ_NOTHROW
virtual const char * what() const ZMQ_NOTHROW ZMQ_OVERRIDE
ZMQ_NODISCARD const zmq_msg_t * handle() const ZMQ_NOTHROW
#define ZMQ_DELETED_FUNCTION
bool operator<=(socket_ref a, socket_ref 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_)
stack_iterator< proxy_t, is_const > operator+(typename stack_iterator< proxy_t, is_const >::difference_type n, const stack_iterator< proxy_t, is_const > &r)
zmq_pollitem_t pollitem_t
virtual void on_event_connect_delayed(const zmq_event_t &event_, const char *addr_)
value_t
the JSON type enumeration
bool connected() const ZMQ_NOTHROW
const char * gets(const char *property_)
void swap(socket_t &other) ZMQ_NOTHROW
void bind(const char *addr_)
basic_string_view< Char > to_string_view(const Char *s)
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_)
ZMQ_NODISCARD void * handle() ZMQ_NOTHROW
ZMQ_NODISCARD bool empty() const ZMQ_NOTHROW
void unbind(std::string const &addr)
void disconnect(const char *addr_)
std::size_t hash(const BasicJsonType &j)
hash a JSON value
#define ZMQ_CONSTEXPR_VAR
bool operator<(socket_ref a, socket_ref 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
basic_string_view< char > string_view
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)
int push(lua_State *L, T &&t, Args &&...args)
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_)
void init(socket_t &socket, const char *addr_, int events=ZMQ_EVENT_ALL)
typename detail::void_t_impl< Ts... >::type void_t
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_)
bool operator!=(socket_ref a, socket_ref b) ZMQ_NOTHROW
virtual void on_event_handshake_failed_no_detail(const zmq_event_t &event_, const char *addr_)
std::ostream & operator<<(std::ostream &os, const message_t &msg)
#define ZMQ_ASSERT(expression)
int num() const ZMQ_NOTHROW
message_t(void *data_, size_t size_, free_fn *ffn_, void *hint_=ZMQ_NULLPTR)
socket_base() ZMQ_NOTHROW
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_)
const T & move(const T &v)
bool operator>(socket_ref a, socket_ref b) ZMQ_NOTHROW
#define assert(a, b, c, d)
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)
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)
virtual void on_monitor_started()
virtual void on_event_bind_failed(const zmq_event_t &event_, const char *addr_)
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)
bool operator==(socket_ref a, socket_ref b) ZMQ_NOTHROW
void swap(socket_t &a, socket_t &b) ZMQ_NOTHROW
void swap(message_t &other) ZMQ_NOTHROW
#define ZMQ_CPP11_DEPRECATED(msg)
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