-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.cpp
263 lines (235 loc) · 7.32 KB
/
server.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
#include <windows.h>
#include "text.h"
#include "operation.h"
#include "console.h"
#include "encryption.h"
#include "license.h"
#include "battleparamentry.h"
#include "itemraretable.h"
#include "player.h"
#include "map.h"
#include "client.h"
#include "listenthread.h"
#include "lobby.h"
#include "server.h"
// Starts a server with the given name, handler routine, and callback, listening on the given ports (terminated with 0)
// Example: SERVER* httpServer = StartServer("HTTP Server",HTTPHandlerRoutine,HTTPConnectCallback,80,81,0)
// This example starts an HTTP server on ports 80 and 81.
SERVER* StartServer(char* serverName,LPTHREAD_START_ROUTINE HandlerRoutine,LISTEN_THREAD_CALLBACK HandleConnection, ...)
{
int x,y,port;
va_list va;
SERVER* server = (SERVER*)malloc(sizeof(SERVER));
if (!server) return NULL;
memset(server,0,sizeof(SERVER));
operation_lock(server);
strcpy(server->name,serverName);
server->maxClients = 0xFFFFFFFF;
server->clientThread = HandlerRoutine;
va_start(va,HandleConnection);
for (x = 0; (port = va_arg(va,int)) && (x < 0x10); x++)
{
operation_lock(&server->threads[x]);
server->threads[x].addr = INADDR_ANY;
server->threads[x].port = port;
server->threads[x].callback = HandleConnection;
server->threads[x].param = (long)server;
operation_unlock(&server->threads[x]);
if (!BeginListenThread(&server->threads[x])) break;
}
va_end(va);
if (port)
{
for (y = 0; y < x; y++) EndListenThread(&server->threads[x],100);
free(server);
server = NULL;
} else operation_unlock(server);
return server;
}
// Stops a server, disconnects all clients, and frees anything associated with it.
bool StopServer(SERVER* s)
{
unsigned int x;
operation_lock(s);
for (x = 0; x < 0x10; x++) if (s->threads[x].thread) EndListenThread(&s->threads[x],100);
for (x = 0; x < s->numLobbies; x++) DeleteLobby(s->lobbies[x]);
free(s->lobbies);
for (x = 0; x < s->numClients; x++) DeleteClient(s->clients[x]);
free(s->clients);
free(s);
return true;
}
////////////////////////////////////////////////////////////////////////////////
// finds a client's index in the list of connected clients to the server.
// the server should be operation_locked before calling this function!
DWORD FindClient(SERVER* s,CLIENT* c)
{
if (!s || !c) return 0xFFFFFFFF;
unsigned int x;
operation_lock(s);
for (x = 0; x < s->numClients; x++) if (s->clients[x] == c) break;
if (x >= s->numClients) x = 0xFFFFFFFF;
operation_unlock(s);
return x;
}
// finds a client on the server given the serial number (Guild Card number)
CLIENT* FindClient(SERVER* s,DWORD serialNumber)
{
DWORD x;
CLIENT* c = NULL;
operation_lock(s);
for (x = 0; x < s->numClients; x++) if (s->clients[x]->license.serialNumber == serialNumber) c = s->clients[x];
operation_unlock(s);
return c;
}
// adds a client to a server
bool AddClient(SERVER* s,CLIENT* c)
{
if (!s || !c) return false;
operation_lock(s);
s->clients = (CLIENT**)realloc(s->clients,sizeof(CLIENT*) * (s->numClients + 1));
if (!s->clients)
{
operation_unlock(s);
return false;
}
s->clients[s->numClients] = c;
s->numClients++;
operation_unlock(s);
return true;
}
// removes a client from a server
bool RemoveClient(SERVER* s,CLIENT* c)
{
if (!s || !c) return false;
operation_lock(s);
unsigned int index = FindClient(s,c);
if ((index < 0) || (index >= s->numClients))
{
operation_unlock(s);
return false;
}
s->numClients--;
memcpy(&s->clients[index],&s->clients[index + 1],sizeof(CLIENT*) * (s->numClients - index));
s->clients = (CLIENT**)realloc(s->clients,sizeof(CLIENT*) * s->numClients);
operation_unlock(s);
if (!s->clients) return false;
return true;
}
// finds a lobby with room, and adds the client to that lobby
bool AddClientToAvailableLobby(SERVER* s,CLIENT* c)
{
unsigned int x,y;
operation_lock(s);
for (x = 0; x < s->numLobbies; x++)
{
if (!(s->lobbies[x]->flags & LOBBY_FLAG_PUBLIC)) continue;
if (AddClient(s->lobbies[x],c)) break;
}
y = s->numLobbies; // do this in case s->numLobbies changes right after we operation_unlock s
operation_unlock(s);
if (x < y) return true;
return false;
}
////////////////////////////////////////////////////////////////////////////////
// finds a lobby's index on the server
DWORD FindLobby(SERVER* s,LOBBY* l)
{
if (!s || !l) return 0xFFFFFFFF;
unsigned int x;
operation_lock(s);
for (x = 0; x < s->numLobbies; x++) if (s->lobbies[x] == l) break;
if (x >= s->numLobbies) x = 0xFFFFFFFF;
operation_unlock(s);
return x;
}
// finds a lobby based on lobby ID
LOBBY* FindLobby(SERVER* s,DWORD lobbyID)
{
unsigned int x;
LOBBY* ret = NULL;
operation_lock(s);
for (x = 0; x < s->numLobbies; x++) if (s->lobbies[x]->lobbyID == lobbyID) break;
if (x < s->numLobbies) ret = s->lobbies[x];
operation_unlock(s);
return ret;
}
// finds a lobby by its block number. not recommended, since lobbies can have the same block number.
LOBBY* FindLobbyByBlockNumber(SERVER* s,DWORD block)
{
unsigned int x;
LOBBY* ret = NULL;
operation_lock(s);
for (x = 0; x < s->numLobbies; x++)
{
if (s->lobbies[x]->flags & LOBBY_FLAG_GAME) continue;
if (s->lobbies[x]->block == block) break;
}
if (x < s->numLobbies) ret = s->lobbies[x];
operation_unlock(s);
return ret;
}
// finds a named lobby (that is, a game)
LOBBY* FindLobbyByName(SERVER* s,wchar_t* name,bool casesensitive)
{
unsigned int x;
LOBBY* ret = NULL;
operation_lock(s);
if (casesensitive)
{
for (x = 0; x < s->numLobbies; x++) if (!wcscmp(s->lobbies[x]->name,name)) break;
} else {
for (x = 0; x < s->numLobbies; x++) if (!wcsicmp(s->lobbies[x]->name,name)) break;
}
if (x < s->numLobbies) ret = s->lobbies[x];
operation_unlock(s);
return ret;
}
// adds a lobby to the server
bool AddLobby(SERVER* s,LOBBY* l)
{
if (!s || !l) return false;
operation_lock(s);
operation_lock(l);
do l->lobbyID = (rand() << 16) | rand();
while (FindLobby(s,l->lobbyID) && l->lobbyID);
operation_unlock(l);
s->lobbies = (LOBBY**)realloc(s->lobbies,sizeof(LOBBY*) * (s->numLobbies + 1));
if (!s->lobbies)
{
operation_unlock(s);
return false;
}
s->lobbies[s->numLobbies] = l;
s->numLobbies++;
operation_unlock(s);
return true;
}
// removes a lobby from the server
bool RemoveLobby(SERVER* s,LOBBY* l)
{
if (!s || !l) return false;
unsigned int x;
operation_lock(l);
for (x = 0; x < l->maxClients; x++) if (l->clients[x]) break;
if (x < l->maxClients)
{
operation_unlock(l);
return false;
}
operation_lock(s);
unsigned int index = FindLobby(s,l);
if ((index < 0) || (index >= s->numLobbies))
{
operation_unlock(s);
operation_unlock(l);
return false;
}
s->numLobbies--;
memcpy(&s->lobbies[index],&s->lobbies[index + 1],sizeof(LOBBY*) * (s->numLobbies - index));
s->lobbies = (LOBBY**)realloc(s->lobbies,sizeof(LOBBY*) * s->numLobbies);
operation_unlock(s);
operation_unlock(l);
if (!s->lobbies) return false;
return true;
}