2017-03-15 19:41:11 +05:30
|
|
|
/*
|
|
|
|
* libwebsockets - small server side websockets and web server implementation
|
|
|
|
*
|
2017-09-23 12:55:21 +08:00
|
|
|
* Copyright (C) 2010-2017 Andy Green <andy@warmcat.com>
|
2017-03-15 19:41:11 +05:30
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation:
|
|
|
|
* version 2.1 of the License.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
* MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "private-libwebsockets.h"
|
|
|
|
|
2018-04-27 08:27:16 +08:00
|
|
|
void lws_feature_status_libevent(const struct lws_context_creation_info *info)
|
2017-03-15 19:41:11 +05:30
|
|
|
{
|
2017-09-23 12:55:21 +08:00
|
|
|
if (lws_check_opt(info->options, LWS_SERVER_OPTION_LIBEVENT))
|
|
|
|
lwsl_info("libevent support compiled in and enabled\n");
|
|
|
|
else
|
|
|
|
lwsl_info("libevent support compiled in but disabled\n");
|
2017-03-15 19:41:11 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
lws_event_cb(evutil_socket_t sock_fd, short revents, void *ctx)
|
|
|
|
{
|
2017-09-23 12:55:21 +08:00
|
|
|
struct lws_io_watcher *lws_io = (struct lws_io_watcher *)ctx;
|
|
|
|
struct lws_context *context = lws_io->context;
|
|
|
|
struct lws_pollfd eventfd;
|
|
|
|
|
|
|
|
if (revents & EV_TIMEOUT)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* !!! EV_CLOSED doesn't exist in libevent2 */
|
|
|
|
#if LIBEVENT_VERSION_NUMBER < 0x02000000
|
|
|
|
if (revents & EV_CLOSED) {
|
2018-04-29 08:34:19 +08:00
|
|
|
event_del(lws_io->event.watcher);
|
|
|
|
event_free(lws_io->event.watcher);
|
2017-09-23 12:55:21 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
eventfd.fd = sock_fd;
|
|
|
|
eventfd.events = 0;
|
|
|
|
eventfd.revents = 0;
|
|
|
|
if (revents & EV_READ) {
|
|
|
|
eventfd.events |= LWS_POLLIN;
|
|
|
|
eventfd.revents |= LWS_POLLIN;
|
|
|
|
}
|
|
|
|
if (revents & EV_WRITE) {
|
|
|
|
eventfd.events |= LWS_POLLOUT;
|
|
|
|
eventfd.revents |= LWS_POLLOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
lws_service_fd(context, &eventfd);
|
2017-03-15 19:41:11 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
LWS_VISIBLE void
|
|
|
|
lws_event_sigint_cb(evutil_socket_t sock_fd, short revents, void *ctx)
|
|
|
|
{
|
2017-09-23 12:55:21 +08:00
|
|
|
struct lws_context_per_thread *pt = ctx;
|
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
if (pt->context->eventlib_signal_cb) {
|
|
|
|
pt->context->eventlib_signal_cb(
|
|
|
|
(void *)(lws_intptr_t)sock_fd, revents);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2018-04-29 08:34:19 +08:00
|
|
|
if (!pt->event_loop_foreign)
|
|
|
|
event_base_loopbreak(pt->event.io_loop);
|
2017-03-15 19:41:11 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
static int
|
|
|
|
elops_init_pt_event(struct lws_context *context, void *_loop, int tsi)
|
2017-03-15 19:41:11 +05:30
|
|
|
{
|
2017-09-23 12:55:21 +08:00
|
|
|
struct lws_vhost *vh = context->vhost_list;
|
2018-04-29 10:44:36 +08:00
|
|
|
struct event_base *loop = (struct event_base *)_loop;
|
2017-09-23 12:55:21 +08:00
|
|
|
|
|
|
|
if (!loop)
|
2018-04-29 08:34:19 +08:00
|
|
|
context->pt[tsi].event.io_loop = event_base_new();
|
2017-09-23 12:55:21 +08:00
|
|
|
else {
|
2018-04-29 08:34:19 +08:00
|
|
|
context->pt[tsi].event_loop_foreign = 1;
|
|
|
|
context->pt[tsi].event.io_loop = loop;
|
2017-09-23 12:55:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize all events with the listening sockets
|
|
|
|
* and register a callback for read operations
|
|
|
|
*/
|
|
|
|
|
|
|
|
while (vh) {
|
|
|
|
if (vh->lserv_wsi) {
|
|
|
|
vh->lserv_wsi->w_read.context = context;
|
2018-04-29 08:34:19 +08:00
|
|
|
vh->lserv_wsi->w_read.event.watcher = event_new(
|
2017-09-23 12:55:21 +08:00
|
|
|
loop, vh->lserv_wsi->desc.sockfd,
|
|
|
|
(EV_READ | EV_PERSIST), lws_event_cb,
|
|
|
|
&vh->lserv_wsi->w_read);
|
2018-04-29 08:34:19 +08:00
|
|
|
event_add(vh->lserv_wsi->w_read.event.watcher, NULL);
|
2017-09-23 12:55:21 +08:00
|
|
|
}
|
|
|
|
vh = vh->vhost_next;
|
|
|
|
}
|
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
/* Register the signal watcher unless it's a foreign loop */
|
|
|
|
if (context->pt[tsi].event_loop_foreign)
|
2017-09-23 12:55:21 +08:00
|
|
|
return 0;
|
|
|
|
|
2018-04-29 08:34:19 +08:00
|
|
|
context->pt[tsi].w_sigint.event.watcher = evsignal_new(loop, SIGINT,
|
2018-04-29 10:44:36 +08:00
|
|
|
lws_event_sigint_cb, &context->pt[tsi]);
|
2018-04-29 08:34:19 +08:00
|
|
|
event_add(context->pt[tsi].w_sigint.event.watcher, NULL);
|
2017-09-23 12:55:21 +08:00
|
|
|
|
|
|
|
return 0;
|
2017-03-15 19:41:11 +05:30
|
|
|
}
|
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
static int
|
|
|
|
elops_init_context_event(struct lws_context *context,
|
|
|
|
const struct lws_context_creation_info *info)
|
2017-03-15 19:41:11 +05:30
|
|
|
{
|
2018-04-29 10:44:36 +08:00
|
|
|
int n;
|
2017-09-23 12:55:21 +08:00
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
context->eventlib_signal_cb = info->signal_cb;
|
2017-09-23 12:55:21 +08:00
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
for (n = 0; n < context->count_threads; n++)
|
|
|
|
context->pt[n].w_sigint.context = context;
|
2017-09-23 12:55:21 +08:00
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
return 0;
|
2017-03-15 19:41:11 +05:30
|
|
|
}
|
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
static void
|
|
|
|
elops_accept_event(struct lws *wsi)
|
2017-03-15 19:41:11 +05:30
|
|
|
{
|
2018-04-29 10:44:36 +08:00
|
|
|
struct lws_context *context = lws_get_context(wsi);
|
2017-09-23 12:55:21 +08:00
|
|
|
struct lws_context_per_thread *pt;
|
|
|
|
int fd;
|
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
wsi->w_read.context = context;
|
|
|
|
wsi->w_write.context = context;
|
2017-09-23 12:55:21 +08:00
|
|
|
|
|
|
|
// Initialize the event
|
2018-04-29 10:44:36 +08:00
|
|
|
pt = &context->pt[(int)wsi->tsi];
|
2017-09-23 12:55:21 +08:00
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
if (wsi->role_ops->file_handle)
|
|
|
|
fd = wsi->desc.filefd;
|
2017-09-23 12:55:21 +08:00
|
|
|
else
|
2018-04-29 10:44:36 +08:00
|
|
|
fd = wsi->desc.sockfd;
|
2017-03-15 19:41:11 +05:30
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
wsi->w_read.event.watcher = event_new(pt->event.io_loop, fd,
|
|
|
|
(EV_READ | EV_PERSIST), lws_event_cb, &wsi->w_read);
|
|
|
|
wsi->w_write.event.watcher = event_new(pt->event.io_loop, fd,
|
|
|
|
(EV_WRITE | EV_PERSIST), lws_event_cb, &wsi->w_write);
|
2018-01-17 02:05:45 +01:00
|
|
|
}
|
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
static void
|
|
|
|
elops_io_event(struct lws *wsi, int flags)
|
2017-03-15 19:41:11 +05:30
|
|
|
{
|
2017-09-23 12:55:21 +08:00
|
|
|
struct lws_context *context = lws_get_context(wsi);
|
|
|
|
struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
|
|
|
|
|
2018-04-29 08:34:19 +08:00
|
|
|
if (!pt->event.io_loop || context->being_destroyed)
|
2017-09-23 12:55:21 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
assert((flags & (LWS_EV_START | LWS_EV_STOP)) &&
|
|
|
|
(flags & (LWS_EV_READ | LWS_EV_WRITE)));
|
|
|
|
|
|
|
|
if (flags & LWS_EV_START) {
|
|
|
|
if (flags & LWS_EV_WRITE)
|
2018-04-29 08:34:19 +08:00
|
|
|
event_add(wsi->w_write.event.watcher, NULL);
|
2017-09-23 12:55:21 +08:00
|
|
|
if (flags & LWS_EV_READ)
|
2018-04-29 08:34:19 +08:00
|
|
|
event_add(wsi->w_read.event.watcher, NULL);
|
2017-09-23 12:55:21 +08:00
|
|
|
} else {
|
|
|
|
if (flags & LWS_EV_WRITE)
|
2018-04-29 08:34:19 +08:00
|
|
|
event_del(wsi->w_write.event.watcher);
|
2017-09-23 12:55:21 +08:00
|
|
|
|
|
|
|
if (flags & LWS_EV_READ)
|
2018-04-29 08:34:19 +08:00
|
|
|
event_del(wsi->w_read.event.watcher);
|
2017-09-23 12:55:21 +08:00
|
|
|
}
|
2017-03-15 19:41:11 +05:30
|
|
|
}
|
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
static void
|
|
|
|
elops_run_pt_event(struct lws_context *context, int tsi)
|
2017-03-15 19:41:11 +05:30
|
|
|
{
|
2018-04-29 10:44:36 +08:00
|
|
|
/* Run / Dispatch the event_base loop */
|
|
|
|
if (context->pt[tsi].event.io_loop &&
|
|
|
|
LWS_LIBEVENT_ENABLED(context))
|
|
|
|
event_base_dispatch(context->pt[tsi].event.io_loop);
|
|
|
|
}
|
2017-09-23 12:55:21 +08:00
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
static void
|
|
|
|
elops_destroy_pt_event(struct lws_context *context, int tsi)
|
|
|
|
{
|
|
|
|
struct lws_context_per_thread *pt = &context->pt[tsi];
|
|
|
|
struct lws_vhost *vh = context->vhost_list;
|
2017-03-15 19:41:11 +05:30
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
if (!lws_check_opt(context->options, LWS_SERVER_OPTION_LIBEVENT))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!pt->event.io_loop)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free all events with the listening sockets
|
|
|
|
*/
|
|
|
|
while (vh) {
|
|
|
|
if (vh->lserv_wsi) {
|
|
|
|
event_free(vh->lserv_wsi->w_read.event.watcher);
|
|
|
|
vh->lserv_wsi->w_read.event.watcher = NULL;
|
|
|
|
}
|
|
|
|
vh = vh->vhost_next;
|
|
|
|
}
|
2017-03-15 19:41:11 +05:30
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
if (!pt->event_loop_foreign)
|
|
|
|
event_free(pt->w_sigint.event.watcher);
|
|
|
|
if (!pt->event_loop_foreign)
|
|
|
|
event_base_free(pt->event.io_loop);
|
2017-03-15 19:41:11 +05:30
|
|
|
}
|
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
static void
|
|
|
|
elops_destroy_wsi_event(struct lws *wsi)
|
2017-03-15 19:41:11 +05:30
|
|
|
{
|
2018-04-29 10:44:36 +08:00
|
|
|
if (!wsi)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(wsi->w_read.event.watcher)
|
|
|
|
event_free(wsi->w_read.event.watcher);
|
|
|
|
|
|
|
|
if(wsi->w_write.event.watcher)
|
|
|
|
event_free(wsi->w_write.event.watcher);
|
2017-03-15 19:41:11 +05:30
|
|
|
}
|
2018-04-29 10:44:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
struct lws_event_loop_ops event_loop_ops_event = {
|
|
|
|
/* name */ "libevent",
|
|
|
|
/* init_context */ elops_init_context_event,
|
|
|
|
/* destroy_context1 */ NULL,
|
|
|
|
/* destroy_context2 */ NULL,
|
|
|
|
/* init_vhost_listen_wsi */ NULL,
|
|
|
|
/* init_pt */ elops_init_pt_event,
|
|
|
|
/* wsi_logical_close */ NULL,
|
|
|
|
/* check_client_connect_ok */ NULL,
|
|
|
|
/* close_handle_manually */ NULL,
|
|
|
|
/* accept */ elops_accept_event,
|
|
|
|
/* io */ elops_io_event,
|
|
|
|
/* run_pt */ elops_run_pt_event,
|
|
|
|
/* destroy_pt */ elops_destroy_pt_event,
|
|
|
|
/* destroy wsi */ elops_destroy_wsi_event,
|
|
|
|
|
|
|
|
/* periodic_events_available */ 0,
|
|
|
|
};
|