> >       char *buffer;
> >       int size = 0;
> >       // can allocate some memory the first time, or leave it at 0 bytes
> >       int rc = zmq_recv(s, buffer, size, 0);
> >       if (rc < 0) {
> >         size = - rc;
> >         buffer = malloc(size);
> >         rc = zmq_recv(s, buffer, size, 0);
> >       }
> 
> That's how UDP works. So far it looks like I am going to mimic that.
> Unless anybody has a better idea, of course. The only alternative system
> I am aware of, SCTP's partial delivery, is, IMO, more complex, with
> little added value for 0MQ

Just to be clear, I am not saying I prefer this way of reading incoming 
messages; in fact, I prefer what now will become zmq_recvmsg(), maybe coupled 
with a user-defined memory allocator in case I want to be smart about memory 
(re)usage.

In addition to that, the ugly code I wrote should probably be a loop:

    char *buffer = 0;
    int size = 100; // Initial buffer size
    int rc = 0;
    do {
        buffer = malloc(size);
        rc = zmq_recv(s, buffer, size, 0);
        if (rc >= 0)
            break;

        size = - rc;
        free(buffer);
        // You could also use realloc().
    } while (0);

    // Buffer has now a message with size bytes and rc has the latest
    // return code from zmq_recv().

    // Use buffer somehow
    // ...
    free(buffer);

Finally, if rc < 0 you should probably set errno to indicate whether more space 
is needed or there was some other kind of error.

-- 
Gonzalo Diethelm

_______________________________________________
zeromq-dev mailing list
[email protected]
http://lists.zeromq.org/mailman/listinfo/zeromq-dev

Reply via email to