1

im not sure if this related to lws but i just can't find way to pass global structure which holds its values between the callbacks . the simple story is that i have simple hashtable in C https://github.com/cgjones/android-system-core/blob/master/libcutils/hashmap.c

i try to explain in the example : i have the main :

//HERE I DEFINE IT AS 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 i init the 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 this is the callback_wsapi C file i removed allot of code just to show the important stuff

//HERE I SET IT AS EXTERN SO IT WILL BE VISIBLE TO ALL 
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 IT LOSSING SCOPE AND THE 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 THE FIRST REQUEST FROM THE CLINET IT WORKS 
                int bfor  = hashmapSize(users_map);
                hashmapPut(users_map, pss->id, pss);
                int after = hashmapSize(users_map);
        }
        //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;
    }

So i can get the hashmap only in the first request when it gets to : LWS_CALLBACK_RECEIVE
then it just losing scope .

Questions :
1. How can i make this hashmap global to the callbacks ? it supposed to hold the server total users .

user63898
  • 29,839
  • 85
  • 272
  • 514
  • 1
    What do you mean by "then it just losing scope"? A global variable can't go out of scope, and its lifetime is the lifetime of the program. – Some programmer dude Jun 12 '16 at 16:30
  • Does the real code define more global stuff, typically defined in the same module as the `user_map` pointer? – alk Jun 12 '16 at 16:39
  • yes there are primitive global variables which are set once and i can get the values form other files : like in main : int debug_level = 7; and in other files it is : extern int debug_level; – user63898 Jun 12 '16 at 16:58

0 Answers0