@@ -43,11 +43,14 @@ namespace zmq
43
43
/* Receive a multipart message.
44
44
45
45
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.
48
48
49
49
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.
51
54
*/
52
55
template <class OutputIt >
53
56
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,
79
82
The flags may be zmq::send_flags::sndmore if there are
80
83
more message parts to be sent after the call to this function.
81
84
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.
84
89
*/
85
90
template <class Range ,
86
91
typename = typename std::enable_if<
@@ -92,17 +97,20 @@ detail::send_result_t send_multipart(socket_ref s, Range&& msgs,
92
97
send_flags flags = send_flags::none)
93
98
{
94
99
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 )
98
103
{
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))
101
107
{
102
108
// zmq ensures atomic delivery of messages
103
109
assert (it == msgs.begin ());
104
110
return {};
105
111
}
112
+ ++msg_count;
113
+ it = next;
106
114
}
107
115
return msg_count;
108
116
}
0 commit comments