Code clean up and refactoring

master
Kirigaya Kazuto 2017-12-15 11:20:39 +08:00
parent 4f79ff7dea
commit fe87f9a31b
5 changed files with 298 additions and 278 deletions

View File

@ -1,141 +1,11 @@
#include "util.h"
/// The following code is part of General Socket Wrapper (GSock) Project
/// Under MIT License
#define _WIN32_WINNT 0x0603
#include <stdexcept>
#include <cstring>
#include <string>
#include <sstream>
#include <memory>
#include <list>
#include <stack>
#include <algorithm>
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#define ZLIB_WINAPI
#include "zlib/zlib.h"
#include "util_include.h"
#include "zlib_wrapper.h"
#include "log.h"
std::string GZDecompress(const std::string& InputStr);
class _init_winsock2_2_class
{
public:
_init_winsock2_2_class()
{
/// Windows Platform need WinSock2.DLL initialization.
WORD wd;
WSAData wdt;
wd=MAKEWORD(2,2);
if(WSAStartup(wd,&wdt)<0)
{
throw std::runtime_error("Unable to load winsock2.dll. ");
}
}
~_init_winsock2_2_class()
{
/// Windows Platform need WinSock2.DLL clean up.
WSACleanup();
}
};
static _init_winsock2_2_class _init_winsock2_2_obj;
class Socket::_impl
{
public:
int sfd;
sockaddr_in saddr;
bool created;
};
Socket::Socket() : _pp(new _impl)
{
_pp->created=false;
}
Socket::~Socket()
{
if(_pp)
{
if(_pp->created)
{
closesocket(_pp->sfd);
}
delete _pp;
}
}
int Socket::connect(const std::string& IPStr,int Port)
{
if(_pp->created)
{
return -2;
}
_pp->sfd=socket(AF_INET,SOCK_STREAM,0);
if(_pp->sfd<0)
{
return -3;
}
_pp->created=true;
// refs
int& sfd=_pp->sfd;
sockaddr_in& saddr=_pp->saddr;
memset(&saddr,0,sizeof(saddr));
saddr.sin_addr.s_addr=inet_addr(IPStr.c_str());
saddr.sin_port=htons(Port);
saddr.sin_family=AF_INET;
return ::connect(sfd,(sockaddr*)&saddr,sizeof(saddr));
}
int Socket::send(const void* Buffer,int Length)
{
return ::send(_pp->sfd,(const char*)Buffer,Length,0);
}
int Socket::recv(void* Buffer,int MaxToRecv)
{
return ::recv(_pp->sfd,(char*)Buffer,MaxToRecv,0);
}
int DNSResolve(const std::string& HostName, std::string& _out_IPStr)
{
/// Use getaddrinfo instead
struct addrinfo hints;
memset(&hints,0,sizeof(hints));
hints.ai_family=AF_UNSPEC;
hints.ai_socktype=SOCK_STREAM;
hints.ai_protocol=IPPROTO_TCP;
struct addrinfo* result=nullptr;
int ret=getaddrinfo(HostName.c_str(),NULL,&hints,&result);
if(ret!=0)
{
return -1;/// API Call Failed.
}
for(struct addrinfo* ptr=result; ptr!=nullptr; ptr=ptr->ai_next)
{
switch(ptr->ai_family)
{
case AF_INET:
sockaddr_in* addr=(struct sockaddr_in*) (ptr->ai_addr) ;
_out_IPStr=inet_ntoa(addr->sin_addr);
return 0;
break;
}
}
/// Unknown error.
return -2;
}
#include <string>
#include <sstream>
#include <algorithm>
HTTPRequest::HTTPRequest()
{
@ -467,146 +337,3 @@ int HTTPRequest::send(HTTPResponse& _out_res)
/// Succeed.
return 0;
}
/// From CSDN
/* Uncompress gzip data */
int _gz_decompress_real(Byte *zdata, uLong nzdata,
Byte *data, uLong *ndata)
{
int err = 0;
z_stream d_stream = {0}; /* decompression stream */
static char dummy_head[2] =
{
0x8 + 0x7 * 0x10,
(((0x8 + 0x7 * 0x10) * 0x100 + 30) / 31 * 31) & 0xFF,
};
d_stream.zalloc = (alloc_func)0;
d_stream.zfree = (free_func)0;
d_stream.opaque = (voidpf)0;
d_stream.next_in = zdata;
d_stream.avail_in = 0;
d_stream.next_out = data;
if(inflateInit2(&d_stream, MAX_WBITS+32) != Z_OK) return -1;
/// Decompress Input Data by byte. (There must be some faster methods)
while (d_stream.total_out < *ndata && d_stream.total_in < nzdata) {
d_stream.avail_in = d_stream.avail_out = 1;
if((err = inflate(&d_stream, Z_NO_FLUSH)) == Z_STREAM_END) break;
if(err != Z_OK )
{
if(err == Z_DATA_ERROR)
{
d_stream.next_in = (Bytef*) dummy_head;
d_stream.avail_in = sizeof(dummy_head);
if((err = inflate(&d_stream, Z_NO_FLUSH)) != Z_OK)
{
/// Clean up.
if(inflateEnd(&d_stream)!=Z_OK)
return -2;
else
return -3;
}
}
else if(err==Z_BUF_ERROR)
{
/// Clean up.
if(inflateEnd(&d_stream)!=Z_OK)
return 1;
else
return 2;
}
else
{
/// Clean up.
if(inflateEnd(&d_stream)!=Z_OK)
return -4;
else
return -5;
}
}
}
err=inflate(&d_stream,Z_FINISH);
if(err==Z_BUF_ERROR)
{
/// Clean up.
if(inflateEnd(&d_stream) != Z_OK)
return 11;
else
return 12;
}
else if(err!=Z_OK&&err!=Z_STREAM_END)
{
/// Clean up.
if(inflateEnd(&d_stream) != Z_OK)
return -15;
else
return -16;
}
uLong total_out=d_stream.total_out;
/// Clean up.
if(inflateEnd(&d_stream) != Z_OK) return -10;
*ndata = total_out;
return 0;
}
std::string GZDecompress(const std::string& InputStr)
{
int ret;
int inputSz=InputStr.size();
char* holder=(char*)malloc(inputSz+32);
if(!holder)
{
dprintf("Failed to malloc holder.\n");
return "";
}
memset(holder,0,inputSz+32);
memcpy(holder,InputStr.data(),inputSz);
std::shared_ptr<char> sp;
sp.reset(holder,free);
int rate=2;
do
{
uLong outSz=inputSz*rate+32;
dprintf("Try decompress with BufferLength: %d\n",(int)outSz);
char* buffer=(char*)malloc(outSz);
if(!buffer)
{
dprintf("Failed to malloc buffer with size=%d\n",(int)outSz);
return "";
}
memset(buffer,0,outSz);
std::shared_ptr<char> xp;
xp.reset(buffer,free);
if((ret=_gz_decompress_real((Byte*)holder,inputSz,(Byte*)buffer,&outSz))!=0)
{
if(ret>0)
{
dprintf("Decompress Failed. Continue with ret %d\n",ret);
rate++;
}
else
{
dprintf("Decompress Failed: ret=%d\n",ret);
return "";
}
}
else
{
return std::string(buffer,outSz);
}
}while(true);
dprintf("Warning: Program should never reaches here.\n");
return "";
}

