Update helper

This commit is contained in:
Kirigaya Kazuto 2018-06-08 12:02:59 +08:00
parent e106e769cf
commit 767e457a41
2 changed files with 103 additions and 0 deletions

View File

@ -1,5 +1,32 @@
/** General Socket Wrapper
* Created By Kiritow. (https://github.com/kiritow)
* Licensed under MIT
*/
#include "gsock_helper.h" #include "gsock_helper.h"
#ifdef _WIN32
/// Using Win8.1
#define _WIN32_WINNT 0x0603
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <errno.h>
#include <sys/epoll.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <fcntl.h>
#define closesocket close
using BYTE = unsigned char;
#define WSAGetLastError() errno
#endif
sock_helper::sock_helper(sock& s) :_s(s) sock_helper::sock_helper(sock& s) :_s(s)
{ {
@ -85,3 +112,72 @@ int sock_helper::recvuntil(void* buff, int max_length,
int x; int x;
return recvuntil(buff, max_length, cond_fn, 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;
}

View File

@ -8,6 +8,7 @@
#pragma once #pragma once
#include "gsock.h" #include "gsock.h"
#include <functional> #include <functional>
#include <string>
class sock_helper class sock_helper
{ {
@ -21,6 +22,12 @@ public:
int recvuntil(void* buff, int max_length, const std::function<bool()>& cond_fn, int& bytes_recv); int recvuntil(void* buff, int max_length, const std::function<bool()>& cond_fn, int& bytes_recv);
int recvuntil(void* buff, int max_length, const std::function<bool(void*, int)>& cond_fn); int recvuntil(void* buff, int max_length, const std::function<bool(void*, int)>& cond_fn);
int recvuntil(void* buff, int max_length, const std::function<bool(void*, int)>& cond_fn, int& bytes_recv); int recvuntil(void* buff, int max_length, const std::function<bool(void*, int)>& 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: private:
sock & _s; sock & _s;
}; };