int remove_wsi_socket_from_fds(struct libwebsocket_context *context, struct libwebsocket *wsi) { int m; struct libwebsocket_pollargs pa = { wsi->sock, 0, 0 }; lws_libev_io(context, wsi, LWS_EV_STOP | LWS_EV_READ | LWS_EV_WRITE); --context->fds_count; #if !defined(_WIN32) && !defined(MBED_OPERATORS) if (wsi->sock > context->max_fds) { lwsl_err("Socket fd %d too high (%d)\n", wsi->sock, context->max_fds); return 1; } #endif lwsl_info("%s: wsi=%p, sock=%d, fds pos=%d\n", __func__, wsi, wsi->sock, wsi->position_in_fds_table); 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 */ /* have the last guy take up the vacant slot */ context->fds[m] = context->fds[context->fds_count]; lws_plat_delete_socket_from_fds(context, wsi, m); /* * end guy's fds_lookup entry remains unchanged * (still same fd pointing to same wsi) */ /* end guy's "position in fds table" changed */ wsi_from_fd(context,context->fds[context->fds_count].fd)-> position_in_fds_table = m; /* deletion guy's lws_lookup entry needs nuking */ delete_from_fd(context,wsi->sock); /* removed wsi has no position any more */ wsi->position_in_fds_table = -1; /* remove also from external POLL support via protocol 0 */ if (lws_socket_is_valid(wsi->sock)) { if (context->protocols[0].callback(context, wsi, LWS_CALLBACK_DEL_POLL_FD, wsi->user_space, (void *) &pa, 0)) return -1; } if (context->protocols[0].callback(context, wsi, LWS_CALLBACK_UNLOCK_POLL, wsi->user_space, (void *) &pa, 0)) return -1; return 0; }
int remove_wsi_socket_from_fds(struct lws *wsi) { struct lws_pollargs pa = { wsi->sock, 0, 0 }; #ifndef LWS_NO_SERVER struct lws_pollargs pa1; #endif struct lws_context *context = wsi->context; struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi]; struct lws *end_wsi; int m, ret = 0; #if !defined(_WIN32) && !defined(MBED_OPERATORS) if (wsi->sock > context->max_fds) { lwsl_err("fd %d too high (%d)\n", wsi->sock, context->max_fds); return 1; } #endif if (context->protocols[0].callback(wsi, LWS_CALLBACK_LOCK_POLL, wsi->user_space, (void *)&pa, 1)) return -1; lws_libev_io(wsi, LWS_EV_STOP | LWS_EV_READ | LWS_EV_WRITE); lws_pt_lock(pt); lwsl_info("%s: wsi=%p, sock=%d, fds pos=%d, end guy pos=%d, endfd=%d\n", __func__, wsi, wsi->sock, wsi->position_in_fds_table, pt->fds_count, pt->fds[pt->fds_count].fd); /* the guy who is to be deleted's slot index in pt->fds */ m = wsi->position_in_fds_table; /* have the last guy take up the now vacant slot */ pt->fds[m] = pt->fds[pt->fds_count - 1]; lws_plat_delete_socket_from_fds(context, wsi, m); /* end guy's "position in fds table" is now the deletion guy's old one */ end_wsi = wsi_from_fd(context, pt->fds[pt->fds_count].fd); assert(end_wsi); end_wsi->position_in_fds_table = m; /* deletion guy's lws_lookup entry needs nuking */ delete_from_fd(context, wsi->sock); /* removed wsi has no position any more */ wsi->position_in_fds_table = -1; /* remove also from external POLL support via protocol 0 */ if (lws_socket_is_valid(wsi->sock)) if (context->protocols[0].callback(wsi, LWS_CALLBACK_DEL_POLL_FD, wsi->user_space, (void *) &pa, 0)) ret = -1; #ifndef LWS_NO_SERVER /* if this made some room, accept connects on this thread */ if ((unsigned int)pt->fds_count < context->fd_limit_per_thread - 1) _lws_change_pollfd(pt->wsi_listening, 0, LWS_POLLIN, &pa1); #endif lws_pt_unlock(pt); if (context->protocols[0].callback(wsi, LWS_CALLBACK_UNLOCK_POLL, wsi->user_space, (void *) &pa, 1)) ret = -1; return ret; }