2014-04-11 13:14:37 +08:00
|
|
|
/*
|
|
|
|
* libwebsockets - small server side websockets and web server implementation
|
|
|
|
*
|
2020-08-27 15:37:14 +01:00
|
|
|
* Copyright (C) 2010 - 2020 Andy Green <andy@warmcat.com>
|
2014-04-11 13:14:37 +08:00
|
|
|
*
|
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:
|
2014-04-11 13:14:37 +08:00
|
|
|
*
|
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.
|
2014-04-11 13:14:37 +08:00
|
|
|
*
|
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.
|
2014-04-11 13:14:37 +08:00
|
|
|
*/
|
|
|
|
|
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-libev.h"
|
|
|
|
|
|
|
|
#define pt_to_priv_ev(_pt) ((struct lws_pt_eventlibs_libev *)(_pt)->evlib_pt)
|
|
|
|
#define vh_to_priv_ev(_vh) ((struct lws_vh_eventlibs_libev *)(_vh)->evlib_vh)
|
|
|
|
#define wsi_to_priv_ev(_w) ((struct lws_wsi_eventlibs_libev *)(_w)->evlib_wsi)
|
2014-04-11 13:14:37 +08:00
|
|
|
|
2018-05-02 18:35:58 +08:00
|
|
|
static void
|
|
|
|
lws_ev_hrtimer_cb(struct ev_loop *loop, struct ev_timer *watcher, int revents)
|
|
|
|
{
|
2020-08-27 15:37:14 +01:00
|
|
|
struct lws_pt_eventlibs_libev *ptpr = lws_container_of(watcher,
|
|
|
|
struct lws_pt_eventlibs_libev, hrtimer);
|
|
|
|
struct lws_context_per_thread *pt = ptpr->pt;
|
2018-05-02 18:35:58 +08:00
|
|
|
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) {
|
2020-08-27 15:37:14 +01:00
|
|
|
ev_timer_set(&ptpr->hrtimer, ((float)us) / 1000000.0, 0);
|
|
|
|
ev_timer_start(ptpr->io_loop, &ptpr->hrtimer);
|
2018-05-02 18:35:58 +08:00
|
|
|
}
|
|
|
|
lws_pt_unlock(pt);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
lws_ev_idle_cb(struct ev_loop *loop, struct ev_idle *handle, int revents)
|
|
|
|
{
|
2020-08-27 15:37:14 +01:00
|
|
|
struct lws_pt_eventlibs_libev *ptpr = lws_container_of(handle,
|
|
|
|
struct lws_pt_eventlibs_libev, idle);
|
|
|
|
struct lws_context_per_thread *pt = ptpr->pt;
|
2020-01-22 19:15:04 +00:00
|
|
|
int reschedule = 0;
|
2020-08-27 15:37:14 +01:00
|
|
|
lws_usec_t us;
|
2018-05-02 18:35:58 +08:00
|
|
|
|
|
|
|
lws_service_do_ripe_rxflow(pt);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* is there anybody with pending stuff that needs service forcing?
|
|
|
|
*/
|
2019-09-16 15:36:12 +01:00
|
|
|
if (!lws_service_adjust_timeout(pt->context, 1, pt->tid))
|
2018-05-02 18:35:58 +08:00
|
|
|
/* -1 timeout means just do forced service */
|
2020-01-22 19:15:04 +00:00
|
|
|
reschedule = _lws_plat_service_forced_tsi(pt->context, pt->tid);
|
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) {
|
2020-08-27 15:37:14 +01:00
|
|
|
ev_timer_set(&ptpr->hrtimer, ((float)us) / 1000000.0, 0);
|
|
|
|
ev_timer_start(ptpr->io_loop, &ptpr->hrtimer);
|
2018-05-02 18:35:58 +08:00
|
|
|
}
|
|
|
|
lws_pt_unlock(pt);
|
|
|
|
|
|
|
|
/* there is nobody who needs service forcing, shut down idle */
|
2020-01-22 19:15:04 +00:00
|
|
|
if (!reschedule)
|
|
|
|
ev_idle_stop(loop, handle);
|
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
|
|
|
}
|
|
|
|
|
2015-12-14 08:52:03 +08:00
|
|
|
static void
|
2016-02-14 09:27:41 +08:00
|
|
|
lws_accept_cb(struct ev_loop *loop, struct ev_io *watcher, int revents)
|
2014-04-11 13:14:37 +08:00
|
|
|
{
|
2020-08-27 15:37:14 +01:00
|
|
|
struct lws_io_watcher_libev *lws_io = lws_container_of(watcher,
|
|
|
|
struct lws_io_watcher_libev, watcher);
|
2015-12-04 11:08:32 +08:00
|
|
|
struct lws_context *context = lws_io->context;
|
2020-08-27 15:37:14 +01:00
|
|
|
struct lws_pt_eventlibs_libev *ptpr;
|
2020-01-23 11:12:28 +00:00
|
|
|
struct lws_context_per_thread *pt;
|
2016-01-09 08:13:55 +08:00
|
|
|
struct lws_pollfd eventfd;
|
2018-05-02 18:35:58 +08:00
|
|
|
struct lws *wsi;
|
2024-01-15 06:12:58 +00:00
|
|
|
int tsi = 0;
|
2014-04-11 13:14:37 +08:00
|
|
|
|
2016-02-14 09:27:41 +08:00
|
|
|
if (revents & EV_ERROR)
|
|
|
|
return;
|
2014-04-11 13:14:37 +08:00
|
|
|
|
2016-02-14 09:27:41 +08:00
|
|
|
eventfd.fd = watcher->fd;
|
2016-01-09 04:12:46 +08:00
|
|
|
eventfd.events = 0;
|
2016-02-14 09:27:41 +08:00
|
|
|
eventfd.revents = EV_NONE;
|
2017-09-23 12:55:21 +08:00
|
|
|
|
2016-02-14 09:27:41 +08:00
|
|
|
if (revents & EV_READ) {
|
2016-01-09 04:12:46 +08:00
|
|
|
eventfd.events |= LWS_POLLIN;
|
2014-04-11 13:14:37 +08:00
|
|
|
eventfd.revents |= LWS_POLLIN;
|
2016-01-09 04:12:46 +08:00
|
|
|
}
|
2016-02-14 09:27:41 +08:00
|
|
|
if (revents & EV_WRITE) {
|
2016-01-09 04:12:46 +08:00
|
|
|
eventfd.events |= LWS_POLLOUT;
|
2014-04-11 13:14:37 +08:00
|
|
|
eventfd.revents |= LWS_POLLOUT;
|
2016-01-09 04:12:46 +08:00
|
|
|
}
|
2017-09-23 12:55:21 +08:00
|
|
|
|
2018-05-02 18:35:58 +08:00
|
|
|
wsi = wsi_from_fd(context, watcher->fd);
|
2024-01-15 06:12:58 +00:00
|
|
|
if (wsi)
|
|
|
|
tsi = (int)wsi->tsi;
|
|
|
|
pt = &context->pt[tsi];
|
2020-08-27 15:37:14 +01:00
|
|
|
ptpr = pt_to_priv_ev(pt);
|
2018-05-02 18:35:58 +08:00
|
|
|
|
2024-01-15 06:12:58 +00:00
|
|
|
lws_service_fd_tsi(context, &eventfd, tsi);
|
2018-05-02 18:35:58 +08:00
|
|
|
|
2020-08-27 15:37:14 +01:00
|
|
|
ev_idle_start(ptpr->io_loop, &ptpr->idle);
|
2014-04-11 13:14:37 +08:00
|
|
|
}
|
|
|
|
|
2020-01-02 08:32:23 +00:00
|
|
|
void
|
2016-02-14 09:27:41 +08:00
|
|
|
lws_ev_sigint_cb(struct ev_loop *loop, struct ev_signal *watcher, int revents)
|
2014-04-11 13:14:37 +08:00
|
|
|
{
|
2018-04-29 10:44:36 +08:00
|
|
|
struct lws_context *context = watcher->data;
|
2014-04-11 13:14:37 +08:00
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
if (context->eventlib_signal_cb) {
|
|
|
|
context->eventlib_signal_cb((void *)watcher, watcher->signum);
|
2016-01-09 08:13:55 +08:00
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
ev_break(loop, EVBREAK_ALL);
|
2016-01-09 08:13:55 +08:00
|
|
|
}
|
2015-04-26 22:50:59 -04:00
|
|
|
|
2021-06-21 08:50:13 +01:00
|
|
|
static int
|
|
|
|
elops_listen_init_ev(struct lws_dll2 *d, void *user)
|
|
|
|
{
|
2024-09-25 07:01:45 +01:00
|
|
|
#if defined(LWS_WITH_SERVER)
|
2021-06-21 08:50:13 +01:00
|
|
|
struct lws *wsi = lws_container_of(d, struct lws, listen_list);
|
|
|
|
struct lws_context *context = (struct lws_context *)user;
|
|
|
|
struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
|
|
|
|
struct lws_pt_eventlibs_libev *ptpr = pt_to_priv_ev(pt);
|
|
|
|
struct lws_wsi_eventlibs_libev *w = wsi_to_priv_ev(wsi);
|
|
|
|
struct lws_vhost *vh = wsi->a.vhost;
|
|
|
|
|
|
|
|
w->w_read.context = context;
|
|
|
|
w->w_write.context = context;
|
|
|
|
vh_to_priv_ev(vh)->w_accept.context = context;
|
|
|
|
|
|
|
|
ev_io_init(&vh_to_priv_ev(vh)->w_accept.watcher,
|
|
|
|
lws_accept_cb, wsi->desc.sockfd, EV_READ);
|
|
|
|
ev_io_start(ptpr->io_loop, &vh_to_priv_ev(vh)->w_accept.watcher);
|
2024-09-25 07:01:45 +01:00
|
|
|
#endif
|
2021-06-21 08:50:13 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
static int
|
|
|
|
elops_init_pt_ev(struct lws_context *context, void *_loop, int tsi)
|
2014-04-11 13:14:37 +08:00
|
|
|
{
|
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_libev *ptpr = pt_to_priv_ev(pt);
|
|
|
|
struct ev_signal *w_sigint = &ptpr->w_sigint.watcher;
|
2020-01-23 11:12:28 +00:00
|
|
|
struct ev_loop *loop = (struct ev_loop *)_loop;
|
2017-09-19 17:30:06 +08:00
|
|
|
const char *backend_name;
|
2020-12-12 06:21:40 +00:00
|
|
|
unsigned int backend;
|
2014-04-11 13:14:37 +08:00
|
|
|
int status = 0;
|
|
|
|
|
2021-06-28 05:05:57 +01:00
|
|
|
lwsl_cx_info(context, "loop %p", _loop);
|
2018-04-30 09:16:04 +08:00
|
|
|
|
2020-08-27 15:37:14 +01:00
|
|
|
ptpr->pt = pt;
|
|
|
|
|
2014-04-11 13:14:37 +08:00
|
|
|
if (!loop)
|
2016-02-14 09:27:41 +08:00
|
|
|
loop = ev_loop_new(0);
|
2016-03-09 07:44:49 +08:00
|
|
|
else
|
2018-04-29 08:34:19 +08:00
|
|
|
context->pt[tsi].event_loop_foreign = 1;
|
2014-04-11 13:14:37 +08:00
|
|
|
|
2018-04-30 09:16:04 +08:00
|
|
|
if (!loop) {
|
2021-06-28 05:05:57 +01:00
|
|
|
lwsl_cx_err(context, "creating event base failed");
|
2018-04-30 09:16:04 +08:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-08-27 15:37:14 +01:00
|
|
|
ptpr->io_loop = loop;
|
2015-12-14 08:52:03 +08:00
|
|
|
|
2021-06-21 08:50:13 +01:00
|
|
|
lws_vhost_foreach_listen_wsi(context, context, elops_listen_init_ev);
|
2015-04-26 22:50:59 -04:00
|
|
|
|
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) {
|
|
|
|
ev_signal_init(w_sigint, lws_ev_sigint_cb, SIGINT);
|
|
|
|
w_sigint->data = context;
|
2018-02-19 10:19:40 +08:00
|
|
|
ev_signal_start(loop, w_sigint);
|
2016-01-09 08:13:55 +08:00
|
|
|
}
|
2014-04-11 13:14:37 +08:00
|
|
|
|
2018-02-19 10:19:40 +08:00
|
|
|
backend = ev_backend(loop);
|
2014-04-11 13:14:37 +08:00
|
|
|
switch (backend) {
|
|
|
|
case EVBACKEND_SELECT:
|
|
|
|
backend_name = "select";
|
|
|
|
break;
|
|
|
|
case EVBACKEND_POLL:
|
|
|
|
backend_name = "poll";
|
|
|
|
break;
|
|
|
|
case EVBACKEND_EPOLL:
|
|
|
|
backend_name = "epoll";
|
|
|
|
break;
|
2020-02-09 05:18:22 +00:00
|
|
|
#if defined(LWS_HAVE_EVBACKEND_LINUXAIO)
|
|
|
|
case EVBACKEND_LINUXAIO:
|
|
|
|
backend_name = "Linux AIO";
|
|
|
|
break;
|
|
|
|
#endif
|
2020-02-21 07:59:09 +00:00
|
|
|
#if defined(LWS_HAVE_EVBACKEND_IOURING)
|
|
|
|
case EVBACKEND_IOURING:
|
|
|
|
backend_name = "Linux io_uring";
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case EVBACKEND_KQUEUE:
|
2014-04-11 13:14:37 +08:00
|
|
|
backend_name = "kqueue";
|
|
|
|
break;
|
|
|
|
case EVBACKEND_DEVPOLL:
|
|
|
|
backend_name = "/dev/poll";
|
|
|
|
break;
|
|
|
|
case EVBACKEND_PORT:
|
|
|
|
backend_name = "Solaris 10 \"port\"";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
backend_name = "Unknown libev backend";
|
|
|
|
break;
|
2016-01-09 08:13:55 +08:00
|
|
|
}
|
2014-04-11 13:14:37 +08:00
|
|
|
|
2021-06-28 05:05:57 +01:00
|
|
|
lwsl_cx_info(context, " libev backend: %s", backend_name);
|
2017-09-19 17:30:06 +08:00
|
|
|
(void)backend_name;
|
2014-04-11 13:14:37 +08:00
|
|
|
|
2020-08-27 15:37:14 +01:00
|
|
|
ev_timer_init(&ptpr->hrtimer, lws_ev_hrtimer_cb, 0, 0);
|
|
|
|
ptpr->hrtimer.data = pt;
|
2018-05-02 18:35:58 +08:00
|
|
|
|
2020-08-27 15:37:14 +01:00
|
|
|
ev_idle_init(&ptpr->idle, lws_ev_idle_cb);
|
2018-05-02 18:35:58 +08:00
|
|
|
|
2014-04-11 13:14:37 +08:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2021-06-21 08:50:13 +01:00
|
|
|
static int
|
|
|
|
elops_listen_destroy_ev(struct lws_dll2 *d, void *user)
|
|
|
|
{
|
2024-09-25 07:01:45 +01:00
|
|
|
#if defined(LWS_WITH_SERVER)
|
2021-06-21 08:50:13 +01:00
|
|
|
struct lws *wsi = lws_container_of(d, struct lws, listen_list);
|
|
|
|
struct lws_context *context = (struct lws_context *)user;
|
|
|
|
struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
|
|
|
|
struct lws_pt_eventlibs_libev *ptpr = pt_to_priv_ev(pt);
|
|
|
|
struct lws_vhost *vh = wsi->a.vhost;
|
|
|
|
|
|
|
|
ev_io_stop(ptpr->io_loop, &vh_to_priv_ev(vh)->w_accept.watcher);
|
2024-09-25 07:01:45 +01:00
|
|
|
#endif
|
2021-06-21 08:50:13 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
static void
|
|
|
|
elops_destroy_pt_ev(struct lws_context *context, int tsi)
|
2016-02-14 09:27:41 +08:00
|
|
|
{
|
|
|
|
struct lws_context_per_thread *pt = &context->pt[tsi];
|
2020-08-27 15:37:14 +01:00
|
|
|
struct lws_pt_eventlibs_libev *ptpr = pt_to_priv_ev(pt);
|
2016-02-14 09:27:41 +08:00
|
|
|
|
2021-06-21 08:50:13 +01:00
|
|
|
lws_vhost_foreach_listen_wsi(context, context, elops_listen_destroy_ev);
|
2018-05-02 18:35:58 +08:00
|
|
|
|
|
|
|
/* static assets */
|
|
|
|
|
2020-08-27 15:37:14 +01:00
|
|
|
ev_timer_stop(ptpr->io_loop, &ptpr->hrtimer);
|
|
|
|
ev_idle_stop(ptpr->io_loop, &ptpr->idle);
|
2018-05-02 18:35:58 +08:00
|
|
|
|
2020-05-12 05:09:15 +01:00
|
|
|
if (!pt->event_loop_foreign)
|
2020-08-27 15:37:14 +01:00
|
|
|
ev_signal_stop(ptpr->io_loop, &ptpr->w_sigint.watcher);
|
2016-02-14 09:27:41 +08:00
|
|
|
}
|
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
static int
|
|
|
|
elops_init_context_ev(struct lws_context *context,
|
|
|
|
const struct lws_context_creation_info *info)
|
2014-04-11 13:14:37 +08:00
|
|
|
{
|
2018-04-29 10:44:36 +08:00
|
|
|
int n;
|
2014-04-11 13:14:37 +08:00
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
context->eventlib_signal_cb = info->signal_cb;
|
2014-04-11 13:14:37 +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_ev(&context->pt[n])->w_sigint.context = context;
|
2018-04-29 10:44:36 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-13 11:59:04 +08:00
|
|
|
static int
|
2018-04-29 10:44:36 +08:00
|
|
|
elops_accept_ev(struct lws *wsi)
|
|
|
|
{
|
2020-08-27 15:37:14 +01:00
|
|
|
struct lws_wsi_eventlibs_libev *w = wsi_to_priv_ev(wsi);
|
2018-04-29 10:44:36 +08:00
|
|
|
int fd;
|
|
|
|
|
|
|
|
if (wsi->role_ops->file_handle)
|
|
|
|
fd = wsi->desc.filefd;
|
2017-02-27 12:55:56 +08:00
|
|
|
else
|
2018-04-29 10:44:36 +08:00
|
|
|
fd = wsi->desc.sockfd;
|
2017-02-27 12:55:56 +08:00
|
|
|
|
2020-08-27 15:37:14 +01:00
|
|
|
w->w_read.context = wsi->a.context;
|
|
|
|
w->w_write.context = wsi->a.context;
|
2018-04-30 19:17:32 +08:00
|
|
|
|
2020-08-27 15:37:14 +01:00
|
|
|
ev_io_init(&w->w_read.watcher, lws_accept_cb, fd, EV_READ);
|
|
|
|
ev_io_init(&w->w_write.watcher, lws_accept_cb, fd, EV_WRITE);
|
2018-10-13 11:59:04 +08:00
|
|
|
|
|
|
|
return 0;
|
2014-04-11 13:14:37 +08:00
|
|
|
}
|
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
static void
|
2020-12-12 06:21:40 +00:00
|
|
|
elops_io_ev(struct lws *wsi, unsigned int flags)
|
2014-04-11 13:14:37 +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
|
|
|
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_libev *ptpr = pt_to_priv_ev(pt);
|
|
|
|
struct lws_wsi_eventlibs_libev *w = wsi_to_priv_ev(wsi);
|
|
|
|
|
2021-06-28 05:05:57 +01:00
|
|
|
lwsl_wsi_debug(wsi, "%s flags 0x%x %p %d", wsi->role_ops->name, flags,
|
|
|
|
ptpr->io_loop,
|
|
|
|
pt->is_destroyed);
|
2015-12-16 18:19:08 +08:00
|
|
|
|
2020-08-27 15:37:14 +01:00
|
|
|
if (!ptpr->io_loop || pt->is_destroyed)
|
2014-04-11 13:14:37 +08:00
|
|
|
return;
|
2014-04-12 10:07:02 +08:00
|
|
|
|
2014-04-11 13:14:37 +08:00
|
|
|
assert((flags & (LWS_EV_START | LWS_EV_STOP)) &&
|
2016-01-09 08:13:55 +08:00
|
|
|
(flags & (LWS_EV_READ | LWS_EV_WRITE)));
|
2014-04-11 13:14:37 +08:00
|
|
|
|
|
|
|
if (flags & LWS_EV_START) {
|
2016-02-14 09:27:41 +08:00
|
|
|
if (flags & LWS_EV_WRITE)
|
2020-08-27 15:37:14 +01:00
|
|
|
ev_io_start(ptpr->io_loop, &w->w_write.watcher);
|
2016-02-14 09:27:41 +08:00
|
|
|
if (flags & LWS_EV_READ)
|
2020-08-27 15:37:14 +01:00
|
|
|
ev_io_start(ptpr->io_loop, &w->w_read.watcher);
|
2014-04-11 13:14:37 +08:00
|
|
|
} else {
|
2016-02-14 09:27:41 +08:00
|
|
|
if (flags & LWS_EV_WRITE)
|
2020-08-27 15:37:14 +01:00
|
|
|
ev_io_stop(ptpr->io_loop, &w->w_write.watcher);
|
2016-02-14 09:27:41 +08:00
|
|
|
if (flags & LWS_EV_READ)
|
2020-08-27 15:37:14 +01:00
|
|
|
ev_io_stop(ptpr->io_loop, &w->w_read.watcher);
|
2014-04-11 13:14:37 +08:00
|
|
|
}
|
2020-01-23 11:12:28 +00:00
|
|
|
|
|
|
|
if (pt->destroy_self)
|
|
|
|
lws_context_destroy(pt->context);
|
2014-04-11 13:14:37 +08:00
|
|
|
}
|
|
|
|
|
2018-04-29 10:44:36 +08:00
|
|
|
static void
|
|
|
|
elops_run_pt_ev(struct lws_context *context, int tsi)
|
2014-04-11 13:14:37 +08:00
|
|
|
{
|
2020-08-27 15:37:14 +01:00
|
|
|
if (pt_to_priv_ev(&context->pt[tsi])->io_loop)
|
|
|
|
ev_run(pt_to_priv_ev(&context->pt[tsi])->io_loop, 0);
|
2014-04-11 13:14:37 +08:00
|
|
|
}
|
2018-04-29 10:44:36 +08:00
|
|
|
|
2018-04-30 09:16:04 +08:00
|
|
|
static int
|
|
|
|
elops_destroy_context2_ev(struct lws_context *context)
|
|
|
|
{
|
|
|
|
struct lws_context_per_thread *pt;
|
2020-08-27 15:37:14 +01:00
|
|
|
struct lws_pt_eventlibs_libev *ptpr;
|
2018-05-02 18:35:58 +08:00
|
|
|
int n, m;
|
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_ev(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;
|
|
|
|
|
2020-11-16 19:32:58 +00:00
|
|
|
if (!context->evlib_finalize_destroy_after_int_loops_stop) {
|
2020-08-27 15:37:14 +01:00
|
|
|
ev_break(ptpr->io_loop, EVBREAK_ONE);
|
2018-04-30 09:16:04 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
while (budget-- &&
|
2020-08-27 15:37:14 +01:00
|
|
|
(m = ev_run(ptpr->io_loop, 0)))
|
2018-04-30 09:16:04 +08:00
|
|
|
;
|
|
|
|
|
2020-08-27 15:37:14 +01:00
|
|
|
ev_loop_destroy(ptpr->io_loop);
|
2018-04-30 09:16:04 +08:00
|
|
|
}
|
|
|
|
|
2018-05-02 18:35:58 +08:00
|
|
|
return 0;
|
2018-04-30 09:16:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
elops_init_vhost_listen_wsi_ev(struct lws *wsi)
|
|
|
|
{
|
2020-08-27 15:37:14 +01:00
|
|
|
struct lws_wsi_eventlibs_libev *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_ev(wsi);
|
|
|
|
w->w_read.context = wsi->a.context;
|
|
|
|
w->w_write.context = wsi->a.context;
|
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
|
|
|
ev_io_init(&w->w_read.watcher, lws_accept_cb, fd, EV_READ);
|
|
|
|
//ev_io_init(&w->w_write.watcher, lws_accept_cb, fd, EV_WRITE);
|
2018-04-30 09:16:04 +08:00
|
|
|
|
|
|
|
elops_io_ev(wsi, LWS_EV_START | LWS_EV_READ);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-02 18:35:58 +08:00
|
|
|
static void
|
|
|
|
elops_destroy_wsi_ev(struct lws *wsi)
|
|
|
|
{
|
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_libev *ptpr = pt_to_priv_ev(pt);
|
|
|
|
struct lws_wsi_eventlibs_libev *w = wsi_to_priv_ev(wsi);
|
2018-05-02 18:35:58 +08:00
|
|
|
|
2020-08-27 15:37:14 +01:00
|
|
|
ev_io_stop(ptpr->io_loop, &w->w_read.watcher);
|
|
|
|
ev_io_stop(ptpr->io_loop, &w->w_write.watcher);
|
2018-05-02 18:35:58 +08:00
|
|
|
}
|
|
|
|
|
2024-01-15 06:12:58 +00:00
|
|
|
static int
|
|
|
|
elops_wsi_logical_close_ev(struct lws *wsi)
|
|
|
|
{
|
|
|
|
elops_destroy_wsi_ev(wsi);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-27 15:37:14 +01:00
|
|
|
static const struct lws_event_loop_ops event_loop_ops_ev = {
|
2018-04-29 10:44:36 +08:00
|
|
|
/* name */ "libev",
|
|
|
|
/* init_context */ elops_init_context_ev,
|
|
|
|
/* destroy_context1 */ NULL,
|
2018-04-30 09:16:04 +08:00
|
|
|
/* destroy_context2 */ elops_destroy_context2_ev,
|
|
|
|
/* init_vhost_listen_wsi */ elops_init_vhost_listen_wsi_ev,
|
2018-04-29 10:44:36 +08:00
|
|
|
/* init_pt */ elops_init_pt_ev,
|
2024-01-15 06:12:58 +00:00
|
|
|
/* wsi_logical_close */ elops_wsi_logical_close_ev,
|
2018-04-29 10:44:36 +08:00
|
|
|
/* check_client_connect_ok */ NULL,
|
|
|
|
/* close_handle_manually */ NULL,
|
|
|
|
/* accept */ elops_accept_ev,
|
|
|
|
/* io */ elops_io_ev,
|
|
|
|
/* run_pt */ elops_run_pt_ev,
|
|
|
|
/* destroy_pt */ elops_destroy_pt_ev,
|
2018-05-02 18:35:58 +08:00
|
|
|
/* destroy wsi */ elops_destroy_wsi_ev,
|
2021-08-10 06:35:59 +01:00
|
|
|
/* foreign_thread */ NULL,
|
2024-09-30 12:33:39 +01:00
|
|
|
/* fake_POLLIN */ NULL,
|
2018-04-29 10:44:36 +08:00
|
|
|
|
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_libev),
|
|
|
|
/* evlib_size_vh */ sizeof(struct lws_vh_eventlibs_libev),
|
|
|
|
/* evlib_size_wsi */ sizeof(struct lws_wsi_eventlibs_libev),
|
|
|
|
};
|
|
|
|
|
|
|
|
#if defined(LWS_WITH_EVLIB_PLUGINS)
|
|
|
|
LWS_VISIBLE
|
|
|
|
#endif
|
|
|
|
const lws_plugin_evlib_t evlib_ev = {
|
|
|
|
.hdr = {
|
|
|
|
"libev event loop",
|
|
|
|
"lws_evlib_plugin",
|
2021-01-25 11:40:54 +00:00
|
|
|
LWS_BUILD_HASH,
|
2020-08-27 15:37:14 +01:00
|
|
|
LWS_PLUGIN_API_MAGIC
|
|
|
|
},
|
|
|
|
|
|
|
|
.ops = &event_loop_ops_ev
|
2018-04-29 10:44:36 +08:00
|
|
|
};
|