static int
lws_calllback_as_writeable(struct lws *wsi)
{
+ struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
int n;
+ lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_C_WRITEABLE_CB, 1);
+#if defined(LWS_WITH_STATS)
+ {
+ uint64_t ul = time_in_microseconds() - wsi->active_writable_req_us;
+
+ lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_MS_WRITABLE_DELAY, ul);
+ lws_stats_atomic_max(wsi->context, pt, LWSSTATS_MS_WORST_WRITABLE_DELAY, ul);
+ wsi->active_writable_req_us = 0;
+ }
+#endif
+
switch (wsi->mode) {
case LWSCM_RAW:
n = LWS_CALLBACK_RAW_WRITEABLE;
//lwsl_err("%s: %p\n", __func__, wsi);
+ wsi->leave_pollout_active = 0;
+ wsi->handling_pollout = 1;
+ /*
+ * if another thread wants POLLOUT on us, from here on while
+ * handling_pollout is set, he will only set leave_pollout_active.
+ * If we are going to disable POLLOUT, we will check that first.
+ */
+
/*
* user callback is lowest priority to get these notifications
* actually, since other pending things cannot be disordered
if (lws_issue_raw(wsi, wsi->trunc_alloc + wsi->trunc_offset,
wsi->trunc_len) < 0) {
lwsl_info("%s signalling to close\n", __func__);
- return -1;
+ goto bail_die;
}
/* leave POLLOUT active either way */
- return 0;
+ goto bail_ok;
} else
- if (wsi->state == LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE)
- return -1; /* retry closing now */
+ if (wsi->state == LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE) {
+ wsi->socket_is_permanently_unusable = 1;
+ goto bail_die; /* retry closing now */
+ }
if (wsi->mode == LWSCM_WSCL_ISSUE_HTTP_BODY)
goto user_service;
wsi->pps = LWS_PPS_NONE;
lws_rx_flow_control(wsi, 1);
- return 0; /* leave POLLOUT active */
+ goto bail_ok; /* leave POLLOUT active */
}
#endif
#ifdef LWS_WITH_CGI
- if (wsi->cgi)
+ if (wsi->cgi) {
+ /* also one shot */
+ if (pollfd)
+ if (lws_change_pollfd(wsi, LWS_POLLOUT, 0)) {
+ lwsl_info("failed at set pollfd\n");
+ return 1;
+ }
goto user_service_go_again;
+ }
#endif
/* Priority 3: pending control packets (pong or close)
n = lws_write(wsi, &wsi->u.ws.ping_payload_buf[LWS_PRE],
wsi->u.ws.ping_payload_len, write_type);
if (n < 0)
- return -1;
+ goto bail_die;
/* well he is sent, mark him done */
wsi->u.ws.ping_pending_flag = 0;
if (wsi->u.ws.payload_is_close)
/* oh... a close frame was it... then we are done */
- return -1;
+ goto bail_die;
/* otherwise for PING, leave POLLOUT active either way */
- return 0;
+ goto bail_ok;
}
if (wsi->state == LWSS_ESTABLISHED &&
n = lws_write(wsi, &wsi->u.ws.ping_payload_buf[LWS_PRE],
0, LWS_WRITE_PING);
if (n < 0)
- return -1;
+ goto bail_die;
/*
* we apparently were able to send the PING in a reasonable time
lws_set_timeout(wsi, PENDING_TIMEOUT_WS_PONG_CHECK_GET_PONG,
wsi->context->timeout_secs);
- return 0;
+ goto bail_ok;
}
/* Priority 4: if we are closing, not allowed to send more data frags
if (wsi->state == LWSS_ESTABLISHED && wsi->u.ws.tx_draining_ext) {
lwsl_ext("SERVICING TX EXT DRAINING\n");
if (lws_write(wsi, NULL, 0, LWS_WRITE_CONTINUATION) < 0)
- return -1;
+ goto bail_die;
/* leave POLLOUT active */
- return 0;
+ goto bail_ok;
}
/* Priority 6: user can get the callback
*/
m = lws_ext_cb_active(wsi, LWS_EXT_CB_IS_WRITEABLE, NULL, 0);
if (m)
- return -1;
+ goto bail_die;
#ifndef LWS_NO_EXTENSIONS
if (!wsi->extension_data_pending)
goto user_service;
&eff_buf, 0);
if (m < 0) {
lwsl_err("ext reports fatal error\n");
- return -1;
+ goto bail_die;
}
if (m)
/*
eff_buf.token_len);
if (n < 0) {
lwsl_info("closing from POLLOUT spill\n");
- return -1;
+ goto bail_die;
}
/*
* Keep amount spilled small to minimize chance of this
if (n != eff_buf.token_len) {
lwsl_err("Unable to spill ext %d vs %d\n",
eff_buf.token_len, n);
- return -1;
+ goto bail_die;
}
} else
continue;
* when we come back here and there's nothing more to spill.
*/
- return 0;
+ goto bail_ok;
}
#ifndef LWS_NO_EXTENSIONS
wsi->extension_data_pending = 0;
user_service:
/* one shot */
- if (pollfd)
- if (lws_change_pollfd(wsi, LWS_POLLOUT, 0)) {
- lwsl_info("failed at set pollfd\n");
- return 1;
- }
+ if (pollfd) {
+ int eff = wsi->leave_pollout_active;
+
+ if (!eff)
+ if (lws_change_pollfd(wsi, LWS_POLLOUT, 0)) {
+ lwsl_info("failed at set pollfd\n");
+ goto bail_die;
+ }
+
+ wsi->handling_pollout = 0;
+
+ /* cannot get leave_pollout_active set after the above */
+
+ if (!eff && wsi->leave_pollout_active)
+ /* got set inbetween sampling eff and clearing
+ * handling_pollout, force POLLOUT on */
+ lws_calllback_as_writeable(wsi);
+
+ wsi->leave_pollout_active = 0;
+ }
if (wsi->mode != LWSCM_WSCL_ISSUE_HTTP_BODY &&
!wsi->hdr_parsing_completed)
- return 0;
+ goto bail_ok;
+
#ifdef LWS_WITH_CGI
user_service_go_again:
wsi->u.http2.requested_POLLOUT = 0;
if (!wsi->u.http2.initialized) {
lwsl_info("pollout on uninitialized http2 conn\n");
- return 0;
+ goto bail_ok;
}
lwsl_info("%s: doing children\n", __func__);
lwsl_info("%s: completed\n", __func__);
- return 0;
+ goto bail_ok;
notify:
#endif
+ wsi->handling_pollout = 0;
+ wsi->leave_pollout_active = 0;
+
return lws_calllback_as_writeable(wsi);
+
+ /*
+ * since these don't disable the POLLOUT, they are always doing the
+ * right thing for leave_pollout_active whether it was set or not.
+ */
+
+bail_ok:
+ wsi->handling_pollout = 0;
+ wsi->leave_pollout_active = 0;
+
+ return 0;
+
+bail_die:
+ wsi->handling_pollout = 0;
+ wsi->leave_pollout_active = 0;
+
+ return -1;
}
int
if (wsi->desc.sockfd != LWS_SOCK_INVALID && wsi->position_in_fds_table >= 0)
n = pt->fds[wsi->position_in_fds_table].events;
+ lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_C_TIMEOUTS, 1);
+
/* no need to log normal idle keepalive timeout */
if (wsi->pending_timeout != PENDING_TIMEOUT_HTTP_KEEPALIVE_IDLE)
lwsl_notice("wsi %p: TIMEDOUT WAITING on %d (did hdr %d, ah %p, wl %d, pfd events %d) %llu vs %llu\n",
while (wsi) {
pt->fds[wsi->position_in_fds_table].revents |=
pt->fds[wsi->position_in_fds_table].events & LWS_POLLIN;
- if (pt->fds[wsi->position_in_fds_table].revents &
- LWS_POLLIN)
+ if (pt->fds[wsi->position_in_fds_table].revents & LWS_POLLIN) {
forced = 1;
+ break;
+ }
wsi = wsi->u.ws.rx_draining_ext_list;
}
return -1;
}
- if (rlen <= 0)
+ if (rlen == 0)
+ return -1;
+
+ if (rlen < 0)
return 0;
*len = rlen;
return 0;
if (wsi->u.http.content_remain &&
- (int)wsi->u.http.content_remain < *len)
- n = wsi->u.http.content_remain;
+ wsi->u.http.content_remain < *len)
+ n = (int)wsi->u.http.content_remain;
else
n = *len;
}
#endif
+static int
+lws_is_ws_with_ext(struct lws *wsi)
+{
+#if defined(LWS_NO_EXTENSIONS)
+ return 0;
+#else
+ return wsi->state == LWSS_ESTABLISHED &&
+ !!wsi->count_act_ext;
+#endif
+}
+
LWS_VISIBLE int
lws_service_fd_tsi(struct lws_context *context, struct lws_pollfd *pollfd, int tsi)
{
if (context->last_timeout_check_s != now) {
context->last_timeout_check_s = now;
+#if defined(LWS_WITH_STATS)
+ if (!tsi && now - context->last_dump > 10) {
+ lws_stats_log_dump(context);
+ context->last_dump = now;
+ }
+#endif
+
lws_plat_service_periodic(context);
/* retire unused deprecated context */
#endif
- lwsl_debug("fd=%d, revents=%d, mode=%d, state=%d\n", pollfd->fd, pollfd->revents, (int)wsi->mode, (int)wsi->state);
- if (pollfd->revents & LWS_POLLHUP)
+// lwsl_debug("fd=%d, revents=%d, mode=%d, state=%d\n", pollfd->fd, pollfd->revents, (int)wsi->mode, (int)wsi->state);
+ if (pollfd->revents & LWS_POLLHUP) {
+ lwsl_debug("pollhup\n");
+ wsi->socket_is_permanently_unusable = 1;
goto close_and_handled;
+ }
#ifdef LWS_OPENSSL_SUPPORT
- if ((wsi->state == LWSS_SHUTDOWN) && lws_is_ssl(wsi) && wsi->ssl)
- {
- n = SSL_shutdown(wsi->ssl);
- lwsl_debug("SSH_shutdown=%d for fd %d\n", n, wsi->desc.sockfd);
- if (n == 1)
- {
- n = shutdown(wsi->desc.sockfd, SHUT_WR);
- goto close_and_handled;
- }
- else
- {
- lws_change_pollfd(wsi, LWS_POLLOUT, LWS_POLLIN);
- n = 0;
- goto handled;
- }
- }
+ if ((wsi->state == LWSS_SHUTDOWN) && lws_is_ssl(wsi) && wsi->ssl)
+ {
+ n = SSL_shutdown(wsi->ssl);
+ lwsl_debug("SSL_shutdown=%d for fd %d\n", n, wsi->desc.sockfd);
+ if (n == 1)
+ {
+ n = shutdown(wsi->desc.sockfd, SHUT_WR);
+ goto close_and_handled;
+ }
+ else if (n == 0)
+ {
+ lws_change_pollfd(wsi, LWS_POLLOUT, LWS_POLLIN);
+ n = 0;
+ goto handled;
+ }
+ else /* n < 0 */
+ {
+ int shutdown_error = SSL_get_error(wsi->ssl, n);
+ lwsl_debug("SSL_shutdown returned %d, SSL_get_error: %d\n", n, shutdown_error);
+ if (shutdown_error == SSL_ERROR_WANT_READ) {
+ lws_change_pollfd(wsi, LWS_POLLOUT, LWS_POLLIN);
+ n = 0;
+ goto handled;
+ } else if (shutdown_error == SSL_ERROR_WANT_WRITE) {
+ lws_change_pollfd(wsi, LWS_POLLOUT, LWS_POLLOUT);
+ n = 0;
+ goto handled;
+ }
+
+ // actual error occurred, just close the connection
+ n = shutdown(wsi->desc.sockfd, SHUT_WR);
+ goto close_and_handled;
+ }
+ }
#endif
/* okay, what we came here to do... */
wsi->u.hdr.ah->rxpos;
} else {
if (wsi->mode != LWSCM_HTTP_CLIENT_ACCEPTED) {
+ /*
+ * extension may not consume everything (eg, pmd may be constrained
+ * as to what it can output...) has to go in per-wsi rx buf area.
+ * Otherwise in large temp serv_buf area.
+ */
+ eff_buf.token = (char *)pt->serv_buf;
+ if (lws_is_ws_with_ext(wsi)) {
+ eff_buf.token_len = wsi->u.ws.rx_ubuf_alloc;
+ } else {
+ eff_buf.token_len = context->pt_serv_buf_size;
+ }
+
+ if ((unsigned int)eff_buf.token_len > context->pt_serv_buf_size)
+ eff_buf.token_len = context->pt_serv_buf_size;
+
eff_buf.token_len = lws_ssl_capable_read(wsi,
- pt->serv_buf, pending ? pending :
- context->pt_serv_buf_size);
+ (unsigned char *)eff_buf.token, pending ? pending :
+ eff_buf.token_len);
switch (eff_buf.token_len) {
case 0:
lwsl_info("%s: zero length read\n", __func__);
lwsl_info("Closing when error\n");
goto close_and_handled;
}
-
- eff_buf.token = (char *)pt->serv_buf;
+ // lwsl_notice("Actual RX %d\n", eff_buf.token_len);
}
}
wsi->protocol->callback,
wsi, LWS_CALLBACK_RECEIVE_CLIENT_HTTP,
wsi->user_space, NULL, 0)) {
- lwsl_debug("LWS_CALLBACK_RECEIVE_CLIENT_HTTP closed it\n");
+ lwsl_notice("LWS_CALLBACK_RECEIVE_CLIENT_HTTP closed it\n");
goto close_and_handled;
}
}
* around again it will pick up from where it
* left off.
*/
+ // lwsl_notice("doing lws_read from pt->serv_buf %p %p for len %d\n", pt->serv_buf, eff_buf.token, (int)eff_buf.token_len);
+
n = lws_read(wsi, (unsigned char *)eff_buf.token,
eff_buf.token_len);
if (n < 0) {
pending = lws_ssl_pending(wsi);
if (pending) {
- pending = pending > context->pt_serv_buf_size ?
+ if (lws_is_ws_with_ext(wsi))
+ pending = pending > wsi->u.ws.rx_ubuf_alloc ?
+ wsi->u.ws.rx_ubuf_alloc : pending;
+ else
+ pending = pending > context->pt_serv_buf_size ?
context->pt_serv_buf_size : pending;
goto read;
}
goto handled;
close_and_handled:
- lwsl_debug("Close and handled\n");
+ lwsl_debug("%p: Close and handled\n", wsi);
lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
/*
* pollfd may point to something else after the close