From 37e9ecf160832626954148c284e29c595bb83635 Mon Sep 17 00:00:00 2001 From: Kiritow <1362050620@qq.com> Date: Sat, 16 Jun 2018 23:27:47 +0800 Subject: [PATCH] Update helper --- gsock_helper.cpp | 22 +++++++++++++++++++++- gsock_helper.h | 1 + 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/gsock_helper.cpp b/gsock_helper.cpp index b582d28..97405a4 100644 --- a/gsock_helper.cpp +++ b/gsock_helper.cpp @@ -184,12 +184,23 @@ int sock_helper::recvpack(std::string& out_data) return done; } +int sock_helper::sendline(const std::string& data, const std::string& separator) +{ + int ret = sendall(data.c_str(), data.size()); + if (ret <= 0) return ret; + int xret = sendall(separator.c_str(), separator.size()); + if (xret < 0) return xret; + return ret + xret; +} + int sock_helper::recvline(std::string& out_data, const std::string& separator, bool keep_sep) { out_data.clear(); char c; int done = 0; + const int spsz = separator.size(); + while (true) { int ret = _s.recv(&c, 1); @@ -199,10 +210,19 @@ int sock_helper::recvline(std::string& out_data, const std::string& separator, b } out_data.push_back(c); done += ret; - if (out_data.find(separator) != std::string::npos) + if (strcmp(out_data.c_str() + out_data.size() - spsz, separator.c_str()) == 0) { break; } } + + if (!keep_sep) + { + for (int i = 0; i < spsz; i++) + { + out_data.pop_back(); + } + } + return done; } \ No newline at end of file diff --git a/gsock_helper.h b/gsock_helper.h index e97803b..3b07fb7 100644 --- a/gsock_helper.h +++ b/gsock_helper.h @@ -30,6 +30,7 @@ public: int sendpack(const std::string& data); int recvpack(std::string& out_data); + int sendline(const std::string& data, const std::string& seperator="\r\n"); int recvline(std::string& out_data, const std::string& separator="\r\n", bool keep_sep = false); private: sock & _s;