Skip to content

Commit

Permalink
net: lib: websocket: call socket poll for websocket
Browse files Browse the repository at this point in the history
If we couldn't send all (or any data) via the socket,
invoke poll instead of blindly retrying and flooding the socket

Signed-off-by: Andrey Dodonov <Andrey.Dodonov@endress.com>
  • Loading branch information
AndreyDodonov-EH authored and aescolar committed Jul 9, 2024
1 parent 15ead53 commit a4123a8
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions subsys/net/lib/websocket/websocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,8 @@ static int websocket_ioctl_vmeth(void *obj, unsigned int request, va_list args)
}

#if !defined(CONFIG_NET_TEST)
static int sendmsg_all(int sock, const struct msghdr *message, int flags)
static int sendmsg_all(int sock, const struct msghdr *message, int flags,
const k_timepoint_t req_end_timepoint)
{
int ret, i;
size_t offset = 0;
Expand All @@ -556,7 +557,25 @@ static int sendmsg_all(int sock, const struct msghdr *message, int flags)

while (offset < total_len) {
ret = zsock_sendmsg(sock, message, flags);
if (ret < 0) {

if ((ret == 0) || (ret < 0 && errno == EAGAIN)) {
struct zsock_pollfd pfd;
int pollres;
k_ticks_t req_timeout_ticks =
sys_timepoint_timeout(req_end_timepoint).ticks;
int req_timeout_ms = k_ticks_to_ms_floor32(req_timeout_ticks);

pfd.fd = sock;
pfd.events = ZSOCK_POLLOUT;
pollres = zsock_poll(&pfd, 1, req_timeout_ms);
if (pollres == 0) {
return -ETIMEDOUT;
} else if (pollres > 0) {
continue;
} else {
return -errno;
}
} else if (ret < 0) {
return -errno;
}

Expand Down Expand Up @@ -622,8 +641,11 @@ static int websocket_prepare_and_send(struct websocket_context *ctx,
tout = K_MSEC(timeout);
}

k_timeout_t req_timeout = K_MSEC(timeout);
k_timepoint_t req_end_timepoint = sys_timepoint_calc(req_timeout);

return sendmsg_all(ctx->real_sock, &msg,
K_TIMEOUT_EQ(tout, K_NO_WAIT) ? MSG_DONTWAIT : 0);
K_TIMEOUT_EQ(tout, K_NO_WAIT) ? MSG_DONTWAIT : 0, req_end_timepoint);
#endif /* CONFIG_NET_TEST */
}

Expand Down

0 comments on commit a4123a8

Please sign in to comment.