[Libwebsockets] How to run run libwebsocket_Service() in a Mainloop?

Andy Green andy at warmcat.com
Thu Apr 10 04:16:32 CEST 2014



On 04/09/2014 05:15 AM, Prathamesh P Ghanekar wrote:
> Hi Andy,Michael
>
> Thanks for pointing me to the repository. I am going through it. But
> meanwhile, I have another confusion. It goes as follows:
>
> 1) I am quoting some lines from the README.coding file of the
> libwebsockets git repository.
>
> /Libwebsockets is singlethreaded/
>
> /-------------------------------/
>
> /Directly performing websocket actions from other threads is not allowed.///
>
> /Aside from the internal data being inconsistent in forked() processes,/
>
> /the scope of a wsi (struct websocket) can end at any time during service/
>
> /with the socket closing and the wsi freed./
>
> /If you need to service other socket or file descriptors as well as the/
>
> /websocket ones, you can combine them together with the websocket ones/
>
> /in one poll loop, see "External Polling Loop support" below, and/
>
> /still do it all in one thread / process context./
>
> What I understand from these lines is that, performing libwebsockets
> actions from other threads is not allowed/encouraged. That is, to create
> a thread for libwebsocket_service() API, and Mainloop should be used to
> integrate websockets with any legacy code.
>
> 2) Whereas, I was going through libwebsockets.c file, where APIs are
> briefly documented.
>
> For libwebsocket_create_context() API, some lines go as follows:
>
> /After initialization, it returns a struct libwebsocket_context * that
> represents this server. After calling, user code needs to take care of
> calling libwebsokcet service() with the context pointer to get the
> server’s sockets serviced. This can be done in the same process context
> or as a forked process, or another thread./
>
> This leaves me in confusion whether to create a thread for calling
> libwebsocket_service() or it’s not allowed/encouraged.

Lws used to try to support threaded operation and the comment comes from 
that time.

Eventually I realized that was never going to work solidly without 
turning lws into a multithreaded lib with proper locking and removed it.

I pushed a patch to clarify the comment.

Since then, it became clear that you probably can do one simple but 
critical thing from another thread, which is request the callback when 
writeable for a websocket connection.  That implies you have to keep a 
list of live wsi by trapping the established and closed callbacks.

Several people seem to be able to do that successfully.

Notice we added special callbacks LWS_CALLBACK_[UN]LOCK_POLL to allow 
this to be threadsafe with the user code providing the locking necessary 
around changes to the list of descriptors waited on in the poll.

-Andy

> Am I misinterpreting anything from the documentation?
>
> Thanks in advance J
>
> Regards,
>
> Prathamesh
>
> -----Original Message-----
> From: Andy Green [mailto:andy.green at linaro.org]
> Sent: 08 April 2014 17:59
> To: Prathamesh P Ghanekar; Michael Haberler
> Subject: Fwd: [Libwebsockets] How to run run libwebsocket_Service() in a
> Mainloop?
>
> Not sure what happened with the To: emails in this one, so forwarding it...
>
> -Andy
>
> ---------- Forwarded message ----------
>
> From: Michael Haberler <mail17 at mah.priv.at <mailto:mail17 at mah.priv.at>>
>
> Date: 8 April 2014 12:26
>
> Subject: Re: [Libwebsockets] How to run run libwebsocket_Service() in a
> Mainloop?
>
> To: Prathamesh P Ghanekar <andy.green at linaro.org
> <mailto:andy.green at linaro.org>>
>
> Prathamesh,
>
> Am 08.04.2014 um 11:29 schrieb Prathamesh P Ghanekar
> <andy.green at linaro.org <mailto:andy.green at linaro.org>>:
>
>  > Hi,
>
>  >
>
>  > I would like to know how should one go about running the
> libwebsocket_service() call in a glib main loop?
>
>  > I am starting a server in the main() using
> libwebsocket_create_context() and wish to avoid while(1) loop for
> polling for client connections. An example code snippet would be of
> great help.
>
> not exactly what you asked - but maybe a starting point: libwebsockets
> integrated with the zeromq event loop:
>
> https://github.com/mhaberler/libwebsockets/commits/zeromq-proxy-example
>
> - Michael
>
>  >
>
>  > Regards,
>
>  > Prathamesh
>
> _______________________________________________
>
>  > Libwebsockets mailing list
>
>  > Libwebsockets at ml.libwebsockets.org
> <mailto:Libwebsockets at ml.libwebsockets.org>
>
>  > http://ml.libwebsockets.org/mailman/listinfo/libwebsockets
>
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Disclaimer~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Information contained and transmitted by this e-mail is confidential and
> proprietary to iGATE and its affiliates and is intended for use only by
> the recipient. If you are not the intended recipient, you are hereby
> notified that any dissemination, distribution, copying or use of this
> e-mail is strictly prohibited and you are requested to delete this
> e-mail immediately and notify the originator or mailadmin at igate.com
> <mailto:mailadmin at igate.com>. iGATE does not enter into any agreement
> with any party by e-mail. Any views expressed by an individual do not
> necessarily reflect the view of iGATE. iGATE is not responsible for the
> consequences of any actions taken on the basis of information provided,
> through this email. The contents of an attachment to this e-mail may
> contain software viruses, which could damage your own computer system.
> While iGATE has taken every reasonable precaution to minimise this risk,
> we cannot accept liability for any damage which you sustain as a result
> of software viruses. You should carry out your own virus checks before
> opening an attachment. To know more about iGATE please visit
> www.igate.com <http://www.igate.com>.
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
>
>
> _______________________________________________
> Libwebsockets mailing list
> Libwebsockets at ml.libwebsockets.org
> http://ml.libwebsockets.org/mailman/listinfo/libwebsockets
>



More information about the Libwebsockets mailing list