[Libwebsockets] polling for file descriptor i/o
andy at warmcat.com
Thu Mar 2 23:21:10 CET 2017
On March 3, 2017 1:00:24 AM GMT+08:00, Per Bothner <per at bothner.com> wrote:
>I got it working, except for close/eof-handling, with the
>attached patch. What do you think?
It's fine, but the child shouldn't point his own user_space to that of his parent. The parent wsi may define his own user_space externally, or by default lws will malloc him one according to the protocols struct. It should only be freed once.
I can add this bit of your patch and adapt the existing callers thismorning.
>Question: In a LWS_CALLBACK_SERVER_WRITEABLE callback, is it safe
>to make multiple lws_write calls, or should I make just a single call?
>(This pertains to how I send an "eof message".)
You can only do one lws_write() each time... the reason is POLLOUT is signalled when a tcp ack is received and there's some space in the tcp window, plus whatever amount of buffering space the kernel is willing to give us from his side at that moment. But although we get told the connection is writeable, we never know how much we may write until we do it.
In the case we want to write more than the connection + kernel will allow, the send() returns telling it wrote less than we asked. Since we allow ephemeral write buffers on the stack, that's a problem. Lws picks up the slack by malloc-ing a buffer to hold the remainder and conceals the problem by disabling any more writeable callbacks on the connection until the buffer is drained automatically.
It only allows one of these buffers per connection, so another lws_write() while it's dealing with a partial write of the first is fatal.
More information about the Libwebsockets