2016-04-06 14:20:10 +02:00
|
|
|
/*
|
2017-07-31 15:17:29 +02:00
|
|
|
Copyright (c) 2016-2017 ZeroMQ community
|
2017-06-07 09:54:37 +02:00
|
|
|
Copyright (c) 2016 VOCA AS / Harald Nøkland
|
2016-04-06 14:20:10 +02:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to
|
|
|
|
deal in the Software without restriction, including without limitation the
|
|
|
|
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __ZMQ_ADDON_HPP_INCLUDED__
|
|
|
|
#define __ZMQ_ADDON_HPP_INCLUDED__
|
|
|
|
|
2019-10-16 14:56:24 +02:00
|
|
|
#include "zmq.hpp"
|
2018-03-07 14:11:41 +01:00
|
|
|
|
2016-04-06 14:20:10 +02:00
|
|
|
#include <deque>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <sstream>
|
2016-12-08 04:03:25 +01:00
|
|
|
#include <stdexcept>
|
2019-04-02 16:01:10 +02:00
|
|
|
#ifdef ZMQ_CPP11
|
2020-03-20 10:59:15 +01:00
|
|
|
#include <limits>
|
2019-04-02 16:01:10 +02:00
|
|
|
#include <functional>
|
|
|
|
#include <unordered_map>
|
|
|
|
#endif
|
2016-04-06 14:20:10 +02:00
|
|
|
|
2018-05-11 21:32:23 +02:00
|
|
|
namespace zmq
|
|
|
|
{
|
2019-10-26 20:29:22 +02:00
|
|
|
#ifdef ZMQ_CPP11
|
|
|
|
|
2019-11-09 13:38:58 +01:00
|
|
|
namespace detail
|
|
|
|
{
|
|
|
|
template<bool CheckN, class OutputIt>
|
2020-01-26 10:09:20 +01:00
|
|
|
recv_result_t
|
|
|
|
recv_multipart_n(socket_ref s, OutputIt out, size_t n, recv_flags flags)
|
2019-10-26 20:29:22 +02:00
|
|
|
{
|
|
|
|
size_t msg_count = 0;
|
|
|
|
message_t msg;
|
2020-01-26 10:09:20 +01:00
|
|
|
while (true) {
|
|
|
|
if (CheckN) {
|
2019-11-09 13:38:58 +01:00
|
|
|
if (msg_count >= n)
|
2020-01-26 10:09:20 +01:00
|
|
|
throw std::runtime_error(
|
|
|
|
"Too many message parts in recv_multipart_n");
|
2019-11-09 13:38:58 +01:00
|
|
|
}
|
2020-01-26 10:09:20 +01:00
|
|
|
if (!s.recv(msg, flags)) {
|
2019-10-26 20:29:22 +02:00
|
|
|
// zmq ensures atomic delivery of messages
|
|
|
|
assert(msg_count == 0);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
++msg_count;
|
|
|
|
const bool more = msg.more();
|
|
|
|
*out++ = std::move(msg);
|
|
|
|
if (!more)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return msg_count;
|
|
|
|
}
|
2019-11-09 13:38:58 +01:00
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
/* Receive a multipart message.
|
|
|
|
|
|
|
|
Writes the zmq::message_t objects to OutputIterator out.
|
|
|
|
The out iterator must handle an unspecified number of writes,
|
|
|
|
e.g. by using std::back_inserter.
|
|
|
|
|
|
|
|
Returns: the number of messages received or nullopt (on EAGAIN).
|
|
|
|
Throws: if recv throws. Any exceptions thrown
|
|
|
|
by the out iterator will be propagated and the message
|
|
|
|
may have been only partially received with pending
|
|
|
|
message parts. It is adviced to close this socket in that event.
|
|
|
|
*/
|
|
|
|
template<class OutputIt>
|
2020-01-26 10:09:20 +01:00
|
|
|
ZMQ_NODISCARD recv_result_t recv_multipart(socket_ref s,
|
|
|
|
OutputIt out,
|
|
|
|
recv_flags flags = recv_flags::none)
|
2019-11-09 13:38:58 +01:00
|
|
|
{
|
|
|
|
return detail::recv_multipart_n<false>(s, std::move(out), 0, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Receive a multipart message.
|
|
|
|
|
|
|
|
Writes at most n zmq::message_t objects to OutputIterator out.
|
|
|
|
If the number of message parts of the incoming message exceeds n
|
|
|
|
then an exception will be thrown.
|
|
|
|
|
|
|
|
Returns: the number of messages received or nullopt (on EAGAIN).
|
|
|
|
Throws: if recv throws. Throws std::runtime_error if the number
|
|
|
|
of message parts exceeds n (exactly n messages will have been written
|
|
|
|
to out). Any exceptions thrown
|
|
|
|
by the out iterator will be propagated and the message
|
|
|
|
may have been only partially received with pending
|
|
|
|
message parts. It is adviced to close this socket in that event.
|
|
|
|
*/
|
|
|
|
template<class OutputIt>
|
2020-01-26 10:09:20 +01:00
|
|
|
ZMQ_NODISCARD recv_result_t recv_multipart_n(socket_ref s,
|
|
|
|
OutputIt out,
|
|
|
|
size_t n,
|
|
|
|
recv_flags flags = recv_flags::none)
|
2019-11-09 13:38:58 +01:00
|
|
|
{
|
|
|
|
return detail::recv_multipart_n<true>(s, std::move(out), n, flags);
|
|
|
|
}
|
2019-10-26 20:29:22 +02:00
|
|
|
|
|
|
|
/* Send a multipart message.
|
|
|
|
|
|
|
|
The range must be a ForwardRange of zmq::message_t,
|
|
|
|
zmq::const_buffer or zmq::mutable_buffer.
|
|
|
|
The flags may be zmq::send_flags::sndmore if there are
|
|
|
|
more message parts to be sent after the call to this function.
|
|
|
|
|
2019-11-05 21:33:55 +01:00
|
|
|
Returns: the number of messages sent (exactly msgs.size()) or nullopt (on EAGAIN).
|
|
|
|
Throws: if send throws. Any exceptions thrown
|
|
|
|
by the msgs range will be propagated and the message
|
|
|
|
may have been only partially sent. It is adviced to close this socket in that event.
|
2019-10-26 20:29:22 +02:00
|
|
|
*/
|
2020-03-07 14:22:01 +01:00
|
|
|
template<class Range
|
|
|
|
#ifndef ZMQ_CPP11_PARTIAL
|
2020-03-20 10:59:15 +01:00
|
|
|
,
|
|
|
|
typename = typename std::enable_if<
|
2020-01-26 10:09:20 +01:00
|
|
|
detail::is_range<Range>::value
|
|
|
|
&& (std::is_same<detail::range_value_t<Range>, message_t>::value
|
2020-03-07 14:22:01 +01:00
|
|
|
|| detail::is_buffer<detail::range_value_t<Range>>::value)>::type
|
|
|
|
#endif
|
2020-03-20 10:59:15 +01:00
|
|
|
>
|
2020-01-26 10:09:20 +01:00
|
|
|
send_result_t
|
|
|
|
send_multipart(socket_ref s, Range &&msgs, send_flags flags = send_flags::none)
|
2019-10-26 20:29:22 +02:00
|
|
|
{
|
2019-11-06 09:15:37 +01:00
|
|
|
using std::begin;
|
|
|
|
using std::end;
|
|
|
|
auto it = begin(msgs);
|
|
|
|
const auto end_it = end(msgs);
|
2019-11-05 21:33:55 +01:00
|
|
|
size_t msg_count = 0;
|
2020-01-26 10:09:20 +01:00
|
|
|
while (it != end_it) {
|
2019-11-05 21:33:55 +01:00
|
|
|
const auto next = std::next(it);
|
2020-01-26 10:09:20 +01:00
|
|
|
const auto msg_flags =
|
|
|
|
flags | (next == end_it ? send_flags::none : send_flags::sndmore);
|
|
|
|
if (!s.send(*it, msg_flags)) {
|
2019-10-26 20:29:22 +02:00
|
|
|
// zmq ensures atomic delivery of messages
|
2019-11-06 09:15:37 +01:00
|
|
|
assert(it == begin(msgs));
|
2019-10-26 20:29:22 +02:00
|
|
|
return {};
|
|
|
|
}
|
2019-11-05 21:33:55 +01:00
|
|
|
++msg_count;
|
|
|
|
it = next;
|
2019-10-26 20:29:22 +02:00
|
|
|
}
|
|
|
|
return msg_count;
|
|
|
|
}
|
|
|
|
|
2020-03-20 10:59:15 +01:00
|
|
|
/* Encode a multipart message.
|
|
|
|
|
|
|
|
The range must be a ForwardRange of zmq::message_t. A
|
|
|
|
zmq::multipart_t or STL container may be passed for encoding.
|
|
|
|
|
|
|
|
Returns: a zmq::message_t holding the encoded multipart data.
|
|
|
|
|
|
|
|
Throws: std::range_error is thrown if the size of any single part
|
|
|
|
can not fit in an unsigned 32 bit integer.
|
|
|
|
|
|
|
|
The encoding is compatible with that used by the CZMQ function
|
|
|
|
zmsg_encode(). Each part consists of a size followed by the data.
|
|
|
|
These are placed contiguously into the output message. A part of
|
|
|
|
size less than 255 bytes will have a single byte size value.
|
|
|
|
Larger parts will have a five byte size value with the first byte
|
|
|
|
set to 0xFF and the remaining four bytes holding the size of the
|
|
|
|
part's data.
|
|
|
|
*/
|
|
|
|
template<class Range
|
|
|
|
#ifndef ZMQ_CPP11_PARTIAL
|
|
|
|
,
|
|
|
|
typename = typename std::enable_if<
|
|
|
|
detail::is_range<Range>::value
|
|
|
|
&& (std::is_same<detail::range_value_t<Range>, message_t>::value
|
|
|
|
|| detail::is_buffer<detail::range_value_t<Range>>::value)>::type
|
2019-10-26 20:29:22 +02:00
|
|
|
#endif
|
2020-03-20 10:59:15 +01:00
|
|
|
>
|
|
|
|
message_t encode(const Range &parts)
|
|
|
|
{
|
|
|
|
size_t mmsg_size = 0;
|
|
|
|
|
|
|
|
// First pass check sizes
|
|
|
|
for (const auto &part : parts) {
|
|
|
|
size_t part_size = part.size();
|
|
|
|
if (part_size > std::numeric_limits<std::uint32_t>::max()) {
|
|
|
|
// Size value must fit into uint32_t.
|
|
|
|
throw std::range_error("Invalid size, message part too large");
|
|
|
|
}
|
|
|
|
size_t count_size = 5;
|
|
|
|
if (part_size < std::numeric_limits<std::uint8_t>::max()) {
|
|
|
|
count_size = 1;
|
|
|
|
}
|
|
|
|
mmsg_size += part_size + count_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
message_t encoded(mmsg_size);
|
|
|
|
unsigned char *buf = encoded.data<unsigned char>();
|
|
|
|
for (const auto &part : parts) {
|
|
|
|
uint32_t part_size = part.size();
|
|
|
|
const unsigned char *part_data =
|
|
|
|
static_cast<const unsigned char *>(part.data());
|
|
|
|
|
|
|
|
// small part
|
|
|
|
if (part_size < std::numeric_limits<std::uint8_t>::max()) {
|
|
|
|
*buf++ = (unsigned char) part_size;
|
|
|
|
memcpy(buf, part_data, part_size);
|
|
|
|
buf += part_size;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// big part
|
|
|
|
*buf++ = std::numeric_limits<uint8_t>::max();
|
|
|
|
*buf++ = (part_size >> 24) & std::numeric_limits<std::uint8_t>::max();
|
|
|
|
*buf++ = (part_size >> 16) & std::numeric_limits<std::uint8_t>::max();
|
|
|
|
*buf++ = (part_size >> 8) & std::numeric_limits<std::uint8_t>::max();
|
|
|
|
*buf++ = part_size & std::numeric_limits<std::uint8_t>::max();
|
|
|
|
memcpy(buf, part_data, part_size);
|
|
|
|
buf += part_size;
|
|
|
|
}
|
|
|
|
return encoded;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Decode an encoded message to multiple parts.
|
|
|
|
|
|
|
|
The given output iterator must be a ForwardIterator to a container
|
|
|
|
holding zmq::message_t such as a zmq::multipart_t or various STL
|
|
|
|
containers.
|
|
|
|
|
|
|
|
Returns the ForwardIterator advanced once past the last decoded
|
|
|
|
part.
|
|
|
|
|
|
|
|
Throws: a std::out_of_range is thrown if the encoded part sizes
|
|
|
|
lead to exceeding the message data bounds.
|
|
|
|
|
|
|
|
The decoding assumes the message is encoded in the manner
|
|
|
|
performed by zmq::encode().
|
|
|
|
*/
|
|
|
|
template<class OutputIt> OutputIt decode(const message_t &encoded, OutputIt out)
|
|
|
|
{
|
|
|
|
const unsigned char *source = encoded.data<unsigned char>();
|
|
|
|
const unsigned char *const limit = source + encoded.size();
|
|
|
|
|
|
|
|
while (source < limit) {
|
|
|
|
size_t part_size = *source++;
|
|
|
|
if (part_size == std::numeric_limits<std::uint8_t>::max()) {
|
|
|
|
if (source > limit - 4) {
|
|
|
|
throw std::out_of_range(
|
|
|
|
"Malformed encoding, overflow in reading size");
|
|
|
|
}
|
|
|
|
part_size = ((uint32_t) source[0] << 24) + ((uint32_t) source[1] << 16)
|
|
|
|
+ ((uint32_t) source[2] << 8) + (uint32_t) source[3];
|
|
|
|
source += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (source > limit - part_size) {
|
|
|
|
throw std::out_of_range("Malformed encoding, overflow in reading part");
|
|
|
|
}
|
|
|
|
*out = message_t(source, part_size);
|
|
|
|
++out;
|
|
|
|
source += part_size;
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2019-10-26 20:29:22 +02:00
|
|
|
|
2016-04-06 14:20:10 +02:00
|
|
|
#ifdef ZMQ_HAS_RVALUE_REFS
|
|
|
|
|
|
|
|
/*
|
|
|
|
This class handles multipart messaging. It is the C++ equivalent of zmsg.h,
|
|
|
|
which is part of CZMQ (the high-level C binding). Furthermore, it is a major
|
2017-06-07 09:54:37 +02:00
|
|
|
improvement compared to zmsg.hpp, which is part of the examples in the ØMQ
|
2016-04-06 14:20:10 +02:00
|
|
|
Guide. Unnecessary copying is avoided by using move semantics to efficiently
|
|
|
|
add/remove parts.
|
|
|
|
*/
|
|
|
|
class multipart_t
|
|
|
|
{
|
2018-05-11 21:32:23 +02:00
|
|
|
private:
|
2016-04-06 14:20:10 +02:00
|
|
|
std::deque<message_t> m_parts;
|
|
|
|
|
2018-05-11 21:32:23 +02:00
|
|
|
public:
|
2020-03-20 10:59:15 +01:00
|
|
|
typedef std::deque<message_t>::value_type value_type;
|
|
|
|
|
2016-12-08 14:39:24 +01:00
|
|
|
typedef std::deque<message_t>::iterator iterator;
|
|
|
|
typedef std::deque<message_t>::const_iterator const_iterator;
|
|
|
|
|
|
|
|
typedef std::deque<message_t>::reverse_iterator reverse_iterator;
|
2018-05-12 18:28:28 +02:00
|
|
|
typedef std::deque<message_t>::const_reverse_iterator const_reverse_iterator;
|
2016-12-08 14:39:24 +01:00
|
|
|
|
2016-10-23 00:27:12 +02:00
|
|
|
// Default constructor
|
2018-05-12 18:28:28 +02:00
|
|
|
multipart_t() {}
|
2016-04-06 14:20:10 +02:00
|
|
|
|
2016-10-23 00:27:12 +02:00
|
|
|
// Construct from socket receive
|
2018-05-12 18:28:28 +02:00
|
|
|
multipart_t(socket_t &socket) { recv(socket); }
|
2016-04-06 14:20:10 +02:00
|
|
|
|
2016-10-23 00:27:12 +02:00
|
|
|
// Construct from memory block
|
2018-05-12 18:28:28 +02:00
|
|
|
multipart_t(const void *src, size_t size) { addmem(src, size); }
|
2016-04-06 14:20:10 +02:00
|
|
|
|
2016-10-23 00:27:12 +02:00
|
|
|
// Construct from string
|
2018-05-12 18:28:28 +02:00
|
|
|
multipart_t(const std::string &string) { addstr(string); }
|
2016-04-06 14:20:10 +02:00
|
|
|
|
2016-10-23 00:27:12 +02:00
|
|
|
// Construct from message part
|
2018-05-12 18:28:28 +02:00
|
|
|
multipart_t(message_t &&message) { add(std::move(message)); }
|
2016-04-06 14:20:10 +02:00
|
|
|
|
2016-10-23 00:27:12 +02:00
|
|
|
// Move constructor
|
2018-05-12 18:28:28 +02:00
|
|
|
multipart_t(multipart_t &&other) { m_parts = std::move(other.m_parts); }
|
2016-04-06 14:20:10 +02:00
|
|
|
|
2016-10-23 00:27:12 +02:00
|
|
|
// Move assignment operator
|
2018-05-12 18:28:28 +02:00
|
|
|
multipart_t &operator=(multipart_t &&other)
|
2016-04-06 14:20:10 +02:00
|
|
|
{
|
2018-05-12 18:28:28 +02:00
|
|
|
m_parts = std::move(other.m_parts);
|
2016-04-06 14:20:10 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-10-23 00:27:12 +02:00
|
|
|
// Destructor
|
2018-05-12 18:28:28 +02:00
|
|
|
virtual ~multipart_t() { clear(); }
|
2016-04-06 14:20:10 +02:00
|
|
|
|
2018-05-12 18:28:28 +02:00
|
|
|
message_t &operator[](size_t n) { return m_parts[n]; }
|
2016-12-08 13:46:52 +01:00
|
|
|
|
2018-05-12 18:28:28 +02:00
|
|
|
const message_t &operator[](size_t n) const { return m_parts[n]; }
|
2016-12-08 13:46:52 +01:00
|
|
|
|
2018-05-12 18:28:28 +02:00
|
|
|
message_t &at(size_t n) { return m_parts.at(n); }
|
2016-12-08 13:46:52 +01:00
|
|
|
|
2018-05-12 18:28:28 +02:00
|
|
|
const message_t &at(size_t n) const { return m_parts.at(n); }
|
2016-12-08 13:46:52 +01:00
|
|
|
|
2018-05-12 18:28:28 +02:00
|
|
|
iterator begin() { return m_parts.begin(); }
|
2016-12-08 14:39:24 +01:00
|
|
|
|
2018-05-12 18:28:28 +02:00
|
|
|
const_iterator begin() const { return m_parts.begin(); }
|
2016-12-08 14:39:24 +01:00
|
|
|
|
2018-05-12 18:28:28 +02:00
|
|
|
const_iterator cbegin() const { return m_parts.cbegin(); }
|
2016-12-08 14:39:24 +01:00
|
|
|
|
2018-05-12 18:28:28 +02:00
|
|
|
reverse_iterator rbegin() { return m_parts.rbegin(); }
|
2016-12-08 14:39:24 +01:00
|
|
|
|
2018-05-12 18:28:28 +02:00
|
|
|
const_reverse_iterator rbegin() const { return m_parts.rbegin(); }
|
2016-12-08 14:39:24 +01:00
|
|
|
|
2018-05-12 18:28:28 +02:00
|
|
|
iterator end() { return m_parts.end(); }
|
2016-12-08 14:39:24 +01:00
|
|
|
|
2018-05-12 18:28:28 +02:00
|
|
|
const_iterator end() const { return m_parts.end(); }
|
2016-12-08 14:39:24 +01:00
|
|
|
|
2018-05-12 18:28:28 +02:00
|
|
|
const_iterator cend() const { return m_parts.cend(); }
|
2016-12-08 14:39:24 +01:00
|
|
|
|
2018-05-12 18:28:28 +02:00
|
|
|
reverse_iterator rend() { return m_parts.rend(); }
|
2016-12-08 14:39:24 +01:00
|
|
|
|
2018-05-12 18:28:28 +02:00
|
|
|
const_reverse_iterator rend() const { return m_parts.rend(); }
|
2016-12-08 14:39:24 +01:00
|
|
|
|
2016-10-23 00:27:12 +02:00
|
|
|
// Delete all parts
|
2018-05-12 18:28:28 +02:00
|
|
|
void clear() { m_parts.clear(); }
|
2016-04-06 14:20:10 +02:00
|
|
|
|
2016-10-23 00:27:12 +02:00
|
|
|
// Get number of parts
|
2018-05-12 18:28:28 +02:00
|
|
|
size_t size() const { return m_parts.size(); }
|
2016-04-06 14:20:10 +02:00
|
|
|
|
2016-10-23 00:27:12 +02:00
|
|
|
// Check if number of parts is zero
|
2018-05-12 18:28:28 +02:00
|
|
|
bool empty() const { return m_parts.empty(); }
|
2016-04-06 14:20:10 +02:00
|
|
|
|
2016-10-23 00:27:12 +02:00
|
|
|
// Receive multipart message from socket
|
2018-05-12 18:28:28 +02:00
|
|
|
bool recv(socket_t &socket, int flags = 0)
|
2016-04-06 14:20:10 +02:00
|
|
|
{
|
2018-05-12 18:28:28 +02:00
|
|
|
clear();
|
2016-04-06 14:20:10 +02:00
|
|
|
bool more = true;
|
2018-05-11 21:32:23 +02:00
|
|
|
while (more) {
|
2016-04-06 14:20:10 +02:00
|
|
|
message_t message;
|
2020-01-26 10:09:20 +01:00
|
|
|
#ifdef ZMQ_CPP11
|
2019-05-10 16:22:04 +02:00
|
|
|
if (!socket.recv(message, static_cast<recv_flags>(flags)))
|
2019-04-22 23:15:51 +02:00
|
|
|
return false;
|
2020-01-26 10:09:20 +01:00
|
|
|
#else
|
2018-05-12 18:28:28 +02:00
|
|
|
if (!socket.recv(&message, flags))
|
2016-04-06 14:20:10 +02:00
|
|
|
return false;
|
2020-01-26 10:09:20 +01:00
|
|
|
#endif
|
2018-05-12 18:28:28 +02:00
|
|
|
more = message.more();
|
|
|
|
add(std::move(message));
|
2016-04-06 14:20:10 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-23 00:27:12 +02:00
|
|
|
// Send multipart message to socket
|
2018-05-12 18:28:28 +02:00
|
|
|
bool send(socket_t &socket, int flags = 0)
|
2016-04-06 14:20:10 +02:00
|
|
|
{
|
2016-08-15 01:46:43 +02:00
|
|
|
flags &= ~(ZMQ_SNDMORE);
|
2018-05-12 18:28:28 +02:00
|
|
|
bool more = size() > 0;
|
2018-05-11 21:32:23 +02:00
|
|
|
while (more) {
|
2018-05-12 18:28:28 +02:00
|
|
|
message_t message = pop();
|
|
|
|
more = size() > 0;
|
2020-01-26 10:09:20 +01:00
|
|
|
#ifdef ZMQ_CPP11
|
|
|
|
if (!socket.send(message, static_cast<send_flags>(
|
|
|
|
(more ? ZMQ_SNDMORE : 0) | flags)))
|
2019-04-22 23:15:51 +02:00
|
|
|
return false;
|
2020-01-26 10:09:20 +01:00
|
|
|
#else
|
2018-05-12 18:28:28 +02:00
|
|
|
if (!socket.send(message, (more ? ZMQ_SNDMORE : 0) | flags))
|
2016-04-06 14:20:10 +02:00
|
|
|
return false;
|
2020-01-26 10:09:20 +01:00
|
|
|
#endif
|
2016-04-06 14:20:10 +02:00
|
|
|
}
|
2018-05-12 18:28:28 +02:00
|
|
|
clear();
|
2016-04-06 14:20:10 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-23 00:27:12 +02:00
|
|
|
// Concatenate other multipart to front
|
2018-05-12 18:28:28 +02:00
|
|
|
void prepend(multipart_t &&other)
|
2016-04-06 14:20:10 +02:00
|
|
|
{
|
2018-05-12 18:28:28 +02:00
|
|
|
while (!other.empty())
|
|
|
|
push(other.remove());
|
2016-04-06 14:20:10 +02:00
|
|
|
}
|
|
|
|
|
2016-10-23 00:27:12 +02:00
|
|
|
// Concatenate other multipart to back
|
2018-05-12 18:28:28 +02:00
|
|
|
void append(multipart_t &&other)
|
2016-04-06 14:20:10 +02:00
|
|
|
{
|
2018-05-12 18:28:28 +02:00
|
|
|
while (!other.empty())
|
|
|
|
add(other.pop());
|
2016-04-06 14:20:10 +02:00
|
|
|
}
|
|
|
|
|
2016-10-23 00:27:12 +02:00
|
|
|
// Push memory block to front
|
2018-05-12 18:28:28 +02:00
|
|
|
void pushmem(const void *src, size_t size)
|
2016-04-06 14:20:10 +02:00
|
|
|
{
|
2018-05-12 18:28:28 +02:00
|
|
|
m_parts.push_front(message_t(src, size));
|
2016-04-06 14:20:10 +02:00
|
|
|
}
|
|
|
|
|
2016-10-23 00:27:12 +02:00
|
|
|
// Push memory block to back
|
2018-05-12 18:28:28 +02:00
|
|
|
void addmem(const void *src, size_t size)
|
2016-04-06 14:20:10 +02:00
|
|
|
{
|
2018-05-12 18:28:28 +02:00
|
|
|
m_parts.push_back(message_t(src, size));
|
2016-04-06 14:20:10 +02:00
|
|
|
}
|
|
|
|
|
2016-10-23 00:27:12 +02:00
|
|
|
// Push string to front
|
2018-05-12 18:28:28 +02:00
|
|
|
void pushstr(const std::string &string)
|
2016-04-06 14:20:10 +02:00
|
|
|
{
|
2018-05-12 18:28:28 +02:00
|
|
|
m_parts.push_front(message_t(string.data(), string.size()));
|
2016-04-06 14:20:10 +02:00
|
|
|
}
|
|
|
|
|
2016-10-23 00:27:12 +02:00
|
|
|
// Push string to back
|
2018-05-12 18:28:28 +02:00
|
|
|
void addstr(const std::string &string)
|
2016-04-06 14:20:10 +02:00
|
|
|
{
|
2018-05-12 18:28:28 +02:00
|
|
|
m_parts.push_back(message_t(string.data(), string.size()));
|
2016-04-06 14:20:10 +02:00
|
|
|
}
|
|
|
|
|
2016-10-23 00:27:12 +02:00
|
|
|
// Push type (fixed-size) to front
|
2018-05-12 18:28:28 +02:00
|
|
|
template<typename T> void pushtyp(const T &type)
|
2016-04-06 14:20:10 +02:00
|
|
|
{
|
2018-05-12 18:28:28 +02:00
|
|
|
static_assert(!std::is_same<T, std::string>::value,
|
|
|
|
"Use pushstr() instead of pushtyp<std::string>()");
|
|
|
|
m_parts.push_front(message_t(&type, sizeof(type)));
|
2016-04-06 14:20:10 +02:00
|
|
|
}
|
|
|
|
|
2016-10-23 00:27:12 +02:00
|
|
|
// Push type (fixed-size) to back
|
2018-05-12 18:28:28 +02:00
|
|
|
template<typename T> void addtyp(const T &type)
|
2016-04-06 14:20:10 +02:00
|
|
|
{
|
2018-05-12 18:28:28 +02:00
|
|
|
static_assert(!std::is_same<T, std::string>::value,
|
|
|
|
"Use addstr() instead of addtyp<std::string>()");
|
|
|
|
m_parts.push_back(message_t(&type, sizeof(type)));
|
2016-04-06 14:20:10 +02:00
|
|
|
}
|
|
|
|
|
2016-10-23 00:27:12 +02:00
|
|
|
// Push message part to front
|
2018-05-12 18:28:28 +02:00
|
|
|
void push(message_t &&message) { m_parts.push_front(std::move(message)); }
|
2016-04-06 14:20:10 +02:00
|
|
|
|
2016-10-23 00:27:12 +02:00
|
|
|
// Push message part to back
|
2018-05-12 18:28:28 +02:00
|
|
|
void add(message_t &&message) { m_parts.push_back(std::move(message)); }
|
2016-04-06 14:20:10 +02:00
|
|
|
|
2020-03-20 10:59:15 +01:00
|
|
|
// Alias to allow std::back_inserter()
|
|
|
|
void push_back(message_t &&message) { m_parts.push_back(std::move(message)); }
|
|
|
|
|
2016-10-23 00:27:12 +02:00
|
|
|
// Pop string from front
|
2018-05-12 18:28:28 +02:00
|
|
|
std::string popstr()
|
2016-04-06 14:20:10 +02:00
|
|
|
{
|
2018-05-12 18:28:28 +02:00
|
|
|
std::string string(m_parts.front().data<char>(), m_parts.front().size());
|
|
|
|
m_parts.pop_front();
|
2016-04-06 14:20:10 +02:00
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2016-10-23 00:27:12 +02:00
|
|
|
// Pop type (fixed-size) from front
|
2018-05-12 18:28:28 +02:00
|
|
|
template<typename T> T poptyp()
|
2018-05-11 21:32:23 +02:00
|
|
|
{
|
2018-05-12 18:28:28 +02:00
|
|
|
static_assert(!std::is_same<T, std::string>::value,
|
|
|
|
"Use popstr() instead of poptyp<std::string>()");
|
|
|
|
if (sizeof(T) != m_parts.front().size())
|
|
|
|
throw std::runtime_error(
|
2018-05-11 21:32:23 +02:00
|
|
|
"Invalid type, size does not match the message size");
|
2018-05-12 18:28:28 +02:00
|
|
|
T type = *m_parts.front().data<T>();
|
|
|
|
m_parts.pop_front();
|
2016-04-06 14:20:10 +02:00
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2016-10-23 00:27:12 +02:00
|
|
|
// Pop message part from front
|
2018-05-12 18:28:28 +02:00
|
|
|
message_t pop()
|
2016-04-06 14:20:10 +02:00
|
|
|
{
|
2018-05-12 18:28:28 +02:00
|
|
|
message_t message = std::move(m_parts.front());
|
|
|
|
m_parts.pop_front();
|
2016-04-06 14:20:10 +02:00
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
2016-10-23 00:27:12 +02:00
|
|
|
// Pop message part from back
|
2018-05-12 18:28:28 +02:00
|
|
|
message_t remove()
|
2016-04-06 14:20:10 +02:00
|
|
|
{
|
2018-05-12 18:28:28 +02:00
|
|
|
message_t message = std::move(m_parts.back());
|
|
|
|
m_parts.pop_back();
|
2016-04-06 14:20:10 +02:00
|
|
|
return message;
|
|
|
|
}
|
2019-04-02 16:01:10 +02:00
|
|
|
|
2018-10-17 09:41:21 +02:00
|
|
|
// get message part from front
|
2020-01-26 10:09:20 +01:00
|
|
|
const message_t &front() { return m_parts.front(); }
|
2018-10-17 09:41:21 +02:00
|
|
|
|
|
|
|
// get message part from back
|
2020-01-26 10:09:20 +01:00
|
|
|
const message_t &back() { return m_parts.back(); }
|
2016-04-06 14:20:10 +02:00
|
|
|
|
2016-10-23 00:27:12 +02:00
|
|
|
// Get pointer to a specific message part
|
2018-05-12 18:28:28 +02:00
|
|
|
const message_t *peek(size_t index) const { return &m_parts[index]; }
|
2018-03-07 14:11:41 +01:00
|
|
|
|
2017-06-07 09:54:37 +02:00
|
|
|
// Get a string copy of a specific message part
|
2018-05-12 18:28:28 +02:00
|
|
|
std::string peekstr(size_t index) const
|
2017-06-07 09:54:37 +02:00
|
|
|
{
|
2018-05-12 18:28:28 +02:00
|
|
|
std::string string(m_parts[index].data<char>(), m_parts[index].size());
|
2017-06-07 09:54:37 +02:00
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Peek type (fixed-size) from front
|
2018-05-12 18:28:28 +02:00
|
|
|
template<typename T> T peektyp(size_t index) const
|
2018-05-11 21:32:23 +02:00
|
|
|
{
|
2018-05-12 18:28:28 +02:00
|
|
|
static_assert(!std::is_same<T, std::string>::value,
|
|
|
|
"Use peekstr() instead of peektyp<std::string>()");
|
|
|
|
if (sizeof(T) != m_parts[index].size())
|
|
|
|
throw std::runtime_error(
|
2018-05-11 21:32:23 +02:00
|
|
|
"Invalid type, size does not match the message size");
|
2018-05-12 18:28:28 +02:00
|
|
|
T type = *m_parts[index].data<T>();
|
2017-08-17 21:03:36 +02:00
|
|
|
return type;
|
2017-06-07 09:54:37 +02:00
|
|
|
}
|
2016-04-06 14:20:10 +02:00
|
|
|
|
2016-10-23 00:27:12 +02:00
|
|
|
// Create multipart from type (fixed-size)
|
2018-05-12 18:28:28 +02:00
|
|
|
template<typename T> static multipart_t create(const T &type)
|
2016-04-06 14:20:10 +02:00
|
|
|
{
|
|
|
|
multipart_t multipart;
|
2018-05-12 18:28:28 +02:00
|
|
|
multipart.addtyp(type);
|
2016-04-06 14:20:10 +02:00
|
|
|
return multipart;
|
|
|
|
}
|
|
|
|
|
2016-10-23 00:27:12 +02:00
|
|
|
// Copy multipart
|
2018-05-12 18:28:28 +02:00
|
|
|
multipart_t clone() const
|
2016-04-06 14:20:10 +02:00
|
|
|
{
|
|
|
|
multipart_t multipart;
|
2018-05-12 18:28:28 +02:00
|
|
|
for (size_t i = 0; i < size(); i++)
|
|
|
|
multipart.addmem(m_parts[i].data(), m_parts[i].size());
|
2016-04-06 14:20:10 +02:00
|
|
|
return multipart;
|
|
|
|
}
|
|
|
|
|
2016-10-23 00:27:12 +02:00
|
|
|
// Dump content to string
|
2018-05-12 18:28:28 +02:00
|
|
|
std::string str() const
|
2016-04-06 14:20:10 +02:00
|
|
|
{
|
|
|
|
std::stringstream ss;
|
2018-05-12 18:28:28 +02:00
|
|
|
for (size_t i = 0; i < m_parts.size(); i++) {
|
|
|
|
const unsigned char *data = m_parts[i].data<unsigned char>();
|
|
|
|
size_t size = m_parts[i].size();
|
2016-04-06 14:20:10 +02:00
|
|
|
|
|
|
|
// Dump the message as text or binary
|
|
|
|
bool isText = true;
|
2018-05-11 21:32:23 +02:00
|
|
|
for (size_t j = 0; j < size; j++) {
|
|
|
|
if (data[j] < 32 || data[j] > 127) {
|
2016-04-06 14:20:10 +02:00
|
|
|
isText = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-05-12 18:28:28 +02:00
|
|
|
ss << "\n[" << std::dec << std::setw(3) << std::setfill('0') << size
|
|
|
|
<< "] ";
|
2018-05-11 21:32:23 +02:00
|
|
|
if (size >= 1000) {
|
2016-04-06 14:20:10 +02:00
|
|
|
ss << "... (to big to print)";
|
|
|
|
continue;
|
|
|
|
}
|
2018-05-11 21:32:23 +02:00
|
|
|
for (size_t j = 0; j < size; j++) {
|
2016-04-06 14:20:10 +02:00
|
|
|
if (isText)
|
2018-05-12 18:28:28 +02:00
|
|
|
ss << static_cast<char>(data[j]);
|
2016-04-06 14:20:10 +02:00
|
|
|
else
|
2018-05-12 18:28:28 +02:00
|
|
|
ss << std::hex << std::setw(2) << std::setfill('0')
|
|
|
|
<< static_cast<short>(data[j]);
|
2016-04-06 14:20:10 +02:00
|
|
|
}
|
|
|
|
}
|
2018-05-12 18:28:28 +02:00
|
|
|
return ss.str();
|
2016-04-06 14:20:10 +02:00
|
|
|
}
|
|
|
|
|
2016-10-23 00:27:12 +02:00
|
|
|
// Check if equal to other multipart
|
2018-05-12 18:28:28 +02:00
|
|
|
bool equal(const multipart_t *other) const
|
2016-04-06 14:20:10 +02:00
|
|
|
{
|
2018-05-12 18:28:28 +02:00
|
|
|
if (size() != other->size())
|
2016-04-06 14:20:10 +02:00
|
|
|
return false;
|
2018-05-12 18:28:28 +02:00
|
|
|
for (size_t i = 0; i < size(); i++)
|
|
|
|
if (*peek(i) != *other->peek(i))
|
2016-04-06 14:20:10 +02:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-03-20 10:59:15 +01:00
|
|
|
#ifdef ZMQ_CPP11
|
|
|
|
|
|
|
|
// Return single part message_t encoded from this multipart_t.
|
|
|
|
message_t encode() const { return zmq::encode(*this); }
|
|
|
|
|
|
|
|
// Decode encoded message into multiple parts and append to self.
|
|
|
|
void decode_append(const message_t &encoded)
|
|
|
|
{
|
|
|
|
zmq::decode(encoded, std::back_inserter(*this));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return a new multipart_t containing the decoded message_t.
|
|
|
|
static multipart_t decode(const message_t &encoded)
|
|
|
|
{
|
|
|
|
multipart_t tmp;
|
|
|
|
zmq::decode(encoded, std::back_inserter(tmp));
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2018-05-11 21:32:23 +02:00
|
|
|
private:
|
2016-04-06 14:20:10 +02:00
|
|
|
// Disable implicit copying (moving is more efficient)
|
2018-05-12 18:28:28 +02:00
|
|
|
multipart_t(const multipart_t &other) ZMQ_DELETED_FUNCTION;
|
|
|
|
void operator=(const multipart_t &other) ZMQ_DELETED_FUNCTION;
|
2018-05-11 21:32:23 +02:00
|
|
|
}; // class multipart_t
|
2016-04-06 14:20:10 +02:00
|
|
|
|
2018-05-12 18:28:28 +02:00
|
|
|
inline std::ostream &operator<<(std::ostream &os, const multipart_t &msg)
|
2018-03-07 14:11:41 +01:00
|
|
|
{
|
2018-05-12 18:28:28 +02:00
|
|
|
return os << msg.str();
|
2016-04-06 14:20:10 +02:00
|
|
|
}
|
|
|
|
|
2018-05-11 21:32:23 +02:00
|
|
|
#endif // ZMQ_HAS_RVALUE_REFS
|
2018-05-11 11:18:21 +02:00
|
|
|
|
2018-05-12 18:28:28 +02:00
|
|
|
#if defined(ZMQ_BUILD_DRAFT_API) && defined(ZMQ_CPP11) && defined(ZMQ_HAVE_POLLER)
|
2018-05-11 21:32:23 +02:00
|
|
|
class active_poller_t
|
|
|
|
{
|
|
|
|
public:
|
2018-05-12 18:28:28 +02:00
|
|
|
active_poller_t() = default;
|
|
|
|
~active_poller_t() = default;
|
2018-05-11 21:32:23 +02:00
|
|
|
|
2018-05-12 18:28:28 +02:00
|
|
|
active_poller_t(const active_poller_t &) = delete;
|
|
|
|
active_poller_t &operator=(const active_poller_t &) = delete;
|
2018-05-11 21:32:23 +02:00
|
|
|
|
2018-05-12 18:28:28 +02:00
|
|
|
active_poller_t(active_poller_t &&src) = default;
|
|
|
|
active_poller_t &operator=(active_poller_t &&src) = default;
|
2018-05-11 21:32:23 +02:00
|
|
|
|
2019-05-15 08:35:12 +02:00
|
|
|
using handler_type = std::function<void(event_flags)>;
|
2018-05-11 21:32:23 +02:00
|
|
|
|
2019-05-15 08:35:12 +02:00
|
|
|
void add(zmq::socket_ref socket, event_flags events, handler_type handler)
|
2018-05-11 21:32:23 +02:00
|
|
|
{
|
2018-05-12 18:28:28 +02:00
|
|
|
auto it = decltype(handlers)::iterator{};
|
2018-05-11 21:32:23 +02:00
|
|
|
auto inserted = bool{};
|
2020-01-26 10:09:20 +01:00
|
|
|
std::tie(it, inserted) = handlers.emplace(
|
|
|
|
socket, std::make_shared<handler_type>(std::move(handler)));
|
2018-05-11 21:32:23 +02:00
|
|
|
try {
|
2018-05-12 18:28:28 +02:00
|
|
|
base_poller.add(socket, events,
|
|
|
|
inserted && *(it->second) ? it->second.get() : nullptr);
|
2018-05-11 21:32:23 +02:00
|
|
|
need_rebuild |= inserted;
|
|
|
|
}
|
|
|
|
catch (const zmq::error_t &) {
|
|
|
|
// rollback
|
|
|
|
if (inserted) {
|
2019-04-07 23:10:04 +02:00
|
|
|
handlers.erase(socket);
|
2018-05-11 10:56:41 +02:00
|
|
|
}
|
2018-05-11 21:32:23 +02:00
|
|
|
throw;
|
2018-05-11 10:56:41 +02:00
|
|
|
}
|
2018-05-11 21:32:23 +02:00
|
|
|
}
|
2018-05-11 10:56:41 +02:00
|
|
|
|
2019-04-07 23:10:04 +02:00
|
|
|
void remove(zmq::socket_ref socket)
|
2018-05-11 21:32:23 +02:00
|
|
|
{
|
2018-05-12 18:28:28 +02:00
|
|
|
base_poller.remove(socket);
|
2019-04-07 23:10:04 +02:00
|
|
|
handlers.erase(socket);
|
2018-05-11 21:32:23 +02:00
|
|
|
need_rebuild = true;
|
|
|
|
}
|
2018-05-11 10:56:41 +02:00
|
|
|
|
2019-05-15 08:35:12 +02:00
|
|
|
void modify(zmq::socket_ref socket, event_flags events)
|
2018-05-11 21:32:23 +02:00
|
|
|
{
|
2018-05-12 18:28:28 +02:00
|
|
|
base_poller.modify(socket, events);
|
2018-05-11 21:32:23 +02:00
|
|
|
}
|
2018-05-11 10:56:41 +02:00
|
|
|
|
2018-05-12 18:28:28 +02:00
|
|
|
size_t wait(std::chrono::milliseconds timeout)
|
2018-05-11 21:32:23 +02:00
|
|
|
{
|
|
|
|
if (need_rebuild) {
|
2018-05-12 18:28:28 +02:00
|
|
|
poller_events.resize(handlers.size());
|
|
|
|
poller_handlers.clear();
|
|
|
|
poller_handlers.reserve(handlers.size());
|
2018-05-11 21:32:23 +02:00
|
|
|
for (const auto &handler : handlers) {
|
2018-05-12 18:28:28 +02:00
|
|
|
poller_handlers.push_back(handler.second);
|
2018-05-11 10:56:41 +02:00
|
|
|
}
|
2018-05-11 21:32:23 +02:00
|
|
|
need_rebuild = false;
|
2018-05-11 10:56:41 +02:00
|
|
|
}
|
2018-05-12 18:28:28 +02:00
|
|
|
const auto count = base_poller.wait_all(poller_events, timeout);
|
2020-01-26 10:09:20 +01:00
|
|
|
std::for_each(poller_events.begin(),
|
|
|
|
poller_events.begin() + static_cast<ptrdiff_t>(count),
|
2019-05-15 08:35:12 +02:00
|
|
|
[](decltype(base_poller)::event_type &event) {
|
|
|
|
if (event.user_data != nullptr)
|
|
|
|
(*event.user_data)(event.events);
|
2018-05-12 18:28:28 +02:00
|
|
|
});
|
2018-05-11 21:32:23 +02:00
|
|
|
return count;
|
|
|
|
}
|
2018-05-11 10:56:41 +02:00
|
|
|
|
2019-05-15 08:35:12 +02:00
|
|
|
ZMQ_NODISCARD bool empty() const noexcept { return handlers.empty(); }
|
2018-05-11 21:32:23 +02:00
|
|
|
|
2019-05-15 08:35:12 +02:00
|
|
|
size_t size() const noexcept { return handlers.size(); }
|
2018-05-11 21:32:23 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
bool need_rebuild{false};
|
2018-05-11 10:56:41 +02:00
|
|
|
|
2019-05-15 08:35:12 +02:00
|
|
|
poller_t<handler_type> base_poller{};
|
|
|
|
std::unordered_map<socket_ref, std::shared_ptr<handler_type>> handlers{};
|
|
|
|
std::vector<decltype(base_poller)::event_type> poller_events{};
|
|
|
|
std::vector<std::shared_ptr<handler_type>> poller_handlers{};
|
2018-05-11 21:32:23 +02:00
|
|
|
}; // class active_poller_t
|
2018-05-11 10:56:41 +02:00
|
|
|
#endif // defined(ZMQ_BUILD_DRAFT_API) && defined(ZMQ_CPP11) && defined(ZMQ_HAVE_POLLER)
|
|
|
|
|
|
|
|
|
2018-05-11 21:32:23 +02:00
|
|
|
} // namespace zmq
|
2018-03-07 14:11:41 +01:00
|
|
|
|
2018-05-11 21:32:23 +02:00
|
|
|
#endif // __ZMQ_ADDON_HPP_INCLUDED__
|