[Libwebsockets] struct lws_context_creation_info user is not reachable in LWS_CALLBACK_WS_PEER_INITIATED_CLOSE and LWS_CALLBACK_SERVER_WRITEABLE

Meir Yanovich meiry242 at gmail.com
Fri Jun 17 23:43:05 CEST 2016


Hello
im realy confused , i try to pass user data which is simple hash map
implementation
in my main : ( i tried with or without the static )

static Hashmap *users_map_main;

int main(int argc, char **argv)
{
struct lws_context_creation_info info;
const char *iface = NULL;
int opts = 0;
int server_port = 3000;

users_map_main = hashmapCreate(10, str_hash_fn, str_eq);

memset(&info, 0, sizeof info);
...
...
info.user = users_map_main;
...
...
}

-----------------------------------------------------------------------------------------------------------
Then in the callback C file the user data passed just fine on
: LWS_CALLBACK_ESTABLISHED and on LWS_CALLBACK_RECEIVE
so i know that my user  data did pass throw

but the wired thing is that latter on the same connection single user
connected on LWS_CALLBACK_SERVER_WRITEABLE

and when user close connection LWS_CALLBACK_WS_PEER_INITIATED_CLOSE

the user data pointer is gone and it is not pointing any where .

this is my shortened callback file :

------------------------------------------------------------------------------------------------------------
extern int debug_level;
static list_declare(users);
connection_num_as_id = 0;
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__iogame *pss =
(struct per_session_data__iogame *)user;
unsigned char *response_string_out = &out[LWS_PRE];
int n;
int m;

switch (reason) {

case LWS_CALLBACK_ESTABLISHED:
users_map = (Hashmap*)lws_context_user(lws_get_context(wsi));
int after = hashmapSize(users_map);
pss->recive_all_from_client = 0;
pss->wsi_user = malloc(sizeof pss->wsi_user);
pss->wsi_user = wsi;
connection_num_as_id++;

break;

case LWS_CALLBACK_SERVER_WRITEABLE:
{
cJSON *root;
char *resp_json;
int n, m;
if (pss->recive_all_from_client == 1)
{

  root = cJSON_CreateObject();
cJSON_AddItemToObject(root, "name", cJSON_CreateString(pss->name));
resp_json = cJSON_Print(root);
n = sprintf((char *)response_string_out, "%s", resp_json);
m = lws_write(pss->wsi_user/*wsi*/, response_string_out, n, LWS_WRITE_TEXT);
if (m < n) {
lwsl_err("ERROR %d writing to the socket\n", sizeof(out));
return -1;
}
}
break;
}
case LWS_CALLBACK_RECEIVE:
{
char* client_req_str;

cJSON *root;
char *resp_json;

if (len <=0)
{
break;
}
client_req_str = (char*)in;
cJSON * user_root = cJSON_Parse(client_req_str);
cJSON * user_type = cJSON_GetObjectItem(user_root, "type");
cJSON * user_name = cJSON_GetObjectItem(user_root, "name");
if (strncmp((const char *)user_type->valuestring, "player\n", 6) == 0)
{
pss->recive_all_from_client = 1;
pss->name = malloc(1 + strlen(user_name->valuestring));
strcpy(pss->name, user_name->valuestring);
int bfor  = hashmapSize(users_map);
hashmapPut(users_map, pss->id, pss);
int after = hashmapSize(users_map);
int stop = 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:
{
users_map = (Hashmap*)lws_context_user(lws_get_context(wsi));
int after = hashmapSize(users_map);
int after = hashmapSize(users_map);
hashmapRemove(users_map, pss->id);
break;
}
case LWS_CALLBACK_CLOSED:
{
lwsl_notice("LWS_CALLBACK_CLOSED: len %d\n",
len);
for (n = 0; n < (int)len; n++)
lwsl_notice(" %d: 0x%02X\n", n,
((unsigned char *)in)[n]);
break;
}
default:
break;
}

return 0;
}


Thanks!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://libwebsockets.org/pipermail/libwebsockets/attachments/20160618/afbce220/attachment.html>


More information about the Libwebsockets mailing list