@@ -43,11 +43,14 @@ namespace zmq
4343/* Receive a multipart message.
4444
4545 Writes the zmq::message_t objects to OutputIterator out.
46- The out iterator must handle an unspecified amount of write ,
47- e.g. using std::back_inserter.
46+ The out iterator must handle an unspecified number of writes ,
47+ e.g. by using std::back_inserter.
4848
4949 Returns: the number of messages received or nullopt (on EAGAIN).
50- Throws: if recv throws.
50+ Throws: if recv throws. Any exceptions thrown
51+ by the out iterator will be propagated and the message
52+ may have been only partially received with pending
53+ message parts. It is adviced to close this socket in that event.
5154*/
5255template <class OutputIt >
5356ZMQ_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,
7982 The flags may be zmq::send_flags::sndmore if there are
8083 more message parts to be sent after the call to this function.
8184
82- Returns: the number of messages sent or nullopt (on EAGAIN).
83- Throws: if send throws.
85+ Returns: the number of messages sent (exactly msgs.size()) or nullopt (on EAGAIN).
86+ Throws: if send throws. Any exceptions thrown
87+ by the msgs range will be propagated and the message
88+ may have been only partially sent. It is adviced to close this socket in that event.
8489*/
8590template <class Range ,
8691 typename = typename std::enable_if<
@@ -92,17 +97,20 @@ detail::send_result_t send_multipart(socket_ref s, Range&& msgs,
9297 send_flags flags = send_flags::none)
9398{
9499 auto it = msgs.begin ();
95- auto last = msgs.end ();
96- const size_t msg_count = static_cast < size_t >( std::distance (it, last)) ;
97- for (; it != last; ++it )
100+ const auto end_it = msgs.end ();
101+ size_t msg_count = 0 ;
102+ while ( it != end_it )
98103 {
99- const auto mf = flags | (std::next (it) == last ? send_flags::none : send_flags::sndmore);
100- if (!s.send (*it, mf))
104+ const auto next = std::next (it);
105+ const auto msg_flags = flags | (next == end_it ? send_flags::none : send_flags::sndmore);
106+ if (!s.send (*it, msg_flags))
101107 {
102108 // zmq ensures atomic delivery of messages
103109 assert (it == msgs.begin ());
104110 return {};
105111 }
112+ ++msg_count;
113+ it = next;
106114 }
107115 return msg_count;
108116}
0 commit comments