From: Maxim Mamontov Date: Sat, 13 Sep 2014 21:57:22 +0000 (+0300) Subject: Added special function to read/write all data (for TCP). X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/237f834a52db0d12c85f70c5e65f48ec1923d00a Added special function to read/write all data (for TCP). --- 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);