-
Notifications
You must be signed in to change notification settings - Fork 2
/
HPPackServer.cpp
153 lines (136 loc) · 5.58 KB
/
HPPackServer.cpp
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "HPPackServer.h"
extern Utils::Logger *gLogger;
HPPackServer::ConnectionMapT HPPackServer::m_sConnections;
HPPackServer::ConnectionMapT HPPackServer::m_newConnections;
Utils::LockFreeQueue<Message::PackMessage> HPPackServer::m_PackMessageQueue(1 << 10);
HPPackServer::HPPackServer(const char *ip, unsigned int port)
{
m_ServerIP = ip;
m_ServerPort = port;
// 创建监听器对象
m_pListener = ::Create_HP_TcpPackServerListener();
// 创建 Socket 对象
m_pServer = ::Create_HP_TcpPackServer(m_pListener);
// 设置 Socket 监听器回调函数
::HP_Set_FN_Server_OnPrepareListen(m_pListener, OnPrepareListen);
::HP_Set_FN_Server_OnAccept(m_pListener, OnAccept);
::HP_Set_FN_Server_OnSend(m_pListener, OnSend);
::HP_Set_FN_Server_OnReceive(m_pListener, OnReceive);
::HP_Set_FN_Server_OnClose(m_pListener, OnClose);
::HP_Set_FN_Server_OnShutdown(m_pListener, OnShutdown);
// 设置包头标识与最大包长限制
::HP_TcpPackServer_SetMaxPackSize(m_pServer, 0xFFFF);
::HP_TcpPackServer_SetPackHeaderFlag(m_pServer, 0x169);
::HP_TcpServer_SetKeepAliveTime(m_pServer, 30*1000);
}
HPPackServer::~HPPackServer()
{
// 销毁 Socket 对象
::Destroy_HP_TcpPackServer(m_pServer);
// 销毁监听器对象
::Destroy_HP_TcpPackServerListener(m_pListener);
}
void HPPackServer::Start()
{
char errorString[512] = {0};
if (::HP_Server_Start(m_pServer, m_ServerIP.c_str(), m_ServerPort))
{
sprintf(errorString, "HPPackServer::Start listen to %s:%d successed", m_ServerIP.c_str(), m_ServerPort);
Utils::gLogger->Log->info(errorString);
}
else
{
sprintf(errorString, "HPPackServer::Start listen to %s:%d failed, error code:%d error massage:%s",
m_ServerIP.c_str(), m_ServerPort, ::HP_Client_GetLastError(m_pServer), HP_Client_GetLastErrorDesc(m_pServer));
Utils::gLogger->Log->warn(errorString);
}
}
void HPPackServer::Stop()
{
//停止服务器
::HP_Server_Stop(m_pServer);
}
void HPPackServer::SendData(HP_CONNID dwConnID, const unsigned char *pBuffer, int iLength)
{
bool ret = ::HP_Server_Send(m_pServer, dwConnID, pBuffer, iLength);
if(!ret)
{
char errorString[512] = {0};
Utils::gLogger->Log->warn("HPPackServer::SendData failed, sys error:{}, error code:{}, error message:{}",
SYS_GetLastErrorStr(), HP_Client_GetLastError(m_pServer), HP_Client_GetLastErrorDesc(m_pServer));
sprintf(errorString, "HPPackServer::SendData failed, sys error:%s, error code:%d, error message:%s",
SYS_GetLastErrorStr(), HP_Client_GetLastError(m_pServer), HP_Client_GetLastErrorDesc(m_pServer));
Utils::gLogger->Log->warn(errorString);
}
}
En_HP_HandleResult __stdcall HPPackServer::OnPrepareListen(HP_Server pSender, UINT_PTR soListen)
{
TCHAR szAddress[50];
int iAddressLen = sizeof(szAddress) / sizeof(TCHAR);
USHORT usPort;
::HP_Server_GetListenAddress(pSender, szAddress, &iAddressLen, &usPort);
return HR_OK;
}
En_HP_HandleResult __stdcall HPPackServer::OnAccept(HP_Server pSender, HP_CONNID dwConnID, UINT_PTR soClient)
{
TCHAR szAddress[50];
int iAddressLen = sizeof(szAddress) / sizeof(TCHAR);
USHORT usPort;
::HP_Server_GetRemoteAddress(pSender, dwConnID, szAddress, &iAddressLen, &usPort);
Connection connection;
connection.dwConnID = dwConnID;
connection.pSender = pSender;
auto it = m_sConnections.find(dwConnID);
if (m_sConnections.end() == it)
{
m_sConnections.insert(std::pair<HP_CONNID, Connection>(dwConnID, connection));
m_newConnections.insert(std::pair<HP_CONNID, Connection>(dwConnID, connection));
}
char errorString[512] = {0};
sprintf(errorString, "HPPackServer::OnAccept accept an new connection dwConnID:%d from %s:%d", dwConnID, szAddress, usPort);
Utils::gLogger->Log->info(errorString);
return HR_OK;
}
En_HP_HandleResult __stdcall HPPackServer::OnSend(HP_Server pSender, HP_CONNID dwConnID, const BYTE *pData, int iLength)
{
return HR_OK;
}
En_HP_HandleResult __stdcall HPPackServer::OnReceive(HP_Server pSender, HP_CONNID dwConnID, const BYTE *pData, int iLength)
{
TCHAR szAddress[50];
int iAddressLen = sizeof(szAddress) / sizeof(TCHAR);
USHORT usPort;
::HP_Server_GetRemoteAddress(pSender, dwConnID, szAddress, &iAddressLen, &usPort);
Message::PackMessage message;
memcpy(&message, pData, iLength);
char messageType[32] = {0};
sprintf(messageType, "0X%X", message.MessageType);
Utils::gLogger->Log->info("HPPackServer::OnReceive receive PackMessage, MessageType:{}", messageType);
while(!m_PackMessageQueue.Push(message));
return HR_OK;
}
En_HP_HandleResult __stdcall HPPackServer::OnClose(HP_Server pSender, HP_CONNID dwConnID, En_HP_SocketOperation enOperation, int iErrorCode)
{
TCHAR szAddress[50];
int iAddressLen = sizeof(szAddress) / sizeof(TCHAR);
USHORT usPort;
::HP_Server_GetRemoteAddress(pSender, dwConnID, szAddress, &iAddressLen, &usPort);
auto it = m_sConnections.find(dwConnID);
if (it != m_sConnections.end())
{
m_sConnections.erase(dwConnID);
}
auto it1 = m_newConnections.find(dwConnID);
if (it1 != m_newConnections.end())
{
m_newConnections.erase(dwConnID);
}
char errorString[512] = {0};
sprintf(errorString, "HPPackServer::OnClose have an connection dwConnID:%d from %s:%d closed", dwConnID, szAddress, usPort);
Utils::gLogger->Log->warn(errorString);
return HR_OK;
}
En_HP_HandleResult __stdcall HPPackServer::OnShutdown(HP_Server pSender)
{
return HR_OK;
}