[Libwebsockets] R: Question about timer

Andy Green andy at warmcat.com
Thu Jul 25 01:30:58 CEST 2019

On 7/24/19 9:49 AM, Stefano Mora wrote:
> Ok, thanks a lot!
> I this new way do I need to setup a timer for each client or only one 
> for everyone?

There is only one timer per vhost - protocol combination.

> The generated callback is LWS_CALLBACK_TIMER or LWS_CALLBACK_USER (eg) ?

You set the reason you will be called back with when you set the time 
you want the callback.


> Regards
> ------------------------------------------------------------------------
> *Da:* Andy Green <andy at warmcat.com>
> *Inviato:* mercoledì 24 luglio 2019 17:34
> *A:* libwebsockets at ml.libwebsockets.org 
> <libwebsockets at ml.libwebsockets.org>; Stefano Mora 
> <stefano.mora at newdep.com>; libwebsockets at ml.libwebsockets.org 
> <libwebsockets at ml.libwebsockets.org>
> *Oggetto:* Re: [Libwebsockets] Question about timer
> On July 24, 2019 7:57:29 AM PDT, Stefano Mora <stefano.mora at newdep.com> 
> wrote:
>>Hi all,
>>in my application I start the timer with:
>>   lws_set_timer_usecs( wsi, TIMER_USECS);
>>and this is ok.
>>A second client connects the server and, since it is running, it do not
>>start another timer.
>>This is ok and the timer continues to flow thanks to:
>> my_timer_handler();
>> lwsl_user("LWS_CB_TIMER t:%d\n", engineData.timer);
>> lws_set_timer_usecs( wsi, engineData.timer ? TIMER_USECS : -1);
>> break;
>>But, when a client disconnects the TIMER 'reason' stops to coming (the
>>last engineData.timer I see is 1) and the other client is running
>>without the timer.
>>I'm pretty sure I do not disable the timer.
>>I my understanding the timer is a server-wide object, I mean it not
>>referred to a particular client, is it?
> No... this "hi res" timer is available for each wsi... there is actually 
> a single sorted queue of pending timers on each pt... this is used to 
> restrict the sleep time for the event loop wait to that of the earliest 
> pending timer.
> But any wsi can participate on it independently.  Each wsi only sees 
> hires timer callbacks that he personally asked for, hence the api takes 
> the wsi as the argument.
> There's a separate second-resolution timer that belongs to the vhost - 
> protocol combination instead of each wsi that sounds more like what you want
> https://libwebsockets.org/git/libwebsockets/tree/include/libwebsockets/lws-timeout-timer.h#n140-163
> Using that api either wsi instance will be setting a timer that will 
> fire independent of their own lifecycle.  Note that because of that, 
> when it fires and calls back with 'reason' (eg, LWS_CALLBACK_USER) it is 
> called with a fake wsi that only has context, vhost and protocol set on 
> it.  By then, there may no longer be any live wsis using it, but that 
> won't stop it coming.  So it's useful for eg, client connect retries etc.
>>Any idea why the timer stops?
> IIUI it's correct, you want the other timer api.
> -Andy
>>Thanks, regards
>>Stefano Mora / SW Engineer
>>stefano.mora at newdep.com<mailto:stefano.mora at newdep.com>
>>Office: +39 0521  1756855/ Fax: +39 0521 631414
>>Sede legale ed operativa: Via Monte Aquila, 2/B - 43124 Parma - ITALY
>>Website: http://www.newdep.com<http://www.newdep.com/>
>>Il presente messaggio, corredato dei relativi allegati, contiene
>>informazioni strettamente riservate ai sensi del D.L.gs. 196/2003 ed è
>>destinato esclusivamente ai destinatari. Chiunque ricevesse questo
>>messaggio per errore è pregato di darne gentilmente comunicazione al
>>mittente, di non copiarlo, divulgarlo o distribuirlo, nonchè di
>>This communication may contain confidential and/or otherwise
>>proprietary material and is thus for use only by the intended
>>recipient. If you received this in error, please contact the sender and
>>delete the e-mail and its attachments from all computers.

More information about the Libwebsockets mailing list