206 lines
3.7 KiB
C++
206 lines
3.7 KiB
C++
|
#include "SocketC.h"
|
|||
|
|
|||
|
bool SocketC::initSocket()
|
|||
|
{
|
|||
|
//<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><D7BD>ֿ<EFBFBD>
|
|||
|
WORD w_req = MAKEWORD(2, 2);//<2F>汾<EFBFBD><E6B1BE>
|
|||
|
WSADATA wsadata;
|
|||
|
int err;
|
|||
|
err = WSAStartup(w_req, &wsadata);
|
|||
|
if (err != 0)
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>汾<EFBFBD><E6B1BE>
|
|||
|
if (LOBYTE(wsadata.wVersion) != 2 || HIBYTE(wsadata.wHighVersion) != 2)
|
|||
|
{
|
|||
|
WSACleanup();
|
|||
|
return false;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
return true;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
SocketC::SocketC(int bufSize, int modo)
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
SocketC::~SocketC()
|
|||
|
{
|
|||
|
if (state == 1)
|
|||
|
{
|
|||
|
Close();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
bool SocketC::Connect(string IP, UINT Prot)
|
|||
|
{
|
|||
|
if (!initSocket())
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
struct addrinfo* answer, hint;
|
|||
|
char ipstr[16] = { 0 };
|
|||
|
ZeroMemory(&hint, sizeof(hint));
|
|||
|
hint.ai_family = AF_INET;
|
|||
|
hint.ai_socktype = SOCK_STREAM;
|
|||
|
|
|||
|
SOCKADDR_IN server_addr;
|
|||
|
|
|||
|
if (getaddrinfo(IP.c_str(), NULL, &hint, &answer) == 0)
|
|||
|
{
|
|||
|
server_addr.sin_family = AF_INET;
|
|||
|
|
|||
|
server_addr.sin_addr.S_un.S_addr = ((struct sockaddr_in*)(answer->ai_addr))->sin_addr.S_un.S_addr;
|
|||
|
server_addr.sin_port = htons(Prot);
|
|||
|
((SOCKADDR_IN*)&hint.ai_addr)->sin_port = htons(Prot);
|
|||
|
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><D7BD><EFBFBD>
|
|||
|
sclient = socket(AF_INET, SOCK_STREAM, 0);
|
|||
|
if (connect(sclient, (SOCKADDR*)&server_addr, sizeof(SOCKADDR)) == SOCKET_ERROR)
|
|||
|
{
|
|||
|
WSACleanup();
|
|||
|
return false;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
state = 1;
|
|||
|
rec = std::thread((&SocketC::Receive), this);
|
|||
|
return true;
|
|||
|
}
|
|||
|
}
|
|||
|
return FALSE;
|
|||
|
}
|
|||
|
|
|||
|
VOID SocketC::Receive()
|
|||
|
{
|
|||
|
char* buff = NULL;
|
|||
|
while(state)
|
|||
|
{
|
|||
|
char buf[1024] = { 0 };
|
|||
|
int len = recv(sclient, buf, 1024, 0);
|
|||
|
if (len ==SOCKET_ERROR || len == 0)
|
|||
|
{
|
|||
|
state = 0;
|
|||
|
if (Cfunc != NULL)
|
|||
|
{
|
|||
|
Cfunc();
|
|||
|
}
|
|||
|
//cout << "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͽ<EFBFBD><CFBF><EFBFBD><EFBFBD><EFBFBD>";
|
|||
|
break;
|
|||
|
}
|
|||
|
int isStr;
|
|||
|
buff = RecvBuff(buf, len, isStr);
|
|||
|
while (buff)
|
|||
|
{
|
|||
|
if (Rfunc != NULL)
|
|||
|
{
|
|||
|
if (isStr == 1)
|
|||
|
{
|
|||
|
AES aes;
|
|||
|
string str = aes.DecryptionAES(buff, "qwertyuiopasdfgh");
|
|||
|
Rfunc((char*)str.c_str(), str.length());
|
|||
|
}
|
|||
|
else
|
|||
|
Rfunc(buff, len);
|
|||
|
}
|
|||
|
delete buff;
|
|||
|
len = 0;
|
|||
|
buff = RecvBuff(NULL, len, isStr);
|
|||
|
}
|
|||
|
}
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
char* SocketC::RecvBuff(char* buf, int& len, int& isStr)
|
|||
|
{
|
|||
|
char* buff = NULL;
|
|||
|
MsgHead* h;
|
|||
|
lock_guard<std::mutex> guard(msgLock);
|
|||
|
if (len != 0)
|
|||
|
memcpy_s(tmpBuf + bufLen, len, buf, len);
|
|||
|
bufLen += len;
|
|||
|
|
|||
|
if (bufLen > sizeof(MsgHead))
|
|||
|
{
|
|||
|
h = (MsgHead*)tmpBuf;
|
|||
|
if (bufLen - sizeof(MsgHead) >= h->bufLen)
|
|||
|
{
|
|||
|
buff = new char[h->bufLen + 1];
|
|||
|
ZeroMemory(buff, h->bufLen + 1);
|
|||
|
memcpy_s(buff, h->bufLen, tmpBuf + sizeof(MsgHead), h->bufLen);
|
|||
|
len = h->bufLen;
|
|||
|
isStr = h->isStr;
|
|||
|
bufLen = bufLen - len - sizeof(MsgHead);
|
|||
|
if (bufLen > 0)
|
|||
|
memcpy_s(tmpBuf, bufLen, tmpBuf + sizeof(MsgHead) + len, bufLen);
|
|||
|
}
|
|||
|
}
|
|||
|
return buff;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
int SocketC::SendData(const char* Date, int len, int isStr)
|
|||
|
{
|
|||
|
MsgHead h;
|
|||
|
h.isStr = isStr;
|
|||
|
h.bufLen = len;
|
|||
|
time(&h.tm);
|
|||
|
string str = to_string(h.tm);
|
|||
|
str += to_string(len);
|
|||
|
md5 md;
|
|||
|
str = md.StringToMD5(str);
|
|||
|
memcpy_s(h.token, str.length(), str.c_str(), str.length());
|
|||
|
char* buff = new char[sizeof(MsgHead) + len];
|
|||
|
memcpy_s(buff, sizeof(MsgHead), &h, sizeof(MsgHead));
|
|||
|
memcpy_s(buff + sizeof(MsgHead), len, Date, len);
|
|||
|
int lenth = send(sclient, buff, len + sizeof(MsgHead), 0) - sizeof(MsgHead);
|
|||
|
delete[] buff;
|
|||
|
if (lenth < 0)
|
|||
|
{
|
|||
|
lenth = -1;
|
|||
|
}
|
|||
|
return lenth;
|
|||
|
}
|
|||
|
|
|||
|
BOOL SocketC::SendStr(string str)
|
|||
|
{
|
|||
|
if (str == "")
|
|||
|
return FALSE;
|
|||
|
AES aes;
|
|||
|
string sendStr = aes.EncryptionAES(str, "qwertyuiopasdfgh");
|
|||
|
if (-1 == SendData(sendStr.c_str(), sendStr.length(), 1))
|
|||
|
{
|
|||
|
return FALSE;
|
|||
|
}
|
|||
|
return TRUE;
|
|||
|
}
|
|||
|
|
|||
|
VOID SocketC::SetRecvFunc(function<VOID(char*, int)> fun)
|
|||
|
{
|
|||
|
Rfunc = fun;
|
|||
|
}
|
|||
|
|
|||
|
VOID SocketC::SetCloseFunc(function<VOID()> fun)
|
|||
|
{
|
|||
|
Cfunc = fun;
|
|||
|
}
|
|||
|
|
|||
|
void SocketC::Close()
|
|||
|
{
|
|||
|
if (state != 0)
|
|||
|
{
|
|||
|
state = 0;
|
|||
|
//<2F>ر<EFBFBD><D8B1><EFBFBD><D7BD><EFBFBD>
|
|||
|
shutdown(sclient, SD_BOTH);
|
|||
|
closesocket(sclient);
|
|||
|
//<2F>ͷ<EFBFBD>DLL<4C><4C>Դ
|
|||
|
rec.join();
|
|||
|
}
|
|||
|
WSACleanup();
|
|||
|
}
|