How to use zmq_msg_more correctly?
Connected to the code volume 1 - page 47 is an example of receiving a multi-page message:
while (1) {
zmq_msg_t message;
zmq_msg_init(&message);
zmq_msg_recv(&message, socket, 0);
// Process the message frame
...
zmq_msg_close(&message);
if (!zmq_msg_more(&message))
break;
}
It is right? Shouldn't be used zmq_msg_close()
after zmq_msg_more()
?
source to share
The API reference for zmq_msg_more () and zmq_msg_recv () (ZeroMQ 3.2.2 Stable) contains examples to show that zmq_msg_close () is called after zmq_msg_more. As far as I know, there is nothing in the API docs that contradicts this, so the example from the Connected code seems to be wrong. The documentation for zmq_msg_close()
specifies that the actual release of memory can be delayed by layers of the subclass, implying that the operation zmq_msg_more()
may succeed but still looks wrong to invoke after the message is closed.
Example from zmq_msg_more () API documentation (3.2.2) (slightly edited for reading):
zmq_msg_t part;
while (true)
{
// Create an empty ØMQ message to hold the message part
int rc = zmq_msg_init (&part);
assert (rc == 0);
// Block until a message is available to be received from socket
rc = zmq_recvmsg (socket, &part, 0);
assert (rc != -1);
if (zmq_msg_more (&part))
fprintf (stderr, "more\n");
else
{
fprintf (stderr, "end\n");
break;
}
zmq_msg_close (part);
}
However, looking at the ZeroMq Manual regarding multi-part messages , this example actually checks for more messages after the message is closed, but this is achieved by checking the socket using zmq_getsockopt () , without using any reference to the message, I suspect code related examples , just used this example and changed from zmq_getsockopt()
to zmq_msg_more()
(maybe not so).
Example from The ZeroMq Manual (multipart posts) :
while (1)
{
zmq_msg_t message;
zmq_msg_init (&message);
zmq_msg_recv (&message, socket, 0);
// Process the message frame
zmq_msg_close (&message);
int more;
size_t more_size = sizeof (more);
zmq_getsockopt (socket, ZMQ_RCVMORE, &more, &more_size);
if (!more)
break; // Last message frame
}
source to share