CSTest/网页请求/SocketC.h

49 lines
1.3 KiB
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; //socket客户端
BOOL state = 0; //当前状态
int m_mode; //当前工作的模式
std::thread rec; //用来接数据的收线程
function<VOID(char*, int)> Rfunc = NULL; //接收到了数据的回调函数
function<VOID()> Cfunc = NULL; //断开连接的回调函数
bool initSocket(); //初始化socket
VOID Receive(); //接收数据函数
char* RecvBuff(char* buf, int& len, int& isStr); //处理收到的数据
BOOL CheckHead(MsgHead* h); //检查消息头
public:
SocketC(int mode = 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(); //断开连接
};