2 * libwebsockets - small server side websockets and web server implementation
4 * Copyright (C) 2010-2015 Andy Green <andy@warmcat.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation:
9 * version 2.1 of the License.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22 #include "private-libwebsockets.h"
25 _lws_change_pollfd(struct lws *wsi, int _and, int _or, struct lws_pollargs *pa)
27 struct lws_context_per_thread *pt;
28 struct lws_context *context;
29 int ret = 0, pa_events = 1;
30 struct lws_pollfd *pfd;
33 if (!wsi || wsi->position_in_fds_table < 0)
36 context = wsi->context;
37 pt = &context->pt[(int)wsi->tsi];
38 assert(wsi->position_in_fds_table >= 0 &&
39 wsi->position_in_fds_table < pt->fds_count);
41 pfd = &pt->fds[wsi->position_in_fds_table];
43 pa->prev_events = pfd->events;
44 pa->events = pfd->events = (pfd->events & ~_and) | _or;
47 if (wsi->http2_substream)
50 if (wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_CHANGE_MODE_POLL_FD,
51 wsi->user_space, (void *)pa, 0)) {
56 if (_and & LWS_POLLIN) {
57 lws_libev_io(wsi, LWS_EV_STOP | LWS_EV_READ);
58 lws_libuv_io(wsi, LWS_EV_STOP | LWS_EV_READ);
60 if (_or & LWS_POLLIN) {
61 lws_libev_io(wsi, LWS_EV_START | LWS_EV_READ);
62 lws_libuv_io(wsi, LWS_EV_START | LWS_EV_READ);
64 if (_and & LWS_POLLOUT) {
65 lws_libev_io(wsi, LWS_EV_STOP | LWS_EV_WRITE);
66 lws_libuv_io(wsi, LWS_EV_STOP | LWS_EV_WRITE);
68 if (_or & LWS_POLLOUT) {
69 lws_libev_io(wsi, LWS_EV_START | LWS_EV_WRITE);
70 lws_libuv_io(wsi, LWS_EV_START | LWS_EV_WRITE);
74 * if we changed something in this pollfd...
75 * ... and we're running in a different thread context
76 * than the service thread...
77 * ... and the service thread is waiting ...
78 * then cancel it to force a restart with our changed events
81 pa_events = pa->prev_events != pa->events;
86 if (lws_plat_change_pollfd(context, wsi, pfd)) {
87 lwsl_info("%s failed\n", __func__);
92 sampled_tid = context->service_tid;
94 tid = wsi->vhost->protocols[0].callback(wsi,
95 LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
100 if (tid != sampled_tid)
101 lws_cancel_service_pt(wsi);
109 insert_wsi_socket_into_fds(struct lws_context *context, struct lws *wsi)
111 struct lws_pollargs pa = { wsi->sock, LWS_POLLIN, 0 };
112 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
114 #ifndef LWS_NO_SERVER
115 struct lws_pollargs pa1;
118 lwsl_debug("%s: %p: tsi=%d, sock=%d, pos-in-fds=%d\n",
119 __func__, wsi, wsi->tsi, wsi->sock, pt->fds_count);
121 if ((unsigned int)pt->fds_count >= context->fd_limit_per_thread) {
122 lwsl_err("Too many fds (%d vs %d)\n", context->max_fds,
123 context->fd_limit_per_thread );
127 #if !defined(_WIN32) && !defined(MBED_OPERATORS) && !defined(LWS_WITH_ESP8266)
128 if (wsi->sock >= context->max_fds) {
129 lwsl_err("Socket fd %d is too high (%d)\n",
130 wsi->sock, context->max_fds);
137 assert(lws_socket_is_valid(wsi->sock));
139 if (wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_LOCK_POLL,
140 wsi->user_space, (void *) &pa, 1))
145 insert_wsi(context, wsi);
146 #if defined(LWS_WITH_ESP8266)
147 if (wsi->position_in_fds_table == -1)
149 wsi->position_in_fds_table = pt->fds_count;
150 pt->fds[wsi->position_in_fds_table].fd = wsi->sock;
152 pt->fds[wsi->position_in_fds_table].events = LWS_POLLIN;
154 pt->fds[wsi->position_in_fds_table].events = 0; // LWS_POLLIN;
156 pa.events = pt->fds[pt->fds_count].events;
158 lws_plat_insert_socket_into_fds(context, wsi);
160 /* external POLL support via protocol 0 */
161 if (wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_ADD_POLL_FD,
162 wsi->user_space, (void *) &pa, 0))
164 #ifndef LWS_NO_SERVER
165 /* if no more room, defeat accepts on this thread */
166 if ((unsigned int)pt->fds_count == context->fd_limit_per_thread - 1)
167 _lws_change_pollfd(pt->wsi_listening, LWS_POLLIN, 0, &pa1);
171 if (wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_UNLOCK_POLL,
172 wsi->user_space, (void *)&pa, 1))
179 remove_wsi_socket_from_fds(struct lws *wsi)
181 struct lws_context *context = wsi->context;
182 struct lws_pollargs pa = { wsi->sock, 0, 0 };
183 #if !defined(LWS_WITH_ESP8266)
184 #ifndef LWS_NO_SERVER
185 struct lws_pollargs pa1;
187 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
193 #if !defined(_WIN32) && !defined(MBED_OPERATORS) && !defined(LWS_WITH_ESP8266)
194 if (wsi->sock > context->max_fds) {
195 lwsl_err("fd %d too high (%d)\n", wsi->sock, context->max_fds);
200 if (wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_LOCK_POLL,
201 wsi->user_space, (void *)&pa, 1))
205 * detach ourselves from vh protocol list if we're on one
207 * A -> C , or, B -> C, or A -> B
209 lwsl_info("%s: removing same prot wsi %p\n", __func__, wsi);
210 if (wsi->same_vh_protocol_prev) {
211 assert (*(wsi->same_vh_protocol_prev) == wsi);
212 lwsl_info("have prev %p, setting him to our next %p\n",
213 wsi->same_vh_protocol_prev,
214 wsi->same_vh_protocol_next);
216 /* guy who pointed to us should point to our next */
217 *(wsi->same_vh_protocol_prev) = wsi->same_vh_protocol_next;
219 //lwsl_err("null wsi->prev\n");
220 /* our next should point back to our prev */
221 if (wsi->same_vh_protocol_next) {
222 lwsl_info("have next %p\n");
223 wsi->same_vh_protocol_next->same_vh_protocol_prev =
224 wsi->same_vh_protocol_prev;
226 //lwsl_err("null wsi->next\n");
228 /* the guy who is to be deleted's slot index in pt->fds */
229 m = wsi->position_in_fds_table;
231 #if !defined(LWS_WITH_ESP8266)
232 lws_libev_io(wsi, LWS_EV_STOP | LWS_EV_READ | LWS_EV_WRITE | LWS_EV_PREPARE_DELETION);
233 lws_libuv_io(wsi, LWS_EV_STOP | LWS_EV_READ | LWS_EV_WRITE | LWS_EV_PREPARE_DELETION);
237 lwsl_debug("%s: wsi=%p, sock=%d, fds pos=%d, end guy pos=%d, endfd=%d\n",
238 __func__, wsi, wsi->sock, wsi->position_in_fds_table,
239 pt->fds_count, pt->fds[pt->fds_count].fd);
241 /* have the last guy take up the now vacant slot */
242 pt->fds[m] = pt->fds[pt->fds_count - 1];
244 /* this decrements pt->fds_count */
245 lws_plat_delete_socket_from_fds(context, wsi, m);
246 #if !defined(LWS_WITH_ESP8266)
247 v = (int) pt->fds[m].fd;
248 /* end guy's "position in fds table" is now the deletion guy's old one */
249 end_wsi = wsi_from_fd(context, v);
251 lwsl_err("no wsi found for sock fd %d at pos %d, pt->fds_count=%d\n", (int)pt->fds[m].fd, m, pt->fds_count);
254 end_wsi->position_in_fds_table = m;
256 /* deletion guy's lws_lookup entry needs nuking */
257 delete_from_fd(context, wsi->sock);
258 /* removed wsi has no position any more */
259 wsi->position_in_fds_table = -1;
261 /* remove also from external POLL support via protocol 0 */
262 if (lws_socket_is_valid(wsi->sock))
263 if (wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_DEL_POLL_FD,
264 wsi->user_space, (void *) &pa, 0))
266 #ifndef LWS_NO_SERVER
267 if (!context->being_destroyed)
268 /* if this made some room, accept connects on this thread */
269 if ((unsigned int)pt->fds_count < context->fd_limit_per_thread - 1)
270 _lws_change_pollfd(pt->wsi_listening, 0, LWS_POLLIN, &pa1);
274 if (wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_UNLOCK_POLL,
275 wsi->user_space, (void *) &pa, 1))
282 lws_change_pollfd(struct lws *wsi, int _and, int _or)
284 struct lws_context_per_thread *pt;
285 struct lws_context *context;
286 struct lws_pollargs pa;
289 if (!wsi || !wsi->protocol || wsi->position_in_fds_table < 0)
292 context = lws_get_context(wsi);
296 if (wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_LOCK_POLL,
297 wsi->user_space, (void *) &pa, 0))
300 pt = &context->pt[(int)wsi->tsi];
303 ret = _lws_change_pollfd(wsi, _and, _or, &pa);
305 if (wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_UNLOCK_POLL,
306 wsi->user_space, (void *) &pa, 0))
313 lws_callback_on_writable(struct lws *wsi)
316 struct lws *network_wsi, *wsi2;
320 if (wsi->state == LWSS_SHUTDOWN)
323 if (wsi->socket_is_permanently_unusable)
327 lwsl_info("%s: %p\n", __func__, wsi);
329 if (wsi->mode != LWSCM_HTTP2_SERVING)
332 if (wsi->u.http2.requested_POLLOUT) {
333 lwsl_info("already pending writable\n");
337 if (wsi->u.http2.tx_credit <= 0) {
339 * other side is not able to cope with us sending
340 * anything so no matter if we have POLLOUT on our side.
342 * Delay waiting for our POLLOUT until peer indicates he has
343 * space for more using tx window command in http2 layer
345 lwsl_info("%s: %p: waiting_tx_credit (%d)\n", __func__, wsi,
346 wsi->u.http2.tx_credit);
347 wsi->u.http2.waiting_tx_credit = 1;
351 network_wsi = lws_http2_get_network_wsi(wsi);
352 already = network_wsi->u.http2.requested_POLLOUT;
354 /* mark everybody above him as requesting pollout */
358 wsi2->u.http2.requested_POLLOUT = 1;
359 lwsl_info("mark %p pending writable\n", wsi2);
360 wsi2 = wsi2->u.http2.parent_wsi;
363 /* for network action, act only on the network wsi */
371 if (lws_ext_cb_active(wsi, LWS_EXT_CB_REQUEST_ON_WRITEABLE, NULL, 0))
374 if (wsi->position_in_fds_table < 0) {
375 lwsl_err("%s: failed to find socket %d\n", __func__, wsi->sock);
379 if (lws_change_pollfd(wsi, 0, LWS_POLLOUT))
386 lws_callback_on_writable_all_protocol_vhost(const struct lws_vhost *vhost,
387 const struct lws_protocols *protocol)
391 if (protocol < vhost->protocols ||
392 protocol >= (vhost->protocols + vhost->count_protocols)) {
393 lwsl_err("%s: protocol is not from vhost\n", __func__);
398 wsi = vhost->same_vh_protocol_list[protocol - vhost->protocols];
399 //lwsl_notice("%s: protocol %p, start wsi %p\n", __func__, protocol, wsi);
401 //lwsl_notice("%s: protocol %p, this wsi %p (wsi->protocol=%p)\n",
402 // __func__, protocol, wsi, wsi->protocol);
403 assert(wsi->protocol == protocol);
404 assert(*wsi->same_vh_protocol_prev == wsi);
405 if (wsi->same_vh_protocol_next) {
406 // lwsl_err("my next says %p\n", wsi->same_vh_protocol_next);
407 // lwsl_err("my next's prev says %p\n",
408 // wsi->same_vh_protocol_next->same_vh_protocol_prev);
409 assert(wsi->same_vh_protocol_next->same_vh_protocol_prev == &wsi->same_vh_protocol_next);
411 //lwsl_notice(" apv: %p\n", wsi);
412 lws_callback_on_writable(wsi);
413 wsi = wsi->same_vh_protocol_next;
420 lws_callback_on_writable_all_protocol(const struct lws_context *context,
421 const struct lws_protocols *protocol)
423 struct lws_vhost *vhost = context->vhost_list;
427 for (n = 0; n < vhost->count_protocols; n++)
428 if (protocol->callback ==
429 vhost->protocols[n].callback &&
430 !strcmp(protocol->name, vhost->protocols[n].name))
432 if (n != vhost->count_protocols)
433 lws_callback_on_writable_all_protocol_vhost(
434 vhost, &vhost->protocols[n]);
436 vhost = vhost->vhost_next;