2017-03-15 19:41:11 +05:30
|
|
|
/*
|
|
|
|
* libwebsockets - small server side websockets and web server implementation
|
|
|
|
*
|
2019-08-14 10:44:14 +01:00
|
|
|
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
|
2017-03-15 19:41:11 +05:30
|
|
|
*
|
2019-08-14 10:44:14 +01:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2017-03-15 19:41:11 +05:30
|
|
|
*
|
2019-08-14 10:44:14 +01:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2017-03-15 19:41:11 +05:30
|
|
|
*
|
2019-08-14 10:44:14 +01:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
2017-03-15 19:41:11 +05:30
|
|
|
*/
|
|
|
|
|
2019-08-15 10:49:52 +01:00
|
|
|
#include "private-lib-core.h"
|
2020-08-27 15:37:14 +01:00
|
|
|
#include "private-lib-event-libs-libevent.h"
|
|
|
|
|
|
|
|
#define pt_to_priv_event(_pt) ((struct lws_pt_eventlibs_libevent *)(_pt)->evlib_pt)
|
|
|
|
#define wsi_to_priv_event(_w) ((struct lws_wsi_eventlibs_libevent *)(_w)->evlib_wsi)
|
2017-03-15 19:41:11 +05:30
|
|
|
|
2018-05-02 18:35:58 +08:00
|
|
|
static void
|
|
|
|
lws_event_hrtimer_cb(int fd, short event, void *p)
|
|
|
|
{
|
|
|
|
struct lws_context_per_thread *pt = (struct lws_context_per_thread *)p;
|
2020-08-27 15:37:14 +01:00
|
|
|
struct lws_pt_eventlibs_libevent *ptpr = pt_to_priv_event(pt);
|
2018-05-02 18:35:58 +08:00
|
|
|
struct timeval tv;
|
|
|
|
lws_usec_t us;
|
|
|
|
|
|
|
|
lws_pt_lock(pt, __func__);
|
2020-05-28 12:48:17 +01:00
|
|
|
us = __lws_sul_service_ripe(pt->pt_sul_owner, LWS_COUNT_PT_SUL_OWNERS,
|
|
|
|
lws_now_usecs());
|
2019-08-08 06:30:14 +01:00
|
|
|
if (us) {
|
|
|
|
tv.tv_sec = us / LWS_US_PER_SEC;
|
|
|
|
tv.tv_usec = us - (tv.tv_sec * LWS_US_PER_SEC);
|
2020-08-27 15:37:14 +01:00
|
|
|
evtimer_add(ptpr->hrtimer, &tv);
|
2018-05-02 18:35:58 +08:00
|
|
|
}
|
|
|
|
lws_pt_unlock(pt);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
lws_event_idle_timer_cb(int fd, short event, void *p)
|
|
|
|
{
|
|
|
|
struct lws_context_per_thread *pt = (struct lws_context_per_thread *)p;
|
2020-08-27 15:37:14 +01:00
|
|
|
struct lws_pt_eventlibs_libevent *ptpr = pt_to_priv_event(pt);
|
2018-05-02 18:35:58 +08:00
|
|
|
struct timeval tv;
|
|
|
|
lws_usec_t us;
|
|
|
|
|
2020-01-23 11:12:28 +00:00
|
|
|
if (pt->is_destroyed)
|
|
|
|
return;
|
|
|
|
|
2018-05-02 18:35:58 +08:00
|
|
|
lws_service_do_ripe_rxflow(pt);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* is there anybody with pending stuff that needs service forcing?
|
|
|
|
*/
|
|
|
|
if (!lws_service_adjust_timeout(pt->context, 1, pt->tid)) {
|
|
|
|
/* -1 timeout means just do forced service */
|
2019-09-16 15:36:12 +01:00
|
|
|
_lws_plat_service_forced_tsi(pt->context, pt->tid);
|
2018-05-02 18:35:58 +08:00
|
|
|
/* still somebody left who wants forced service? */
|
|
|
|
if (!lws_service_adjust_timeout(pt->context, 1, pt->tid)) {
|
|
|
|
/* yes... come back again later */
|
|
|
|
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 1000;
|
2020-08-27 15:37:14 +01:00
|
|
|
evtimer_add(ptpr->idle_timer, &tv);
|
2018-05-02 18:35:58 +08:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-16 17:26:10 +08:00
|
|
|
lwsl_debug("%s: wait\n", __func__);
|
|
|
|
|
2018-05-02 18:35:58 +08:00
|
|
|
/* account for hrtimer */
|
|
|
|
|
|
|
|
lws_pt_lock(pt, __func__);
|
2020-05-28 12:48:17 +01:00
|
|
|
us = __lws_sul_service_ripe(pt->pt_sul_owner, LWS_COUNT_PT_SUL_OWNERS,
|
|
|
|
lws_now_usecs());
|
2019-08-08 06:30:14 +01:00
|
|
|
if (us) {
|
|
|
|
tv.tv_sec = us / LWS_US_PER_SEC;
|
|
|
|
tv.tv_usec = us - (tv.tv_sec * LWS_US_PER_SEC);
|
2020-08-27 15:37:14 +01:00
|
|
|
evtimer_add(ptpr->hrtimer, &tv);
|
2018-05-02 18:35:58 +08:00
|
|
|
}
|
|
|
|
lws_pt_unlock(pt);
|
2020-01-23 11:12:28 +00:00
|
|
|
|
|
|
|
if (pt->destroy_self)
|
|
|
|
lws_context_destroy(pt->context);
|
2018-05-02 18:35:58 +08:00
|
|
|
}
|
|
|
|
|
2017-03-15 19:41:11 +05:30
|
|
|
static void
|
|
|
|
lws_event_cb(evutil_socket_t sock_fd, short revents, void *ctx)
|
|
|
|
{
|
2020-08-27 15:37:14 +01:00
|
|
|
struct lws_signal_watcher_libevent *lws_io =
|
|
|
|
(struct lws_signal_watcher_libevent *)ctx;
|
2017-09-23 12:55:21 +08:00
|
|
|
struct lws_context *context = lws_io->context;
|
2018-05-02 18:35:58 +08:00
|
|
|
struct lws_context_per_thread *pt;
|
2017-09-23 12:55:21 +08:00
|
|
|
struct lws_pollfd eventfd;
|
2018-05-02 18:35:58 +08:00
|
|
|
struct timeval tv;
|
|
|
|
struct lws *wsi;
|
2017-09-23 12:55:21 +08:00
|
|
|
|
|
|
|
if (revents & EV_TIMEOUT)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* !!! EV_CLOSED doesn't exist in libevent2 */
|
2018-05-02 18:35:58 +08:00
|
|
|
#if LIBEVENT_VERSION_NUMBER < 0x02000000
|
2017-09-23 12:55:21 +08:00
|
|
|
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;
|
|
|
|
}
|
2018-05-02 18:35:58 +08:00
|
|
|
#endif
|
2017-09-23 12:55:21 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-05-02 18:35:58 +08:00
|
|
|
wsi = wsi_from_fd(context, sock_fd);
|
2020-01-23 11:12:28 +00:00
|
|
|
if (!wsi)
|
2019-03-21 09:27:45 +08:00
|
|
|
return;
|
2020-01-23 11:12:28 +00:00
|
|
|
|
2018-05-02 18:35:58 +08:00
|
|
|
pt = &context->pt[(int)wsi->tsi];
|
2020-01-23 11:12:28 +00:00
|
|
|
if (pt->is_destroyed)
|
|
|
|
return;
|
2018-05-02 18:35:58 +08:00
|
|
|
|
|
|
|
lws_service_fd_tsi(context, &eventfd, wsi->tsi);
|
|
|
|
|
2020-01-23 11:12:28 +00:00
|
|
|
if (pt->destroy_self) {
|
2020-03-10 19:13:43 +00:00
|
|
|
lwsl_notice("%s: pt destroy self coming true\n", __func__);
|
2020-01-23 11:12:28 +00:00
|
|
|
lws_context_destroy(pt->context);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-02 18:35:58 +08:00
|
|
|
/* set the idle timer for 1ms ahead */
|
|
|
|
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 1000;
|
2020-08-27 15:37:14 +01:00
|
|
|
evtimer_add(pt_to_priv_event(pt)->idle_timer, &tv);
|
2017-03-15 19:41:11 +05:30
|
|
|
}
|
|
|
|
|
2020-01-02 08:32:23 +00:00
|
|
|
void
|
2017-03-15 19:41:11 +05:30
|
|
|
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;
|
2020-10-17 12:52:18 +01:00
|
|
|
struct event *signal = pt_to_priv_event(pt)->w_sigint.watcher;
|
2017-09-23 12:55:21 +08:00
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
if (pt->context->eventlib_signal_cb) {
|
2018-04-30 09:16:04 +08:00
|
|
|
pt->context->eventlib_signal_cb((void *)(lws_intptr_t)sock_fd,
|
|
|
|
event_get_signal(signal));
|
2018-04-29 10:44:36 +08:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2018-04-29 08:34:19 +08:00
|
|
|
if (!pt->event_loop_foreign)
|
2020-08-27 15:37:14 +01:00
|
|
|
event_base_loopbreak(pt_to_priv_event(pt)->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;
|
2018-05-02 18:35:58 +08:00
|
|
|
struct lws_context_per_thread *pt = &context->pt[tsi];
|
2020-08-27 15:37:14 +01:00
|
|
|
struct lws_pt_eventlibs_libevent *ptpr = pt_to_priv_event(pt);
|
2017-09-23 12:55:21 +08:00
|
|
|
|
2018-04-30 09:16:04 +08:00
|
|
|
lwsl_info("%s: loop %p\n", __func__, _loop);
|
|
|
|
|
2017-09-23 12:55:21 +08:00
|
|
|
if (!loop)
|
2018-04-30 09:16:04 +08:00
|
|
|
loop = event_base_new();
|
|
|
|
else
|
2018-04-29 08:34:19 +08:00
|
|
|
context->pt[tsi].event_loop_foreign = 1;
|
2018-04-30 09:16:04 +08:00
|
|
|
|
|
|
|
if (!loop) {
|
|
|
|
lwsl_err("%s: creating event base failed\n", __func__);
|
|
|
|
|
|
|
|
return -1;
|
2017-09-23 12:55:21 +08:00
|
|
|
}
|
|
|
|
|
2020-08-27 15:37:14 +01:00
|
|
|
ptpr->io_loop = loop;
|
2018-04-30 09:16:04 +08:00
|
|
|
|
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) {
|
2020-08-27 15:37:14 +01:00
|
|
|
struct lws_io_watcher_libevent *w_read =
|
|
|
|
&(wsi_to_priv_event(vh->lserv_wsi)->w_read);
|
|
|
|
|
|
|
|
w_read->context = context;
|
|
|
|
w_read->watcher = event_new(
|
2017-09-23 12:55:21 +08:00
|
|
|
loop, vh->lserv_wsi->desc.sockfd,
|
|
|
|
(EV_READ | EV_PERSIST), lws_event_cb,
|
2020-08-27 15:37:14 +01:00
|
|
|
w_read);
|
|
|
|
event_add(w_read->watcher, NULL);
|
|
|
|
w_read->set = 1;
|
2017-09-23 12:55:21 +08:00
|
|
|
}
|
|
|
|
vh = vh->vhost_next;
|
|
|
|
}
|
|
|
|
|
2018-05-02 18:35:58 +08:00
|
|
|
/* static event loop objects */
|
|
|
|
|
2020-08-27 15:37:14 +01:00
|
|
|
ptpr->hrtimer = event_new(loop, -1, EV_PERSIST,
|
2018-05-02 18:35:58 +08:00
|
|
|
lws_event_hrtimer_cb, pt);
|
|
|
|
|
2020-08-27 15:37:14 +01:00
|
|
|
ptpr->idle_timer = event_new(loop, -1, 0,
|
2019-02-16 17:26:10 +08:00
|
|
|
lws_event_idle_timer_cb, pt);
|
2018-05-02 18:35:58 +08:00
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
/* Register the signal watcher unless it's a foreign loop */
|
2018-05-02 18:35:58 +08:00
|
|
|
|
|
|
|
if (pt->event_loop_foreign)
|
2017-09-23 12:55:21 +08:00
|
|
|
return 0;
|
|
|
|
|
2020-08-27 15:37:14 +01:00
|
|
|
ptpr->w_sigint.watcher = evsignal_new(loop, SIGINT,
|
2018-05-02 18:35:58 +08:00
|
|
|
lws_event_sigint_cb, pt);
|
2020-08-27 15:37:14 +01:00
|
|
|
event_add(ptpr->w_sigint.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++)
|
2020-08-27 15:37:14 +01:00
|
|
|
pt_to_priv_event(&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-10-13 11:59:04 +08:00
|
|
|
static int
|
2018-04-29 10:44:36 +08:00
|
|
|
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;
|
2020-08-27 15:37:14 +01:00
|
|
|
struct lws_pt_eventlibs_libevent *ptpr;
|
|
|
|
struct lws_wsi_eventlibs_libevent *wpr = wsi_to_priv_event(wsi);
|
2017-09-23 12:55:21 +08:00
|
|
|
int fd;
|
|
|
|
|
2020-08-27 15:37:14 +01:00
|
|
|
wpr->w_read.context = context;
|
|
|
|
wpr->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];
|
2020-08-27 15:37:14 +01:00
|
|
|
ptpr = pt_to_priv_event(pt);
|
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
|
|
|
|
2020-08-27 15:37:14 +01:00
|
|
|
wpr->w_read.watcher = event_new(ptpr->io_loop, fd,
|
|
|
|
(EV_READ | EV_PERSIST), lws_event_cb, &wpr->w_read);
|
|
|
|
wpr->w_write.watcher = event_new(ptpr->io_loop, fd,
|
|
|
|
(EV_WRITE | EV_PERSIST), lws_event_cb, &wpr->w_write);
|
2018-10-13 11:59:04 +08:00
|
|
|
|
|
|
|
return 0;
|
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
|
|
|
{
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
struct lws_context_per_thread *pt = &wsi->a.context->pt[(int)wsi->tsi];
|
2020-08-27 15:37:14 +01:00
|
|
|
struct lws_pt_eventlibs_libevent *ptpr = pt_to_priv_event(pt);
|
|
|
|
struct lws_wsi_eventlibs_libevent *wpr = wsi_to_priv_event(wsi);
|
2017-09-23 12:55:21 +08:00
|
|
|
|
2020-08-27 15:37:14 +01:00
|
|
|
if (!ptpr->io_loop || wsi->a.context->being_destroyed ||
|
2020-01-23 11:12:28 +00:00
|
|
|
pt->is_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) {
|
2020-08-27 15:37:14 +01:00
|
|
|
if ((flags & LWS_EV_WRITE) && !wpr->w_write.set) {
|
|
|
|
event_add(wpr->w_write.watcher, NULL);
|
|
|
|
wpr->w_write.set = 1;
|
2020-06-19 19:54:17 +01:00
|
|
|
}
|
2018-04-30 09:16:04 +08:00
|
|
|
|
2020-08-27 15:37:14 +01:00
|
|
|
if ((flags & LWS_EV_READ) && !wpr->w_read.set) {
|
|
|
|
event_add(wpr->w_read.watcher, NULL);
|
|
|
|
wpr->w_read.set = 1;
|
2020-06-19 19:54:17 +01:00
|
|
|
}
|
2017-09-23 12:55:21 +08:00
|
|
|
} else {
|
2020-08-27 15:37:14 +01:00
|
|
|
if ((flags & LWS_EV_WRITE) && wpr->w_write.set) {
|
|
|
|
event_del(wpr->w_write.watcher);
|
|
|
|
wpr->w_write.set = 0;
|
2020-06-19 19:54:17 +01:00
|
|
|
}
|
2017-09-23 12:55:21 +08:00
|
|
|
|
2020-08-27 15:37:14 +01:00
|
|
|
if ((flags & LWS_EV_READ) && wpr->w_read.set) {
|
|
|
|
event_del(wpr->w_read.watcher);
|
|
|
|
wpr->w_read.set = 0;
|
2020-06-19 19:54:17 +01:00
|
|
|
}
|
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 */
|
2020-08-27 15:37:14 +01:00
|
|
|
if (pt_to_priv_event(&context->pt[tsi])->io_loop)
|
|
|
|
event_base_dispatch(
|
|
|
|
pt_to_priv_event(&context->pt[tsi])->io_loop);
|
2018-04-29 10:44:36 +08:00
|
|
|
}
|
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];
|
2020-08-27 15:37:14 +01:00
|
|
|
struct lws_pt_eventlibs_libevent *ptpr = pt_to_priv_event(pt);
|
2018-04-29 10:44:36 +08:00
|
|
|
struct lws_vhost *vh = context->vhost_list;
|
2017-03-15 19:41:11 +05:30
|
|
|
|
2018-04-30 09:16:04 +08:00
|
|
|
lwsl_info("%s\n", __func__);
|
2018-04-29 10:44:36 +08:00
|
|
|
|
2020-08-27 15:37:14 +01:00
|
|
|
if (!ptpr->io_loop)
|
2018-04-29 10:44:36 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free all events with the listening sockets
|
|
|
|
*/
|
|
|
|
while (vh) {
|
|
|
|
if (vh->lserv_wsi) {
|
2020-08-27 15:37:14 +01:00
|
|
|
struct lws_wsi_eventlibs_libevent *w =
|
|
|
|
wsi_to_priv_event(vh->lserv_wsi);
|
|
|
|
|
|
|
|
event_free(w->w_read.watcher);
|
|
|
|
w->w_read.watcher = NULL;
|
|
|
|
event_free(w->w_write.watcher);
|
|
|
|
w->w_write.watcher = NULL;
|
2018-04-29 10:44:36 +08:00
|
|
|
}
|
|
|
|
vh = vh->vhost_next;
|
|
|
|
}
|
2017-03-15 19:41:11 +05:30
|
|
|
|
2020-08-27 15:37:14 +01:00
|
|
|
event_free(ptpr->hrtimer);
|
|
|
|
event_free(ptpr->idle_timer);
|
2018-05-02 18:35:58 +08:00
|
|
|
|
|
|
|
if (!pt->event_loop_foreign) {
|
2020-08-27 15:37:14 +01:00
|
|
|
event_del(ptpr->w_sigint.watcher);
|
|
|
|
event_free(ptpr->w_sigint.watcher);
|
|
|
|
event_base_loopexit(ptpr->io_loop, NULL);
|
2020-03-10 19:13:43 +00:00
|
|
|
// event_base_free(pt->event.io_loop);
|
|
|
|
// pt->event.io_loop = NULL;
|
|
|
|
lwsl_notice("%s: set to exit loop\n", __func__);
|
2018-05-02 18:35:58 +08:00
|
|
|
}
|
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
|
|
|
{
|
2020-01-23 11:12:28 +00:00
|
|
|
struct lws_context_per_thread *pt;
|
2020-08-27 15:37:14 +01:00
|
|
|
struct lws_wsi_eventlibs_libevent *w;
|
2020-01-23 11:12:28 +00:00
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
if (!wsi)
|
|
|
|
return;
|
|
|
|
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
pt = &wsi->a.context->pt[(int)wsi->tsi];
|
2020-01-23 11:12:28 +00:00
|
|
|
if (pt->is_destroyed)
|
|
|
|
return;
|
|
|
|
|
2020-08-27 15:37:14 +01:00
|
|
|
w = wsi_to_priv_event(wsi);
|
|
|
|
|
|
|
|
if (w->w_read.watcher) {
|
|
|
|
event_free(w->w_read.watcher);
|
|
|
|
w->w_read.watcher = NULL;
|
2020-01-23 11:12:28 +00:00
|
|
|
}
|
2018-04-29 10:44:36 +08:00
|
|
|
|
2020-08-27 15:37:14 +01:00
|
|
|
if (w->w_write.watcher) {
|
|
|
|
event_free(w->w_write.watcher);
|
|
|
|
w->w_write.watcher = NULL;
|
2020-01-23 11:12:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
elops_wsi_logical_close_event(struct lws *wsi)
|
|
|
|
{
|
|
|
|
elops_destroy_wsi_event(wsi);
|
|
|
|
|
|
|
|
return 0;
|
2017-03-15 19:41:11 +05:30
|
|
|
}
|
2018-04-29 10:44:36 +08:00
|
|
|
|
2018-04-30 09:16:04 +08:00
|
|
|
static int
|
|
|
|
elops_init_vhost_listen_wsi_event(struct lws *wsi)
|
|
|
|
{
|
|
|
|
struct lws_context_per_thread *pt;
|
2020-08-27 15:37:14 +01:00
|
|
|
struct lws_pt_eventlibs_libevent *ptpr;
|
|
|
|
struct lws_wsi_eventlibs_libevent *w;
|
2018-04-30 09:16:04 +08:00
|
|
|
int fd;
|
|
|
|
|
|
|
|
if (!wsi) {
|
|
|
|
assert(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-27 15:37:14 +01:00
|
|
|
w = wsi_to_priv_event(wsi);
|
|
|
|
|
|
|
|
w->w_read.context = wsi->a.context;
|
|
|
|
w->w_write.context = wsi->a.context;
|
2018-04-30 09:16:04 +08:00
|
|
|
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
pt = &wsi->a.context->pt[(int)wsi->tsi];
|
2020-08-27 15:37:14 +01:00
|
|
|
ptpr = pt_to_priv_event(pt);
|
2018-04-30 09:16:04 +08:00
|
|
|
|
|
|
|
if (wsi->role_ops->file_handle)
|
|
|
|
fd = wsi->desc.filefd;
|
|
|
|
else
|
|
|
|
fd = wsi->desc.sockfd;
|
|
|
|
|
2020-08-27 15:37:14 +01:00
|
|
|
w->w_read.watcher = event_new(ptpr->io_loop, fd, (EV_READ | EV_PERSIST),
|
|
|
|
lws_event_cb, &w->w_read);
|
|
|
|
w->w_write.watcher = event_new(ptpr->io_loop, fd,
|
|
|
|
(EV_WRITE | EV_PERSIST),
|
|
|
|
lws_event_cb, &w->w_write);
|
2018-04-30 09:16:04 +08:00
|
|
|
|
|
|
|
elops_io_event(wsi, LWS_EV_START | LWS_EV_READ);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
elops_destroy_context2_event(struct lws_context *context)
|
|
|
|
{
|
|
|
|
struct lws_context_per_thread *pt;
|
2020-08-27 15:37:14 +01:00
|
|
|
struct lws_pt_eventlibs_libevent *ptpr;
|
2018-05-02 18:35:58 +08:00
|
|
|
int n, m;
|
2018-04-30 09:16:04 +08:00
|
|
|
|
2019-03-21 09:27:45 +08:00
|
|
|
lwsl_debug("%s: in\n", __func__);
|
2018-04-30 09:16:04 +08:00
|
|
|
|
|
|
|
for (n = 0; n < context->count_threads; n++) {
|
|
|
|
int budget = 1000;
|
|
|
|
|
|
|
|
pt = &context->pt[n];
|
2020-08-27 15:37:14 +01:00
|
|
|
ptpr = pt_to_priv_event(pt);
|
2018-04-30 09:16:04 +08:00
|
|
|
|
|
|
|
/* only for internal loops... */
|
|
|
|
|
2020-08-27 15:37:14 +01:00
|
|
|
if (pt->event_loop_foreign || !ptpr->io_loop)
|
2018-04-30 09:16:04 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!context->finalize_destroy_after_internal_loops_stopped) {
|
2020-08-27 15:37:14 +01:00
|
|
|
event_base_loopexit(ptpr->io_loop, NULL);
|
2018-04-30 09:16:04 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
while (budget-- &&
|
2020-08-27 15:37:14 +01:00
|
|
|
(m = event_base_loop(ptpr->io_loop, EVLOOP_NONBLOCK)))
|
2018-04-30 09:16:04 +08:00
|
|
|
;
|
|
|
|
#if 0
|
|
|
|
if (m) {
|
|
|
|
lwsl_err("%s: tsi %d: NOT everything closed\n",
|
|
|
|
__func__, n);
|
2020-08-27 15:37:14 +01:00
|
|
|
event_base_dump_events(ptpr->io_loop, stderr);
|
2018-04-30 09:16:04 +08:00
|
|
|
} else
|
|
|
|
lwsl_debug("%s: %d: everything closed OK\n", __func__, n);
|
|
|
|
#endif
|
2020-03-10 19:13:43 +00:00
|
|
|
lwsl_err("%s: event_base_free\n", __func__);
|
2020-08-27 15:37:14 +01:00
|
|
|
event_base_free(ptpr->io_loop);
|
|
|
|
ptpr->io_loop = NULL;
|
2018-04-30 09:16:04 +08:00
|
|
|
}
|
|
|
|
|
2019-03-21 09:27:45 +08:00
|
|
|
lwsl_debug("%s: out\n", __func__);
|
|
|
|
|
2018-05-02 18:35:58 +08:00
|
|
|
return 0;
|
2018-04-30 09:16:04 +08:00
|
|
|
}
|
2018-04-29 10:44:36 +08:00
|
|
|
|
2020-08-27 15:37:14 +01:00
|
|
|
static const struct lws_event_loop_ops event_loop_ops_event = {
|
2018-04-29 10:44:36 +08:00
|
|
|
/* name */ "libevent",
|
|
|
|
/* init_context */ elops_init_context_event,
|
|
|
|
/* destroy_context1 */ NULL,
|
2018-04-30 09:16:04 +08:00
|
|
|
/* destroy_context2 */ elops_destroy_context2_event,
|
|
|
|
/* init_vhost_listen_wsi */ elops_init_vhost_listen_wsi_event,
|
2018-04-29 10:44:36 +08:00
|
|
|
/* init_pt */ elops_init_pt_event,
|
2020-01-23 11:12:28 +00:00
|
|
|
/* wsi_logical_close */ elops_wsi_logical_close_event,
|
2018-04-29 10:44:36 +08:00
|
|
|
/* 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,
|
|
|
|
|
2020-02-07 11:39:32 +00:00
|
|
|
/* flags */ 0,
|
2020-08-27 15:37:14 +01:00
|
|
|
|
|
|
|
/* evlib_size_ctx */ 0,
|
|
|
|
/* evlib_size_pt */ sizeof(struct lws_pt_eventlibs_libevent),
|
|
|
|
/* evlib_size_vh */ 0,
|
|
|
|
/* evlib_size_wsi */ sizeof(struct lws_wsi_eventlibs_libevent),
|
|
|
|
};
|
|
|
|
|
|
|
|
#if defined(LWS_WITH_EVLIB_PLUGINS)
|
|
|
|
LWS_VISIBLE
|
|
|
|
#endif
|
|
|
|
const lws_plugin_evlib_t evlib_event = {
|
|
|
|
.hdr = {
|
|
|
|
"libevent event loop",
|
|
|
|
"lws_evlib_plugin",
|
|
|
|
LWS_PLUGIN_API_MAGIC
|
|
|
|
},
|
|
|
|
|
|
|
|
.ops = &event_loop_ops_event
|
2018-04-29 10:44:36 +08:00
|
|
|
};
|