From 767e457a41ca23aaba9b34cfa85cffc16290fe3e Mon Sep 17 00:00:00 2001 From: Kiritow <1362050620@qq.com> Date: Fri, 8 Jun 2018 12:02:59 +0800 Subject: [PATCH] Update helper --- gsock_helper.cpp | 96 ++++++++++++++++++++++++++++++++++++++++++++++++ gsock_helper.h | 7 ++++ 2 files changed, 103 insertions(+) diff --git a/gsock_helper.cpp b/gsock_helper.cpp index c8a3422..779626d 100644 --- a/gsock_helper.cpp +++ b/gsock_helper.cpp @@ -1,5 +1,32 @@ +/** General Socket Wrapper +* Created By Kiritow. (https://github.com/kiritow) +* Licensed under MIT +*/ + #include "gsock_helper.h" +#ifdef _WIN32 +/// Using Win8.1 +#define _WIN32_WINNT 0x0603 + +#include +#include +#else +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define closesocket close +using BYTE = unsigned char; +#define WSAGetLastError() errno +#endif + sock_helper::sock_helper(sock& s) :_s(s) { @@ -84,4 +111,73 @@ int sock_helper::recvuntil(void* buff, int max_length, { int x; return recvuntil(buff, max_length, cond_fn, x); +} + +int sock_helper::sendpack(const void* ptr, int datasz) +{ + long net_size = htonl(datasz); + int ret = sendall(&net_size, sizeof(net_size)); + if (ret <= 0) return ret; + ret = sendall(ptr, datasz); + if (ret <= 0) return ret; + return datasz + sizeof(net_size); +} + +int sock_helper::sendpack(const std::string& data) +{ + return sendpack(data.data(), data.size()); +} + +int sock_helper::recvpack(std::string& out_data) +{ + long net_size; + int ret = recvuntil(&net_size, sizeof(long), []() {return false; }); + if (ret <= 0) + { + return ret; + } + std::string str; + char c; + long data_size = ntohl(net_size); + if (data_size <= 0) + { + return -2; + } + long done = 0; + while (done < data_size) + { + int ret = _s.recv(&c, 1); + if (ret <= 0) + { + return ret; + } + str.push_back(c); + done += ret; + } + + out_data = str; + return done; +} + +int sock_helper::recvline(std::string& out_data, const std::string& separator, bool keep_sep) +{ + out_data.clear(); + + char c; + int done = 0; + while (true) + { + int ret = _s.recv(&c, 1); + if (ret <= 0) + { + return ret; + } + out_data.push_back(c); + done += ret; + if (out_data.find(separator) != std::string::npos) + { + break; + } + } + return done; } \ No newline at end of file diff --git a/gsock_helper.h b/gsock_helper.h index 7605b72..46a6cab 100644 --- a/gsock_helper.h +++ b/gsock_helper.h @@ -8,6 +8,7 @@ #pragma once #include "gsock.h" #include +#include class sock_helper { @@ -21,6 +22,12 @@ public: int recvuntil(void* buff, int max_length, const std::function& cond_fn, int& bytes_recv); int recvuntil(void* buff, int max_length, const std::function& cond_fn); int recvuntil(void* buff, int max_length, const std::function& cond_fn, int& bytes_recv); + + int sendpack(const void* ptr, int datasz); + int sendpack(const std::string& data); + int recvpack(std::string& out_data); + + int recvline(std::string& out_data, const std::string& separator="\r\n", bool keep_sep = false); private: sock & _s; }; \ No newline at end of file