[Libwebsockets] Wrong timing in test-server.c

Timo Korthals tkorthals at cit-ec.uni-bielefeld.de
Thu Jun 26 10:53:33 CEST 2014

Hi there,

we are using your " test-server.c" as a template and noticed a strange 
double call of the callback functions.
If you look close at line 902-915 you find the minor bug:
You use the function "gettimeofday()" which holds the seconds and 
Unfortunately you use the microseconds to check, if you run out of time.
The member "tv_usec" only counts till 1000000, which is exactly one 
second, and then starts over.
this leads to the fact, that (tv.tv_usec - oldus) becomes negative, and 
therefore very big because of the uint cast.

So you do two callbacks.
- One, if your difference becomes naturally greater then the given value.
- Second, if tv.tv_usec starts over.

We are using chrono like this :
while (n >= 0 && !force_exit) {

          * This provokes the LWS_CALLBACK_SERVER_WRITEABLE for every
          * live websocket connection using the DUMB_INCREMENT protocol,
          * as soon as it can take more packets (usually immediately)

         std::chrono::high_resolution_clock::time_point t2 = 
         std::chrono::duration<float> time_span = 
std::chrono::duration_cast<std::chrono::duration<float>>(t2 - t1);

         // Check if the time difference is bigger than the value in 
seconds, then execute the callbacks
         if (time_span.count() > 0.1) {
             if(frameReceived == true){
                           frameReceived = false;
             t1 = std::chrono::high_resolution_clock::now();



Timo Korthals, M.Sc.
Universität Bielefeld
AG Kognitronik & Sensorik
Exzellenzcluster Cognitive Interaction Technology (CITEC)
Inspiration 1 (Zehlendorfer Damm 199)
33619 Bielefeld - Germany

Office  : 3.037
Phone   : +49 521 106-67368
eMail   : tkorthals at cit-ec.uni-bielefeld.de
Internet: http://www.ks.cit-ec.uni-bielefeld.de/

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://libwebsockets.org/pipermail/libwebsockets/attachments/20140626/a062fe67/attachment.html>

More information about the Libwebsockets mailing list