[Libwebsockets] How to send data asynchronously to the client?
bas at appelo.nl
Sun Jul 6 12:09:32 CEST 2014
>> I don’t really understand what Andy means with this:
>>>>>>> >Libwebsockets is basically single-threaded. But, with care from another
>>>>>>> >server thread, you can call either
>> What does ‘With care’ means, I rewrote my code so that it doesn’t call
>> service_cancel, but skips that and now does
> It means there is deliberately zero threadsafety / locking in this library and if you approach it like there is, it will blow up.
> It's designed to have a single service thread so it is very unusually efficient and lightweight and can be used on very small and weak devices with low footprint.
> It just happens that if you follow that architecture with your code, it's also very very efficient with large numbers of connections too.
>> libwebsocket_callback_on_writable(context, wsi) from what I called
>> thread-2. This works! But only the first time (could be because then
>> there is also other traffic…) But in a quite environment, I can see it
>> does something, but I’m not receiving LWS_CALLBACK_SERVER_WRITEABLE.
> We got a contribution a few months ago that redid the guts of all this to use the pipe. So it is supposed to work now just by calling the writeable callback from another thread context (it will discover that is happening by checking your thread ID).
>> I’m doing this, because I’m still having issues with my first approach,
>> it works like charm in a single libwebsocket_context, but if I create
>> two, I’m not receiving any LWS_CALLBACK_SERVER_WRITEABLE, with both
>> approaches (cancel thread-2 libwebsocket_callback_on_writable thread-1
>> or libwebsocket_callback_on_writable thread -2)
>> If you would like any additional info (like a small prog that reproduces
>> this, or the dump logging (which don’t tell me much, no errors)) please ask!
> I think a small prog to reproduce that can be made into a sample app would be a good idea.
I still have problems calling libwebsocket_callback_on_writable from another thread, calling libwebsocket_cancel_service works better.
I wrote an little c++ program with the intention of showing where my problem shows up:
It includes a Makefile that works on my mac, building should not be too difficult.
It works fine as it is, but contains two defines in main on line 6 and WebSocket line 5, if uncommented two problems will show.
The first is that if I change from calling cancel from the foreign thread to calling libwebsocket_callback_on_writable directly from this foreign thread I’m not actually receiving a callback.
I added the mentioned check thread ID callback, but this did not help.
The other problem is that if I create a second instance of libwebsocket_context I also stop receiving callbacks.
Could you check if I’m using libwebsocket decently in this example? At least the cancel_service part works nice, why the other two issues are there I don’t know, could be my misunderstanding of how to use libwebsocket. Worth to mention I’m on a Mac(that could be the issue), I think up to date in most important parts, at least on the libwebsocket code.
If you like my approach, or at least the basis case, I’d be happy to add some comments and make it really thread-safe[I kinda brutally cut this from my project code] so it could serve as an example for the questions I see every now and then about threading and libwebsocket.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Libwebsockets