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;
46 if (wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_CHANGE_MODE_POLL_FD,
47 wsi->user_space, (void *)pa, 0)) {
52 if (_and & LWS_POLLIN) {
53 lws_libev_io(wsi, LWS_EV_STOP | LWS_EV_READ);
54 lws_libuv_io(wsi, LWS_EV_STOP | LWS_EV_READ);
56 if (_or & LWS_POLLIN) {
57 lws_libev_io(wsi, LWS_EV_START | LWS_EV_READ);
58 lws_libuv_io(wsi, LWS_EV_START | LWS_EV_READ);
60 if (_and & LWS_POLLOUT) {
61 lws_libev_io(wsi, LWS_EV_STOP | LWS_EV_WRITE);
62 lws_libuv_io(wsi, LWS_EV_STOP | LWS_EV_WRITE);
64 if (_or & LWS_POLLOUT) {
65 lws_libev_io(wsi, LWS_EV_START | LWS_EV_WRITE);
66 lws_libuv_io(wsi, LWS_EV_START | LWS_EV_WRITE);
70 * if we changed something in this pollfd...
71 * ... and we're running in a different thread context
72 * than the service thread...
73 * ... and the service thread is waiting ...
74 * then cancel it to force a restart with our changed events
77 pa_events = pa->prev_events != pa->events;
81 if (lws_plat_change_pollfd(context, wsi, pfd)) {
82 lwsl_info("%s failed\n", __func__);
87 sampled_tid = context->service_tid;
89 tid = wsi->vhost->protocols[0].callback(wsi,
90 LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
95 if (tid != sampled_tid)
96 lws_cancel_service_pt(wsi);
104 insert_wsi_socket_into_fds(struct lws_context *context, struct lws *wsi)
106 struct lws_pollargs pa = { wsi->sock, LWS_POLLIN, 0 };
107 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
109 #ifndef LWS_NO_SERVER
110 struct lws_pollargs pa1;
113 lwsl_debug("%s: %p: tsi=%d, sock=%d, pos-in-fds=%d\n",
114 __func__, wsi, wsi->tsi, wsi->sock, pt->fds_count);
116 if ((unsigned int)pt->fds_count >= context->fd_limit_per_thread) {
117 lwsl_err("Too many fds (%d vs %d)\n", context->max_fds,
118 context->fd_limit_per_thread );
122 #if !defined(_WIN32) && !defined(MBED_OPERATORS)
123 if (wsi->sock >= context->max_fds) {
124 lwsl_err("Socket fd %d is too high (%d)\n",
125 wsi->sock, context->max_fds);
132 assert(lws_socket_is_valid(wsi->sock));
134 if (wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_LOCK_POLL,
135 wsi->user_space, (void *) &pa, 1))
140 insert_wsi(context, wsi);
141 wsi->position_in_fds_table = pt->fds_count;
142 pt->fds[pt->fds_count].fd = wsi->sock;
143 pt->fds[pt->fds_count].events = LWS_POLLIN;
144 pa.events = pt->fds[pt->fds_count].events;
146 lws_plat_insert_socket_into_fds(context, wsi);
148 /* external POLL support via protocol 0 */
149 if (wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_ADD_POLL_FD,
150 wsi->user_space, (void *) &pa, 0))
152 #ifndef LWS_NO_SERVER
153 /* if no more room, defeat accepts on this thread */
154 if ((unsigned int)pt->fds_count == context->fd_limit_per_thread - 1)
155 _lws_change_pollfd(pt->wsi_listening, LWS_POLLIN, 0, &pa1);
159 if (wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_UNLOCK_POLL,
160 wsi->user_space, (void *)&pa, 1))
167 remove_wsi_socket_from_fds(struct lws *wsi)
169 struct lws_pollargs pa = { wsi->sock, 0, 0 };
170 #ifndef LWS_NO_SERVER
171 struct lws_pollargs pa1;
173 struct lws_context *context = wsi->context;
174 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
178 #if !defined(_WIN32) && !defined(MBED_OPERATORS)
179 if (wsi->sock > context->max_fds) {
180 lwsl_err("fd %d too high (%d)\n", wsi->sock, context->max_fds);
185 if (wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_LOCK_POLL,
186 wsi->user_space, (void *)&pa, 1))
189 lws_libev_io(wsi, LWS_EV_STOP | LWS_EV_READ | LWS_EV_WRITE | LWS_EV_PREPARE_DELETION);
190 lws_libuv_io(wsi, LWS_EV_STOP | LWS_EV_READ | LWS_EV_WRITE | LWS_EV_PREPARE_DELETION);
194 lwsl_info("%s: wsi=%p, sock=%d, fds pos=%d, end guy pos=%d, endfd=%d\n",
195 __func__, wsi, wsi->sock, wsi->position_in_fds_table,
196 pt->fds_count, pt->fds[pt->fds_count].fd);
198 /* the guy who is to be deleted's slot index in pt->fds */
199 m = wsi->position_in_fds_table;
201 /* have the last guy take up the now vacant slot */
202 pt->fds[m] = pt->fds[pt->fds_count - 1];
204 lws_plat_delete_socket_from_fds(context, wsi, m);
206 /* end guy's "position in fds table" is now the deletion guy's old one */
207 end_wsi = wsi_from_fd(context, pt->fds[pt->fds_count].fd);
209 end_wsi->position_in_fds_table = m;
211 /* deletion guy's lws_lookup entry needs nuking */
212 delete_from_fd(context, wsi->sock);
213 /* removed wsi has no position any more */
214 wsi->position_in_fds_table = -1;
216 /* remove also from external POLL support via protocol 0 */
217 if (lws_socket_is_valid(wsi->sock))
218 if (wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_DEL_POLL_FD,
219 wsi->user_space, (void *) &pa, 0))
221 #ifndef LWS_NO_SERVER
222 if (!context->being_destroyed)
223 /* if this made some room, accept connects on this thread */
224 if ((unsigned int)pt->fds_count < context->fd_limit_per_thread - 1)
225 _lws_change_pollfd(pt->wsi_listening, 0, LWS_POLLIN, &pa1);
229 if (wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_UNLOCK_POLL,
230 wsi->user_space, (void *) &pa, 1))
237 lws_change_pollfd(struct lws *wsi, int _and, int _or)
239 struct lws_context_per_thread *pt;
240 struct lws_context *context;
241 struct lws_pollargs pa;
244 if (!wsi || !wsi->protocol || wsi->position_in_fds_table < 0)
247 context = lws_get_context(wsi);
251 if (wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_LOCK_POLL,
252 wsi->user_space, (void *) &pa, 0))
255 pt = &context->pt[(int)wsi->tsi];
258 ret = _lws_change_pollfd(wsi, _and, _or, &pa);
260 if (wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_UNLOCK_POLL,
261 wsi->user_space, (void *) &pa, 0))
269 * lws_callback_on_writable() - Request a callback when this socket
270 * becomes able to be written to without
273 * @wsi: Websocket connection instance to get callback for
277 lws_callback_on_writable(struct lws *wsi)
280 struct lws *network_wsi, *wsi2;
284 if (wsi->state == LWSS_SHUTDOWN)
288 lwsl_info("%s: %p\n", __func__, wsi);
290 if (wsi->mode != LWSCM_HTTP2_SERVING)
293 if (wsi->u.http2.requested_POLLOUT) {
294 lwsl_info("already pending writable\n");
298 if (wsi->u.http2.tx_credit <= 0) {
300 * other side is not able to cope with us sending
301 * anything so no matter if we have POLLOUT on our side.
303 * Delay waiting for our POLLOUT until peer indicates he has
304 * space for more using tx window command in http2 layer
306 lwsl_info("%s: %p: waiting_tx_credit (%d)\n", __func__, wsi,
307 wsi->u.http2.tx_credit);
308 wsi->u.http2.waiting_tx_credit = 1;
312 network_wsi = lws_http2_get_network_wsi(wsi);
313 already = network_wsi->u.http2.requested_POLLOUT;
315 /* mark everybody above him as requesting pollout */
319 wsi2->u.http2.requested_POLLOUT = 1;
320 lwsl_info("mark %p pending writable\n", wsi2);
321 wsi2 = wsi2->u.http2.parent_wsi;
324 /* for network action, act only on the network wsi */
332 if (lws_ext_cb_active(wsi, LWS_EXT_CB_REQUEST_ON_WRITEABLE, NULL, 0))
335 if (wsi->position_in_fds_table < 0) {
336 lwsl_err("%s: failed to find socket %d\n", __func__, wsi->sock);
340 if (lws_change_pollfd(wsi, 0, LWS_POLLOUT))
347 * lws_callback_on_writable_all_protocol() - Request a callback for
348 * all connections using the given protocol when it
349 * becomes possible to write to each socket without
352 * @context: lws_context
353 * @protocol: Protocol whose connections will get callbacks
357 lws_callback_on_writable_all_protocol(const struct lws_context *context,
358 const struct lws_protocols *protocol)
360 const struct lws_context_per_thread *pt = &context->pt[0];
361 unsigned int n, m = context->count_threads;
365 for (n = 0; n < pt->fds_count; n++) {
366 wsi = wsi_from_fd(context, pt->fds[n].fd);
369 if (wsi->protocol == protocol)
370 lws_callback_on_writable(wsi);