129
src/Socket.cpp Normal file
View File

@ -0,0 +1,129 @@
#include "util.h"
#include "util_include.h"
/// The following code is part of General Socket Wrapper (GSock) Project
/// Under MIT License
#define _WIN32_WINNT 0x0603
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <string>
#include <stdexcept>
class _init_winsock2_2_class
{
public:
_init_winsock2_2_class()
{
/// Windows Platform need WinSock2.DLL initialization.
WORD wd;
WSAData wdt;
wd=MAKEWORD(2,2);
if(WSAStartup(wd,&wdt)<0)
{
throw std::runtime_error("Unable to load winsock2.dll. ");
}
}
~_init_winsock2_2_class()
{
/// Windows Platform need WinSock2.DLL clean up.
WSACleanup();
}
};
static _init_winsock2_2_class _init_winsock2_2_obj;
class Socket::_impl
{
public:
int sfd;
sockaddr_in saddr;
bool created;
};
Socket::Socket() : _pp(new _impl)
{
_pp->created=false;
}
Socket::~Socket()
{
if(_pp)
{
if(_pp->created)
{
closesocket(_pp->sfd);
}
delete _pp;
}
}
int Socket::connect(const std::string& IPStr,int Port)
{
if(_pp->created)
{
return -2;
}
_pp->sfd=socket(AF_INET,SOCK_STREAM,0);
if(_pp->sfd<0)
{
return -3;
}
_pp->created=true;
// refs
int& sfd=_pp->sfd;
sockaddr_in& saddr=_pp->saddr;
memset(&saddr,0,sizeof(saddr));
saddr.sin_addr.s_addr=inet_addr(IPStr.c_str());
saddr.sin_port=htons(Port);
saddr.sin_family=AF_INET;
return ::connect(sfd,(sockaddr*)&saddr,sizeof(saddr));
}
int Socket::send(const void* Buffer,int Length)
{
return ::send(_pp->sfd,(const char*)Buffer,Length,0);
}
int Socket::recv(void* Buffer,int MaxToRecv)
{
return ::recv(_pp->sfd,(char*)Buffer,MaxToRecv,0);
}
int DNSResolve(const std::string& HostName, std::string& _out_IPStr)
{
/// Use getaddrinfo instead
struct addrinfo hints;
memset(&hints,0,sizeof(hints));
hints.ai_family=AF_UNSPEC;
hints.ai_socktype=SOCK_STREAM;
hints.ai_protocol=IPPROTO_TCP;
struct addrinfo* result=nullptr;
int ret=getaddrinfo(HostName.c_str(),NULL,&hints,&result);
if(ret!=0)
{
return -1;/// API Call Failed.
}
for(struct addrinfo* ptr=result; ptr!=nullptr; ptr=ptr->ai_next)
{
switch(ptr->ai_family)
{
case AF_INET:
sockaddr_in* addr=(struct sockaddr_in*) (ptr->ai_addr) ;
_out_IPStr=inet_ntoa(addr->sin_addr);
return 0;
break;
}
}
/// Unknown error.
return -2;
}

