From c5019de982e7e9848565addada3d183793b1e38a Mon Sep 17 00:00:00 2001 From: Peter McGoron Date: Sun, 2 Apr 2023 21:46:10 +0000 Subject: [PATCH] make read_buf read until it fills up all of bufptr --- software/src/sock.c | 23 ++++++++++------------- software/src/sock.h | 2 +- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/software/src/sock.c b/software/src/sock.c index e662303..b522c78 100644 --- a/software/src/sock.c +++ b/software/src/sock.c @@ -48,8 +48,8 @@ server_accept_client(int server) struct sockaddr_in addr; socklen_t len = sizeof(addr); - /* Accept clients in a loop. This is halting so other threads will - * be able to process clients. + /* Accept clients in a loop. This should block when there are no clients + * so other threads can run. */ do { client = zsock_accept(server, (struct sockaddr *)&addr, &len); @@ -64,23 +64,20 @@ server_accept_client(int server) } bool -sock_read_buf(int sock, struct bufptr *bp) +sock_read_buf(int sock, struct bufptr *bp, bool entire) { if (bp->left < 2) return false; - ssize_t l = zsock_recv(sock, bp->p, bp->left - 1, 0); - if (l < 0) - return false; + do { + ssize_t l = zsock_recv(sock, bp->p, bp->left - 1, 0); + if (l < 0) + return false; - bp->left -= l; - bp->p += l; - if (bp->left == 0) { - LOG_ERR("internal out of bounds error in %s", __func__); - k_fatal_halt(K_ERR_KERNEL_PANIC); - } + bp->left -= l; + bp->p += l; + } while (entire && bp->left > 0); - *bp->p = 0; return true; } diff --git a/software/src/sock.h b/software/src/sock.h index fef6661..ad6ad52 100644 --- a/software/src/sock.h +++ b/software/src/sock.h @@ -13,7 +13,7 @@ int server_accept_client(int server); /* Read data into buffer. Returns false if error occurs. This is * raw binary (no NUL termination). */ -bool sock_read_buf(int sock, struct bufptr *bp); +bool sock_read_buf(int sock, struct bufptr *bp, bool entire); /* Write raw buffer data into socket. This data is raw binary and * does not have to be NUL terminated.