From d479033a614dc3600ec23fd9c5d54f82c51942c3 Mon Sep 17 00:00:00 2001 From: Igor Kotrasinski Date: Tue, 27 Nov 2018 11:28:28 +0100 Subject: [PATCH] Rewrite some read/write loops for readability Change-Id: I2f26120a82c51f0a285f96cb16b0f9f2499c2c77 Signed-off-by: Igor Kotrasinski --- TEECLib/src/teec_connection.c | 24 ++++++++++++++---------- ssflib/src/ssf_client.cpp | 20 ++++++++++++++------ 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/TEECLib/src/teec_connection.c b/TEECLib/src/teec_connection.c index da805d3..7ae18cc 100644 --- a/TEECLib/src/teec_connection.c +++ b/TEECLib/src/teec_connection.c @@ -130,12 +130,14 @@ static uint32_t sendCommandtoDaemon(int32_t sockfd, char *fdata, size_t size) if (sockfd >= 0) { // send size number of bytes to Simulator Daemon - do { + while (nbytes < size) { nwrite = send(sockfd, fdata + nbytes, size - nbytes, 0); - } while ((nwrite == -1 && errno == EINTR) || (nwrite > 0 && ((nbytes += - - nwrite) < size))); - + if (nwrite == -1 && errno == EINTR) + continue; + if (nwrite <= 0) + break; + nbytes += nwrite; + } return (size != nbytes) ? errno : 0; } @@ -163,12 +165,14 @@ static uint32_t receiveResponse(int32_t sockfd, char *fdata, size_t size) if (sockfd >= 0) { // receive size number of bytes to Simulator Daemon - do { + while (nbytes < size) { nread = recv(sockfd, fdata + nbytes, size - nbytes, 0); - } while ((nread == -1 && errno == EINTR) - - || (nread > 0 && ((nbytes += nread) < size))); - + if (nread == -1 && errno == EINTR) + continue; + if (nread <= 0) + break; + nbytes += nread; + } return (size != nbytes) ? errno : 0; } diff --git a/ssflib/src/ssf_client.cpp b/ssflib/src/ssf_client.cpp index e0cec5d..7164f38 100644 --- a/ssflib/src/ssf_client.cpp +++ b/ssflib/src/ssf_client.cpp @@ -100,10 +100,14 @@ static uint32_t sendCommandtoDaemon(int32_t sockfd, char* fdata, size_t size) { ssize_t nwrite = 0; size_t nbytes = 0; if (sockfd >= 0) { - do { + while (nbytes < size) { nwrite = send(sockfd, fdata + nbytes, size - nbytes, 0); - } while ((nwrite == -1 && errno == EINTR) || (nwrite > 0 && ((nbytes += - nwrite) < size))); + if (nwrite == -1 && errno == EINTR) + continue; + if (nwrite <= 0) + break; + nbytes += nwrite; + } return (size != nbytes) ? errno : 0; } LOGE(MODULE_SSF_LIB, "failed"); @@ -123,10 +127,14 @@ static uint32_t receiveResponse(int32_t sockfd, char* fdata, size_t size) { ssize_t nread = 0; size_t nbytes = 0; if (sockfd >= 0) { - do { + while (nbytes < size) { nread = recv(sockfd, fdata + nbytes, size - nbytes, 0); - } while ((nread == -1 && errno == EINTR) - || (nread > 0 && ((nbytes += nread) < size))); + if (nread == -1 && errno == EINTR) + continue; + if (nread <= 0) + break; + nbytes += nread; + } return (size != nbytes) ? errno : 0; } LOGE(MODULE_SSF_LIB, "failed"); -- 2.7.4