2017-12-21 03:09:18 +01:00
|
|
|
/*
|
|
|
|
* libwebsockets - small server side websockets and web server implementation
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010-2017 Andy Green <andy@warmcat.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation:
|
|
|
|
* version 2.1 of the License.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
* MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2018-06-07 13:33:24 +02:00
|
|
|
#include "core/private.h"
|
2017-12-21 03:09:18 +01:00
|
|
|
|
|
|
|
int
|
|
|
|
_lws_change_pollfd(struct lws *wsi, int _and, int _or, struct lws_pollargs *pa)
|
|
|
|
{
|
2018-06-07 13:33:24 +02:00
|
|
|
#if !defined(LWS_WITH_LIBUV) && !defined(LWS_WITH_LIBEV) && !defined(LWS_WITH_LIBEVENT)
|
|
|
|
volatile struct lws_context_per_thread *vpt;
|
|
|
|
#endif
|
2017-12-21 03:09:18 +01:00
|
|
|
struct lws_context_per_thread *pt;
|
|
|
|
struct lws_context *context;
|
|
|
|
int ret = 0, pa_events = 1;
|
|
|
|
struct lws_pollfd *pfd;
|
|
|
|
int sampled_tid, tid;
|
|
|
|
|
2018-06-07 13:33:24 +02:00
|
|
|
if (!wsi)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
assert(wsi->position_in_fds_table == LWS_NO_FDS_POS ||
|
|
|
|
wsi->position_in_fds_table >= 0);
|
|
|
|
|
|
|
|
if (wsi->position_in_fds_table == LWS_NO_FDS_POS)
|
2017-12-21 03:09:18 +01:00
|
|
|
return 0;
|
|
|
|
|
2018-06-07 13:33:24 +02:00
|
|
|
if (((volatile struct lws *)wsi)->handling_pollout &&
|
|
|
|
!_and && _or == LWS_POLLOUT) {
|
2017-12-21 03:09:18 +01:00
|
|
|
/*
|
|
|
|
* Happening alongside service thread handling POLLOUT.
|
|
|
|
* The danger is when he is finished, he will disable POLLOUT,
|
|
|
|
* countermanding what we changed here.
|
|
|
|
*
|
|
|
|
* Instead of changing the fds, inform the service thread
|
|
|
|
* what happened, and ask it to leave POLLOUT active on exit
|
|
|
|
*/
|
2018-06-07 13:33:24 +02:00
|
|
|
((volatile struct lws *)wsi)->leave_pollout_active = 1;
|
2017-12-21 03:09:18 +01:00
|
|
|
/*
|
|
|
|
* by definition service thread is not in poll wait, so no need
|
|
|
|
* to cancel service
|
|
|
|
*/
|
|
|
|
|
|
|
|
lwsl_debug("%s: using leave_pollout_active\n", __func__);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
context = wsi->context;
|
|
|
|
pt = &context->pt[(int)wsi->tsi];
|
2018-06-07 13:33:24 +02:00
|
|
|
|
|
|
|
assert(wsi->position_in_fds_table < (int)pt->fds_count);
|
|
|
|
|
|
|
|
#if !defined(LWS_WITH_LIBUV) && \
|
|
|
|
!defined(LWS_WITH_LIBEV) && \
|
|
|
|
!defined(LWS_WITH_LIBEVENT)
|
|
|
|
/*
|
|
|
|
* This only applies when we use the default poll() event loop.
|
|
|
|
*
|
|
|
|
* BSD can revert pa->events at any time, when the kernel decides to
|
|
|
|
* exit from poll(). We can't protect against it using locking.
|
|
|
|
*
|
|
|
|
* Therefore we must check first if the service thread is in poll()
|
|
|
|
* wait; if so, we know we must be being called from a foreign thread,
|
|
|
|
* and we must keep a strictly ordered list of changes we made instead
|
|
|
|
* of trying to apply them, since when poll() exits, which may happen
|
|
|
|
* at any time it would revert our changes.
|
|
|
|
*
|
|
|
|
* The plat code will apply them when it leaves the poll() wait
|
|
|
|
* before doing anything else.
|
|
|
|
*/
|
|
|
|
|
|
|
|
vpt = (volatile struct lws_context_per_thread *)pt;
|
|
|
|
|
|
|
|
vpt->foreign_spinlock = 1;
|
|
|
|
lws_memory_barrier();
|
|
|
|
|
|
|
|
if (vpt->inside_poll) {
|
|
|
|
struct lws_foreign_thread_pollfd *ftp, **ftp1;
|
|
|
|
/*
|
|
|
|
* We are certainly a foreign thread trying to change events
|
|
|
|
* while the service thread is in the poll() wait.
|
|
|
|
*
|
|
|
|
* Create a list of changes to be applied after poll() exit,
|
|
|
|
* instead of trying to apply them now.
|
|
|
|
*/
|
|
|
|
ftp = lws_malloc(sizeof(*ftp), "ftp");
|
|
|
|
if (!ftp) {
|
|
|
|
vpt->foreign_spinlock = 0;
|
|
|
|
lws_memory_barrier();
|
|
|
|
ret = -1;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
ftp->_and = _and;
|
|
|
|
ftp->_or = _or;
|
|
|
|
ftp->fd_index = wsi->position_in_fds_table;
|
|
|
|
ftp->next = NULL;
|
|
|
|
|
|
|
|
/* place at END of list to maintain order */
|
|
|
|
ftp1 = (struct lws_foreign_thread_pollfd **)
|
|
|
|
&vpt->foreign_pfd_list;
|
|
|
|
while (*ftp1)
|
|
|
|
ftp1 = &((*ftp1)->next);
|
|
|
|
|
|
|
|
*ftp1 = ftp;
|
|
|
|
vpt->foreign_spinlock = 0;
|
|
|
|
lws_memory_barrier();
|
|
|
|
lws_cancel_service_pt(wsi);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
vpt->foreign_spinlock = 0;
|
|
|
|
lws_memory_barrier();
|
|
|
|
#endif
|
2017-12-21 03:09:18 +01:00
|
|
|
|
|
|
|
pfd = &pt->fds[wsi->position_in_fds_table];
|
|
|
|
pa->fd = wsi->desc.sockfd;
|
2019-03-06 01:07:13 +01:00
|
|
|
lwsl_debug("%s: wsi %p: fd %d events %d -> %d\n", __func__, wsi,
|
|
|
|
pa->fd, pfd->events, (pfd->events & ~_and) | _or);
|
2017-12-21 03:09:18 +01:00
|
|
|
pa->prev_events = pfd->events;
|
|
|
|
pa->events = pfd->events = (pfd->events & ~_and) | _or;
|
|
|
|
|
|
|
|
if (wsi->http2_substream)
|
|
|
|
return 0;
|
|
|
|
|
2018-06-07 13:33:24 +02:00
|
|
|
if (wsi->vhost &&
|
|
|
|
wsi->vhost->protocols[0].callback(wsi,
|
|
|
|
LWS_CALLBACK_CHANGE_MODE_POLL_FD,
|
|
|
|
wsi->user_space, (void *)pa, 0)) {
|
2017-12-21 03:09:18 +01:00
|
|
|
ret = -1;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
2018-06-07 13:33:24 +02:00
|
|
|
if (context->event_loop_ops->io) {
|
|
|
|
if (_and & LWS_POLLIN)
|
|
|
|
context->event_loop_ops->io(wsi,
|
|
|
|
LWS_EV_STOP | LWS_EV_READ);
|
|
|
|
|
|
|
|
if (_or & LWS_POLLIN)
|
|
|
|
context->event_loop_ops->io(wsi,
|
|
|
|
LWS_EV_START | LWS_EV_READ);
|
|
|
|
|
|
|
|
if (_and & LWS_POLLOUT)
|
|
|
|
context->event_loop_ops->io(wsi,
|
|
|
|
LWS_EV_STOP | LWS_EV_WRITE);
|
|
|
|
|
|
|
|
if (_or & LWS_POLLOUT)
|
|
|
|
context->event_loop_ops->io(wsi,
|
|
|
|
LWS_EV_START | LWS_EV_WRITE);
|
2017-12-21 03:09:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if we changed something in this pollfd...
|
|
|
|
* ... and we're running in a different thread context
|
|
|
|
* than the service thread...
|
|
|
|
* ... and the service thread is waiting ...
|
|
|
|
* then cancel it to force a restart with our changed events
|
|
|
|
*/
|
|
|
|
pa_events = pa->prev_events != pa->events;
|
|
|
|
|
|
|
|
if (pa_events) {
|
|
|
|
if (lws_plat_change_pollfd(context, wsi, pfd)) {
|
|
|
|
lwsl_info("%s failed\n", __func__);
|
|
|
|
ret = -1;
|
|
|
|
goto bail;
|
|
|
|
}
|
2019-03-06 01:07:13 +01:00
|
|
|
sampled_tid = pt->service_tid;
|
2018-06-07 13:33:24 +02:00
|
|
|
if (sampled_tid && wsi->vhost) {
|
2017-12-21 03:09:18 +01:00
|
|
|
tid = wsi->vhost->protocols[0].callback(wsi,
|
|
|
|
LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
|
|
|
|
if (tid == -1) {
|
|
|
|
ret = -1;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
if (tid != sampled_tid)
|
|
|
|
lws_cancel_service_pt(wsi);
|
|
|
|
}
|
|
|
|
}
|
2018-06-07 13:33:24 +02:00
|
|
|
|
2017-12-21 03:09:18 +01:00
|
|
|
bail:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef LWS_NO_SERVER
|
2018-06-07 13:33:24 +02:00
|
|
|
/*
|
|
|
|
* Enable or disable listen sockets on this pt globally...
|
|
|
|
* it's modulated according to the pt having space for a new accept.
|
|
|
|
*/
|
2017-12-21 03:09:18 +01:00
|
|
|
static void
|
2018-06-07 13:33:24 +02:00
|
|
|
lws_accept_modulation(struct lws_context *context,
|
|
|
|
struct lws_context_per_thread *pt, int allow)
|
2017-12-21 03:09:18 +01:00
|
|
|
{
|
|
|
|
struct lws_vhost *vh = context->vhost_list;
|
|
|
|
struct lws_pollargs pa1;
|
|
|
|
|
|
|
|
while (vh) {
|
2018-06-07 13:33:24 +02:00
|
|
|
if (vh->lserv_wsi) {
|
|
|
|
if (allow)
|
|
|
|
_lws_change_pollfd(vh->lserv_wsi,
|
2017-12-21 03:09:18 +01:00
|
|
|
0, LWS_POLLIN, &pa1);
|
2018-06-07 13:33:24 +02:00
|
|
|
else
|
|
|
|
_lws_change_pollfd(vh->lserv_wsi,
|
2017-12-21 03:09:18 +01:00
|
|
|
LWS_POLLIN, 0, &pa1);
|
2018-06-07 13:33:24 +02:00
|
|
|
}
|
2017-12-21 03:09:18 +01:00
|
|
|
vh = vh->vhost_next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int
|
2018-06-07 13:33:24 +02:00
|
|
|
__insert_wsi_socket_into_fds(struct lws_context *context, struct lws *wsi)
|
2017-12-21 03:09:18 +01:00
|
|
|
{
|
|
|
|
struct lws_pollargs pa = { wsi->desc.sockfd, LWS_POLLIN, 0 };
|
|
|
|
struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
|
|
|
|
lwsl_debug("%s: %p: tsi=%d, sock=%d, pos-in-fds=%d\n",
|
|
|
|
__func__, wsi, wsi->tsi, wsi->desc.sockfd, pt->fds_count);
|
|
|
|
|
|
|
|
if ((unsigned int)pt->fds_count >= context->fd_limit_per_thread) {
|
|
|
|
lwsl_err("Too many fds (%d vs %d)\n", context->max_fds,
|
|
|
|
context->fd_limit_per_thread );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-06-07 13:33:24 +02:00
|
|
|
#if !defined(_WIN32)
|
|
|
|
if (wsi->desc.sockfd - lws_plat_socket_offset() >= context->max_fds) {
|
|
|
|
lwsl_err("Socket fd %d is too high (%d) offset %d\n",
|
2019-03-06 01:07:13 +01:00
|
|
|
wsi->desc.sockfd, context->max_fds,
|
|
|
|
lws_plat_socket_offset());
|
2017-12-21 03:09:18 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
assert(wsi);
|
2018-06-07 13:33:24 +02:00
|
|
|
assert(wsi->event_pipe || wsi->vhost);
|
2017-12-21 03:09:18 +01:00
|
|
|
assert(lws_socket_is_valid(wsi->desc.sockfd));
|
|
|
|
|
2018-06-07 13:33:24 +02:00
|
|
|
if (wsi->vhost &&
|
|
|
|
wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_LOCK_POLL,
|
2017-12-21 03:09:18 +01:00
|
|
|
wsi->user_space, (void *) &pa, 1))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
pt->count_conns++;
|
|
|
|
insert_wsi(context, wsi);
|
2018-06-07 13:33:24 +02:00
|
|
|
wsi->position_in_fds_table = pt->fds_count;
|
2017-12-21 03:09:18 +01:00
|
|
|
|
|
|
|
pt->fds[wsi->position_in_fds_table].fd = wsi->desc.sockfd;
|
|
|
|
pt->fds[wsi->position_in_fds_table].events = LWS_POLLIN;
|
|
|
|
pa.events = pt->fds[pt->fds_count].events;
|
|
|
|
|
|
|
|
lws_plat_insert_socket_into_fds(context, wsi);
|
|
|
|
|
|
|
|
/* external POLL support via protocol 0 */
|
2018-06-07 13:33:24 +02:00
|
|
|
if (wsi->vhost &&
|
|
|
|
wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_ADD_POLL_FD,
|
2017-12-21 03:09:18 +01:00
|
|
|
wsi->user_space, (void *) &pa, 0))
|
|
|
|
ret = -1;
|
|
|
|
#ifndef LWS_NO_SERVER
|
|
|
|
/* if no more room, defeat accepts on this thread */
|
|
|
|
if ((unsigned int)pt->fds_count == context->fd_limit_per_thread - 1)
|
2018-06-07 13:33:24 +02:00
|
|
|
lws_accept_modulation(context, pt, 0);
|
2017-12-21 03:09:18 +01:00
|
|
|
#endif
|
|
|
|
|
2018-06-07 13:33:24 +02:00
|
|
|
if (wsi->vhost &&
|
|
|
|
wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_UNLOCK_POLL,
|
2017-12-21 03:09:18 +01:00
|
|
|
wsi->user_space, (void *)&pa, 1))
|
|
|
|
ret = -1;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2018-06-07 13:33:24 +02:00
|
|
|
__remove_wsi_socket_from_fds(struct lws *wsi)
|
2017-12-21 03:09:18 +01:00
|
|
|
{
|
|
|
|
struct lws_context *context = wsi->context;
|
|
|
|
struct lws_pollargs pa = { wsi->desc.sockfd, 0, 0 };
|
|
|
|
struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
|
|
|
|
struct lws *end_wsi;
|
|
|
|
int v;
|
|
|
|
int m, ret = 0;
|
|
|
|
|
2018-06-07 13:33:24 +02:00
|
|
|
#if !defined(_WIN32)
|
|
|
|
if (wsi->desc.sockfd - lws_plat_socket_offset() > context->max_fds) {
|
2017-12-21 03:09:18 +01:00
|
|
|
lwsl_err("fd %d too high (%d)\n", wsi->desc.sockfd,
|
|
|
|
context->max_fds);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-06-07 13:33:24 +02:00
|
|
|
if (wsi->vhost &&
|
|
|
|
wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_LOCK_POLL,
|
2017-12-21 03:09:18 +01:00
|
|
|
wsi->user_space, (void *)&pa, 1))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
lws_same_vh_protocol_remove(wsi);
|
|
|
|
|
|
|
|
/* the guy who is to be deleted's slot index in pt->fds */
|
|
|
|
m = wsi->position_in_fds_table;
|
|
|
|
|
2018-06-07 13:33:24 +02:00
|
|
|
/* these are the only valid possibilities for position_in_fds_table */
|
|
|
|
assert(m == LWS_NO_FDS_POS || (m >= 0 &&
|
|
|
|
(unsigned int)m < pt->fds_count));
|
2017-12-21 03:09:18 +01:00
|
|
|
|
2018-06-07 13:33:24 +02:00
|
|
|
if (context->event_loop_ops->io)
|
|
|
|
context->event_loop_ops->io(wsi,
|
|
|
|
LWS_EV_STOP | LWS_EV_READ | LWS_EV_WRITE |
|
|
|
|
LWS_EV_PREPARE_DELETION);
|
2017-12-21 03:09:18 +01:00
|
|
|
|
2019-03-06 01:07:13 +01:00
|
|
|
lwsl_debug("%s: wsi=%p, skt=%d, fds pos=%d, end guy pos=%d, endfd=%d\n",
|
2017-12-21 03:09:18 +01:00
|
|
|
__func__, wsi, wsi->desc.sockfd, wsi->position_in_fds_table,
|
|
|
|
pt->fds_count, pt->fds[pt->fds_count].fd);
|
|
|
|
|
2018-06-07 13:33:24 +02:00
|
|
|
if (m != LWS_NO_FDS_POS) {
|
|
|
|
|
|
|
|
/* have the last guy take up the now vacant slot */
|
|
|
|
pt->fds[m] = pt->fds[pt->fds_count - 1];
|
|
|
|
/* this decrements pt->fds_count */
|
|
|
|
lws_plat_delete_socket_from_fds(context, wsi, m);
|
2019-03-06 01:07:13 +01:00
|
|
|
pt->count_conns--;
|
2018-06-07 13:33:24 +02:00
|
|
|
v = (int) pt->fds[m].fd;
|
2019-03-06 01:07:13 +01:00
|
|
|
/* end guy's "position in fds table" is now the deletion
|
|
|
|
* guy's old one */
|
2018-06-07 13:33:24 +02:00
|
|
|
end_wsi = wsi_from_fd(context, v);
|
|
|
|
if (!end_wsi) {
|
2019-03-06 01:07:13 +01:00
|
|
|
lwsl_err("no wsi for fd %d pos %d, pt->fds_count=%d\n",
|
2018-06-07 13:33:24 +02:00
|
|
|
(int)pt->fds[m].fd, m, pt->fds_count);
|
|
|
|
assert(0);
|
|
|
|
} else
|
|
|
|
end_wsi->position_in_fds_table = m;
|
|
|
|
|
|
|
|
/* deletion guy's lws_lookup entry needs nuking */
|
|
|
|
delete_from_fd(context, wsi->desc.sockfd);
|
|
|
|
|
|
|
|
/* removed wsi has no position any more */
|
|
|
|
wsi->position_in_fds_table = LWS_NO_FDS_POS;
|
|
|
|
}
|
2017-12-21 03:09:18 +01:00
|
|
|
|
|
|
|
/* remove also from external POLL support via protocol 0 */
|
2018-06-07 13:33:24 +02:00
|
|
|
if (lws_socket_is_valid(wsi->desc.sockfd) && wsi->vhost &&
|
|
|
|
wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_DEL_POLL_FD,
|
|
|
|
wsi->user_space, (void *) &pa, 0))
|
|
|
|
ret = -1;
|
|
|
|
|
2017-12-21 03:09:18 +01:00
|
|
|
#ifndef LWS_NO_SERVER
|
2018-06-07 13:33:24 +02:00
|
|
|
if (!context->being_destroyed &&
|
|
|
|
/* if this made some room, accept connects on this thread */
|
|
|
|
(unsigned int)pt->fds_count < context->fd_limit_per_thread - 1)
|
|
|
|
lws_accept_modulation(context, pt, 1);
|
2017-12-21 03:09:18 +01:00
|
|
|
#endif
|
|
|
|
|
2018-06-07 13:33:24 +02:00
|
|
|
if (wsi->vhost &&
|
|
|
|
wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_UNLOCK_POLL,
|
2017-12-21 03:09:18 +01:00
|
|
|
wsi->user_space, (void *) &pa, 1))
|
|
|
|
ret = -1;
|
2018-06-07 13:33:24 +02:00
|
|
|
|
2017-12-21 03:09:18 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2018-06-07 13:33:24 +02:00
|
|
|
__lws_change_pollfd(struct lws *wsi, int _and, int _or)
|
2017-12-21 03:09:18 +01:00
|
|
|
{
|
|
|
|
struct lws_context *context;
|
|
|
|
struct lws_pollargs pa;
|
|
|
|
int ret = 0;
|
|
|
|
|
2018-06-07 13:33:24 +02:00
|
|
|
if (!wsi || (!wsi->protocol && !wsi->event_pipe) ||
|
|
|
|
wsi->position_in_fds_table == LWS_NO_FDS_POS)
|
|
|
|
return 0;
|
2017-12-21 03:09:18 +01:00
|
|
|
|
|
|
|
context = lws_get_context(wsi);
|
|
|
|
if (!context)
|
|
|
|
return 1;
|
|
|
|
|
2018-06-07 13:33:24 +02:00
|
|
|
if (wsi->vhost &&
|
|
|
|
wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_LOCK_POLL,
|
|
|
|
wsi->user_space, (void *) &pa, 0))
|
2017-12-21 03:09:18 +01:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
ret = _lws_change_pollfd(wsi, _and, _or, &pa);
|
2018-06-07 13:33:24 +02:00
|
|
|
if (wsi->vhost &&
|
|
|
|
wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_UNLOCK_POLL,
|
2017-12-21 03:09:18 +01:00
|
|
|
wsi->user_space, (void *) &pa, 0))
|
|
|
|
ret = -1;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-06-07 13:33:24 +02:00
|
|
|
int
|
|
|
|
lws_change_pollfd(struct lws *wsi, int _and, int _or)
|
|
|
|
{
|
|
|
|
struct lws_context_per_thread *pt;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
pt = &wsi->context->pt[(int)wsi->tsi];
|
|
|
|
|
|
|
|
lws_pt_lock(pt, __func__);
|
|
|
|
ret = __lws_change_pollfd(wsi, _and, _or);
|
|
|
|
lws_pt_unlock(pt);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-12-21 03:09:18 +01:00
|
|
|
LWS_VISIBLE int
|
|
|
|
lws_callback_on_writable(struct lws *wsi)
|
|
|
|
{
|
|
|
|
struct lws_context_per_thread *pt;
|
|
|
|
|
2018-06-07 13:33:24 +02:00
|
|
|
if (lwsi_state(wsi) == LRS_SHUTDOWN)
|
2017-12-21 03:09:18 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (wsi->socket_is_permanently_unusable)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
pt = &wsi->context->pt[(int)wsi->tsi];
|
|
|
|
|
|
|
|
lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_C_WRITEABLE_CB_REQ, 1);
|
|
|
|
#if defined(LWS_WITH_STATS)
|
|
|
|
if (!wsi->active_writable_req_us) {
|
2019-03-06 01:07:13 +01:00
|
|
|
wsi->active_writable_req_us = lws_time_in_microseconds();
|
2018-06-07 13:33:24 +02:00
|
|
|
lws_stats_atomic_bump(wsi->context, pt,
|
|
|
|
LWSSTATS_C_WRITEABLE_CB_EFF_REQ, 1);
|
2017-12-21 03:09:18 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2018-06-07 13:33:24 +02:00
|
|
|
if (wsi->role_ops->callback_on_writable) {
|
|
|
|
if (wsi->role_ops->callback_on_writable(wsi))
|
|
|
|
return 1;
|
|
|
|
wsi = lws_get_network_wsi(wsi);
|
2017-12-21 03:09:18 +01:00
|
|
|
}
|
|
|
|
|
2018-06-07 13:33:24 +02:00
|
|
|
if (wsi->position_in_fds_table == LWS_NO_FDS_POS) {
|
|
|
|
lwsl_debug("%s: failed to find socket %d\n", __func__,
|
|
|
|
wsi->desc.sockfd);
|
2017-12-21 03:09:18 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-06-07 13:33:24 +02:00
|
|
|
if (__lws_change_pollfd(wsi, 0, LWS_POLLOUT))
|
2017-12-21 03:09:18 +01:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-06-07 13:33:24 +02:00
|
|
|
|
2017-12-21 03:09:18 +01:00
|
|
|
/*
|
|
|
|
* stitch protocol choice into the vh protocol linked list
|
|
|
|
* We always insert ourselves at the start of the list
|
|
|
|
*
|
|
|
|
* X <-> B
|
|
|
|
* X <-> pAn <-> pB
|
|
|
|
*
|
|
|
|
* Illegal to attach more than once without detach inbetween
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
lws_same_vh_protocol_insert(struct lws *wsi, int n)
|
|
|
|
{
|
2018-06-07 13:33:24 +02:00
|
|
|
lws_vhost_lock(wsi->vhost);
|
|
|
|
|
2019-03-06 01:07:13 +01:00
|
|
|
if (!lws_dll_is_null(&wsi->same_vh_protocol))
|
|
|
|
lws_dll_lws_remove(&wsi->same_vh_protocol);
|
2017-12-21 03:09:18 +01:00
|
|
|
|
2019-03-06 01:07:13 +01:00
|
|
|
lws_dll_lws_add_front(&wsi->same_vh_protocol,
|
|
|
|
&wsi->vhost->same_vh_protocol_heads[n]);
|
2018-06-07 13:33:24 +02:00
|
|
|
|
|
|
|
lws_vhost_unlock(wsi->vhost);
|
2017-12-21 03:09:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-03-06 01:07:13 +01:00
|
|
|
__lws_same_vh_protocol_remove(struct lws *wsi)
|
2017-12-21 03:09:18 +01:00
|
|
|
{
|
2019-03-06 01:07:13 +01:00
|
|
|
if (!lws_dll_is_null(&wsi->same_vh_protocol))
|
|
|
|
lws_dll_lws_remove(&wsi->same_vh_protocol);
|
|
|
|
}
|
2017-12-21 03:09:18 +01:00
|
|
|
|
2019-03-06 01:07:13 +01:00
|
|
|
void
|
|
|
|
lws_same_vh_protocol_remove(struct lws *wsi)
|
|
|
|
{
|
|
|
|
if (!wsi->vhost)
|
2018-06-07 13:33:24 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
lws_vhost_lock(wsi->vhost);
|
|
|
|
|
2019-03-06 01:07:13 +01:00
|
|
|
__lws_same_vh_protocol_remove(wsi);
|
2018-06-07 13:33:24 +02:00
|
|
|
|
|
|
|
lws_vhost_unlock(wsi->vhost);
|
2017-12-21 03:09:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LWS_VISIBLE int
|
|
|
|
lws_callback_on_writable_all_protocol_vhost(const struct lws_vhost *vhost,
|
2019-03-06 01:07:13 +01:00
|
|
|
const struct lws_protocols *protocol)
|
2017-12-21 03:09:18 +01:00
|
|
|
{
|
|
|
|
struct lws *wsi;
|
2019-03-06 01:07:13 +01:00
|
|
|
int n;
|
2017-12-21 03:09:18 +01:00
|
|
|
|
|
|
|
if (protocol < vhost->protocols ||
|
|
|
|
protocol >= (vhost->protocols + vhost->count_protocols)) {
|
|
|
|
lwsl_err("%s: protocol %p is not from vhost %p (%p - %p)\n",
|
|
|
|
__func__, protocol, vhost->protocols, vhost,
|
|
|
|
(vhost->protocols + vhost->count_protocols));
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-03-06 01:07:13 +01:00
|
|
|
n = (int)(protocol - vhost->protocols);
|
2017-12-21 03:09:18 +01:00
|
|
|
|
2019-03-06 01:07:13 +01:00
|
|
|
lws_start_foreach_dll_safe(struct lws_dll_lws *, d, d1,
|
|
|
|
vhost->same_vh_protocol_heads[n].next) {
|
|
|
|
wsi = lws_container_of(d, struct lws, same_vh_protocol);
|
|
|
|
|
|
|
|
assert(wsi->protocol == protocol);
|
2017-12-21 03:09:18 +01:00
|
|
|
lws_callback_on_writable(wsi);
|
2019-03-06 01:07:13 +01:00
|
|
|
|
|
|
|
} lws_end_foreach_dll_safe(d, d1);
|
2017-12-21 03:09:18 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
LWS_VISIBLE int
|
|
|
|
lws_callback_on_writable_all_protocol(const struct lws_context *context,
|
|
|
|
const struct lws_protocols *protocol)
|
|
|
|
{
|
2018-03-18 16:37:51 +01:00
|
|
|
struct lws_vhost *vhost;
|
2017-12-21 03:09:18 +01:00
|
|
|
int n;
|
|
|
|
|
2018-03-18 16:37:51 +01:00
|
|
|
if (!context)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
vhost = context->vhost_list;
|
|
|
|
|
2017-12-21 03:09:18 +01:00
|
|
|
while (vhost) {
|
|
|
|
for (n = 0; n < vhost->count_protocols; n++)
|
|
|
|
if (protocol->callback ==
|
2019-03-06 01:07:13 +01:00
|
|
|
vhost->protocols[n].callback &&
|
2017-12-21 03:09:18 +01:00
|
|
|
!strcmp(protocol->name, vhost->protocols[n].name))
|
|
|
|
break;
|
|
|
|
if (n != vhost->count_protocols)
|
|
|
|
lws_callback_on_writable_all_protocol_vhost(
|
|
|
|
vhost, &vhost->protocols[n]);
|
|
|
|
|
|
|
|
vhost = vhost->vhost_next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|