From 237f834a52db0d12c85f70c5e65f48ec1923d00a Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Sun, 14 Sep 2014 00:57:22 +0300 Subject: [PATCH] Added special function to read/write all data (for TCP). --- stglibs/common.lib/common.cpp | 32 +++++++++++++++++++++++++ stglibs/common.lib/include/stg/common.h | 3 +++ 2 files changed, 35 insertions(+) diff --git a/stglibs/common.lib/common.cpp b/stglibs/common.lib/common.cpp index fb6cdbb1..425c3ee6 100644 --- a/stglibs/common.lib/common.cpp +++ b/stglibs/common.lib/common.cpp @@ -1067,3 +1067,35 @@ if (res == 0) // Timeout return true; } + +bool ReadAll(int sd, void * dest, size_t size) +{ +size_t done = 0; +char * ptr = static_cast(dest); +while (done < size) + { + if (!WaitPackets(sd)) + return false; + ssize_t res = read(sd, ptr + done, size - done); + if (res < 0) + return false; + if (res == 0) + return true; + done += res; + } +return true; +} + +bool WriteAll(int sd, const void * source, size_t size) +{ +size_t done = 0; +const char * ptr = static_cast(source); +while (done < size) + { + ssize_t res = write(sd, ptr + done, size - done); + if (res <= 0) + return false; + done += res; + } +return true; +} diff --git a/stglibs/common.lib/include/stg/common.h b/stglibs/common.lib/include/stg/common.h index 6af1309f..41cdfa56 100644 --- a/stglibs/common.lib/include/stg/common.h +++ b/stglibs/common.lib/include/stg/common.h @@ -154,6 +154,9 @@ int ParseYesNo(const std::string & str, bool * val); bool WaitPackets(int sd); +bool ReadAll(int sd, void * dest, size_t size); +bool WriteAll(int sd, const void * source, size_t size); + //----------------------------------------------------------------------------- int str2x(const std::string & str, int32_t & x); int str2x(const std::string & str, uint32_t & x); -- 2.44.2