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-Tag: 2.409~286 X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/9bcc50116ad21379a31ab8d65deb36688f2044b3?ds=sidebyside 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 c7c1bf05..5bd66c87 100644 --- a/stglibs/common.lib/common.cpp +++ b/stglibs/common.lib/common.cpp @@ -1015,3 +1015,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 94c26a8b..0791f093 100644 --- a/stglibs/common.lib/include/stg/common.h +++ b/stglibs/common.lib/include/stg/common.h @@ -139,6 +139,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);