1
0
Fork 0
mirror of https://github.com/warmcat/libwebsockets.git synced 2025-03-09 00:00:04 +01:00

improve callback close checking

Signed-off-by: Andy Green <andy.green@linaro.org>
This commit is contained in:
Andy Green 2015-10-15 07:39:33 +08:00
parent f54715bd5f
commit 1963c9aa7f
5 changed files with 41 additions and 26 deletions

View file

@ -152,7 +152,8 @@ struct libwebsocket *libwebsocket_client_connect_2(
wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_CONNECT;
lws_libev_accept(context, wsi, wsi->sock);
insert_wsi_socket_into_fds(context, wsi);
if (insert_wsi_socket_into_fds(context, wsi))
goto oom4;
libwebsocket_set_timeout(wsi,
PENDING_TIMEOUT_AWAITING_CONNECT_RESPONSE,

View file

@ -236,6 +236,7 @@ just_kill_connection:
lws_ssl_remove_wsi_from_buffered_list(context, wsi);
// checking return redundant since we anyway close
remove_wsi_socket_from_fds(context, wsi);
wsi->state = WSI_STATE_DEAD_SOCKET;

View file

@ -46,9 +46,9 @@ insert_wsi_socket_into_fds(struct libwebsocket_context *context,
lwsl_info("insert_wsi_socket_into_fds: wsi=%p, sock=%d, fds pos=%d\n",
wsi, wsi->sock, context->fds_count);
context->protocols[0].callback(context, wsi,
LWS_CALLBACK_LOCK_POLL,
wsi->user_space, (void *) &pa, 0);
if (context->protocols[0].callback(context, wsi,
LWS_CALLBACK_LOCK_POLL, wsi->user_space, (void *) &pa, 0))
return -1;
insert_wsi(context, wsi);
wsi->position_in_fds_table = context->fds_count;
@ -58,13 +58,13 @@ insert_wsi_socket_into_fds(struct libwebsocket_context *context,
lws_plat_insert_socket_into_fds(context, wsi);
/* external POLL support via protocol 0 */
context->protocols[0].callback(context, wsi,
LWS_CALLBACK_ADD_POLL_FD,
wsi->user_space, (void *) &pa, 0);
if (context->protocols[0].callback(context, wsi,
LWS_CALLBACK_ADD_POLL_FD, wsi->user_space, (void *) &pa, 0))
return -1;
context->protocols[0].callback(context, wsi,
LWS_CALLBACK_UNLOCK_POLL,
wsi->user_space, (void *)&pa, 0);
if (context->protocols[0].callback(context, wsi,
LWS_CALLBACK_UNLOCK_POLL, wsi->user_space, (void *)&pa, 0))
return -1;
return 0;
}
@ -91,9 +91,9 @@ remove_wsi_socket_from_fds(struct libwebsocket_context *context,
lwsl_info("%s: wsi=%p, sock=%d, fds pos=%d\n", __func__,
wsi, wsi->sock, wsi->position_in_fds_table);
context->protocols[0].callback(context, wsi,
LWS_CALLBACK_LOCK_POLL,
wsi->user_space, (void *)&pa, 0);
if (context->protocols[0].callback(context, wsi,
LWS_CALLBACK_LOCK_POLL, wsi->user_space, (void *)&pa, 0))
return -1;
m = wsi->position_in_fds_table; /* replace the contents for this */
@ -116,13 +116,16 @@ remove_wsi_socket_from_fds(struct libwebsocket_context *context,
/* remove also from external POLL support via protocol 0 */
if (wsi->sock) {
context->protocols[0].callback(context, wsi,
if (context->protocols[0].callback(context, wsi,
LWS_CALLBACK_DEL_POLL_FD, wsi->user_space,
(void *) &pa, 0);
(void *) &pa, 0))
return -1;
}
context->protocols[0].callback(context, wsi,
if (context->protocols[0].callback(context, wsi,
LWS_CALLBACK_UNLOCK_POLL,
wsi->user_space, (void *) &pa, 0);
wsi->user_space, (void *) &pa, 0))
return -1;
return 0;
}
@ -145,15 +148,17 @@ lws_change_pollfd(struct libwebsocket *wsi, int _and, int _or)
pfd = &context->fds[wsi->position_in_fds_table];
pa.fd = wsi->sock;
context->protocols[0].callback(context, wsi,
LWS_CALLBACK_LOCK_POLL, wsi->user_space, (void *) &pa, 0);
if (context->protocols[0].callback(context, wsi,
LWS_CALLBACK_LOCK_POLL, wsi->user_space, (void *) &pa, 0))
return -1;
pa.prev_events = pfd->events;
pa.events = pfd->events = (pfd->events & ~_and) | _or;
context->protocols[0].callback(context, wsi,
if (context->protocols[0].callback(context, wsi,
LWS_CALLBACK_CHANGE_MODE_POLL_FD,
wsi->user_space, (void *) &pa, 0);
wsi->user_space, (void *) &pa, 0))
return -1;
/*
* if we changed something in this pollfd...
@ -173,13 +178,16 @@ lws_change_pollfd(struct libwebsocket *wsi, int _and, int _or)
if (sampled_tid) {
tid = context->protocols[0].callback(context, NULL,
LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
if (tid == -1)
return -1;
if (tid != sampled_tid)
libwebsocket_cancel_service(context);
}
}
context->protocols[0].callback(context, wsi,
LWS_CALLBACK_UNLOCK_POLL, wsi->user_space, (void *) &pa, 0);
if (context->protocols[0].callback(context, wsi,
LWS_CALLBACK_UNLOCK_POLL, wsi->user_space, (void *) &pa, 0))
return -1;
return 0;
}

View file

@ -119,7 +119,10 @@ int lws_context_init_server(struct lws_context_creation_info *info,
wsi->sock = sockfd;
wsi->mode = LWS_CONNMODE_SERVER_LISTENER;
insert_wsi_socket_into_fds(context, wsi);
if (insert_wsi_socket_into_fds(context, wsi)) {
compatible_close(sockfd);
return 1;
}
context->listen_service_modulo = LWS_LISTEN_SERVICE_MODULO;
context->listen_service_count = 0;
@ -825,7 +828,8 @@ try_pollout:
lwsl_debug("accepted new conn port %u on fd=%d\n",
ntohs(cli_addr.sin_port), accept_fd);
insert_wsi_socket_into_fds(context, new_wsi);
if (insert_wsi_socket_into_fds(context, new_wsi))
goto fail;
}
break;

View file

@ -579,7 +579,8 @@ lws_server_socket_service_ssl(struct libwebsocket_context *context,
*pwsi = new_wsi;
wsi = *pwsi;
wsi->mode = LWS_CONNMODE_SSL_ACK_PENDING;
insert_wsi_socket_into_fds(context, wsi);
if (insert_wsi_socket_into_fds(context, wsi))
goto fail;
libwebsocket_set_timeout(wsi, PENDING_TIMEOUT_SSL_ACCEPT,
AWAITING_TIMEOUT);