[zeromq-dev] libevent kqueue doesn't work on fd returned from zmq_getsockopt()
Jon Dyte
jon at totient.co.uk
Thu May 9 21:52:07 CEST 2013
Hi
I think you need to loop until zmq_events is clear...
These articles may be of help:
http://funcptr.net/2013/04/20/embedding-zeromq-in-the-libev-event-loop/
http://funcptr.net/2012/09/10/zeromq---edge-triggered-notification/
Jon
On 8 May 2013 19:14, Pritesh Acharya <priteshacharya at gmail.com> wrote:
> I tried ZMQ_EVENTS, it worked but not completely. I am only able to
> receive few message and then the callback never hits.
> I'm calling zmq_getsockopt with ZMQ_EVENTS twice. before zmq_recv and
> after. Here is the complete code snippet of event callback method.
>
> static char * zmq_msg_recv_all(evutil_socket_t fd, short event, void
> *receiver)
> {
> char *msg = NULL;
>
> unsigned int zmq_events;
> size_t zmq_events_size = sizeof(zmq_events);
> zmq_getsockopt (receiver, ZMQ_EVENTS, &zmq_events, &zmq_events_size);
>
> if (zmq_events & ZMQ_POLLIN)
> {
> msg=calloc(1024,sizeof(char));
> int size = zmq_recv (receiver, msg, 255, ZMQ_DONTWAIT);
>
> if (size != -1) {
> #ifdef DEBUG
> printf("msg = %s\n",msg);
> #endif
> }
> else
> {
> free(msg);
> msg=NULL;
> }
> zmq_getsockopt (receiver, ZMQ_EVENTS, &zmq_events, &zmq_events_size);
> }
> return msg;
> }
>
>> ------------------------------
>>
>> Message: 3
>> Date: Fri, 3 May 2013 11:07:42 +0100
>> From: Jon Dyte <jon at totient.co.uk>
>> Subject: Re: [zeromq-dev] libevent kqueue doesn't work on fd returned
>> from zmq_getsockopt()
>> To: ZeroMQ development list <zeromq-dev at lists.zeromq.org>
>> Message-ID:
>> <
>> CAGGrMhom1t0yDvHaKDFeMo5SmD6L74qC1zsvcdHHPi5tYpzVcw at mail.gmail.com>
>> Content-Type: text/plain; charset="iso-8859-1"
>>
>>
>> you need to read the man page about ZMQ_FD and ZMQ_EVENTS
>>
>> http://api.zeromq.org/3-2:zmq-getsockopt
>>
>> in short, once ZMQ_FD is readable you need to look at ZMQ_EVENTS and
>> consume all messages
>> until ZMQ_EVENTS is clear
>>
>> jon
>>
>>
>>
>> On 3 May 2013 11:01, Pritesh Acharya <priteshacharya at gmail.com> wrote:
>>
>> > I'm writing a service in C programming using libevent and zmq. Msg is
>> > pushed from python code to C service using PUSH-PULL pattern.
>> >
>> > fd received from zmq socket:
>> >
>> > void *receiver = zmq_socket (base.zmq_ctx, ZMQ_PULL);
>> > zmq_connect (receiver, "tcp://localhost:5557");int fd=0;size_t fd_len =
>> sizeof(fd);
>> > zmq_getsockopt (receiver, ZMQ_FD, &fd, &fd_len);
>> >
>> > Using Libevent, event registered with fd for persistent read
>> >
>> > struct event *read_data_on_zmq =event_new(base.evbase, fd, EV_READ |
>> EV_PERSIST , read_data_on_zmq_cb,receiver);
>> > event_add(read_data_on_zmq,NULL);
>> > event_base_dispatch(base.evbase);
>> >
>> > On the callback method I'm doing a non-blocking receive
>> >
>> > void read_data_on_zmq_cb(evutil_socket_t fd, short what, void *arg){
>> > char *msg = calloc(1024,sizeof(char));
>> > int size = zmq_recv (receiver, msg, 255, ZMQ_DONTWAIT);
>> > if (size != -1)
>> > {
>> > puts ("is size is not -1");
>> > printf("msg = %s\n",msg);
>> > }}
>> >
>> > In the python code I'm continuously sending message to the socket.
>> >
>> > import zmqimport time
>>
>> >
>> > c=zmq.Context()
>> > s=c.socket(zmq.PUSH)
>> > s.bind('tcp://127.0.0.1:5557')while(True):
>> > s.send("abc")
>> > time.sleep(2)
>> >
>> > The problem is I'm only able to receive the message once, after that
>> the
>> > event callback never gets hit. If I do zmq_connect inside the
>> > read_data_on_zmq_cb after zmq_recv, then it works fine, but I guess
>> that is
>> > redundant and not the correct way to do it. What is the problem here?
>> >
>> > _______________________________________________
>> > zeromq-dev mailing list
>> > zeromq-dev at lists.zeromq.org
>> > http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>> >
>> >
>> -------------- next part --------------
>> An HTML attachment was scrubbed...
>> URL:
>> http://lists.zeromq.org/pipermail/zeromq-dev/attachments/20130503/9444f03d/attachment-0001.htm
>>
>> ------------------------------
>>
>
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20130509/bd75387a/attachment.htm>
More information about the zeromq-dev
mailing list