/
hsserver.cpp
311 lines (261 loc) · 7.66 KB
/
hsserver.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// -----------------------------------------------------------------------
// $Id: hsserver.cpp,v 1.10 2006/04/04 12:41:11 mark Exp $
// -----------------------------------------------------------------------
#ifdef WIN32
#pragma warning(disable:4786)
#endif
// check for the odd location for select() under cygwin
#if defined(__CYGWIN32__) || defined(__MINGW32__)
#include <sys/select.h>
#endif
#if defined(__FreeBSD__)
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#endif
#ifndef WIN32
#include <time.h>
#include <sys/timeb.h>
#include <pthread.h>
#else
#include <Windows.h>
#endif
#include "hstypes.h"
#include "HSNetwork.h"
#include "HSPacketHandler.h"
#include "HSPackets.h"
#include "hsutils.h"
#include "HSHandlerLogin.h"
#include "HSHandlerClass.h"
#include "HSHandlerMisc.h"
#include "HSHandlerUniverse.h"
#include "HSHandlerObject.h"
#include "HSHandlerWeapon.h"
#include "hsconf.h"
#include "hsserver.h"
HS_BOOL8 g_bHSThreadShutdown = false;
#define CREATE_HANDLER_OBJECT(CLASS_NAME) \
CLASS_NAME* p##CLASS_NAME = new CLASS_NAME; \
if (!p##CLASS_NAME->Initialize()) \
{\
delete p##CLASS_NAME; \
return false;\
}\
else \
{\
m_listPacketHandlers.push_back(p##CLASS_NAME);\
}
CHSServer myCHSServer;
#ifdef WIN32
unsigned long __stdcall
#else
void *
#endif
HSServerThread(void *pvParam)
{
#ifndef WIN32
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 20000;
#endif
// Loop every 20 ms.
while (1)
{
hsEnterMutex();
if (g_bHSThreadShutdown)
{
hsLeaveMutex();
#ifdef WIN32
ExitThread(0);
#else
pthread_exit(NULL);
#endif
}
else
{
CHSServer::GetInstance().DoThreadedCycle();
hsLeaveMutex();
#ifdef WIN32
SleepEx(20, true);
#else
select(0, NULL, NULL, NULL, &tv);
tv.tv_sec = 0;
tv.tv_usec = 20000;
#endif
}
}
}
HS_BOOL8 CHSServerDef::StartServer(HS_UINT16 usPort)
{
// Tell the CHSNetwork singleton to listen.
if (HSNetwork.Listen(usPort) == true)
{
// We're listening, so register all packet handlers and initializers.
CREATE_HANDLER_OBJECT(CHSHandlerLogin)
CREATE_HANDLER_OBJECT(CHSHandlerClass)
CREATE_HANDLER_OBJECT(CHSHandlerMisc)
CREATE_HANDLER_OBJECT(CHSHandlerUniverse)
CREATE_HANDLER_OBJECT(CHSHandlerObject)
CREATE_HANDLER_OBJECT(CHSHandlerWeapon)
// Is the admin server threaded?
if (HSCONF.admin_server_threaded)
{
#ifdef WIN32
DWORD dwThreadID;
m_tWorkerThread =
CreateThread(NULL, 0, &HSServerThread, NULL, 0, &dwThreadID);
if (m_tWorkerThread != NULL)
{
hs_log("ADMIN SERVER: Created in a separate thread.");
return true;
}
#else
if (pthread_create(&m_tWorkerThread, NULL, &HSServerThread, NULL)
== 0)
{
hs_log("ADMIN SERVER: Created in a separate thread.");
return true;
}
#endif
return false;
}
hs_log("ADMIN SERVER: Created in main process thread.");
return true;
}
return false;
}
void CHSServerDef::ShutdownServer()
{
if (HSCONF.admin_server_threaded)
{
hsEnterMutex();
g_bHSThreadShutdown = true;
HSNetwork.Shutdown();
#ifdef WIN32
// Harsh, but seemingly necessary.
TerminateThread(m_tWorkerThread, 0);
#endif
hsLeaveMutex();
}
}
void CHSServerDef::DoCycle()
{
// If the admin server is not threaded, run the thread cycle too.
if (!HSCONF.admin_server_threaded)
{
DoThreadedCycle();
}
// Process packets that need handled in the main thread.
while (!m_queThreadUnsafePackets.empty())
{
CHSPacket *pPacket = m_queThreadUnsafePackets.front();
m_queThreadUnsafePackets.pop_front();
HandlePacket(pPacket);
delete pPacket;
}
}
void CHSServerDef::DoThreadedCycle()
{
// Process the network.
HSNetwork.Process();
// See if any inbound packets are waiting to be processed.
CHSPacket *pPacket = HSNetwork.GetPendingPacket();
while (pPacket)
{
// If this is not a login packet, the source address needs to already
// be validated.
if (pPacket->GetPacketType() != PT_LOGIN)
{
// Check to see if this source address is validated.
CSTLValidatedPlayerSet::iterator iter =
m_setValidatedPlayers.find(pPacket->GetPacketAddress());
if (iter == m_setValidatedPlayers.end())
{
// This is someone trying to send a packet when they are not yet logged in.
HSNetwork.CloseConnection(pPacket->GetPacketAddress());
delete pPacket;
pPacket = HSNetwork.GetPendingPacket();
continue;
}
else
{
// If this is a closed connection, remove the player from the validated set.
if (pPacket->GetPacketType() == PT_CONNECTION_CLOSED)
{
m_setValidatedPlayers.erase(iter);
delete pPacket;
pPacket = HSNetwork.GetPendingPacket();
continue;
}
}
}
// The packet has passed validation.
// Is the handler for this packet ok to run in the worker thread?
if (!IsHandlerThreadSafe(pPacket->GetPacketType()))
{
// Put this packet on the main thread queue.
m_queThreadUnsafePackets.push_back(pPacket);
}
else
{
// Find a handler for it.
HandlePacket(pPacket);
delete pPacket;
}
pPacket = HSNetwork.GetPendingPacket();
}
}
void CHSServerDef::HandlePacket(CHSPacket * pPacket)
{
CSTLPacketHandlerMap::iterator iter =
m_mapPacketHandlers.find(pPacket->GetPacketType());
if (iter == m_mapPacketHandlers.end())
{
hs_log("Failed to find a packet handler for an inbound packet.");
}
else
{
THSPacketHandlerEntry & rtEntry = iter->second;
CHSPacketHandler *pHandler = rtEntry.pHandler;
((pHandler)->*rtEntry.pFunc) (pPacket);
}
}
void CHSServerDef::AddValidatedPlayer(unsigned int iContext)
{
m_setValidatedPlayers.insert(iContext);
}
HS_BOOL8 CHSServerDef::RegisterPacketHandler(EHSPacketType eType,
CHSPacketHandler * pHandler,
PACKET_HANDLER_FUNC pFunc,
HS_BOOL8 bThreadSafe)
{
if (!pFunc)
{
hs_log("NULL handler sent to RegisterPacketHandler().");
return false;
}
CSTLPacketHandlerMap::iterator iter;
if ((iter = m_mapPacketHandlers.find(eType)) != m_mapPacketHandlers.end())
{
// There's already a handler registered for this type.
hs_log("Handler already registered for specified packet type.");
return false;
}
THSPacketHandlerEntry tEntry;
tEntry.pFunc = pFunc;
tEntry.pHandler = pHandler;
tEntry.bThreadSafe = bThreadSafe;
m_mapPacketHandlers[eType] = tEntry;
return true; // Good to go.
}
HS_BOOL8 CHSServerDef::IsHandlerThreadSafe(EHSPacketType eType)
{
CSTLPacketHandlerMap::iterator iter;
if ((iter = m_mapPacketHandlers.find(eType)) != m_mapPacketHandlers.end())
{
// Check to see if this handler is thread safe.
THSPacketHandlerEntry & rtEntry = iter->second;
return rtEntry.bThreadSafe;
}
return true;
}