View File

@ -0,0 +1,7 @@
#pragma once
#include <string>
/** Internal Shared Functions */
/// DNS Resolve: Resolve from hostname to IPAddr.
/// Return: 0 Success -1 Failed.
int DNSResolve(const std::string& HostName, std::string& _out_IPStr);

View File

@ -0,0 +1,10 @@
#pragma once
#ifdef __WIN32 /// Windows Needs this Definition.
#define ZLIB_WINAPI
#endif // __WIN32
#include "zlib/zlib.h"
#include <string>
/// Decompress GZIP data. InputStr can contains binary data.
std::string GZDecompress(const std::string& InputStr);

147
src/zlib_wrapper.cpp Normal file
View File

@ -0,0 +1,147 @@
#include "zlib_wrapper.h"
#include "log.h"
#include <memory>
#include <string>
/// From CSDN
/* Uncompress gzip data */
static int _gz_decompress_real(Byte *zdata, uLong nzdata,
Byte *data, uLong *ndata)
{
int err = 0;
z_stream d_stream = {0}; /* decompression stream */
static char dummy_head[2] =
{
0x8 + 0x7 * 0x10,
(((0x8 + 0x7 * 0x10) * 0x100 + 30) / 31 * 31) & 0xFF,
};
d_stream.zalloc = (alloc_func)0;
d_stream.zfree = (free_func)0;
d_stream.opaque = (voidpf)0;
d_stream.next_in = zdata;
d_stream.avail_in = 0;
d_stream.next_out = data;
if(inflateInit2(&d_stream, MAX_WBITS+32) != Z_OK) return -1;
/// Decompress Input Data by byte. (There must be some faster methods)
while (d_stream.total_out < *ndata && d_stream.total_in < nzdata) {
d_stream.avail_in = d_stream.avail_out = 1;
if((err = inflate(&d_stream, Z_NO_FLUSH)) == Z_STREAM_END) break;
if(err != Z_OK )
{
if(err == Z_DATA_ERROR)
{
d_stream.next_in = (Bytef*) dummy_head;
d_stream.avail_in = sizeof(dummy_head);
if((err = inflate(&d_stream, Z_NO_FLUSH)) != Z_OK)
{
/// Clean up.
if(inflateEnd(&d_stream)!=Z_OK)
return -2;
else
return -3;
}
}
else if(err==Z_BUF_ERROR)
{
/// Clean up.
if(inflateEnd(&d_stream)!=Z_OK)
return 1;
else
return 2;
}
else
{
/// Clean up.
if(inflateEnd(&d_stream)!=Z_OK)
return -4;
else
return -5;
}
}
}
err=inflate(&d_stream,Z_FINISH);
if(err==Z_BUF_ERROR)
{
/// Clean up.
if(inflateEnd(&d_stream) != Z_OK)
return 11;
else
return 12;
}
else if(err!=Z_OK&&err!=Z_STREAM_END)
{
/// Clean up.
if(inflateEnd(&d_stream) != Z_OK)
return -15;
else
return -16;
}
uLong total_out=d_stream.total_out;
/// Clean up.
if(inflateEnd(&d_stream) != Z_OK) return -10;
*ndata = total_out;
return 0;
}
std::string GZDecompress(const std::string& InputStr)
{
int ret;
int inputSz=InputStr.size();
char* holder=(char*)malloc(inputSz+32);
if(!holder)
{
dprintf("Failed to malloc holder.\n");
return "";
}
memset(holder,0,inputSz+32);
memcpy(holder,InputStr.data(),inputSz);
std::shared_ptr<char> sp;
sp.reset(holder,free);
int rate=2;
do
{
uLong outSz=inputSz*rate+32;
dprintf("Try decompress with BufferLength: %d\n",(int)outSz);
char* buffer=(char*)malloc(outSz);
if(!buffer)
{
dprintf("Failed to malloc buffer with size=%d\n",(int)outSz);
return "";
}
memset(buffer,0,outSz);
std::shared_ptr<char> xp;
xp.reset(buffer,free);
if((ret=_gz_decompress_real((Byte*)holder,inputSz,(Byte*)buffer,&outSz))!=0)
{
if(ret>0)
{
dprintf("Decompress Failed. Continue with ret %d\n",ret);
rate++;
}
else
{
dprintf("Decompress Failed: ret=%d\n",ret);
return "";
}
}
else
{
return std::string(buffer,outSz);
}
}while(true);
dprintf("Warning: Program should never reaches here.\n");
return "";
}