Non blocking read triggers log message #4

Closed
whatupdave opened this Issue May 24, 2011 · 6 comments

Projects

None yet

5 participants

@whatupdave

If you do a non blocking read:

id data = [socket receiveDataWithFlags:ZMQ_NOBLOCK];

and there's nothing on the queue, you get the log message:

zmq_recv: Resource temporarily unavailable

Perhaps it should check for the ZMQ_NOBLOCK flag?

@tonywok

I am also getting this. Is this the intended behavior?

Is this library still being maintained?

@rmcgibbo

I had to switch the code to using 0MQ 2 instead of but here's a fix for the nonblocking issue:

If I have time later, I'll package this all up as a PR

Basically, you just need to check for errno=EAGAIN per http://api.zeromq.org/2-1:zmq-recv

#pragma mark Communication
- (NSData *) recv:(ZMQMessageReceiveFlags) flags {
    zmq_msg_t data;
    int err = zmq_msg_init(&data);
    if (err) {
        ZMQLogError(self, @"zmq_msg_init");
        return nil;
    }

    errno = 0;
    err = zmq_recv(self.socket, &data, flags);
    if (err) {
        if (flags == ZMQ_NOBLOCK && errno == EAGAIN) {
            zmq_msg_close(&data);
            return nil;
        } else {
            ZMQLogError(self, @"zmq_recv");
            err = zmq_msg_close(&data);
            if (err) {
                ZMQLogError(self, @"zmq_msg_close");
            }
        }
    }

    size_t length = zmq_msg_size(&data);
    NSData *msg = [NSData dataWithBytes:zmq_msg_data(&data) length:length];

    err = zmq_msg_close(&data);
    if (err) {
        ZMQLogError(self, @"zmq_msg_close");
        /* fall through */
    }
    return msg;
}
@rmcgibbo

I just learned objective-c today, but fortunately this library is pretty simple, if poorly documented.

@liaa

@rmcgibbo you save my day. Thank you.

@liaa

@rmcgibbo not working when receive data from server

<ZMQSocket: 0x9155cc0 (ctx=0x9153da0, type=ZMQ_XREQ, endpoint=tcp://10.8.8.18:12345, closed=0)>: zmq_recv: Undefined error: 0
@jeremy-w
Owner

Contributions are welcome.

@jeremy-w jeremy-w closed this Dec 13, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment