[Libwebsockets] Fwd: trouble with external POLL array handling

Edwin van den Oetelaar edwin at oetelaar.com
Mon Jan 14 17:05:05 CET 2013


Summary:

in the handling of "libwebsocket_service_fd(context,  &pollfds[n]))"
the poll array is changed which lead to trouble, eg. unhandled sockets.

Is anyone else having trouble with this? How to handle it, would using
epoll() help?
If you need more info about this, please just ask.

Thanks for your time and effort already,
Best regards, and I think it is a beautiful piece of code anyways.
Edwin


snip of code from :
/* libwebsockets-test-server-extpoll - libwebsockets external poll loop sample
/*
         * callbacks for managing the external poll() array appear in
         * protocol 0 callback
         */

        case LWS_CALLBACK_ADD_POLL_FD:
                pollfds[count_pollfds].fd = (int)(long)user;
                pollfds[count_pollfds].events = (int)len;
                pollfds[count_pollfds++].revents = 0;
                break;

        case LWS_CALLBACK_DEL_POLL_FD:
                for (n = 0; n < count_pollfds; n++)
                        if (pollfds[n].fd == (int)(long)user)
                                while (n < count_pollfds) {
                                        pollfds[n] = pollfds[n + 1];
// << TROUBLE
                                        n++;
                                }
                count_pollfds--; // << TROUBLE
                break;

        case LWS_CALLBACK_SET_MODE_POLL_FD:
                for (n = 0; n < count_pollfds; n++)
                        if (pollfds[n].fd == (int)(long)user)
                                pollfds[n].events |= (int)(long)len;
                break;

        case LWS_CALLBACK_CLEAR_MODE_POLL_FD:
                for (n = 0; n < count_pollfds; n++)
                        if (pollfds[n].fd == (int)(long)user)
                                pollfds[n].events &= ~(int)(long)len;
                break;

// later the mainloop is checking
// but the pollfds[] array changed, leading to fd's not being handled,
since they moved to a lower index

n = poll(pollfds, count_pollfds, 25);
                if (n < 0)
                        goto done;

                if (n)
                        for (n = 0; n < count_pollfds; n++)
                                if (pollfds[n].revents)
                                        /*
                                        * returns immediately if the fd does not
                                        * match anything under libwebsockets
                                        * control
                                        */
                                        if (libwebsocket_service_fd(context,
                                                                  &pollfds[n]))
                                                goto done;

// -- end of code



More information about the Libwebsockets mailing list