How to pass global user defined structures to each callbacks libwebsockets C -
im not sure if related lws can't find way pass global structure holds values between callbacks . simple story have simple hashtable in c https://github.com/cgjones/android-system-core/blob/master/libcutils/hashmap.c
i try explain in example : have main :
//here define global hashmap *users_map; static struct lws_protocols protocols[] = { { "wsapi", callback_wsapi, sizeof(struct per_session_data__apigataway), 128, } , { null, null, 0, 0 } /* terminator */ }; int main(int argc, char **argv) { struct lws_context_creation_info info; //here init hash map users_map = hashmapcreate(10, str_hash_fn, str_eq); memset(&info, 0, sizeof info); info.port = server_port; info.protocols = protocols; ... info.options = opts | lws_server_option_libuv; context = lws_create_context(&info); if (lws_uv_initloop(context, null, 0)) { lwsl_err("lws_uv_initloop failed\n"); goto bail; } uv_timer_init(lws_uv_getloop(context, 0), &timeout_watcher); uv_timer_start(&timeout_watcher, main_loop_count_callback, 1000, 1000); lws_libuv_run(context, 0); return 0; }
and callback_wsapi c file removed allot of code show important stuff
//here set extern visible extern hashmap *users_map; int callback_iogame(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len) { unsigned char out[lws_pre + 512]; struct per_session_data__apigataway *pss = (struct per_session_data__apigataway *)user; switch (reason) { case lws_callback_established: break; case lws_callback_server_writeable: { //here lossing scope , hashmap not initialized int bfor2 = hashmapsize(users_map); break; } case lws_callback_receive: { char* client_req_str; client_req_str = (char*)in; if (strncmp((const char *)client_req_str, "player\n",6) == 0) { //on first request clinet works int bfor = hashmapsize(users_map); hashmapput(users_map, pss->id, pss); int after = hashmapsize(users_map); } //only invoke callback client when baby client ready eat lws_callback_on_writable(wsi); break; } case lws_callback_filter_protocol_connection: break; case lws_callback_ws_peer_initiated_close: break; default: break; }
so can hashmap in first request when gets : lws_callback_receive
losing scope .
questions :
1. how can make hashmap global callbacks ? supposed hold server total users .
Comments
Post a Comment