Domanda

I am using zmq (3.2) and am utilizing multipart messages. I am handling some error cases where the first message part is invalid and I want to discard the remaining parts. What I was thinking was something along the lines of.

void zmq_drain_multipart(void *sock)
{
    int more;
    size_t size = sizeof(int);
    zmq_getsockopt(sock, ZMQ_RCVMORE, &more, &size);
    while (more)
    {
        zmq_recv(sock, NULL, 0, 0);
        zmq_getsockopt(sock, ZMQ_RCVMORE, &more, &size);
    }
}

Is something like this supposed to work.

È stato utile?

Soluzione

IMO, the final authority for correctness is the API page at http://api.zeromq.org/3-2:zmq-recv. Even though your code works - and probably will continue work with new versions of ZeroMQ - you are not following the rules.

Argument 2 (buf) is a reference to a buffer. There is no mention of any special case where you can provide NULL. Fortunately if the received payload is larger than argument 3 (len), then the data will be truncated. Which means you can provide a really small buffer. It can be stack based to avoid any memory allocation cost as shown below.

void zmq_drain_multipart(void *sock)
{
    int more;
    size_t size = sizeof(int);
    zmq_getsockopt(sock, ZMQ_RCVMORE, &more, &size);
    while (more)
    {
        char buf[32];
        zmq_recv(sock, buf, 32, 0);
        zmq_getsockopt(sock, ZMQ_RCVMORE, &more, &size);
    }
}

This also has a minor advantage when things go wrong. When stepping through a debugger, the above code will let you examine the first few bytes of each fragment that is being received and drained.

Altri suggerimenti

In ZMQ 4.3 you can do this oficially: "The buf argument may be null if len is zero." http://api.zeromq.org/4-3:zmq-recv

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top