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"
24 void lws_feature_status_libev(struct lws_context_creation_info *info)
26 if (info->options & LWS_SERVER_OPTION_LIBEV)
27 lwsl_notice("libev support compiled in and enabled\n");
29 lwsl_notice("libev support compiled in but disabled\n");
33 lws_accept_cb(struct ev_loop *loop, struct ev_io *watcher, int revents)
35 struct lws_io_watcher *lws_io = container_of(watcher,
36 struct lws_io_watcher, ev_watcher);
37 struct lws_context *context = lws_io->context;
38 struct lws_pollfd eventfd;
40 if (revents & EV_ERROR)
43 eventfd.fd = watcher->fd;
45 eventfd.revents = EV_NONE;
46 if (revents & EV_READ) {
47 eventfd.events |= LWS_POLLIN;
48 eventfd.revents |= LWS_POLLIN;
50 if (revents & EV_WRITE) {
51 eventfd.events |= LWS_POLLOUT;
52 eventfd.revents |= LWS_POLLOUT;
54 lws_service_fd(context, &eventfd);
58 lws_ev_sigint_cb(struct ev_loop *loop, struct ev_signal *watcher, int revents)
60 ev_break(loop, EVBREAK_ALL);
64 lws_ev_sigint_cfg(struct lws_context *context, int use_ev_sigint,
65 lws_ev_signal_cb_t *cb)
67 context->use_ev_sigint = use_ev_sigint;
69 context->lws_ev_sigint_cb = cb;
71 context->lws_ev_sigint_cb = &lws_ev_sigint_cb;
77 lws_ev_initloop(struct lws_context *context, struct ev_loop *loop, int tsi)
79 struct ev_signal *w_sigint = &context->pt[tsi].w_sigint.ev_watcher;
80 struct ev_io *w_accept = &context->pt[tsi].w_accept.ev_watcher;
81 const char * backend_name;
86 loop = ev_loop_new(0);
88 context->pt[tsi].ev_loop_foreign = 1;
90 context->pt[tsi].io_loop_ev = loop;
93 * Initialize the accept w_accept with the listening socket
94 * and register a callback for read operations
96 ev_io_init(w_accept, lws_accept_cb, context->pt[tsi].lserv_fd, EV_READ);
97 ev_io_start(context->pt[tsi].io_loop_ev, w_accept);
99 /* Register the signal watcher unless the user says not to */
100 if (context->use_ev_sigint) {
101 ev_signal_init(w_sigint, context->lws_ev_sigint_cb, SIGINT);
102 ev_signal_start(context->pt[tsi].io_loop_ev, w_sigint);
104 backend = ev_backend(loop);
107 case EVBACKEND_SELECT:
108 backend_name = "select";
111 backend_name = "poll";
113 case EVBACKEND_EPOLL:
114 backend_name = "epoll";
116 case EVBACKEND_KQUEUE:
117 backend_name = "kqueue";
119 case EVBACKEND_DEVPOLL:
120 backend_name = "/dev/poll";
123 backend_name = "Solaris 10 \"port\"";
126 backend_name = "Unknown libev backend";
130 lwsl_notice(" libev backend: %s\n", backend_name);
136 lws_libev_destroyloop(struct lws_context *context, int tsi)
138 struct lws_context_per_thread *pt = &context->pt[tsi];
140 if (!(context->options & LWS_SERVER_OPTION_LIBEV))
146 ev_io_stop(pt->io_loop_ev, &pt->w_accept.ev_watcher);
147 if (context->use_ev_sigint)
148 ev_signal_stop(pt->io_loop_ev,
149 &pt->w_sigint.ev_watcher);
150 if (!pt->ev_loop_foreign)
151 ev_loop_destroy(pt->io_loop_ev);
155 lws_libev_accept(struct lws *new_wsi, int accept_fd)
157 struct lws_context *context = lws_get_context(new_wsi);
158 struct ev_io *r = &new_wsi->w_read.ev_watcher;
159 struct ev_io *w = &new_wsi->w_write.ev_watcher;
161 if (!LWS_LIBEV_ENABLED(context))
164 new_wsi->w_read.context = context;
165 new_wsi->w_write.context = context;
166 ev_io_init(r, lws_accept_cb, accept_fd, EV_READ);
167 ev_io_init(w, lws_accept_cb, accept_fd, EV_WRITE);
171 lws_libev_io(struct lws *wsi, int flags)
173 struct lws_context *context = lws_get_context(wsi);
174 struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
176 if (!LWS_LIBEV_ENABLED(context))
179 if (!pt->io_loop_ev || context->being_destroyed)
182 assert((flags & (LWS_EV_START | LWS_EV_STOP)) &&
183 (flags & (LWS_EV_READ | LWS_EV_WRITE)));
185 if (flags & LWS_EV_START) {
186 if (flags & LWS_EV_WRITE)
187 ev_io_start(pt->io_loop_ev, &wsi->w_write.ev_watcher);
188 if (flags & LWS_EV_READ)
189 ev_io_start(pt->io_loop_ev, &wsi->w_read.ev_watcher);
191 if (flags & LWS_EV_WRITE)
192 ev_io_stop(pt->io_loop_ev, &wsi->w_write.ev_watcher);
193 if (flags & LWS_EV_READ)
194 ev_io_stop(pt->io_loop_ev, &wsi->w_read.ev_watcher);
199 lws_libev_init_fd_table(struct lws_context *context)
203 if (!LWS_LIBEV_ENABLED(context))
206 for (n = 0; n < context->count_threads; n++) {
207 context->pt[n].w_accept.context = context;
208 context->pt[n].w_sigint.context = context;
215 lws_libev_run(const struct lws_context *context, int tsi)
217 if (context->pt[tsi].io_loop_ev && LWS_LIBEV_ENABLED(context))
218 ev_run(context->pt[tsi].io_loop_ev, 0);