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)))
71 #define ZMQ_DEPRECATED(msg)
74 #if defined(ZMQ_CPP17)
75 #define ZMQ_NODISCARD [[nodiscard]]
80 #if defined(ZMQ_CPP11)
81 #define ZMQ_NOTHROW noexcept
82 #define ZMQ_EXPLICIT explicit
83 #define ZMQ_OVERRIDE override
84 #define ZMQ_NULLPTR nullptr
85 #define ZMQ_CONSTEXPR_FN constexpr
86 #define ZMQ_CONSTEXPR_VAR constexpr
87 #define ZMQ_CPP11_DEPRECATED(msg) ZMQ_DEPRECATED(msg)
89 #define ZMQ_NOTHROW throw()
93 #define ZMQ_CONSTEXPR_FN
94 #define ZMQ_CONSTEXPR_VAR const
95 #define ZMQ_CPP11_DEPRECATED(msg)
97 #if defined(ZMQ_CPP14) && (!defined(_MSC_VER) || _MSC_VER > 1900) && (!defined(__GNUC__) || __GNUC__ > 5 || (__GNUC__ == 5 && __GNUC_MINOR__ > 3))
98 #define ZMQ_EXTENDED_CONSTEXPR
100 #if defined(ZMQ_CPP17)
101 #define ZMQ_INLINE_VAR inline
102 #define ZMQ_CONSTEXPR_IF constexpr
104 #define ZMQ_INLINE_VAR
105 #define ZMQ_CONSTEXPR_IF
124 #if defined(__has_include) && defined(ZMQ_CPP17)
125 #define CPPZMQ_HAS_INCLUDE_CPP17(X) __has_include(X)
127 #define CPPZMQ_HAS_INCLUDE_CPP17(X) 0
130 #if CPPZMQ_HAS_INCLUDE_CPP17(<optional>) && !defined(CPPZMQ_HAS_OPTIONAL)
131 #define CPPZMQ_HAS_OPTIONAL 1
133 #ifndef CPPZMQ_HAS_OPTIONAL
134 #define CPPZMQ_HAS_OPTIONAL 0
135 #elif CPPZMQ_HAS_OPTIONAL
139 #if CPPZMQ_HAS_INCLUDE_CPP17(<string_view>) && !defined(CPPZMQ_HAS_STRING_VIEW)
140 #define CPPZMQ_HAS_STRING_VIEW 1
142 #ifndef CPPZMQ_HAS_STRING_VIEW
143 #define CPPZMQ_HAS_STRING_VIEW 0
144 #elif CPPZMQ_HAS_STRING_VIEW
145 #include <string_view>
149 #define CPPZMQ_VERSION_MAJOR 4
150 #define CPPZMQ_VERSION_MINOR 10
151 #define CPPZMQ_VERSION_PATCH 0
153 #define CPPZMQ_VERSION \
154 ZMQ_MAKE_VERSION(CPPZMQ_VERSION_MAJOR, CPPZMQ_VERSION_MINOR, \
155 CPPZMQ_VERSION_PATCH)
158 #if (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 2)) \
159 && defined(__GXX_EXPERIMENTAL_CXX0X__))
160 #define ZMQ_HAS_RVALUE_REFS
161 #define ZMQ_DELETED_FUNCTION = delete
162 #elif defined(__clang__)
163 #if __has_feature(cxx_rvalue_references)
164 #define ZMQ_HAS_RVALUE_REFS
167 #if __has_feature(cxx_deleted_functions)
168 #define ZMQ_DELETED_FUNCTION = delete
170 #define ZMQ_DELETED_FUNCTION
172 #elif defined(_MSC_VER) && (_MSC_VER >= 1900)
173 #define ZMQ_HAS_RVALUE_REFS
174 #define ZMQ_DELETED_FUNCTION = delete
175 #elif defined(_MSC_VER) && (_MSC_VER >= 1600)
176 #define ZMQ_HAS_RVALUE_REFS
177 #define ZMQ_DELETED_FUNCTION
179 #define ZMQ_DELETED_FUNCTION
182 #if defined(ZMQ_CPP11) && !defined(__llvm__) && !defined(__INTEL_COMPILER) \
183 && defined(__GNUC__) && __GNUC__ < 5
184 #define ZMQ_CPP11_PARTIAL
185 #elif defined(__GLIBCXX__) && __GLIBCXX__ < 20160805
188 #define ZMQ_CPP11_PARTIAL
192 #ifdef ZMQ_CPP11_PARTIAL
193 #define ZMQ_IS_TRIVIALLY_COPYABLE(T) __has_trivial_copy(T)
195 #include <type_traits>
196 #define ZMQ_IS_TRIVIALLY_COPYABLE(T) std::is_trivially_copyable<T>::value
200 #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(3, 3, 0)
201 #define ZMQ_NEW_MONITOR_EVENT_LAYOUT
204 #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 1, 0)
205 #define ZMQ_HAS_PROXY_STEERABLE
215 #if ZMQ_VERSION < ZMQ_MAKE_VERSION(3, 2, 0)
216 #define zmq_msg_recv(msg, socket, flags) zmq_recvmsg(socket, msg, flags)
223 #define ZMQ_ASSERT(expression) assert(expression)
225 #define ZMQ_ASSERT(expression) (void) (expression)
237 template<
class T>
auto begin(T &&r) -> decltype(
begin(std::forward<T>(r)))
239 return begin(std::forward<T>(r));
241 template<
class T>
auto end(T &&r) -> decltype(
end(std::forward<T>(r)))
243 return end(std::forward<T>(r));
250 using iter_value_t =
typename std::iterator_traits<Iter>::value_type;
252 template<
class Range>
253 using range_iter_t = decltype(
256 template<
class Range>
using range_value_t = iter_value_t<range_iter_t<Range>>;
258 template<
class T,
class =
void>
struct is_range : std::false_type
266 ranges::
begin(std::declval<typename std::remove_reference<T>::type &>())
267 ==
ranges::end(std::declval<typename std::remove_reference<T>::type &>()))>>
281 typedef unsigned __int64
fd_t;
283 typedef unsigned int fd_t;
296 return zmq_strerror(
errnum);
305 inline int poll(zmq_pollitem_t *items_,
size_t nitems_,
long timeout_)
307 int rc = zmq_poll(items_,
static_cast<int>(nitems_), timeout_);
315 ZMQ_DEPRECATED(
"from 4.8.0, use poll taking std::chrono::duration instead of long")
316 inline
int poll(zmq_pollitem_t *items_,
size_t nitems_,
long timeout_)
318 inline int poll(zmq_pollitem_t *items_,
size_t nitems_,
long timeout_ = -1)
325 inline
int poll(zmq_pollitem_t const *items_,
size_t nitems_,
long timeout_ = -1)
327 return detail::poll(
const_cast<zmq_pollitem_t *
>(items_), nitems_, timeout_);
333 poll(zmq_pollitem_t const *items,
size_t nitems,
std::chrono::milliseconds timeout)
335 return detail::poll(
const_cast<zmq_pollitem_t *
>(items), nitems,
336 static_cast<long>(timeout.count()));
340 inline
int poll(
std::vector<zmq_pollitem_t> const &items,
341 std::chrono::milliseconds timeout)
343 return detail::poll(
const_cast<zmq_pollitem_t *
>(items.data()), items.size(),
344 static_cast<long>(timeout.count()));
348 inline
int poll(
std::vector<zmq_pollitem_t> const &items,
long timeout_ = -1)
350 return detail::poll(
const_cast<zmq_pollitem_t *
>(items.data()), items.size(), timeout_);
354 poll(zmq_pollitem_t *items,
size_t nitems, std::chrono::milliseconds timeout = std::chrono::milliseconds{-1})
356 return detail::poll(items, nitems,
static_cast<long>(timeout.count()));
359 inline int poll(std::vector<zmq_pollitem_t> &items,
360 std::chrono::milliseconds timeout = std::chrono::milliseconds{-1})
362 return detail::poll(items.data(), items.size(),
static_cast<long>(timeout.count()));
365 ZMQ_DEPRECATED(
"from 4.3.1, use poll taking std::chrono::duration instead of long")
366 inline
int poll(
std::vector<zmq_pollitem_t> &items,
long timeout_)
368 return detail::poll(items.data(), items.size(), timeout_);
371 template<std::
size_t SIZE>
372 inline int poll(std::array<zmq_pollitem_t, SIZE> &items,
373 std::chrono::milliseconds timeout = std::chrono::milliseconds{-1})
375 return detail::poll(items.data(), items.size(),
static_cast<long>(timeout.count()));
380 inline void version(
int *major_,
int *minor_,
int *patch_)
382 zmq_version(major_, minor_, patch_);
386 inline std::tuple<int, int, int>
version()
388 std::tuple<int, int, int> v;
389 zmq_version(&std::get<0>(v), &std::get<1>(v), &std::get<2>(v));
393 #if !defined(ZMQ_CPP11_PARTIAL)
396 template<
class T>
struct is_char_type
399 static constexpr
bool value =
413 int rc = zmq_msg_init(&
msg);
419 int rc = zmq_msg_init_size(&
msg, size_);
424 template<
class ForwardIter>
message_t(ForwardIter first, ForwardIter last)
426 typedef typename std::iterator_traits<ForwardIter>::value_type
value_t;
428 assert(std::distance(first, last) >= 0);
430 static_cast<size_t>(std::distance(first, last)) *
sizeof(
value_t);
431 int const rc = zmq_msg_init_size(&
msg, size_);
434 std::copy(first, last, data<value_t>());
439 int rc = zmq_msg_init_size(&
msg, size_);
445 memcpy(
data(), data_, size_);
451 int rc = zmq_msg_init_data(&
msg, data_, size_, ffn_, hint_);
457 #if defined(ZMQ_CPP11) && !defined(ZMQ_CPP11_PARTIAL)
466 ZMQ_DEPRECATED(
"from 4.7.0, use constructors taking iterators, (pointer, size) "
467 "or strings instead")
473 template<
class Range,
477 && !detail::is_char_type<detail::range_value_t<Range>>::value
486 #if CPPZMQ_HAS_STRING_VIEW
492 #ifdef ZMQ_HAS_RVALUE_REFS
495 int rc = zmq_msg_init(&rhs.msg);
508 int rc = zmq_msg_close(&
msg);
514 int rc = zmq_msg_close(&
msg);
517 rc = zmq_msg_init(&
msg);
523 int rc = zmq_msg_close(&
msg);
526 rc = zmq_msg_init_size(&
msg, size_);
533 int rc = zmq_msg_close(&
msg);
536 rc = zmq_msg_init_size(&
msg, size_);
539 memcpy(
data(), data_, size_);
549 int rc = zmq_msg_close(&
msg);
552 rc = zmq_msg_init_data(&
msg, data_, size_, ffn_, hint_);
557 ZMQ_DEPRECATED(
"from 4.3.1, use move taking non-const reference instead")
560 int rc = zmq_msg_move(&
msg,
const_cast<zmq_msg_t *
>(msg_->handle()));
567 int rc = zmq_msg_move(&
msg, msg_.
handle());
572 ZMQ_DEPRECATED(
"from 4.3.1, use copy taking non-const reference instead")
575 int rc = zmq_msg_copy(&
msg,
const_cast<zmq_msg_t *
>(msg_->handle()));
582 int rc = zmq_msg_copy(&
msg, msg_.
handle());
589 int rc = zmq_msg_more(
const_cast<zmq_msg_t *
>(&
msg));
597 return zmq_msg_data(
const_cast<zmq_msg_t *
>(&
msg));
602 return zmq_msg_size(
const_cast<zmq_msg_t *
>(&
msg));
611 return static_cast<T
const *
>(
data());
619 const size_t my_size =
size();
620 return my_size == other.size() && 0 == memcmp(
data(), other.data(), my_size);
625 return !(*
this == other);
628 #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(3, 2, 0)
631 int value = zmq_msg_get(&
msg, property_);
638 #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 1, 0)
639 const char *
gets(
const char *property_)
641 const char *value = zmq_msg_gets(&
msg, property_);
648 #if defined(ZMQ_BUILD_DRAFT_API) && ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 0)
649 uint32_t routing_id()
const
651 return zmq_msg_routing_id(
const_cast<zmq_msg_t *
>(&
msg));
654 void set_routing_id(uint32_t routing_id)
656 int rc = zmq_msg_set_routing_id(&
msg, routing_id);
661 const char *group()
const
663 return zmq_msg_group(
const_cast<zmq_msg_t *
>(&
msg));
666 void set_group(
const char *group)
668 int rc = zmq_msg_set_group(&
msg, group);
677 return std::string(
static_cast<const char *
>(
data()),
size());
679 #if CPPZMQ_HAS_STRING_VIEW
696 std::stringstream os;
698 const unsigned char *msg_data = this->data<unsigned char>();
703 os <<
"zmq::message_t [size " << std::dec << std::setw(3)
704 << std::setfill(
'0') <<
size <<
"] (";
707 os <<
"... too big to print)";
712 is_ascii[1] = (
byte >= 32 &&
byte < 127);
720 << std::setfill(
'0') <<
static_cast<short>(
byte);
759 #ifdef ZMQ_IO_THREADS
760 io_threads = ZMQ_IO_THREADS,
762 #ifdef ZMQ_THREAD_SCHED_POLICY
763 thread_sched_policy = ZMQ_THREAD_SCHED_POLICY,
765 #ifdef ZMQ_THREAD_PRIORITY
766 thread_priority = ZMQ_THREAD_PRIORITY,
768 #ifdef ZMQ_THREAD_AFFINITY_CPU_ADD
769 thread_affinity_cpu_add = ZMQ_THREAD_AFFINITY_CPU_ADD,
771 #ifdef ZMQ_THREAD_AFFINITY_CPU_REMOVE
772 thread_affinity_cpu_remove = ZMQ_THREAD_AFFINITY_CPU_REMOVE,
774 #ifdef ZMQ_THREAD_NAME_PREFIX
775 thread_name_prefix = ZMQ_THREAD_NAME_PREFIX,
778 max_msgsz = ZMQ_MAX_MSGSZ,
780 #ifdef ZMQ_ZERO_COPY_RECV
781 zero_copy_recv = ZMQ_ZERO_COPY_RECV,
783 #ifdef ZMQ_MAX_SOCKETS
784 max_sockets = ZMQ_MAX_SOCKETS,
786 #ifdef ZMQ_SOCKET_LIMIT
787 socket_limit = ZMQ_SOCKET_LIMIT,
792 #ifdef ZMQ_MSG_T_SIZE
793 msg_t_size = ZMQ_MSG_T_SIZE
809 explicit context_t(
int io_threads_,
int max_sockets_ = ZMQ_MAX_SOCKETS_DFLT)
815 int rc = zmq_ctx_set(
ptr, ZMQ_IO_THREADS, io_threads_);
818 rc = zmq_ctx_set(
ptr, ZMQ_MAX_SOCKETS, max_sockets_);
822 #ifdef ZMQ_HAS_RVALUE_REFS
837 int rc = zmq_ctx_set(
ptr, option_, optval_);
846 void set(ctxopt option,
int optval)
848 int rc = zmq_ctx_set(
ptr,
static_cast<int>(option), optval);
855 int rc = zmq_ctx_get(
ptr,
static_cast<int>(option));
873 rc = zmq_ctx_term(
ptr);
874 }
while (rc == -1 && errno == EINTR);
887 int rc = zmq_ctx_shutdown(
ptr);
919 struct recv_buffer_size
922 size_t untruncated_size;
926 return size != untruncated_size;
930 #if CPPZMQ_HAS_OPTIONAL
932 using send_result_t = std::optional<size_t>;
933 using recv_result_t = std::optional<size_t>;
934 using recv_buffer_result_t = std::optional<recv_buffer_size>;
942 template<
class T>
class trivial_optional
946 using value_type = T;
948 trivial_optional() =
default;
949 trivial_optional(T value) noexcept : _value(value), _has_value(
true) {}
951 const T *operator->() const noexcept
956 T *operator->() noexcept
962 const T &operator*() const noexcept
967 T &operator*() noexcept
976 throw std::exception();
979 const T &
value()
const
982 throw std::exception();
986 explicit operator bool() const noexcept {
return _has_value; }
987 bool has_value() const noexcept {
return _has_value; }
991 bool _has_value{
false};
995 using send_result_t = detail::trivial_optional<size_t>;
996 using recv_result_t = detail::trivial_optional<size_t>;
997 using recv_buffer_result_t = detail::trivial_optional<recv_buffer_size>;
1003 template<
class T> constexpr T enum_bit_or(T a, T b) noexcept
1006 using U =
typename std::underlying_type<T>::type;
1007 return static_cast<T
>(
static_cast<U
>(a) |
static_cast<U
>(b));
1009 template<
class T> constexpr T enum_bit_and(T a, T b) noexcept
1012 using U =
typename std::underlying_type<T>::type;
1013 return static_cast<T
>(
static_cast<U
>(a) &
static_cast<U
>(b));
1015 template<
class T> constexpr T enum_bit_xor(T a, T b) noexcept
1018 using U =
typename std::underlying_type<T>::type;
1019 return static_cast<T
>(
static_cast<U
>(a) ^
static_cast<U
>(b));
1021 template<
class T> constexpr T enum_bit_not(T a) noexcept
1024 using U =
typename std::underlying_type<T>::type;
1025 return static_cast<T
>(~static_cast<U>(a));
1030 enum class send_flags : int
1033 dontwait = ZMQ_DONTWAIT,
1034 sndmore = ZMQ_SNDMORE
1037 constexpr send_flags
operator|(send_flags a, send_flags b) noexcept
1039 return detail::enum_bit_or(a, b);
1041 constexpr send_flags
operator&(send_flags a, send_flags b) noexcept
1043 return detail::enum_bit_and(a, b);
1045 constexpr send_flags
operator^(send_flags a, send_flags b) noexcept
1047 return detail::enum_bit_xor(a, b);
1049 constexpr send_flags
operator~(send_flags a) noexcept
1051 return detail::enum_bit_not(a);
1055 enum class recv_flags : int
1058 dontwait = ZMQ_DONTWAIT
1061 constexpr recv_flags
operator|(recv_flags a, recv_flags b) noexcept
1063 return detail::enum_bit_or(a, b);
1065 constexpr recv_flags
operator&(recv_flags a, recv_flags b) noexcept
1067 return detail::enum_bit_and(a, b);
1069 constexpr recv_flags
operator^(recv_flags a, recv_flags b) noexcept
1071 return detail::enum_bit_xor(a, b);
1073 constexpr recv_flags
operator~(recv_flags a) noexcept
1075 return detail::enum_bit_not(a);
1083 class mutable_buffer
1086 constexpr mutable_buffer() noexcept : _data(
nullptr), _size(0) {}
1087 constexpr mutable_buffer(
void *
p,
size_t n) noexcept : _data(
p), _size(
n)
1089 #ifdef ZMQ_EXTENDED_CONSTEXPR
1090 assert(
p !=
nullptr ||
n == 0);
1094 constexpr
void *data() const noexcept {
return _data; }
1095 constexpr
size_t size() const noexcept {
return _size; }
1096 mutable_buffer &operator+=(
size_t n) noexcept
1099 const auto shift = (std::min)(
n, _size);
1100 _data =
static_cast<char *
>(_data) + shift;
1110 inline mutable_buffer
operator+(
const mutable_buffer &mb,
size_t n) noexcept
1112 return mutable_buffer(
static_cast<char *
>(mb.data()) + (std::min)(
n, mb.size()),
1113 mb.size() - (std::min)(
n, mb.size()));
1115 inline mutable_buffer
operator+(
size_t n,
const mutable_buffer &mb) noexcept
1123 constexpr const_buffer() noexcept : _data(
nullptr), _size(0) {}
1124 constexpr const_buffer(
const void *
p,
size_t n) noexcept : _data(
p), _size(
n)
1126 #ifdef ZMQ_EXTENDED_CONSTEXPR
1127 assert(
p !=
nullptr ||
n == 0);
1130 constexpr const_buffer(
const mutable_buffer &mb) noexcept :
1131 _data(mb.data()), _size(mb.size())
1135 constexpr
const void *data() const noexcept {
return _data; }
1136 constexpr
size_t size() const noexcept {
return _size; }
1137 const_buffer &operator+=(
size_t n) noexcept
1139 const auto shift = (std::min)(
n, _size);
1140 _data =
static_cast<const char *
>(_data) + shift;
1150 inline const_buffer
operator+(
const const_buffer &cb,
size_t n) noexcept
1152 return const_buffer(
static_cast<const char *
>(cb.data())
1153 + (std::min)(
n, cb.size()),
1154 cb.size() - (std::min)(
n, cb.size()));
1156 inline const_buffer
operator+(
size_t n,
const const_buffer &cb) noexcept
1163 constexpr mutable_buffer
buffer(
void *
p,
size_t n) noexcept
1165 return mutable_buffer(
p,
n);
1167 constexpr const_buffer
buffer(
const void *
p,
size_t n) noexcept
1169 return const_buffer(
p,
n);
1171 constexpr mutable_buffer
buffer(
const mutable_buffer &mb) noexcept
1175 inline mutable_buffer
buffer(
const mutable_buffer &mb,
size_t n) noexcept
1177 return mutable_buffer(mb.data(), (std::min)(mb.size(),
n));
1179 constexpr const_buffer
buffer(
const const_buffer &cb) noexcept
1183 inline const_buffer
buffer(
const const_buffer &cb,
size_t n) noexcept
1185 return const_buffer(cb.data(), (std::min)(cb.size(),
n));
1190 template<
class T>
struct is_buffer
1192 static constexpr
bool value =
1196 template<
class T>
struct is_pod_like
1202 static constexpr
bool value =
1206 template<
class C> constexpr
auto seq_size(
const C &c) noexcept -> decltype(c.size())
1210 template<
class T,
size_t N>
1211 constexpr
size_t seq_size(
const T (&)[N]) noexcept
1217 auto buffer_contiguous_sequence(Seq &&seq) noexcept
1220 using T =
typename std::remove_cv<
1221 typename std::remove_reference<decltype(*
std::begin(seq))>::type>::type;
1224 const auto size = seq_size(seq);
1229 auto buffer_contiguous_sequence(Seq &&seq,
size_t n_bytes) noexcept
1230 -> decltype(buffer_contiguous_sequence(seq))
1232 using T =
typename std::remove_cv<
1233 typename std::remove_reference<decltype(*
std::begin(seq))>::type>::type;
1236 const auto size = seq_size(seq);
1238 (std::min)(
size *
sizeof(T), n_bytes));
1244 template<
class T,
size_t N> mutable_buffer
buffer(T (&data)[N]) noexcept
1246 return detail::buffer_contiguous_sequence(data);
1248 template<
class T,
size_t N>
1249 mutable_buffer
buffer(T (&data)[N],
size_t n_bytes) noexcept
1251 return detail::buffer_contiguous_sequence(data, n_bytes);
1253 template<
class T,
size_t N> const_buffer
buffer(
const T (&data)[N]) noexcept
1255 return detail::buffer_contiguous_sequence(data);
1257 template<
class T,
size_t N>
1258 const_buffer
buffer(
const T (&data)[N],
size_t n_bytes) noexcept
1260 return detail::buffer_contiguous_sequence(data, n_bytes);
1263 template<
class T,
size_t N> mutable_buffer
buffer(std::array<T, N> &data) noexcept
1265 return detail::buffer_contiguous_sequence(data);
1267 template<
class T,
size_t N>
1268 mutable_buffer
buffer(std::array<T, N> &data,
size_t n_bytes) noexcept
1270 return detail::buffer_contiguous_sequence(data, n_bytes);
1272 template<
class T,
size_t N>
1273 const_buffer
buffer(std::array<const T, N> &data) noexcept
1275 return detail::buffer_contiguous_sequence(data);
1277 template<
class T,
size_t N>
1278 const_buffer
buffer(std::array<const T, N> &data,
size_t n_bytes) noexcept
1280 return detail::buffer_contiguous_sequence(data, n_bytes);
1282 template<
class T,
size_t N>
1283 const_buffer
buffer(
const std::array<T, N> &data) noexcept
1285 return detail::buffer_contiguous_sequence(data);
1287 template<
class T,
size_t N>
1288 const_buffer
buffer(
const std::array<T, N> &data,
size_t n_bytes) noexcept
1290 return detail::buffer_contiguous_sequence(data, n_bytes);
1293 template<
class T,
class Allocator>
1294 mutable_buffer
buffer(std::vector<T, Allocator> &data) noexcept
1296 return detail::buffer_contiguous_sequence(data);
1298 template<
class T,
class Allocator>
1299 mutable_buffer
buffer(std::vector<T, Allocator> &data,
size_t n_bytes) noexcept
1301 return detail::buffer_contiguous_sequence(data, n_bytes);
1303 template<
class T,
class Allocator>
1304 const_buffer
buffer(
const std::vector<T, Allocator> &data) noexcept
1306 return detail::buffer_contiguous_sequence(data);
1308 template<
class T,
class Allocator>
1309 const_buffer
buffer(
const std::vector<T, Allocator> &data,
size_t n_bytes) noexcept
1311 return detail::buffer_contiguous_sequence(data, n_bytes);
1314 template<
class T,
class Traits,
class Allocator>
1315 mutable_buffer
buffer(std::basic_string<T, Traits, Allocator> &data) noexcept
1317 return detail::buffer_contiguous_sequence(data);
1319 template<
class T,
class Traits,
class Allocator>
1320 mutable_buffer
buffer(std::basic_string<T, Traits, Allocator> &data,
1321 size_t n_bytes) noexcept
1323 return detail::buffer_contiguous_sequence(data, n_bytes);
1325 template<
class T,
class Traits,
class Allocator>
1326 const_buffer
buffer(
const std::basic_string<T, Traits, Allocator> &data) noexcept
1328 return detail::buffer_contiguous_sequence(data);
1330 template<
class T,
class Traits,
class Allocator>
1331 const_buffer
buffer(
const std::basic_string<T, Traits, Allocator> &data,
1332 size_t n_bytes) noexcept
1334 return detail::buffer_contiguous_sequence(data, n_bytes);
1337 #if CPPZMQ_HAS_STRING_VIEW
1339 template<
class T,
class Traits>
1340 const_buffer
buffer(std::basic_string_view<T, Traits> data) noexcept
1342 return detail::buffer_contiguous_sequence(data);
1344 template<
class T,
class Traits>
1345 const_buffer
buffer(std::basic_string_view<T, Traits> data,
size_t n_bytes) noexcept
1347 return detail::buffer_contiguous_sequence(data, n_bytes);
1354 template<
class Char,
size_t N>
1355 constexpr const_buffer str_buffer(
const Char (&data)[N]) noexcept
1358 #ifdef ZMQ_EXTENDED_CONSTEXPR
1359 assert(data[N - 1] == Char{0});
1361 return const_buffer(
static_cast<const Char *
>(data), (N - 1) *
sizeof(Char));
1366 constexpr const_buffer
operator"" _zbuf(
const char *str,
size_t len) noexcept
1368 return const_buffer(str, len *
sizeof(
char));
1370 constexpr const_buffer
operator"" _zbuf(
const wchar_t *str,
size_t len) noexcept
1372 return const_buffer(str, len *
sizeof(
wchar_t));
1374 constexpr const_buffer
operator"" _zbuf(
const char16_t *str,
size_t len) noexcept
1376 return const_buffer(str, len *
sizeof(char16_t));
1378 constexpr const_buffer
operator"" _zbuf(
const char32_t *str,
size_t len) noexcept
1380 return const_buffer(str, len *
sizeof(char32_t));
1385 enum class socket_type : int
1389 dealer = ZMQ_DEALER,
1390 router = ZMQ_ROUTER,
1397 #if defined(ZMQ_BUILD_DRAFT_API) && ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 0)
1398 server = ZMQ_SERVER,
1399 client = ZMQ_CLIENT,
1402 gather = ZMQ_GATHER,
1403 scatter = ZMQ_SCATTER,
1406 #if defined(ZMQ_BUILD_DRAFT_API) && ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 3, 3)
1408 channel = ZMQ_CHANNEL,
1410 #if ZMQ_VERSION_MAJOR >= 4
1411 stream = ZMQ_STREAM,
1424 template<
int Opt,
class T,
bool BoolUnit = false>
struct integral_option
1432 template<
int Opt,
int NullTerm = 1>
struct array_option
1436 #define ZMQ_DEFINE_INTEGRAL_OPT(OPT, NAME, TYPE) \
1437 using NAME##_t = integral_option<OPT, TYPE, false>; \
1438 ZMQ_INLINE_VAR ZMQ_CONSTEXPR_VAR NAME##_t NAME {}
1439 #define ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(OPT, NAME, TYPE) \
1440 using NAME##_t = integral_option<OPT, TYPE, true>; \
1441 ZMQ_INLINE_VAR ZMQ_CONSTEXPR_VAR NAME##_t NAME {}
1442 #define ZMQ_DEFINE_ARRAY_OPT(OPT, NAME) \
1443 using NAME##_t = array_option<OPT>; \
1444 ZMQ_INLINE_VAR ZMQ_CONSTEXPR_VAR NAME##_t NAME {}
1445 #define ZMQ_DEFINE_ARRAY_OPT_BINARY(OPT, NAME) \
1446 using NAME##_t = array_option<OPT, 0>; \
1447 ZMQ_INLINE_VAR ZMQ_CONSTEXPR_VAR NAME##_t NAME {}
1448 #define ZMQ_DEFINE_ARRAY_OPT_BIN_OR_Z85(OPT, NAME) \
1449 using NAME##_t = array_option<OPT, 2>; \
1450 ZMQ_INLINE_VAR ZMQ_CONSTEXPR_VAR NAME##_t NAME {}
1456 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_AFFINITY, affinity, uint64_t);
1459 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_BACKLOG, backlog,
int);
1461 #ifdef ZMQ_BINDTODEVICE
1462 ZMQ_DEFINE_ARRAY_OPT_BINARY(ZMQ_BINDTODEVICE, bindtodevice);
1465 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_CONFLATE, conflate,
int);
1467 #ifdef ZMQ_CONNECT_ROUTING_ID
1468 ZMQ_DEFINE_ARRAY_OPT(ZMQ_CONNECT_ROUTING_ID, connect_routing_id);
1470 #ifdef ZMQ_CONNECT_TIMEOUT
1471 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_CONNECT_TIMEOUT, connect_timeout,
int);
1473 #ifdef ZMQ_CURVE_PUBLICKEY
1474 ZMQ_DEFINE_ARRAY_OPT_BIN_OR_Z85(ZMQ_CURVE_PUBLICKEY, curve_publickey);
1476 #ifdef ZMQ_CURVE_SECRETKEY
1477 ZMQ_DEFINE_ARRAY_OPT_BIN_OR_Z85(ZMQ_CURVE_SECRETKEY, curve_secretkey);
1479 #ifdef ZMQ_CURVE_SERVER
1480 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_CURVE_SERVER, curve_server,
int);
1482 #ifdef ZMQ_CURVE_SERVERKEY
1483 ZMQ_DEFINE_ARRAY_OPT_BIN_OR_Z85(ZMQ_CURVE_SERVERKEY, curve_serverkey);
1485 #ifdef ZMQ_DISCONNECT_MSG
1486 ZMQ_DEFINE_ARRAY_OPT_BINARY(ZMQ_DISCONNECT_MSG, disconnect_msg);
1489 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_EVENTS, events,
int);
1492 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_FD, fd, ::
zmq::fd_t);
1494 #ifdef ZMQ_GSSAPI_PLAINTEXT
1495 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_GSSAPI_PLAINTEXT, gssapi_plaintext,
int);
1497 #ifdef ZMQ_GSSAPI_SERVER
1498 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_GSSAPI_SERVER, gssapi_server,
int);
1500 #ifdef ZMQ_GSSAPI_SERVICE_PRINCIPAL
1501 ZMQ_DEFINE_ARRAY_OPT(ZMQ_GSSAPI_SERVICE_PRINCIPAL, gssapi_service_principal);
1503 #ifdef ZMQ_GSSAPI_SERVICE_PRINCIPAL_NAMETYPE
1504 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_GSSAPI_SERVICE_PRINCIPAL_NAMETYPE,
1505 gssapi_service_principal_nametype,
1508 #ifdef ZMQ_GSSAPI_PRINCIPAL
1509 ZMQ_DEFINE_ARRAY_OPT(ZMQ_GSSAPI_PRINCIPAL, gssapi_principal);
1511 #ifdef ZMQ_GSSAPI_PRINCIPAL_NAMETYPE
1512 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_GSSAPI_PRINCIPAL_NAMETYPE,
1513 gssapi_principal_nametype,
1516 #ifdef ZMQ_HANDSHAKE_IVL
1517 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_HANDSHAKE_IVL, handshake_ivl,
int);
1519 #ifdef ZMQ_HEARTBEAT_IVL
1520 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_HEARTBEAT_IVL, heartbeat_ivl,
int);
1522 #ifdef ZMQ_HEARTBEAT_TIMEOUT
1523 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_HEARTBEAT_TIMEOUT, heartbeat_timeout,
int);
1525 #ifdef ZMQ_HEARTBEAT_TTL
1526 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_HEARTBEAT_TTL, heartbeat_ttl,
int);
1528 #ifdef ZMQ_HELLO_MSG
1529 ZMQ_DEFINE_ARRAY_OPT_BINARY(ZMQ_HELLO_MSG, hello_msg);
1531 #ifdef ZMQ_IMMEDIATE
1532 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_IMMEDIATE, immediate,
int);
1534 #ifdef ZMQ_INVERT_MATCHING
1535 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_INVERT_MATCHING, invert_matching,
int);
1538 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_IPV6, ipv6,
int);
1540 #ifdef ZMQ_LAST_ENDPOINT
1541 ZMQ_DEFINE_ARRAY_OPT(ZMQ_LAST_ENDPOINT, last_endpoint);
1544 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_LINGER, linger,
int);
1546 #ifdef ZMQ_MAXMSGSIZE
1547 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_MAXMSGSIZE, maxmsgsize, int64_t);
1549 #ifdef ZMQ_MECHANISM
1550 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_MECHANISM, mechanism,
int);
1553 ZMQ_DEFINE_ARRAY_OPT(ZMQ_METADATA, metadata);
1555 #ifdef ZMQ_MULTICAST_HOPS
1556 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_MULTICAST_HOPS, multicast_hops,
int);
1558 #ifdef ZMQ_MULTICAST_LOOP
1559 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_MULTICAST_LOOP, multicast_loop,
int);
1561 #ifdef ZMQ_MULTICAST_MAXTPDU
1562 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_MULTICAST_MAXTPDU, multicast_maxtpdu,
int);
1564 #ifdef ZMQ_ONLY_FIRST_SUBSCRIBE
1565 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_ONLY_FIRST_SUBSCRIBE, only_first_subscribe,
int);
1567 #ifdef ZMQ_PLAIN_SERVER
1568 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_PLAIN_SERVER, plain_server,
int);
1570 #ifdef ZMQ_PLAIN_PASSWORD
1571 ZMQ_DEFINE_ARRAY_OPT(ZMQ_PLAIN_PASSWORD, plain_password);
1573 #ifdef ZMQ_PLAIN_USERNAME
1574 ZMQ_DEFINE_ARRAY_OPT(ZMQ_PLAIN_USERNAME, plain_username);
1577 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_PRIORITY, priority,
int);
1580 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_USE_FD, use_fd,
int);
1582 #ifdef ZMQ_PROBE_ROUTER
1583 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_PROBE_ROUTER, probe_router,
int);
1586 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_RATE, rate,
int);
1589 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_RCVBUF, rcvbuf,
int);
1592 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_RCVHWM, rcvhwm,
int);
1595 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_RCVMORE, rcvmore,
int);
1598 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_RCVTIMEO, rcvtimeo,
int);
1600 #ifdef ZMQ_RECONNECT_IVL
1601 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_RECONNECT_IVL, reconnect_ivl,
int);
1603 #ifdef ZMQ_RECONNECT_IVL_MAX
1604 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_RECONNECT_IVL_MAX, reconnect_ivl_max,
int);
1606 #ifdef ZMQ_RECONNECT_STOP
1607 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_RECONNECT_STOP, reconnect_stop,
int);
1609 #ifdef ZMQ_RECOVERY_IVL
1610 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_RECOVERY_IVL, recovery_ivl,
int);
1612 #ifdef ZMQ_REQ_CORRELATE
1613 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_REQ_CORRELATE, req_correlate,
int);
1615 #ifdef ZMQ_REQ_RELAXED
1616 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_REQ_RELAXED, req_relaxed,
int);
1618 #ifdef ZMQ_ROUTER_HANDOVER
1619 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_ROUTER_HANDOVER, router_handover,
int);
1621 #ifdef ZMQ_ROUTER_MANDATORY
1622 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_ROUTER_MANDATORY, router_mandatory,
int);
1624 #ifdef ZMQ_ROUTER_NOTIFY
1625 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_ROUTER_NOTIFY, router_notify,
int);
1627 #ifdef ZMQ_ROUTING_ID
1628 ZMQ_DEFINE_ARRAY_OPT_BINARY(ZMQ_ROUTING_ID, routing_id);
1631 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_SNDBUF, sndbuf,
int);
1634 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_SNDHWM, sndhwm,
int);
1637 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_SNDTIMEO, sndtimeo,
int);
1639 #ifdef ZMQ_SOCKS_PASSWORD
1640 ZMQ_DEFINE_ARRAY_OPT(ZMQ_SOCKS_PASSWORD, socks_password);
1642 #ifdef ZMQ_SOCKS_PROXY
1643 ZMQ_DEFINE_ARRAY_OPT(ZMQ_SOCKS_PROXY, socks_proxy);
1645 #ifdef ZMQ_SOCKS_USERNAME
1646 ZMQ_DEFINE_ARRAY_OPT(ZMQ_SOCKS_USERNAME, socks_username);
1648 #ifdef ZMQ_STREAM_NOTIFY
1649 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_STREAM_NOTIFY, stream_notify,
int);
1651 #ifdef ZMQ_SUBSCRIBE
1652 ZMQ_DEFINE_ARRAY_OPT(ZMQ_SUBSCRIBE, subscribe);
1654 #ifdef ZMQ_TCP_KEEPALIVE
1655 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TCP_KEEPALIVE, tcp_keepalive,
int);
1657 #ifdef ZMQ_TCP_KEEPALIVE_CNT
1658 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TCP_KEEPALIVE_CNT, tcp_keepalive_cnt,
int);
1660 #ifdef ZMQ_TCP_KEEPALIVE_IDLE
1661 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TCP_KEEPALIVE_IDLE, tcp_keepalive_idle,
int);
1663 #ifdef ZMQ_TCP_KEEPALIVE_INTVL
1664 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TCP_KEEPALIVE_INTVL, tcp_keepalive_intvl,
int);
1666 #ifdef ZMQ_TCP_MAXRT
1667 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TCP_MAXRT, tcp_maxrt,
int);
1669 #ifdef ZMQ_THREAD_SAFE
1670 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_THREAD_SAFE, thread_safe,
int);
1673 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TOS, tos,
int);
1676 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TYPE, type,
int);
1678 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TYPE, socket_type, socket_type);
1681 #ifdef ZMQ_UNSUBSCRIBE
1682 ZMQ_DEFINE_ARRAY_OPT(ZMQ_UNSUBSCRIBE, unsubscribe);
1684 #ifdef ZMQ_VMCI_BUFFER_SIZE
1685 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_VMCI_BUFFER_SIZE, vmci_buffer_size, uint64_t);
1687 #ifdef ZMQ_VMCI_BUFFER_MIN_SIZE
1688 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_VMCI_BUFFER_MIN_SIZE, vmci_buffer_min_size, uint64_t);
1690 #ifdef ZMQ_VMCI_BUFFER_MAX_SIZE
1691 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_VMCI_BUFFER_MAX_SIZE, vmci_buffer_max_size, uint64_t);
1693 #ifdef ZMQ_VMCI_CONNECT_TIMEOUT
1694 ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_VMCI_CONNECT_TIMEOUT, vmci_connect_timeout,
int);
1696 #ifdef ZMQ_XPUB_VERBOSE
1697 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_XPUB_VERBOSE, xpub_verbose,
int);
1699 #ifdef ZMQ_XPUB_VERBOSER
1700 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_XPUB_VERBOSER, xpub_verboser,
int);
1702 #ifdef ZMQ_XPUB_MANUAL
1703 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_XPUB_MANUAL, xpub_manual,
int);
1705 #ifdef ZMQ_XPUB_MANUAL_LAST_VALUE
1706 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_XPUB_MANUAL_LAST_VALUE, xpub_manual_last_value,
int);
1708 #ifdef ZMQ_XPUB_NODROP
1709 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_XPUB_NODROP, xpub_nodrop,
int);
1711 #ifdef ZMQ_XPUB_WELCOME_MSG
1712 ZMQ_DEFINE_ARRAY_OPT(ZMQ_XPUB_WELCOME_MSG, xpub_welcome_msg);
1714 #ifdef ZMQ_ZAP_ENFORCE_DOMAIN
1715 ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_ZAP_ENFORCE_DOMAIN, zap_enforce_domain,
int);
1717 #ifdef ZMQ_ZAP_DOMAIN
1718 ZMQ_DEFINE_ARRAY_OPT(ZMQ_ZAP_DOMAIN, zap_domain);
1733 template<
typename T>
1743 int rc = zmq_setsockopt(_handle, option_, optval_, optvallen_);
1751 int rc = zmq_getsockopt(_handle, option_, optval_, optvallen_);
1756 template<
typename T>
1761 size_t optlen =
sizeof(T);
1769 template<
int Opt,
class T,
bool BoolUnit>
1770 void set(sockopt::integral_option<Opt, T, BoolUnit>,
const T &val)
1778 template<
int Opt,
class T>
1779 void set(sockopt::integral_option<Opt, T, true>,
bool val)
1788 template<
int Opt,
int NullTerm>
1789 void set(sockopt::array_option<Opt, NullTerm>,
const char *buf)
1796 template<
int Opt,
int NullTerm>
1797 void set(sockopt::array_option<Opt, NullTerm>, const_buffer buf)
1804 template<
int Opt,
int NullTerm>
1805 void set(sockopt::array_option<Opt, NullTerm>,
const std::string &buf)
1810 #if CPPZMQ_HAS_STRING_VIEW
1813 template<
int Opt,
int NullTerm>
1822 template<
int Opt,
class T,
bool BoolUnit>
1827 size_t size =
sizeof val;
1829 assert(
size ==
sizeof val);
1835 template<
int Opt,
int NullTerm>
1837 mutable_buffer buf)
const
1839 size_t size = buf.size();
1848 template<
int Opt,
int NullTerm>
1850 size_t init_size = 1024)
const
1853 if (init_size == 1024) {
1857 std::string str(init_size,
'\0');
1858 size_t size =
get(sockopt::array_option<Opt>{},
buffer(str));
1861 assert(str[
size - 1] ==
'\0');
1867 assert(str[
size - 1] ==
'\0');
1876 void bind(std::string
const &addr) {
bind(addr.c_str()); }
1880 int rc = zmq_bind(_handle, addr_);
1889 int rc = zmq_unbind(_handle, addr_);
1898 int rc = zmq_connect(_handle, addr_);
1907 int rc = zmq_disconnect(_handle, addr_);
1912 ZMQ_DEPRECATED(
"from 4.7.1, use handle() != nullptr or operator bool")
1918 int nbytes = zmq_send(_handle, buf_, len_,
flags_);
1920 return static_cast<size_t>(nbytes);
1921 if (zmq_errno() == EAGAIN)
1930 int nbytes = zmq_msg_send(msg_.handle(), _handle,
flags_);
1933 if (zmq_errno() == EAGAIN)
1938 template<
typename T>
1940 "from 4.4.1, use send taking message_t or buffer (for contiguous "
1941 "ranges), and send_flags")
1945 int nbytes = zmq_msg_send(msg.
handle(), _handle,
flags_);
1948 if (zmq_errno() == EAGAIN)
1953 #ifdef ZMQ_HAS_RVALUE_REFS
1959 return send(msg_,
static_cast<send_flags
>(
flags_)).has_value();
1967 send_result_t
send(const_buffer buf, send_flags flags = send_flags::none)
1970 zmq_send(_handle, buf.data(), buf.size(),
static_cast<int>(flags));
1972 return static_cast<size_t>(nbytes);
1973 if (zmq_errno() == EAGAIN)
1978 send_result_t
send(message_t &msg, send_flags flags)
1980 int nbytes = zmq_msg_send(msg.handle(), _handle,
static_cast<int>(flags));
1982 return static_cast<size_t>(nbytes);
1983 if (zmq_errno() == EAGAIN)
1988 send_result_t
send(message_t &&msg, send_flags flags)
1990 return send(msg, flags);
1995 "from 4.3.1, use recv taking a mutable_buffer and recv_flags")
1996 size_t recv(
void *buf_,
size_t len_,
int flags_ = 0)
1998 int nbytes = zmq_recv(_handle, buf_, len_,
flags_);
2000 return static_cast<size_t>(nbytes);
2001 if (zmq_errno() == EAGAIN)
2007 "from 4.3.1, use recv taking a reference to message_t and recv_flags")
2008 bool recv(message_t *msg_,
int flags_ = 0)
2010 int nbytes = zmq_msg_recv(msg_->handle(), _handle,
flags_);
2013 if (zmq_errno() == EAGAIN)
2020 recv_buffer_result_t recv(mutable_buffer buf,
2021 recv_flags flags = recv_flags::none)
2024 zmq_recv(_handle, buf.data(), buf.size(),
static_cast<int>(flags));
2026 return recv_buffer_size{
2027 (std::min)(
static_cast<size_t>(nbytes), buf.size()),
2028 static_cast<size_t>(nbytes)};
2030 if (zmq_errno() == EAGAIN)
2036 recv_result_t recv(message_t &msg, recv_flags flags = recv_flags::none)
2039 zmq_msg_recv(msg.handle(), _handle,
static_cast<int>(flags));
2041 assert(msg.size() ==
static_cast<size_t>(nbytes));
2042 return static_cast<size_t>(nbytes);
2044 if (zmq_errno() == EAGAIN)
2050 #if defined(ZMQ_BUILD_DRAFT_API) && ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 0)
2051 void join(
const char *group)
2053 int rc = zmq_join(_handle, group);
2058 void leave(
const char *group)
2060 int rc = zmq_leave(_handle, group);
2078 void set_option(
int option_,
const void *optval_,
size_t optvallen_)
2080 int rc = zmq_setsockopt(_handle, option_, optval_, optvallen_);
2085 void get_option(
int option_,
void *optval_,
size_t *optvallen_)
const
2087 int rc = zmq_getsockopt(_handle, option_, optval_, optvallen_);
2123 return sr.handle() ==
nullptr;
2127 return sr.handle() ==
nullptr;
2131 return !(sr ==
nullptr);
2135 return !(sr ==
nullptr);
2141 return std::equal_to<const void *>()(a.handle(), b.handle());
2149 return std::less<const void *>()(a.handle(), b.handle());
2169 template<>
struct hash<
zmq::socket_ref>
2173 return hash<void *>()(sr.handle());
2190 ctxptr(context_.handle())
2198 socket_t(context_, static_cast<int>(type_))
2203 #ifdef ZMQ_HAS_RVALUE_REFS
2230 int rc = zmq_close(_handle);
2269 int rc = zmq_proxy(frontend, backend,
capture);
2277 int rc = zmq_proxy(frontend.handle(), backend.handle(),
capture.handle());
2282 #ifdef ZMQ_HAS_PROXY_STEERABLE
2283 ZMQ_DEPRECATED(
"from 4.3.1, use proxy_steerable taking socket_t objects")
2287 int rc = zmq_proxy_steerable(frontend, backend,
capture,
control);
2297 int rc = zmq_proxy_steerable(frontend.handle(), backend.handle(),
2311 #ifdef ZMQ_HAS_RVALUE_REFS
2332 monitor(socket, addr.c_str(), events);
2337 init(socket, addr_, events);
2343 void init(
socket_t &socket, std::string
const &addr,
int events = ZMQ_EVENT_ALL)
2345 init(socket, addr.c_str(), events);
2350 int rc = zmq_socket_monitor(socket.handle(), addr_, events);
2372 zmq::poll(&items[0], 1, std::chrono::milliseconds(timeout));
2377 if (items[0].revents & ZMQ_POLLIN) {
2379 if (rc == -1 && zmq_errno() == ETERM)
2387 #if ZMQ_VERSION_MAJOR >= 4
2388 const char *data =
static_cast<const char *
>(eventMsg.
data());
2390 memcpy(&msgEvent.
event, data,
sizeof(uint16_t));
2391 data +=
sizeof(uint16_t);
2392 memcpy(&msgEvent.
value, data,
sizeof(int32_t));
2398 #ifdef ZMQ_NEW_MONITOR_EVENT_LAYOUT
2401 if (rc == -1 && zmq_errno() == ETERM) {
2406 std::string address = addrMsg.
to_string();
2409 std::string address =
event->data.connected.addr;
2412 #ifdef ZMQ_EVENT_MONITOR_STOPPED
2413 if (event->event == ZMQ_EVENT_MONITOR_STOPPED) {
2419 switch (event->event) {
2420 case ZMQ_EVENT_CONNECTED:
2423 case ZMQ_EVENT_CONNECT_DELAYED:
2426 case ZMQ_EVENT_CONNECT_RETRIED:
2429 case ZMQ_EVENT_LISTENING:
2432 case ZMQ_EVENT_BIND_FAILED:
2435 case ZMQ_EVENT_ACCEPTED:
2438 case ZMQ_EVENT_ACCEPT_FAILED:
2441 case ZMQ_EVENT_CLOSED:
2444 case ZMQ_EVENT_CLOSE_FAILED:
2447 case ZMQ_EVENT_DISCONNECTED:
2450 #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 3, 0) || (defined(ZMQ_BUILD_DRAFT_API) && ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 3))
2451 case ZMQ_EVENT_HANDSHAKE_FAILED_NO_DETAIL:
2454 case ZMQ_EVENT_HANDSHAKE_FAILED_PROTOCOL:
2457 case ZMQ_EVENT_HANDSHAKE_FAILED_AUTH:
2460 case ZMQ_EVENT_HANDSHAKE_SUCCEEDED:
2463 #elif defined(ZMQ_BUILD_DRAFT_API) && ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 1)
2464 case ZMQ_EVENT_HANDSHAKE_FAILED:
2465 on_event_handshake_failed(*event, address.c_str());
2467 case ZMQ_EVENT_HANDSHAKE_SUCCEED:
2468 on_event_handshake_succeed(*event, address.c_str());
2479 #ifdef ZMQ_EVENT_MONITOR_STOPPED
2541 #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 3)
2566 #elif ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 1)
2567 virtual void on_event_handshake_failed(
const zmq_event_t &event_,
2573 virtual void on_event_handshake_succeed(
const zmq_event_t &event_,
2601 #if defined(ZMQ_BUILD_DRAFT_API) && defined(ZMQ_CPP11) && defined(ZMQ_HAVE_POLLER)
2604 enum class event_flags : short
2607 pollin = ZMQ_POLLIN,
2608 pollout = ZMQ_POLLOUT,
2609 pollerr = ZMQ_POLLERR,
2610 pollpri = ZMQ_POLLPRI
2613 constexpr event_flags
operator|(event_flags a, event_flags b) noexcept
2615 return detail::enum_bit_or(a, b);
2617 constexpr event_flags
operator&(event_flags a, event_flags b) noexcept
2619 return detail::enum_bit_and(a, b);
2621 constexpr event_flags
operator^(event_flags a, event_flags b) noexcept
2623 return detail::enum_bit_xor(a, b);
2625 constexpr event_flags
operator~(event_flags a) noexcept
2627 return detail::enum_bit_not(a);
2630 struct no_user_data;
2633 template<
class T = no_user_data>
struct poller_event
2641 template<
typename T = no_user_data>
class poller_t
2644 using event_type = poller_event<T>;
2646 poller_t() : poller_ptr(zmq_poller_new())
2653 typename Dummy =
void,
2658 add_impl(socket, events, user_data);
2663 add_impl(socket, events,
nullptr);
2667 typename Dummy =
void,
2670 void add(
fd_t fd, event_flags events, T *user_data)
2672 add_impl(fd, events, user_data);
2675 void add(
fd_t fd, event_flags events) { add_impl(fd, events,
nullptr); }
2679 if (0 != zmq_poller_remove(poller_ptr.get(), socket.handle())) {
2687 != zmq_poller_modify(poller_ptr.get(), socket.handle(),
2688 static_cast<short>(events))) {
2693 size_t wait_all(std::vector<event_type> &poller_events,
2694 const std::chrono::milliseconds timeout)
2696 int rc = zmq_poller_wait_all(
2698 reinterpret_cast<zmq_poller_event_t *
>(poller_events.data()),
2699 static_cast<int>(poller_events.size()),
2700 static_cast<long>(timeout.count()));
2702 return static_cast<size_t>(rc);
2704 #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 3)
2705 if (zmq_errno() == EAGAIN)
2707 if (zmq_errno() == ETIMEDOUT)
2714 #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 3, 3)
2715 size_t size() const noexcept
2717 int rc = zmq_poller_size(
const_cast<void *
>(poller_ptr.get()));
2719 return static_cast<size_t>(std::max(rc, 0));
2724 struct destroy_poller_t
2726 void operator()(
void *ptr) noexcept
2728 int rc = zmq_poller_destroy(&ptr);
2733 std::unique_ptr<void, destroy_poller_t> poller_ptr;
2735 void add_impl(
zmq::socket_ref socket, event_flags events, T *user_data)
2738 != zmq_poller_add(poller_ptr.get(), socket.handle(), user_data,
2739 static_cast<short>(events))) {
2744 void add_impl(
fd_t fd, event_flags events, T *user_data)
2747 != zmq_poller_add_fd(poller_ptr.get(), fd, user_data,
2748 static_cast<short>(events))) {
2753 #endif // defined(ZMQ_BUILD_DRAFT_API) && defined(ZMQ_CPP11) && defined(ZMQ_HAVE_POLLER)
2757 return os << msg.
str();
2762 #endif // __ZMQ_HPP_INCLUDED__