forked from ylmbtm/GameProject3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnection.h
132 lines (79 loc) · 2.8 KB
/
Connection.h
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#ifndef _CONNECTION_H_
#define _CONNECTION_H_
#include "IBufferHandler.h"
#include "ReaderWriterQueue.h"
#define RECV_BUF_SIZE 8192
class CConnection
{
public:
CConnection();
virtual ~CConnection();
public:
BOOL HandleRecvEvent(INT32 nBytes);
UINT32 GetConnectionID();
UINT64 GetConnectionData();
VOID SetConnectionID(INT32 nConnID);
VOID SetConnectionData(UINT64 uData);
BOOL Close();
BOOL SetSocket(SOCKET hSocket);
uv_tcp_t* GetSocket();
BOOL SetDataHandler(IDataHandler* pHandler);
BOOL ExtractBuffer();
BOOL DoReceive();
BOOL IsConnectionOK();
BOOL SetConnectionOK(BOOL bOk);
BOOL Reset();
BOOL SendBuffer(IDataBuffer* pBuff);
BOOL DoSend();
void HandReaddata(size_t len);
void HandWritedata(size_t len);
BOOL CheckHeader(CHAR* m_pPacket);
UINT32 GetIpAddr(BOOL bHost = TRUE);
VOID EnableCheck(BOOL bCheck);
public:
uv_tcp_t m_hSocket;
uv_connect_t m_ConnectReq;
uv_write_t m_WriteReq;
uv_shutdown_t m_ShutdownReq;
uv_async_t m_AsyncReq;
BOOL m_bConnected;
INT32 m_nConnID;
UINT64 m_uConnData;
IDataHandler* m_pDataHandler;
UINT32 m_dwIpAddr;
INT32 m_nDataLen;
CHAR m_pRecvBuf[RECV_BUF_SIZE];
CHAR* m_pBufPos;
IDataBuffer* m_pCurRecvBuffer;
INT32 m_nCurBufferSize;
INT32 m_nCheckNo;
volatile BOOL m_IsSending;
CConnection* m_pNext;
UINT64 m_uLastRecvTick;
moodycamel::ReaderWriterQueue< IDataBuffer*> m_SendBuffList;
IDataBuffer* m_pSendingBuffer;
};
class CConnectionMgr
{
private:
CConnectionMgr();
~CConnectionMgr();
public:
static CConnectionMgr* GetInstancePtr();
public:
BOOL InitConnectionList(INT32 nMaxCons);
CConnection* CreateConnection();
BOOL DeleteConnection(CConnection* pConnection);
BOOL DeleteConnection(INT32 nConnID);
CConnection* GetConnectionByID(INT32 nConnID);
///////////////////////////////////////////
BOOL CloseAllConnection();
BOOL DestroyAllConnection();
BOOL CheckConntionAvalible(INT32 nInterval);
public:
CConnection* m_pFreeConnRoot;
CConnection* m_pFreeConnTail;
std::vector<CConnection*> m_vtConnList; //连接列表
std::mutex m_ConnListMutex;
};
#endif