1
0
Fork 0
mirror of https://github.com/warmcat/libwebsockets.git synced 2025-03-16 00:00:07 +01:00
libwebsockets/minimal-examples/http-server/minimal-http-server-eventlib-foreign
Andy Green c6c7ab2b44 event libs: default to building as dynamically loaded plugins
Event lib support as it has been isn't scaling well, at the low level
libevent and libev headers have a namespace conflict so they can't
both be built into the same image, and at the distro level, binding
all the event libs to libwebsockets.so makes a bloaty situation for
packaging, lws will drag in all the event libs every time.

This patch implements the plan discussed here

https://github.com/warmcat/libwebsockets/issues/1980

and refactors the event lib support so they are built into isolated
plugins and bound at runtime according to what the application says
it wants to use.  The event lib plugins can be packaged individually
so that only the needed sets of support are installed (perhaps none
of them if the user code is OK with the default poll() loop).  And
dependent user code can mark the specific event loop plugin package
as required so pieces are added as needed.

The eventlib-foreign example is also refactored to build the selected
lib support isolated.

A readme is added detailing the changes and how to use them.

https://libwebsockets.org/git/libwebsockets/tree/READMEs/README.event-libs.md
2020-08-31 16:51:37 +01:00
..
mount-origin lws logo: update minimal examples 2020-03-06 04:46:39 +00:00
CMakeLists.txt event libs: default to building as dynamically loaded plugins 2020-08-31 16:51:37 +01:00
glib.c event libs: default to building as dynamically loaded plugins 2020-08-31 16:51:37 +01:00
libev.c event libs: default to building as dynamically loaded plugins 2020-08-31 16:51:37 +01:00
libevent.c event libs: default to building as dynamically loaded plugins 2020-08-31 16:51:37 +01:00
libuv.c event libs: default to building as dynamically loaded plugins 2020-08-31 16:51:37 +01:00
localhost-100y.cert refactor: remove more test apps and replace with minimal-examples 2018-04-30 09:16:04 +08:00
localhost-100y.key refactor: remove more test apps and replace with minimal-examples 2018-04-30 09:16:04 +08:00
minimal-http-server-eventlib-foreign.c event libs: default to building as dynamically loaded plugins 2020-08-31 16:51:37 +01:00
private.h event libs: default to building as dynamically loaded plugins 2020-08-31 16:51:37 +01:00
README.md hrtimer: add support for libevent and libev 2018-05-02 19:27:33 +08:00

lws minimal http server eventlib foreign

Commandline option Meaning
-d Debug verbosity in decimal, eg, -d15
--uv Use the libuv event library (lws must have been configured with -DLWS_WITH_LIBUV=1)
--event Use the libevent library (lws must have been configured with -DLWS_WITH_LIBEVENT=1)
--ev Use the libev event library (lws must have been configured with -DLWS_WITH_LIBEV=1)

Notice libevent and libev cannot coexist in the one library. But all the other combinations are OK.

x libuv libevent libev
libuv - OK OK
libevent OK - no
libev OK no -

This demonstrates having lws take part in a libuv loop owned by something else, with its own objects running in the loop.

Lws can join the loop, and clean up perfectly after itself without leaving anything behind or making trouble in the larger loop, which does not need to stop during lws creation or destruction.

First the foreign loop is created with a 1s timer, and runs alone for 5s.

Then the lws context is created inside the timer callback and runs for 10s... during this period you can visit http://localhost:7681 for normal lws service using the foreign loop.

After the 10s are up, the lws context is destroyed inside the foreign loop timer. The foreign loop runs alone again for a further 5s and then exits itself.

build

 $ cmake . && make

usage

 $ ./lws-minimal-http-server-eventlib-foreign
[2018/03/29 12:19:31:3480] USER: LWS minimal http server eventlib + foreign loop | visit http://localhost:7681
[2018/03/29 12:19:31:3724] NOTICE: Creating Vhost 'default' port 7681, 1 protocols, IPv6 off
[2018/03/29 12:19:31:3804] NOTICE:  Using foreign event loop...
[2018/03/29 12:19:31:3938] USER: Foreign 1Hz timer
[2018/03/29 12:19:32:4011] USER: Foreign 1Hz timer
[2018/03/29 12:19:33:4024] USER: Foreign 1Hz timer
^C[2018/03/29 12:19:33:8868] NOTICE: Signal 2 caught, exiting...
[2018/03/29 12:19:33:8963] USER: main: starting exit cleanup...
[2018/03/29 12:19:33:9064] USER: main: lws context destroyed: cleaning the foreign loop
[2018/03/29 12:19:33:9108] USER: main: exiting...

Visit http://localhost:7681