2 * libwebsockets - small server side websockets and web server implementation
4 * Copyright (C) 2010-2016 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_feature_status_libuv(struct lws_context_creation_info *info)
27 if (lws_check_opt(info->options, LWS_SERVER_OPTION_LIBUV))
28 lwsl_notice("libuv support compiled in and enabled\n");
30 lwsl_notice("libuv support compiled in but disabled\n");
34 lws_io_cb(uv_poll_t *watcher, int status, int revents)
36 struct lws_io_watcher *lws_io = container_of(watcher,
37 struct lws_io_watcher, uv_watcher);
38 struct lws_context *context = lws_io->context;
39 struct lws_pollfd eventfd;
41 eventfd.fd = watcher->io_watcher.fd;
46 /* at this point status will be an UV error, like UV_EBADF,
47 we treat all errors as LWS_POLLHUP */
49 /* you might want to return; instead of servicing the fd in some cases */
50 if (status == UV_EAGAIN)
53 eventfd.events |= LWS_POLLHUP;
54 eventfd.revents |= LWS_POLLHUP;
56 if (revents & UV_READABLE) {
57 eventfd.events |= LWS_POLLIN;
58 eventfd.revents |= LWS_POLLIN;
60 if (revents & UV_WRITABLE) {
61 eventfd.events |= LWS_POLLOUT;
62 eventfd.revents |= LWS_POLLOUT;
65 lws_service_fd(context, &eventfd);
69 lws_uv_sigint_cb(uv_signal_t *watcher, int signum)
71 lwsl_info("internal signal handler caught signal %d\n", signum);
72 lws_libuv_stop(watcher->data);
76 lws_uv_sigint_cfg(struct lws_context *context, int use_uv_sigint,
79 context->use_ev_sigint = use_uv_sigint;
81 context->lws_uv_sigint_cb = cb;
83 context->lws_uv_sigint_cb = &lws_uv_sigint_cb;
89 lws_uv_timeout_cb(uv_timer_t *timer)
91 struct lws_context_per_thread *pt = container_of(timer,
92 struct lws_context_per_thread, uv_timeout_watcher);
94 lwsl_debug("%s\n", __func__);
95 /* do timeout check only */
96 lws_service_fd_tsi(pt->context, NULL, pt->tid);
99 static const int sigs[] = { SIGINT, SIGTERM, SIGSEGV, SIGFPE };
102 lws_uv_initloop(struct lws_context *context, uv_loop_t *loop, int tsi)
104 struct lws_context_per_thread *pt = &context->pt[tsi];
105 struct lws *wsi = wsi_from_fd(context, pt->lserv_fd);
109 loop = lws_malloc(sizeof(*loop));
111 pt->ev_loop_foreign = 0;
113 pt->ev_loop_foreign = 1;
115 pt->io_loop_uv = loop;
117 if (pt->context->use_ev_sigint) {
118 assert(ARRAY_SIZE(sigs) <= ARRAY_SIZE(pt->signals));
119 for (n = 0; n < ARRAY_SIZE(sigs); n++) {
120 uv_signal_init(loop, &pt->signals[n]);
121 pt->signals[n].data = pt->context;
122 uv_signal_start(&pt->signals[n], context->lws_uv_sigint_cb, sigs[n]);
127 * Initialize the accept wsi read watcher with the listening socket
128 * and register a callback for read operations
130 * We have to do it here because the uv loop(s) are not
131 * initialized until after context creation.
134 wsi->w_read.context = context;
135 uv_poll_init(pt->io_loop_uv, &wsi->w_read.uv_watcher,
137 uv_poll_start(&wsi->w_read.uv_watcher, UV_READABLE,
141 uv_timer_init(pt->io_loop_uv, &pt->uv_timeout_watcher);
142 uv_timer_start(&pt->uv_timeout_watcher, lws_uv_timeout_cb, 1000, 1000);
147 void lws_uv_close_cb(uv_handle_t *handle)
152 void lws_uv_walk_cb(uv_handle_t *handle, void *arg)
154 uv_close(handle, lws_uv_close_cb);
158 lws_libuv_destroyloop(struct lws_context *context, int tsi)
160 struct lws_context_per_thread *pt = &context->pt[tsi];
163 if (!lws_check_opt(context->options, LWS_SERVER_OPTION_LIBUV))
169 if (context->use_ev_sigint)
170 uv_signal_stop(&pt->w_sigint.uv_watcher);
171 for (m = 0; m < ARRAY_SIZE(sigs); m++)
172 uv_signal_stop(&pt->signals[m]);
173 if (!pt->ev_loop_foreign) {
174 uv_stop(pt->io_loop_uv);
175 uv_walk(pt->io_loop_uv, lws_uv_walk_cb, NULL);
176 while (uv_run(pt->io_loop_uv, UV_RUN_NOWAIT));
177 m = uv_loop_close(pt->io_loop_uv);
179 lwsl_debug("%s: uv_loop_close: UV_EBUSY\n", __func__);
180 lws_free(pt->io_loop_uv);
185 lws_libuv_accept(struct lws *wsi, int accept_fd)
187 struct lws_context *context = lws_get_context(wsi);
188 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
190 if (!LWS_LIBUV_ENABLED(context))
193 lwsl_debug("%s: new wsi %p\n", __func__, wsi);
195 wsi->w_read.context = context;
197 uv_poll_init(pt->io_loop_uv, &wsi->w_read.uv_watcher, accept_fd);
201 lws_libuv_io(struct lws *wsi, int flags)
203 struct lws_context *context = lws_get_context(wsi);
204 struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
205 int current_events = wsi->w_read.uv_watcher.io_watcher.pevents &
206 (UV_READABLE | UV_WRITABLE);
207 struct lws_io_watcher *w = &wsi->w_read;
209 if (!LWS_LIBUV_ENABLED(context))
212 lwsl_debug("%s: wsi: %p, flags:%d\n", __func__, wsi, flags);
214 if (!pt->io_loop_uv) {
215 lwsl_info("%s: no io loop yet\n", __func__);
219 assert((flags & (LWS_EV_START | LWS_EV_STOP)) &&
220 (flags & (LWS_EV_READ | LWS_EV_WRITE)));
222 if (flags & LWS_EV_START) {
223 if (flags & LWS_EV_WRITE)
224 current_events |= UV_WRITABLE;
226 if (flags & LWS_EV_READ)
227 current_events |= UV_READABLE;
229 uv_poll_start(&w->uv_watcher, current_events, lws_io_cb);
231 if (flags & LWS_EV_WRITE)
232 current_events &= ~UV_WRITABLE;
234 if (flags & LWS_EV_READ)
235 current_events &= ~UV_READABLE;
237 if (!(current_events & (UV_READABLE | UV_WRITABLE)))
238 uv_poll_stop(&w->uv_watcher);
240 uv_poll_start(&w->uv_watcher, current_events,
246 lws_libuv_init_fd_table(struct lws_context *context)
250 if (!LWS_LIBUV_ENABLED(context))
253 for (n = 0; n < context->count_threads; n++) {
254 context->pt[n].w_sigint.context = context;
261 lws_libuv_run(const struct lws_context *context, int tsi)
263 if (context->pt[tsi].io_loop_uv && LWS_LIBUV_ENABLED(context))
264 uv_run(context->pt[tsi].io_loop_uv, 0);
268 lws_libuv_kill(const struct lws_context *context)
272 for (n = 0; n < context->count_threads; n++)
273 if (context->pt[n].io_loop_uv && LWS_LIBUV_ENABLED(context))
274 uv_stop(context->pt[n].io_loop_uv);
275 // TODO uv_stop check foreign loop? or not?
279 * This does not actually stop the event loop. The reason is we have to pass
280 * libuv handle closures through its event loop. So this tries to close all
281 * wsi, and set a flag; when all the wsi closures are finalized then we
282 * actually stop the libuv event loops.
286 lws_libuv_stop(struct lws_context *context)
288 struct lws_context_per_thread *pt;
291 context->requested_kill = 1;
293 m = context->count_threads;
294 context->being_destroyed = 1;
297 pt = &context->pt[m];
299 for (n = 0; (unsigned int)n < context->pt[m].fds_count; n++) {
300 struct lws *wsi = wsi_from_fd(context, pt->fds[n].fd);
304 lws_close_free_wsi(wsi,
305 LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY
306 /* no protocol close */);
311 if (context->count_wsi_allocated == 0)
312 lws_libuv_kill(context);
315 LWS_VISIBLE uv_loop_t *
316 lws_uv_getloop(struct lws_context *context, int tsi)
318 if (context->pt[tsi].io_loop_uv && LWS_LIBUV_ENABLED(context))
319 return context->pt[tsi].io_loop_uv;
325 lws_libuv_closewsi(uv_handle_t* handle)
327 struct lws *n = NULL, *wsi = (struct lws *)(((void *)handle) -
328 (void *)(&n->w_read.uv_watcher));
329 struct lws_context *context = lws_get_context(wsi);
331 lws_close_free_wsi_final(wsi);
333 if (context->requested_kill && context->count_wsi_allocated == 0)
334 lws_libuv_kill(context);
338 lws_libuv_closehandle(struct lws *wsi)
340 struct lws_context *context = lws_get_context(wsi);
342 /* required to defer actual deletion until libuv has processed it */
344 uv_close((uv_handle_t*)&wsi->w_read.uv_watcher, lws_libuv_closewsi);
346 if (context->requested_kill && context->count_wsi_allocated == 0)
347 lws_libuv_kill(context);