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 <[email protected]>
  • Loading branch information
AndreyDodonov-EH committed Jul 4, 2024
1 parent 4da8926 commit d3ea16d
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions subsys/net/lib/websocket/websocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -544,19 +544,39 @@ 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, int32_t timeout)
{
int ret, i;
size_t offset = 0;
size_t total_len = 0;
int64_t timestamp = k_uptime_get();

for (i = 0; i < message->msg_iovlen; i++) {
total_len += message->msg_iov[i].iov_len;
}

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;

timeout -= (int32_t)k_uptime_delta(&timestamp);
if (timeout < 0) {
/* timeout, make poll return immediately */
timeout = 0;
}

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

Expand Down Expand Up @@ -623,7 +643,7 @@ static int websocket_prepare_and_send(struct websocket_context *ctx,
}

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, timeout);
#endif /* CONFIG_NET_TEST */
}

Expand Down

0 comments on commit d3ea16d

Please sign in to comment.