#pragma once
|
#include <WinSock2.h>
|
#include <iostream>
|
#include <map>
|
#include <thread>
|
#include <mutex>
|
#include "ThreadPool.hpp"
|
#pragma comment(lib,"ws2_32.lib")
|
|
#define PORT 9527
|
#define HEART_CHECK_REQ 101
|
#define HEART_CHECK_RES 102
|
#define HEART_CHECK_TIMES 6
|
#define HEARTBEAT_INTERVAL 2
|
|
|
|
using namespace std;
|
|
struct Head
|
{
|
int type; // Çø·Ö²»Í¬¹¦ÄÜÀàÐÍ
|
int len; // ±íʾÕû¸ö·â°üµÄ³¤¶È£¬ÊÇΪÁ˺óÃæ´¦ÀíÕ³°üÎÊÌâµÄ
|
};
|
|
// ¼ä¸ô£º5Ã룬ãÐֵΪ6£¬µÝ¼õ´Ó6¿ªÊ¼£¬µ½0»òСÓÚ0 ¶Ï¿ª/ÖØÁ¬
|
// ÐÄÌøÇëÇó°ü
|
struct HeartCheckReq
|
{
|
int type;
|
int len;
|
HeartCheckReq()
|
{
|
type = 101;
|
len = sizeof(HeartCheckReq);
|
}
|
};
|
|
// ÐÄÌøÏìÓ¦°ü
|
struct HeartCheckRes
|
{
|
int type;
|
int len;
|
HeartCheckRes()
|
{
|
type = 102;
|
len = sizeof(HeartCheckRes);
|
}
|
};
|
|
class ServerSocket
|
{
|
public:
|
ServerSocket();
|
virtual ~ServerSocket();
|
|
bool initSocket();
|
void acceptThread();
|
void recvAndSendThread(SOCKET client);
|
void heartBeatThread(SOCKET client);
|
|
void mMapInsert(SOCKET client);
|
void mMapUpdate(SOCKET client);
|
void mMapDelete();
|
private:
|
SOCKET m_server;
|
map<SOCKET, int> m_clientMap;
|
mutex m_mutex;
|
};
|