jeremy-w/objc-zmq

Non blocking read triggers log message

Closed this issue · 6 comments

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?

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

Is this library still being maintained?

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;
}

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

@rmcgibbo you save my day. Thank you.

@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

Contributions are welcome.