73 lines
1.4 KiB
C++
73 lines
1.4 KiB
C++
#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>
|
|
#include <functional>
|
|
#include <string>
|
|
|
|
#pragma comment(lib, "ws2_32.lib")
|
|
#pragma comment(lib, "Mswsock.lib")
|
|
|
|
using namespace std;
|
|
|
|
#pragma warning(disable:4996)
|
|
|
|
class SocketS
|
|
{
|
|
public:
|
|
private:
|
|
struct clienInfo
|
|
{
|
|
SOCKET c_Sock;
|
|
OVERLAPPED c_Olp;
|
|
int buflen = 0;
|
|
char buf[10240] = { 0 };
|
|
char c_strRecv[1024];
|
|
mutex msgLock; //消息锁
|
|
mutex sendLock; //消息锁
|
|
};
|
|
|
|
struct MsgHead
|
|
{
|
|
int MyIndex = 0;
|
|
int bufLen = 0;
|
|
time_t tm = 0;
|
|
char token[34] = { 0 };
|
|
};
|
|
|
|
BOOL g_flag = TRUE; //状态
|
|
HANDLE hPort = NULL; //完成端口句柄
|
|
HANDLE* pThread = NULL; //线程句柄
|
|
int g_count = 0; //当前客户端ID
|
|
map<int, clienInfo>ClienMap; //所有客户端
|
|
|
|
function<VOID(int,char*,int)> Rfunc = NULL;
|
|
function<VOID(int)> Cfunc = NULL;
|
|
|
|
static DWORD WINAPI ThreadProc(LPVOID lpParameter); //工作者线程
|
|
|
|
int PostAccept(void);
|
|
int PostRecv(int index);
|
|
int PostSend(int index, const char* buf, int len);
|
|
char* RecvBuff(int index, char* buf, int& len);
|
|
public:
|
|
SocketS();
|
|
BOOL Creat(int Prot);
|
|
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);
|
|
|
|
};
|
|
|