[Libwebsockets] Example of custom event loop integration?

Andy Green andy at warmcat.com
Sun Jun 6 10:33:42 CEST 2021



On 5/30/21 1:46 PM, Felipe Gasper wrote:

>> if you think that will do you, I can add it.  But that api is not exactly designed for hacking in someone's random existing poll wait, it's designed to allow integration of these complicated event libs at the right spots.
>>
>> What I can imagine is an event lib plugin that exposes a way user code can register a simpler "ops" struct with function callbacks to get stuff done on an external fd wait.  But at a minimum, the existing wait is going to have to check with us to learn the max wait timeout for its poll(), and understand which fds are ours and handle them differently.
>>
>> Another way to do it would be add apis in lws that allow the app to register its own fds to participate on the lws wait, be it the default poll() one or handled by an event lib.  Then we can take care of which fds have external handlers and which belong to lws.
> 
> I’m not experienced enough with LWS to feel confident in offering an opinion on its architecture. But once/if such a method is added I’ll be interested in making this library available in Perl. (I may do so anyway, just with the requirement to use one of the supported event loops.) All of the WebSocket solutions in Perl that I know of are pure-Perl; having a C implementation would be a boon on multiple levels.
> 
> It would also facilitate bindings in other languages; for example, Twisted (Python) does its own select/poll/epoll by default.

I have added support for providing an "event lib plugin" object at 
context creation time.  It's not a plugin when delivered like that, just 
reuses the structures.

There's also now a minimal example for http-server using this to bind 
lws to a "custom poll wait" which should be pretty representative.

https://libwebsockets.org/git/libwebsockets/tree/minimal-examples/http-server/minimal-http-server-eventlib-custom/minimal-http-server.c?h=main

The first 170 lines of the file is just implementing the custom poll() 
loop with helpers to add and remove fds, and change fd event waits. 
This would be what your application already has.

The next 100 lines is binding the lws event lib object callbacks to the 
custom poll wait apis defined earlier in the file, only 5 are needed.

Finally the remaining 75 lines is the minimal http example as usual, 
just the addition of setting info.event_lib_custom and setting the 
info.foreign loops to point to the custom poll wait object so it's 
available in the callbacks.

-Andy


More information about the Libwebsockets mailing list