98c62d6247a321ed9c9fe5cde42acfe9e715ebf9
[platform/upstream/libwebsockets.git] / lib / libevent.c
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010-2014 Andy Green <andy@warmcat.com>
5  *
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.
10  *
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.
15  *
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,
19  *  MA  02110-1301  USA
20  */
21
22 #include "private-libwebsockets.h"
23
24 void lws_feature_status_libevent(struct lws_context_creation_info *info)
25 {
26   if (lws_check_opt(info->options, LWS_SERVER_OPTION_LIBEVENT))
27     lwsl_notice("libevent support compiled in and enabled\n");
28   else
29     lwsl_notice("libevent support compiled in but disabled\n");
30 }
31
32 static void
33 lws_event_cb(evutil_socket_t sock_fd, short revents, void *ctx)
34 {
35   struct lws_io_watcher *lws_io = (struct lws_io_watcher *)ctx;
36   struct lws_context *context = lws_io->context;
37   struct lws_pollfd eventfd;
38
39   if (revents & EV_TIMEOUT)
40     return;
41
42   if (revents & EV_CLOSED)
43   {
44     event_del(lws_io->event_watcher);
45     event_free(lws_io->event_watcher);
46     return;
47   }
48
49   eventfd.fd = sock_fd;
50   eventfd.events = 0;
51   eventfd.revents = 0;
52   if (revents & EV_READ)
53   {
54     eventfd.events |= LWS_POLLIN;
55     eventfd.revents |= LWS_POLLIN;
56   }
57   if (revents & EV_WRITE)
58   {
59     eventfd.events |= LWS_POLLOUT;
60     eventfd.revents |= LWS_POLLOUT;
61   }
62   lws_service_fd(context, &eventfd);
63 }
64
65 LWS_VISIBLE void
66 lws_event_sigint_cb(evutil_socket_t sock_fd, short revents, void *ctx)
67 {
68   struct lws_context_per_thread *pt = ctx;
69   if (!pt->ev_loop_foreign)
70     event_base_loopbreak(pt->io_loop_event_base);
71 }
72
73 LWS_VISIBLE int
74 lws_event_sigint_cfg(struct lws_context *context, int use_event_sigint,
75       lws_event_signal_cb_t *cb)
76 {
77   context->use_ev_sigint = use_event_sigint;
78   if (cb)
79     context->lws_event_sigint_cb = cb;
80   else
81     context->lws_event_sigint_cb = &lws_event_sigint_cb;
82
83   return 0;
84 }
85
86 LWS_VISIBLE int
87 lws_event_initloop(struct lws_context *context, struct event_base *loop,
88     int tsi)
89 {
90   if (!loop)
91   {
92     context->pt[tsi].io_loop_event_base = event_base_new();
93   }
94   else
95   {
96     context->pt[tsi].ev_loop_foreign = 1;
97     context->pt[tsi].io_loop_event_base = loop;
98   }
99
100   /*
101    * Initialize all events with the listening sockets
102    * and register a callback for read operations
103    */
104   struct lws_vhost *vh = context->vhost_list;
105   while (vh)
106   {
107     if (vh->lserv_wsi)
108     {
109       vh->lserv_wsi->w_read.context = context;
110       vh->lserv_wsi->w_read.event_watcher = event_new(
111           loop,
112           vh->lserv_wsi->desc.sockfd,
113           (EV_READ | EV_PERSIST),
114           lws_event_cb,
115           &vh->lserv_wsi->w_read);
116       event_add(vh->lserv_wsi->w_read.event_watcher, NULL);
117     }
118     vh = vh->vhost_next;
119   }
120
121   /* Register the signal watcher unless the user says not to */
122   if (context->use_ev_sigint)
123   {
124     struct event *w_sigint = evsignal_new(loop, SIGINT,
125         context->lws_event_sigint_cb, &context->pt[tsi]);
126     context->pt[tsi].w_sigint.event_watcher = w_sigint;
127     event_add(w_sigint, NULL);
128   }
129
130   return 0;
131 }
132
133 void
134 lws_libevent_destroyloop(struct lws_context *context, int tsi)
135 {
136   if (!lws_check_opt(context->options, LWS_SERVER_OPTION_LIBEVENT))
137     return;
138
139   struct lws_context_per_thread *pt = &context->pt[tsi];
140   if (!pt->io_loop_event_base)
141     return;
142
143   /*
144    * Free all events with the listening sockets
145    */
146   struct lws_vhost *vh = context->vhost_list;
147   while (vh)
148   {
149     if (vh->lserv_wsi)
150     {
151       event_free(vh->lserv_wsi->w_read.event_watcher);
152       vh->lserv_wsi->w_read.event_watcher = NULL;
153     }
154     vh = vh->vhost_next;
155   }
156
157   if (context->use_ev_sigint)
158     event_free(pt->w_sigint.event_watcher);
159   if (!pt->ev_loop_foreign)
160     event_base_free(pt->io_loop_event_base);
161 }
162
163 LWS_VISIBLE void
164 lws_libevent_accept(struct lws *new_wsi, lws_sock_file_fd_type desc)
165 {
166   struct lws_context *context = lws_get_context(new_wsi);
167   if (!LWS_LIBEVENT_ENABLED(context))
168     return;
169
170   new_wsi->w_read.context = context;
171   new_wsi->w_write.context = context;
172
173   // Initialize the event
174   struct lws_context_per_thread *pt = &context->pt[(int)new_wsi->tsi];
175   int fd;
176   if (new_wsi->mode == LWSCM_RAW_FILEDESC)
177     fd = desc.filefd;
178   else
179     fd = desc.sockfd;
180   new_wsi->w_read.event_watcher = event_new(pt->io_loop_event_base, fd,
181       (EV_READ | EV_PERSIST), lws_event_cb, &new_wsi->w_read);
182   new_wsi->w_write.event_watcher = event_new(pt->io_loop_event_base, fd,
183       (EV_WRITE | EV_PERSIST), lws_event_cb, &new_wsi->w_write);
184 }
185
186 LWS_VISIBLE void
187 lws_libevent_io(struct lws *wsi, int flags)
188 {
189   struct lws_context *context = lws_get_context(wsi);
190
191   if (!LWS_LIBEVENT_ENABLED(context))
192     return;
193
194   struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
195   if (!pt->io_loop_event_base || context->being_destroyed)
196     return;
197
198   assert((flags & (LWS_EV_START | LWS_EV_STOP)) &&
199          (flags & (LWS_EV_READ | LWS_EV_WRITE)));
200
201   if (flags & LWS_EV_START)
202   {
203     if (flags & LWS_EV_WRITE)
204     {
205       event_add(wsi->w_write.event_watcher, NULL);
206     }
207     if (flags & LWS_EV_READ)
208     {
209       event_add(wsi->w_read.event_watcher, NULL);
210     }
211   }
212   else
213   {
214     if (flags & LWS_EV_WRITE)
215     {
216       event_del(wsi->w_write.event_watcher);
217     }
218     if (flags & LWS_EV_READ)
219     {
220       event_del(wsi->w_read.event_watcher);
221     }
222   }
223 }
224
225 LWS_VISIBLE int
226 lws_libevent_init_fd_table(struct lws_context *context)
227 {
228   if (!LWS_LIBEVENT_ENABLED(context))
229     return 0;
230
231   int n;
232   for (n = 0; n < context->count_threads; n++)
233   {
234     context->pt[n].w_sigint.context = context;
235   }
236
237   return 1;
238 }
239
240 LWS_VISIBLE void
241 lws_libevent_run(const struct lws_context *context, int tsi)
242 {
243   // Run/Dispatch the event_base loop
244   if (context->pt[tsi].io_loop_event_base && LWS_LIBEVENT_ENABLED(context))
245     event_base_dispatch(context->pt[tsi].io_loop_event_base);
246 }