[Libwebsockets] Reading incoming data NOT in callback

Andy Green andy at warmcat.com
Tue Aug 19 02:51:06 CEST 2014



On 16 May 2014 13:25:41 GMT+08:00, David Haykazyan <david.haykazyan at onetick.com> wrote:
>Hi,
>
>My server serves multiple clients simultaneously and for the best
>performance I need to communicate with each client in a separate
>thread.
>Writing seems to be straightforward, libwebsocket_write method works
>(even
>though its thread safety is yet to be tested). But what to do with
>reading?
>I've tried libwebsocket_read - doesn't work (returns 0 and does not
>fill
>the buffer). I've even tried not to call libwebsocket_service, but
>libwebsocket_read still does not give me data. The data is definitely
>there, as soon as I call libwebsocket_service, it calls my callback
>with
>LWS_CALLBACK_RECEIVE reason.
>
>Any help is much appreciated.

This approach won't fly, lws is definitively a singlethreaded creature.

There must be one thread that handles all lws stuff and spins in the service loop.

Other threads may only call the on_writeable() function for wsis that they monitored by the ESTABLISHED and CLOSED callbacks.  Any work to be done is then done is the WRITEABLE callback in the lws thread context ONLY.

Calling libwebsocket_read() is also the wrong idea, you will simply get a callback for the wsi with read data when there is something to read.

Just try and study the test server structure to get a feel for how it should work, and bear in mind the only threadsafe api you can call from other threads is the one that asks for a callback when the wsi is writeable.

-Andy

>Best regards,
>David.




More information about the Libwebsockets mailing list