[Libwebsockets] How to get wsi reference when using uv_timer_start and libuv

Andy Green andy at warmcat.com
Tue Aug 2 20:08:34 CEST 2016



On August 2, 2016 9:17:51 PM GMT+08:00, Meir Yanovich <meiry242 at gmail.com> wrote:
>Hello
>im trying to use uv_timer_start in the test-server-mirror.c , the timer
>will triggered every
>100 ms
>to send the response back to the clients , the problem is how to get
>the wsi of the current session
>in the uv_timer_start callback
>
>This is what i Intend to do :
>i removed the from case LWS_CALLBACK_RECEIVE:
>the call to lws_callback_on_writable_all_protocol as suggested by Andy
>and
>moved it to the tmer callback
>
>
>static void game_loop_cb(uv_timer_t* handle
>#if UV_VERSION_MAJOR == 0
>    , int status
>#endif
>    ) {
>
>    ASSERT(handle != NULL);
>    ASSERT(1 == uv_is_active((uv_handle_t*)handle));
>    if (ringbuffer_head >-1)
>    {
>        if (((ringbuffer_head - pss->ringbuffer_tail) &
>            (MAX_MESSAGE_QUEUE - 1)) != (MAX_MESSAGE_QUEUE - 2))
>        {
>        lws_callback_on_writable_all_protocol(lws_get_context(wsi),  //
><--- THIS IS THE problem how can i get the wsi of the current session ?
>pointer what ? other lws method ?

Look at how the dumb increment plugin does it...

https://github.com/warmcat/libwebsockets/blob/master/plugins/protocol_dumb_increment.c

 - you don't need any wsi in the timer callback.  Store the vhost pointer in your per-vhost struct when initializing the protocol.

 - compose the uv_timer_t as a member of the per-vhost struct (it doesn't need to be the first member)

 - when the timer fires, he calls his callback function with a pointer to the uv_timer_t: use the container_of() macro to do the pointer arithmetic to find the address of the per-vhost struct we know that uv_timer_t lives inside.

 - enjoy access to whatever you put in your per-vhost struct from inside the uv timer callback (in this case, a pointer to the vhost is desirable)

-Andy

>                lws_get_protocol(wsi));
>        }
>    }
>
>}
>
>
>
>Thanks
>
>
>case LWS_CALLBACK_PROTOCOL_INIT:
>    {
>
>        uv_timer_init(lws_uv_getloop(lws_get_context(wsi), 0),
>&timeout_watcher);
>        //every 100ms
>        uv_timer_start(&timeout_watcher, game_loop_cb, 1000, 100);
>        break;
>    }
>
>
>------------------------------------------------------------------------
>
>_______________________________________________
>Libwebsockets mailing list
>Libwebsockets at ml.libwebsockets.org
>http://libwebsockets.org/mailman/listinfo/libwebsockets




More information about the Libwebsockets mailing list