Thank you, Pieter.  So, this is what I've ended up with:

int read_zmq_connections() {
        zmq_pollitem_t items [] = {
                { zmq_responder, 0, ZMQ_POLLIN, 0 }
        };
        errno = 0;
        int ret = 0;
        while ( errno == 0 ) {
                zmq_msg_t message;
                int rc = zmq_poll(items, 1, 0);
                if (rc == -1) break;
                if (items[0].revents & ZMQ_POLLIN) {
                        zmq_msg_init(&message);
                int size = zmq_msg_recv(&message, zmq_responder, 0);
                if (size == -1)
                                break;
                        char *str = malloc(size + 1);
                memcpy(str, zmq_msg_data(&message), size);
                str[size] = 0;
                        add_remote_account(str);
                        free(str);
                        zmq_msg_close(&message);
                        ++ret;
                } else {
                        break;
                }
        }
        return ret;
}

I replaced the helper functions with the core API methods, as I didn't want to 
be limited to 255 character strings.  The packets I'm sending aren't bit, but 
may be a little bigger (100 - 350 chars approx).

I'll give this a thorough testing in the morning, but I'm currently happier 
with it right now.

Thanks again,
Lee




On 19 Feb 2013, at 22:11, Pieter Hintjens <[email protected]> wrote:

