[Libwebsockets] Synchronisation of objects when using uv timer_start in LWS_CALLBACK_PROTOCOL_INIT

Meir Yanovich meiry242 at gmail.com
Sun Jul 31 19:37:09 CEST 2016


Looking at the protocol_example_standalone.c
there this code :
----------------------------------------------------------------------------------
static void
uv_timeout_cb_dumb_increment(uv_timer_t *w
#if UV_VERSION_MAJOR == 0
, int status
#endif
)
{
struct per_vhost_data__dumb_increment *vhd = lws_container_of(w,
struct per_vhost_data__dumb_increment, timeout_watcher);
lws_callback_on_writable_all_protocol_vhost(vhd->vhost, vhd->protocol);
}


what is : lws_container_of
and lws_callback_on_writable_all_protocol_vhost
does those or similar methods help to sync the session data ?

On Sun, Jul 31, 2016 at 8:24 PM, Meir Yanovich <meiry242 at gmail.com> wrote:

> Hello
> first of all i can't post full source code here. so i will try to explain
> the problem as best as i can
>
> I receiving WS calls on high rate , i capture them and process them .
> now i have timer which invokes every 100ms , on this timer callback i
> handle data caming from the session_data .
> the problem is that the values i receive are wrong .is missing.
> probably  this is , i guess because the high rate of the WS calls i
> receive from the client .
> The question is , how do i handle the session_data to sync , isn't in
> async server they suppose to stack until they served ?
>
>
> #include "game_handler.h"
> #include "simplog.h"
>
>
> extern int debug_level;
> connection_num_as_id = 0;
> Hashmap *users_map_main = NULL;
> //Array *gems_array = NULL;
> uv_timer_t timeout_watcher;
>
>
> //response to every 100ms   loop tick
> static bool response_to_client_cp(void* key, void* value, void* context)
> {
> struct per_session_data__apigataway *pss =
> (struct per_session_data__apigataway *)hashmapGet(users_map_main, (char
> *)key);
>         // HERE the values are not as expected
> int s = pss->status;
>
> return true;
> }
>
> static void game_loop_cb(uv_timer_t* handle) {
>
> ASSERT(handle != NULL);
> ASSERT(1 == uv_is_active((uv_handle_t*)handle));
>
>
> if (hashmapSize(users_map_main)>0)
> {
> hashmapForEach(users_map_main, &response_to_client_cp, users_map_main);
> }
>
>
> }
>
> int callback_wsapi(struct lws *wsi, enum lws_callback_reasons reason,
> void *user, void *in, size_t len)
> {
> if (users_map_main == NULL)
> {
> users_map_main = hashmapCreate(10, str_hash_fn, str_eq);
> }
> //char *resp_json;
> unsigned char response_to_client[LWS_PRE + 1024];
> struct per_session_data__apigataway *pss =
> (struct per_session_data__apigataway *)user;
> unsigned char *p_response_to_clientout = &response_to_client[LWS_PRE];
> int n;
>   switch (reason) {
> 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;
> }
> case LWS_CALLBACK_ESTABLISHED:
> {
>
>
> break;
> }
> case LWS_CALLBACK_SERVER_WRITEABLE:
> {
>
>        struct per_session_data__apigataway *pss  =
> hashmapPut(users_map_main, pss->player_id, pss);
>
>
> break;
> }
> default:
> lwsl_notice("Invalid status \n");
>
> }
> break;
> }
> case LWS_CALLBACK_RECEIVE:
> {
> if (len < 1)
> {
> break;
> }
> pss->binary = lws_frame_is_binary(wsi);
>
> memcpy(&pss->request_from_client_buf, in, len);
> >request_from_client_buf);
> pss->recive_all_from_client = 1;
> //Only invoke callback back to client when baby client is ready to eat
> lws_callback_on_writable(wsi);
> break;
> }
>
> case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION:
>
> break;
>
> case LWS_CALLBACK_WS_PEER_INITIATED_CLOSE:
> {
> break;
>
> }
>
> default:
> break;
> }
>
> return 0;
> }
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://libwebsockets.org/pipermail/libwebsockets/attachments/20160731/d399f5c9/attachment-0001.html>


More information about the Libwebsockets mailing list