码蚁0817班级,AI项目,脸谱,双团队架构,赛马机制
mxlll
2024-12-04 e705b0c3fdcfa03750e76002c818752673248471
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#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;
};