> Lee,
> 
> For what it's worth, this is the poll construct I always use:
> 
>    while (true) {
>        zmq_pollitem_t items [] = {
>            { onesocket, 0, ZMQ_POLLIN, 0 }
>            { twosocket, 0, ZMQ_POLLIN, 0 }
>        };
>        //  Calculate timeout here or use 0 for infinity
>        int rc = zmq_poll (items, 1, mytimeout);
>        if (rc == -1)
>            break;              //  Always check for Ctrl-C
> 
>        if (items [0].revents & ZMQ_POLLIN) {
>            zmsg_t *msg = zmsg_recv (onesocket);
>            if (!msg)
>                break;          //  Always check for Ctrl-C
>            //  Process message
>            zmsg_destroy (&msg);
>        }
>        //  Test all sockets (not an else here)
>        if (items [1].revents & ZMQ_POLLIN) {
>            ...
>        }
>        //  Do regular maintenance if needed
>    }
> 
> -Pieter
> 
> On Tue, Feb 19, 2013 at 11:04 PM, Lee Sylvester <[email protected]> 
> wrote:
>> Okay, so this brings me kinda to where I was before:
>> 
>> int read_zmq_connections() {
>>        zmq_pollitem_t items [] = {
>>                { zmq_responder, 0, ZMQ_POLLIN, 0 }
>>        };
>>        errno = 0;
>>        int ret = 0;
>>        while ( errno == 0 ) {
>>                zmq_poll(items, 1, 0);
>>                if (items [0].revents & ZMQ_POLLIN) {
>>                        char *str = s_recv(zmq_responder);
>>                        add_remote_account(str);
>>                        free(str);
>>                        ++ret;
>>                } else {
>>                        break;
>>                }
>>        }
>>        return ret;
>> }
>> 
>> So, I poll the events and, if an error occurs or if there is no event, then 
>> I return the number of messages retrieved.  Have I missed something?
>> 
>> Thanks,
>> Lee
>> 
>> 
>> On 19 Feb 2013, at 17:11, Charles Remes <[email protected]> wrote:
>> 
>>> It looks better except for the use of "size" in the loop control. The 
>>> return code from zmq_poll is the *number of sockets* that have pending 
>>> events. The way you are using it appears as though you believe the return 
>>> code indicates the *number of messages* which is incorrect. The way the 
>>> loop is written now, it will loop once and exit at most.
>>> 
>>> 
>>> On Feb 19, 2013, at 10:29 AM, Lee Sylvester <[email protected]> wrote:
>>> 
>>>> Okay, thank you.  So I now have the following:
>>>> 
>>>> int read_zmq_connections() {
>>>>     zmq_pollitem_t items [] = {
>>>>             { zmq_responder, 0, ZMQ_POLLIN, 0 }
>>>>     };
>>>>     errno = 0;
>>>>     int ret = 0, size = zmq_poll(items, 1, 0);
>>>>     zmq_msg_t message;
>>>>     while ( size > 0 && errno == 0 ) {
>>>>             char *str = s_recv(zmq_responder);
>>>>             parse_new_data(str);
>>>>             free(str);
>>>>             ++ret;
>>>>             --size;
>>>>     }
>>>>     return ret;
>>>> }
>>>> 
>>>> I'm going to test it a little later once I've written my client code.  :-)
>>>> 
>>>> Thanks again.
>>>> 
>>>> Cheers,
>>>> Lee
>>>> 
>>>> 
>>>> 
>>>> On 19 Feb 2013, at 16:21, Charles Remes <[email protected]> wrote:
>>>> 
>>>>> Yes, you are missing out on being able to differentiate between reads & 
>>>>> writes since you aren't checking the revents. However, in your case you 
>>>>> only have a single socket and you only register for POLLIN, so you can 
>>>>> just use the return code and skip the hard stuff. Any time it returns 1 
>>>>> then you know that your socket is readable.
>>>>> 
>>>>> 
>>>>> On Feb 19, 2013, at 10:18 AM, Lee Sylvester <[email protected]> 
>>>>> wrote:
>>>>> 
>>>>>> Okay, thanks.  I'm looking at the reference and I can see that zmq_poll 
>>>>>> returns the number of items, but it feels like I'm missing something 
>>>>>> when I rely on that :-S
>>>>>> 
>>>>>> Lee
>>>>>> 
>>>>>> 
>>>>>> On 19 Feb 2013, at 15:52, Charles Remes <[email protected]> wrote:
>>>>>> 
>>>>>>> Hmmm, I'm not sure that's exactly right.
>>>>>>> 
>>>>>>> The basic idea is that you want to check the return code from zmq_poll. 
>>>>>>> If it is greater than 0, then the socket can be read from. You should 
>>>>>>> then read from the socket until no more messages are available. I don't 
>>>>>>> know how it works with the #s_recv() function (presumably that is part 
>>>>>>> of the czmq binding) but you want to read until the socket is empty or 
>>>>>>> you get EAGAIN. Perhaps that function does that for you under the 
>>>>>>> covers.
>>>>>>> 
>>>>>>> So, the loop should be around reading from the socket and *not* around 
>>>>>>> zmq_poll. Does that make sense?
>>>>>>> 
>>>>>>> 
>>>>>>> 
>>>>>>> On Feb 19, 2013, at 9:16 AM, Lee Sylvester <[email protected]> 
>>>>>>> wrote:
>>>>>>> 
>>>>>>>> Thank you, that's great.  So, based on what I've read, does this look 
>>>>>>>> correct for what I'm trying to accomplish?
>>>>>>>> 
>>>>>>>> int read_zmq_connections() {
>>>>>>>> zmq_pollitem_t items [] = {
>>>>>>>>         { zmq_responder, 0, ZMQ_POLLIN, 0 }
>>>>>>>> };
>>>>>>>> while (1) {
>>>>>>>>         zmq_msg_t message;
>>>>>>>>         zmq_poll(items, 1, 0);
>>>>>>>>         if (items[0].revents & ZMQ_POLLIN) {
>>>>>>>>                 char *str = s_recv(zmq_responder);
>>>>>>>>                 parse_new_data(str);
>>>>>>>>                 free(str);
>>>>>>>>         } else {
>>>>>>>>                 break;
>>>>>>>>         }
>>>>>>>> }
>>>>>>>> return 0;
>>>>>>>> }
>>>>>>>> 
>>>>>>>> Thanks,
>>>>>>>> Lee
>>>>>>>> 
>>>>>>>> 
>>>>>>>> 
>>>>>>>> 
>>>>>>>> On 19 Feb 2013, at 14:52, Charles Remes <[email protected]> wrote:
>>>>>>>> 
>>>>>>>>> Take a look at the man page for zmq_poll. You can do a non-blocking 
>>>>>>>>> poll for incoming messages on your socket. If it returns immediately 
>>>>>>>>> with 0, then no sockets in your pollset have pending messages to read.
>>>>>>>>> 
>>>>>>>>> Be aware that when zmq_poll does indicate that you have messages, you 
>>>>>>>>> must read *all* of them from the socket before zmq_poll will work 
>>>>>>>>> again. I'm pretty sure the man page explains this.
>>>>>>>>> 
>>>>>>>>> Good luck.
>>>>>>>>> 
>>>>>>>>> On Feb 19, 2013, at 8:44 AM, Lee Sylvester <[email protected]> 
>>>>>>>>> wrote:
>>>>>>>>> 
>>>>>>>>>> Hey guys,
>>>>>>>>>> 
>>>>>>>>>> So, I've integrated ØMQ into my server.  Now, I want to use ØMQ as a 
>>>>>>>>>> means to supply information to a HTTP server from a separate 
>>>>>>>>>> management app.  So, in theory, it will look something like this
>>>>>>>>>> 
>>>>>>>>>> int read_zmq_connections() {
>>>>>>>>>> int ret = 0;
>>>>>>>>>> while (zmq_has_messages(zmq_responder)) {
>>>>>>>>>> char *str = s_recv(zmq_responder);
>>>>>>>>>> parse_new_data(str);
>>>>>>>>>> free(str);
>>>>>>>>>> ++ret;
>>>>>>>>>> }
>>>>>>>>>> return ret;
>>>>>>>>>> }
>>>>>>>>>> 
>>>>>>>>>> This way, if there are no messages on zmq_responder, then the 
>>>>>>>>>> function will simply return.  What I don't know how to do (and can't 
>>>>>>>>>> quite find) is how to check if messages exist on the connection.  
>>>>>>>>>> Can anyone please point me in the right direction?
>>>>>>>>>> 
>>>>>>>>>> The reason why I need this non-blocking is that I will only be 
>>>>>>>>>> calling 'read_zmq_connections' approximately once every five minutes 
>>>>>>>>>> and I don't want my app to hang while waiting for messages.
>>>>>>>>>> 
>>>>>>>>>> Thanks loads in advance,
>>>>>>>>>> Lee
>>>>>>>>>> _______________________________________________
>>>>>>>>>> zeromq-dev mailing list
>>>>>>>>>> [email protected]
>>>>>>>>>> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>>>>>>>>> 
>>>>>>>>> _______________________________________________
>>>>>>>>> zeromq-dev mailing list
>>>>>>>>> [email protected]
>>>>>>>>> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>>>>>>>> 
>>>>>>>> _______________________________________________
>>>>>>>> zeromq-dev mailing list
>>>>>>>> [email protected]
>>>>>>>> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>>>>>>> 
>>>>>>> _______________________________________________
>>>>>>> zeromq-dev mailing list
>>>>>>> [email protected]
>>>>>>> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>>>>>> 
>>>>>> _______________________________________________
>>>>>> zeromq-dev mailing list
>>>>>> [email protected]
>>>>>> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>>>>> 
>>>>> _______________________________________________
>>>>> zeromq-dev mailing list
>>>>> [email protected]
>>>>> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>>>> 
>>>> _______________________________________________
>>>> zeromq-dev mailing list
>>>> [email protected]
>>>> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>>> 
>>> _______________________________________________
>>> zeromq-dev mailing list
>>> [email protected]
>>> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>> 
>> _______________________________________________
>> zeromq-dev mailing list
>> [email protected]
>> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
> _______________________________________________
> zeromq-dev mailing list
> [email protected]
> http://lists.zeromq.org/mailman/listinfo/zeromq-dev

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

Reply via email to