diff --git a/lib/core-net/private.h b/lib/core-net/private.h index 2cab9a125..fd0a62d37 100644 --- a/lib/core-net/private.h +++ b/lib/core-net/private.h @@ -877,6 +877,10 @@ enum { LWSRXFC_TRIMMED = 2, }; + +int +_lws_plat_service_forced_tsi(struct lws_context *context, int tsi); + LWS_EXTERN int lws_rxflow_cache(struct lws *wsi, unsigned char *buf, int n, int len); diff --git a/lib/event-libs/libev/libev.c b/lib/event-libs/libev/libev.c index 4b85c1988..676def7d3 100644 --- a/lib/event-libs/libev/libev.c +++ b/lib/event-libs/libev/libev.c @@ -49,14 +49,9 @@ lws_ev_idle_cb(struct ev_loop *loop, struct ev_idle *handle, int revents) /* * is there anybody with pending stuff that needs service forcing? */ - if (!lws_service_adjust_timeout(pt->context, 1, pt->tid)) { + if (!lws_service_adjust_timeout(pt->context, 1, pt->tid)) /* -1 timeout means just do forced service */ - _lws_plat_service_tsi(pt->context, -1, pt->tid); - /* still somebody left who wants forced service? */ - if (!lws_service_adjust_timeout(pt->context, 1, pt->tid)) - /* yes... come back again later */ - return; - } + _lws_plat_service_forced_tsi(pt->context, pt->tid); /* account for hrtimer */ diff --git a/lib/event-libs/libevent/libevent.c b/lib/event-libs/libevent/libevent.c index cc89c7598..9f620cb13 100644 --- a/lib/event-libs/libevent/libevent.c +++ b/lib/event-libs/libevent/libevent.c @@ -50,9 +50,9 @@ lws_event_idle_timer_cb(int fd, short event, void *p) /* * is there anybody with pending stuff that needs service forcing? */ - if (!lws_service_adjust_timeout(pt->context, 1, pt->tid)) { + if (!lws_service_adjust_timeout(pt->context, 1, pt->tid)) /* -1 timeout means just do forced service */ - _lws_plat_service_tsi(pt->context, -1, pt->tid); + _lws_plat_service_forced_tsi(pt->context, pt->tid); /* still somebody left who wants forced service? */ if (!lws_service_adjust_timeout(pt->context, 1, pt->tid)) { /* yes... come back again later */ diff --git a/lib/event-libs/libuv/libuv.c b/lib/event-libs/libuv/libuv.c index 58c2056e6..8228ed642 100644 --- a/lib/event-libs/libuv/libuv.c +++ b/lib/event-libs/libuv/libuv.c @@ -56,14 +56,9 @@ lws_uv_idle(uv_idle_t *handle /* * is there anybody with pending stuff that needs service forcing? */ - if (!lws_service_adjust_timeout(pt->context, 1, pt->tid)) { + if (!lws_service_adjust_timeout(pt->context, 1, pt->tid)) /* -1 timeout means just do forced service */ - _lws_plat_service_tsi(pt->context, -1, pt->tid); - /* still somebody left who wants forced service? */ - if (!lws_service_adjust_timeout(pt->context, 1, pt->tid)) - /* yes... come back again later */ - return; - } + _lws_plat_service_forced_tsi(pt->context, pt->tid); /* account for sultimer */ diff --git a/lib/plat/unix/unix-service.c b/lib/plat/unix/unix-service.c index 19cbb4ecb..56b14b398 100644 --- a/lib/plat/unix/unix-service.c +++ b/lib/plat/unix/unix-service.c @@ -28,14 +28,46 @@ lws_poll_listen_fd(struct lws_pollfd *fd) return poll(fd, 1, 0); } -LWS_EXTERN int +int +_lws_plat_service_forced_tsi(struct lws_context *context, int tsi) +{ + struct lws_context_per_thread *pt = &context->pt[tsi]; + int m, n; + + lws_service_flag_pending(context, tsi); + + /* any socket with events to service? */ + for (n = 0; n < (int)pt->fds_count; n++) { + if (!pt->fds[n].revents) + continue; + + m = lws_service_fd_tsi(context, &pt->fds[n], tsi); + if (m < 0) { + lwsl_err("%s: lws_service_fd_tsi returned %d\n", + __func__, m); + return -1; + } + /* if something closed, retry this slot */ + if (m) + n--; + } + + lws_service_do_ripe_rxflow(pt); + + return 0; +} + +int _lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi) { volatile struct lws_foreign_thread_pollfd *ftp, *next; volatile struct lws_context_per_thread *vpt; struct lws_context_per_thread *pt; lws_usec_t timeout_us; - int n = -1, m, c, a = 0; + int n = -1, a = 0; +#if (defined(LWS_ROLE_WS) && !defined(LWS_WITHOUT_EXTENSIONS)) || defined(LWS_WITH_TLS) + int m; +#endif /* stay dead once we are dead */ @@ -140,21 +172,23 @@ again: lws_pt_unlock(pt); +#if (defined(LWS_ROLE_WS) && !defined(LWS_WITHOUT_EXTENSIONS)) || defined(LWS_WITH_TLS) m = 0; - #if defined(LWS_ROLE_WS) && !defined(LWS_WITHOUT_EXTENSIONS) +#endif +#if defined(LWS_ROLE_WS) && !defined(LWS_WITHOUT_EXTENSIONS) m |= !!pt->ws.rx_draining_ext_list; - #endif +#endif - #if defined(LWS_WITH_TLS) +#if defined(LWS_WITH_TLS) if (pt->context->tls_ops && pt->context->tls_ops->fake_POLLIN_for_buffered) m |= pt->context->tls_ops->fake_POLLIN_for_buffered(pt); - #endif +#endif if ( - #if (defined(LWS_ROLE_WS) && !defined(LWS_WITHOUT_EXTENSIONS)) || defined(LWS_WITH_TLS) +#if (defined(LWS_ROLE_WS) && !defined(LWS_WITHOUT_EXTENSIONS)) || defined(LWS_WITH_TLS) !m && - #endif +#endif !n) { /* nothing to do */ lws_service_do_ripe_rxflow(pt); @@ -163,36 +197,9 @@ again: } else a = 1; - m = lws_service_flag_pending(context, tsi); - if (m) - c = -1; /* unknown limit */ - else - if (n < 0) { - if (LWS_ERRNO != LWS_EINTR) - return -1; - return 0; - } else - c = n; + if (_lws_plat_service_forced_tsi(context, tsi)) + return -1; - /* any socket with events to service? */ - for (n = 0; n < (int)pt->fds_count && c; n++) { - if (!pt->fds[n].revents) - continue; - - c--; - - m = lws_service_fd_tsi(context, &pt->fds[n], tsi); - if (m < 0) { - lwsl_err("%s: lws_service_fd_tsi returned %d\n", - __func__, m); - return -1; - } - /* if something closed, retry this slot */ - if (m) - n--; - } - - lws_service_do_ripe_rxflow(pt); if (a) goto again; diff --git a/lib/plat/windows/windows-service.c b/lib/plat/windows/windows-service.c index 4397f07b0..02ca34d59 100644 --- a/lib/plat/windows/windows-service.c +++ b/lib/plat/windows/windows-service.c @@ -25,6 +25,33 @@ #include "core/private.h" +int +_lws_plat_service_forced_tsi(struct lws_context *context, int tsi) +{ + struct lws_context_per_thread *pt = &context->pt[tsi]; + int m, n; + + lws_service_flag_pending(context, tsi); + + /* any socket with events to service? */ + for (n = 0; n < (int)pt->fds_count; n++) { + if (!pt->fds[n].revents) + continue; + + m = lws_service_fd_tsi(context, &pt->fds[n], tsi); + if (m < 0) + return -1; + /* if something closed, retry this slot */ + if (m) + n--; + } + + lws_service_do_ripe_rxflow(pt); + + return 0; +} + + LWS_EXTERN int _lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi) { @@ -97,22 +124,8 @@ _lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi) /* * is there anybody with pending stuff that needs service forcing? */ - if (!lws_service_adjust_timeout(context, 1, tsi) && - lws_service_flag_pending(context, tsi)) { - /* any socket with events to service? */ - for (n = 0; n < (int)pt->fds_count; n++) { - int m; - if (!pt->fds[n].revents) - continue; - - m = lws_service_fd_tsi(context, &pt->fds[n], tsi); - if (m < 0) - return -1; - /* if something closed, retry this slot */ - if (m) - n--; - } - } + if (!lws_service_adjust_timeout(context, 1, tsi)) + _lws_plat_service_forced_tsi(context, tsi); if (timeout_us) { lws_usec_t us;