Update helper

This commit is contained in:
Kirigaya Kazuto 2018-06-16 23:27:47 +08:00
parent 78a37687d1
commit 37e9ecf160
2 changed files with 22 additions and 1 deletions

View File

@ -184,12 +184,23 @@ int sock_helper::recvpack(std::string& out_data)
return done; 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) int sock_helper::recvline(std::string& out_data, const std::string& separator, bool keep_sep)
{ {
out_data.clear(); out_data.clear();
char c; char c;
int done = 0; int done = 0;
const int spsz = separator.size();
while (true) while (true)
{ {
int ret = _s.recv(&c, 1); 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); out_data.push_back(c);
done += ret; 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; break;
} }
} }
if (!keep_sep)
{
for (int i = 0; i < spsz; i++)
{
out_data.pop_back();
}
}
return done; return done;
} }

View File

@ -30,6 +30,7 @@ public:
int sendpack(const std::string& data); int sendpack(const std::string& data);
int recvpack(std::string& out_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); int recvline(std::string& out_data, const std::string& separator="\r\n", bool keep_sep = false);
private: private:
sock & _s; sock & _s;