Improve algorithms and documentation

This commit is contained in:
Gudmundur Adalsteinsson 2019-11-05 20:33:55 +00:00
parent 41dee3e7a3
commit 5cc7793ef5
2 changed files with 19 additions and 11 deletions

View File

@ -41,7 +41,7 @@ Supported platforms
Examples Examples
======== ========
These examples requires at least C++11. These examples require at least C++11.
```c++ ```c++
#include <zmq.hpp> #include <zmq.hpp>

View File

@ -43,11 +43,14 @@ namespace zmq
/* Receive a multipart message. /* Receive a multipart message.
Writes the zmq::message_t objects to OutputIterator out. Writes the zmq::message_t objects to OutputIterator out.
The out iterator must handle an unspecified amount of write, The out iterator must handle an unspecified number of writes,
e.g. using std::back_inserter. e.g. by using std::back_inserter.
Returns: the number of messages received or nullopt (on EAGAIN). Returns: the number of messages received or nullopt (on EAGAIN).
Throws: if recv throws. 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> template<class OutputIt>
ZMQ_NODISCARD detail::recv_result_t recv_multipart(socket_ref s, OutputIt out, ZMQ_NODISCARD detail::recv_result_t recv_multipart(socket_ref s, OutputIt out,
@ -79,8 +82,10 @@ ZMQ_NODISCARD detail::recv_result_t recv_multipart(socket_ref s, OutputIt out,
The flags may be zmq::send_flags::sndmore if there are The flags may be zmq::send_flags::sndmore if there are
more message parts to be sent after the call to this function. more message parts to be sent after the call to this function.
Returns: the number of messages sent or nullopt (on EAGAIN). Returns: the number of messages sent (exactly msgs.size()) or nullopt (on EAGAIN).
Throws: if send throws. 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.
*/ */
template<class Range, template<class Range,
typename = typename std::enable_if< typename = typename std::enable_if<
@ -92,17 +97,20 @@ detail::send_result_t send_multipart(socket_ref s, Range&& msgs,
send_flags flags = send_flags::none) send_flags flags = send_flags::none)
{ {
auto it = msgs.begin(); auto it = msgs.begin();
auto last = msgs.end(); const auto end_it = msgs.end();
const size_t msg_count = static_cast<size_t>(std::distance(it, last)); size_t msg_count = 0;
for (; it != last; ++it) while (it != end_it)
{ {
const auto mf = flags | (std::next(it) == last ? send_flags::none : send_flags::sndmore); const auto next = std::next(it);
if (!s.send(*it, mf)) const auto msg_flags = flags | (next == end_it ? send_flags::none : send_flags::sndmore);
if (!s.send(*it, msg_flags))
{ {
// zmq ensures atomic delivery of messages // zmq ensures atomic delivery of messages
assert(it == msgs.begin()); assert(it == msgs.begin());
return {}; return {};
} }
++msg_count;
it = next;
} }
return msg_count; return msg_count;
} }