2 * libwebsockets - small server side websockets and web server implementation
4 * Copyright (C) 2010-2014 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 insert_wsi_socket_into_fds(struct lws_context *context,
28 struct lws_pollargs pa = { wsi->sock, LWS_POLLIN, 0 };
30 if (context->fds_count >= context->max_fds) {
31 lwsl_err("Too many fds (%d)\n", context->max_fds);
35 #if !defined(_WIN32) && !defined(MBED_OPERATORS)
36 if (wsi->sock >= context->max_fds) {
37 lwsl_err("Socket fd %d is too high (%d)\n",
38 wsi->sock, context->max_fds);
44 assert(lws_socket_is_valid(wsi->sock));
46 // lwsl_info("insert_wsi_socket_into_fds: wsi=%p, sock=%d, fds pos=%d\n",
47 // wsi, wsi->sock, context->fds_count);
49 if (context->protocols[0].callback(wsi, LWS_CALLBACK_LOCK_POLL,
50 wsi->user_space, (void *) &pa, 1))
53 insert_wsi(context, wsi);
54 wsi->position_in_fds_table = context->fds_count;
55 context->fds[context->fds_count].fd = wsi->sock;
56 context->fds[context->fds_count].events = LWS_POLLIN;
58 lws_plat_insert_socket_into_fds(context, wsi);
60 /* external POLL support via protocol 0 */
61 if (context->protocols[0].callback(wsi, LWS_CALLBACK_ADD_POLL_FD,
62 wsi->user_space, (void *) &pa, 0))
65 if (context->protocols[0].callback(wsi, LWS_CALLBACK_UNLOCK_POLL,
66 wsi->user_space, (void *)&pa, 1))
73 remove_wsi_socket_from_fds(struct lws *wsi)
76 struct lws_pollargs pa = { wsi->sock, 0, 0 };
77 struct lws_context *context = wsi->context;
79 lws_libev_io(wsi, LWS_EV_STOP | LWS_EV_READ | LWS_EV_WRITE);
83 #if !defined(_WIN32) && !defined(MBED_OPERATORS)
84 if (wsi->sock > context->max_fds) {
85 lwsl_err("Socket fd %d too high (%d)\n",
86 wsi->sock, context->max_fds);
91 lwsl_info("%s: wsi=%p, sock=%d, fds pos=%d\n", __func__,
92 wsi, wsi->sock, wsi->position_in_fds_table);
94 if (context->protocols[0].callback(wsi, LWS_CALLBACK_LOCK_POLL,
95 wsi->user_space, (void *)&pa, 1))
98 m = wsi->position_in_fds_table; /* replace the contents for this */
100 /* have the last guy take up the vacant slot */
101 context->fds[m] = context->fds[context->fds_count];
103 lws_plat_delete_socket_from_fds(context, wsi, m);
106 * end guy's fds_lookup entry remains unchanged
107 * (still same fd pointing to same wsi)
109 /* end guy's "position in fds table" changed */
110 wsi_from_fd(context,context->fds[context->fds_count].fd)->
111 position_in_fds_table = m;
112 /* deletion guy's lws_lookup entry needs nuking */
113 delete_from_fd(context,wsi->sock);
114 /* removed wsi has no position any more */
115 wsi->position_in_fds_table = -1;
117 /* remove also from external POLL support via protocol 0 */
118 if (lws_socket_is_valid(wsi->sock)) {
119 if (context->protocols[0].callback(wsi,
120 LWS_CALLBACK_DEL_POLL_FD, wsi->user_space,
124 if (context->protocols[0].callback(wsi, LWS_CALLBACK_UNLOCK_POLL,
125 wsi->user_space, (void *) &pa, 1))
132 lws_change_pollfd(struct lws *wsi, int _and, int _or)
134 struct lws_context *context;
137 struct lws_pollfd *pfd;
138 struct lws_pollargs pa;
141 if (!wsi || !wsi->protocol || wsi->position_in_fds_table < 0)
144 context = lws_get_ctx(wsi);
148 pfd = &context->fds[wsi->position_in_fds_table];
151 if (context->protocols[0].callback(wsi, LWS_CALLBACK_LOCK_POLL,
152 wsi->user_space, (void *) &pa, 0))
155 pa.prev_events = pfd->events;
156 pa.events = pfd->events = (pfd->events & ~_and) | _or;
158 if (context->protocols[0].callback(wsi, LWS_CALLBACK_CHANGE_MODE_POLL_FD,
159 wsi->user_space, (void *) &pa, 0))
163 * if we changed something in this pollfd...
164 * ... and we're running in a different thread context
165 * than the service thread...
166 * ... and the service thread is waiting ...
167 * then cancel it to force a restart with our changed events
170 pa_events = pa.prev_events != pa.events;
174 if (lws_plat_change_pollfd(context, wsi, pfd)) {
175 lwsl_info("%s failed\n", __func__);
179 sampled_tid = context->service_tid;
181 tid = context->protocols[0].callback(NULL,
182 LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
185 if (tid != sampled_tid)
186 lws_cancel_service(context);
190 if (context->protocols[0].callback(wsi, LWS_CALLBACK_UNLOCK_POLL,
191 wsi->user_space, (void *) &pa, 0))
199 * lws_callback_on_writable() - Request a callback when this socket
200 * becomes able to be written to without
203 * @context: libwebsockets context
204 * @wsi: Websocket connection instance to get callback for
208 lws_callback_on_writable(struct lws *wsi)
211 struct lws *network_wsi, *wsi2;
214 lwsl_info("%s: %p\n", __func__, wsi);
216 if (wsi->mode != LWS_CONNMODE_HTTP2_SERVING)
219 if (wsi->u.http2.requested_POLLOUT) {
220 lwsl_info("already pending writable\n");
224 if (wsi->u.http2.tx_credit <= 0) {
226 * other side is not able to cope with us sending
227 * anything so no matter if we have POLLOUT on our side.
229 * Delay waiting for our POLLOUT until peer indicates he has
230 * space for more using tx window command in http2 layer
232 lwsl_info("%s: %p: waiting_tx_credit (%d)\n", __func__, wsi,
233 wsi->u.http2.tx_credit);
234 wsi->u.http2.waiting_tx_credit = 1;
238 network_wsi = lws_http2_get_network_wsi(wsi);
239 already = network_wsi->u.http2.requested_POLLOUT;
241 /* mark everybody above him as requesting pollout */
245 wsi2->u.http2.requested_POLLOUT = 1;
246 lwsl_info("mark %p pending writable\n", wsi2);
247 wsi2 = wsi2->u.http2.parent_wsi;
250 /* for network action, act only on the network wsi */
258 if (lws_ext_callback_for_each_active(wsi,
259 LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE, NULL, 0))
262 if (wsi->position_in_fds_table < 0) {
263 lwsl_err("%s: failed to find socket %d\n", __func__, wsi->sock);
267 if (lws_change_pollfd(wsi, 0, LWS_POLLOUT))
270 lws_libev_io(wsi, LWS_EV_START | LWS_EV_WRITE);
276 * lws_callback_on_writable_all_protocol() - Request a callback for
277 * all connections using the given protocol when it
278 * becomes possible to write to each socket without
281 * @protocol: Protocol whose connections will get callbacks
285 lws_callback_on_writable_all_protocol(const struct lws_context *context,
286 const struct lws_protocols *protocol)
291 for (n = 0; n < context->fds_count; n++) {
292 wsi = wsi_from_fd(context,context->fds[n].fd);
295 if (wsi->protocol == protocol)
296 lws_callback_on_writable(wsi);