[Libwebsockets] libwebsocket_callback_on_writable

Julio A. Becerra julioabg at gmail.com
Tue Apr 22 04:46:57 CEST 2014


I think I was bit confused. According to the documentation,
libwebsocket_service() deals with any websocket event for three kinds of
traffic, but it explicitly mentions only new connections and receive
callbacks. I just realized it also handles "callback on writeable".

It also seems that I need to request the callback everytime I want the
event loop (i.e. libwebsocket_service) to call my callback function. So
there's no need to remove the request. I understood it wrong, and I thought
that once you request the callback, it lasts forever.

In any case, the source of my confusion was the role of the
libwebsocket_service() function. That's why I was also calling
libwebsocket_callback_on_writable_all_protocol(), which is not necessary at
all in my server implementation.

Thanks a lot for your help. I would say my problem is solved.




2014-04-22 4:16 GMT+02:00 Andy Green <andy at warmcat.com>:

>
>
> On 04/22/2014 10:31 AM, Julio A. Becerra wrote:
>
>> Well, both the library and my own code.
>>
>> In the main "event" loop of the server, I call
>> libwebsocket_callback_on_writable_all_protocol() in order to write all
>> pending data.
>>
>
> Ah I maybe see your problem.
>
> There's a "targeted" version of that call that only gets a callback to
> come for a specific connection.
>
> LWS_VISIBLE LWS_EXTERN int
> libwebsocket_callback_on_writable(struct libwebsocket_context *context,
>                                                       struct libwebsocket
> *wsi);
>
> -Andy
>
>  Let's suppose I have 10 clients connected:
>> 1. An event occurs and I want to send something to client A.
>> 2. The job is done, but the client is still connected.
>> 3. An event occurs and I want to send something to client B.
>>
>> Since libwebsocket_callback_on_writable_all_protocol() calls the
>> callback functions for all descriptors, it means the callback function
>> for the client A is still being called.
>>
>> Moreover, don't we have a way to have only one blocking function (i.e.
>> wrapper for select() system call, both for read and write), instead of
>> calling both libwebsocket_callback_on_writable_all_protocol() and
>> libwebsocket_service() ?
>>
>> Thanks
>>
>>
>> 2014-04-22 3:19 GMT+02:00 Andy Green <andy at warmcat.com
>> <mailto:andy at warmcat.com>>:
>>
>>
>>
>>
>>     On 22 April 2014 10:08:54 GMT+09:00, "Julio A. Becerra"
>>     <julioabg at gmail.com <mailto:julioabg at gmail.com>> wrote:
>>      >Thanks for the answer!
>>      >
>>      >Well, let's say that a client is connected to my libwebsockets
>> server.
>>      >When
>>      >some event occurs, I want to send (write) something to this client,
>> so
>>      >I
>>      >request the callback on writable.
>>      >
>>      >When the job is done (everything is sent), I do not want to close
>> the
>>      >connection, I want the client to keep connected. But my callback
>>
>>     Yes it's normal, you should send your last thing and then not ask
>>     for a callback on writeable.  Almost 0 cpu will be used in that case.
>>
>>      >function
>>      >is still being continuously called because I cannot find a way to
>>      >remove
>>      >this request.
>>
>>     Who is doing the request to callback on writeable in that case?
>>       Something in the library or your code?
>>
>>     -Andy
>>
>>      >Hope my explanation is clear enough. In short: the client is always
>>      >connected, and I want to request the callback on writeable at my
>>      >convenience... so I need a way to remove the request.
>>      >
>>      >Thanks again!
>>      >
>>      >
>>      >
>>      >2014-04-22 2:59 GMT+02:00 Andy Green <andy at warmcat.com
>>     <mailto:andy at warmcat.com>>:
>>
>>      >
>>      >>
>>      >>
>>      >> On 22 April 2014 09:39:29 GMT+09:00, "Julio A. Becerra" <
>>      >> julioabg at gmail.com <mailto:julioabg at gmail.com>> wrote:
>>      >> >I am new to libwebsockets and I have a question I couldn't solve
>>      >yet.
>>      >> >
>>      >> >Since we have to call libwebsocket_callback_on_writable in
>> order to
>>      >> >request
>>      >> >a callback when the socket is able to be written, is it possible
>> to
>>      >> >remove
>>      >> >this request when we don't need to write on the socket for a
>> while?
>>      >> >I.e.,
>>      >> >saving CPU cycles.
>>      >> >
>>      >> >I guess there should be a trivial way to do it, besides
>>     implementing
>>      >> >your
>>      >> >own custom functions.
>>      >>
>>      >> Sorry not sure I get your point.
>>      >>
>>      >> If you do not request the callback when writeable, no cpu cycles
>>     will
>>      >be
>>      >> burned.
>>      >>
>>      >> When something happens that you want to write something, only
>>     call it
>>      >then
>>      >> to get that job done.
>>      >>
>>      >> There's no need to keep calling it forever when there's nothing to
>>      >send.
>>      >>
>>      >> 'Andy
>>      >>
>>      >> >
>>      >>
>>      >>----------------------------------------------------------
>> --------------
>>      >> >
>>      >> >_______________________________________________
>>      >> >Libwebsockets mailing list
>>      >> >Libwebsockets at ml.libwebsockets.org
>>     <mailto:Libwebsockets at ml.libwebsockets.org>
>>      >> >http://ml.libwebsockets.org/mailman/listinfo/libwebsockets
>>      >>
>>      >>
>>
>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://libwebsockets.org/pipermail/libwebsockets/attachments/20140422/254a94e7/attachment-0001.html>


More information about the Libwebsockets mailing list