CSTest/网络服务端/SocketS.h

73 lines
1.4 KiB
C
Raw Normal View History

2024-10-17 15:56:43 +08:00
#pragma once
#define WIN32_LEAN_AND_MEAN
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <WinSock2.h>
#include <mswsock.h>
#include <map>
#include <string>
#include <list>
#include <mutex>
2024-10-23 23:58:57 +08:00
#include <functional>
#include <string>
2024-10-17 15:56:43 +08:00
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "Mswsock.lib")
2024-10-23 23:58:57 +08:00
using namespace std;
#pragma warning(disable:4996)
2024-10-17 15:56:43 +08:00
class SocketS
{
public:
private:
struct clienInfo
{
SOCKET c_Sock;
OVERLAPPED c_Olp;
2024-10-23 23:58:57 +08:00
int buflen = 0;
char buf[10240] = { 0 };
2024-10-17 15:56:43 +08:00
char c_strRecv[1024];
2024-10-23 23:58:57 +08:00
mutex msgLock; //<2F><>Ϣ<EFBFBD><CFA2>
mutex sendLock; //<2F><>Ϣ<EFBFBD><CFA2>
};
struct MsgHead
{
int MyIndex = 0;
int bufLen = 0;
time_t tm = 0;
char token[34] = { 0 };
2024-10-17 15:56:43 +08:00
};
BOOL g_flag = TRUE; //״̬
HANDLE hPort = NULL; //<2F><><EFBFBD>ɶ˿ھ<CBBF><DABE><EFBFBD>
HANDLE* pThread = NULL; //<2F>߳̾<DFB3><CCBE><EFBFBD>
int g_count = 0; //<2F><>ǰ<EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD>ID
2024-10-23 23:58:57 +08:00
map<int, clienInfo>ClienMap; //<2F><><EFBFBD>пͻ<D0BF><CDBB><EFBFBD>
function<VOID(int,char*,int)> Rfunc = NULL;
function<VOID(int)> Cfunc = NULL;
2024-10-17 15:56:43 +08:00
static DWORD WINAPI ThreadProc(LPVOID lpParameter); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>
int PostAccept(void);
int PostRecv(int index);
int PostSend(int index, const char* buf, int len);
2024-10-23 23:58:57 +08:00
char* RecvBuff(int index, char* buf, int& len);
public:
SocketS();
2024-10-17 15:56:43 +08:00
BOOL Creat(int Prot);
2024-10-23 23:58:57 +08:00
void Close();
VOID SetRecvFunc(function<VOID(int, char*, int)> fun);
VOID SetCloseFunc(function<VOID(int)> fun);
VOID GetClienName(int index, string& IP, int& Prot);
int Send(int index, const char* buf, int len);
VOID CloseClien(int index);
2024-10-17 15:56:43 +08:00
};