Skip to content

Commit

Permalink
Always read again immediately after reading a full recv buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
uNetworkingAB committed Feb 7, 2024
1 parent 2c1c6b5 commit 833497e
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -351,9 +351,19 @@ void us_internal_dispatch_ready_poll(struct us_poll_t *p, int error, int events)
}
}

int length = bsd_recv(us_poll_fd(&s->p), s->context->loop->data.recv_buf + LIBUS_RECV_BUFFER_PADDING, LIBUS_RECV_BUFFER_LENGTH, 0);
int length;
read_more:
length = bsd_recv(us_poll_fd(&s->p), s->context->loop->data.recv_buf + LIBUS_RECV_BUFFER_PADDING, LIBUS_RECV_BUFFER_LENGTH, 0);
if (length > 0) {
s = s->context->on_data(s, s->context->loop->data.recv_buf + LIBUS_RECV_BUFFER_PADDING, length);

/* If we filled the entire recv buffer, we need to immediately read again since otherwise a
* pending hangup event in the same even loop iteration can close the socket before we get
* the chance to read again next iteration */
if (length == LIBUS_RECV_BUFFER_LENGTH && s && !us_socket_is_closed(0, s)) {
goto read_more;
}

} else if (!length) {
if (us_socket_is_shut_down(0, s)) {
/* We got FIN back after sending it */
Expand Down

0 comments on commit 833497e

Please sign in to comment.