CSTest/网络客户端/SocketC.h

49 lines
935 B
C++

#pragma once
#include<iostream>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <thread>
#include <mutex>
#include <functional>
#include "md5.h"
#include "AES.h"
#pragma comment(lib,"ws2_32.lib")
using namespace std;
class SocketC
{
private:
struct MsgHead
{
int isStr = 0;
int bufLen = 0;
time_t tm = 0;
char token[34] = { 0 };
};
char tmpBuf[10240] = { 0 };
int bufLen = 0;
SOCKET sclient = 0;
BOOL state = 0;
std::thread rec;
std::mutex msgLock;
function<VOID(char*, int)> Rfunc = NULL;
function<VOID()> Cfunc = NULL;
bool initSocket();
VOID Receive();
char* RecvBuff(char* buf, int& len, int& isStr);
public:
SocketC(int bufSize = 4096, int modo = 0);
~SocketC();
bool Connect(string IP, UINT Prot);
int SendData(const char* Date, int len, int isStr = 0);
BOOL SendStr(string str);
VOID SetRecvFunc(function<VOID(char*, int)> fun);
VOID SetCloseFunc(function<VOID()> fun);
void Close();
};