-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.c
431 lines (375 loc) · 13.7 KB
/
server.c
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#include <netdb.h>
#include <netinet/in.h>
#include <pthread.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>
#include "beast-manager.h"
#include "config.h"
#include "game/entity.h"
#include "game/world.h"
#include "util/api.h"
#include "util/image.h"
#include "util/socket.h"
#define ERROR(msg) \
{ \
perror(msg); \
exit(EXIT_FAILURE); \
}
World world;
int tick = 0;
bool server_active = true;
pthread_t keyboard_thread, game_thread, waiter_thread;
int server_socket;
int beast_handler_pid;
// Changes state of `server_active` and stops any running threads/processes
void endGame() {
pthread_cancel(waiter_thread);
kill(beast_handler_pid, SIGUSR2);
for (int i = 0; i < MAX_PLAYERS; i++) {
if (world.players[i] == NULL) {
continue;
}
pthread_cancel(world.players[i]->thread);
}
server_active = false;
}
// Runs in keyboard_thread
void keyboardHandler() {
struct termios old_term_settings, new_term_settings;
tcgetattr(STDIN_FILENO, &old_term_settings);
new_term_settings = old_term_settings;
// Disable terminal canonical mode
// This sends stdin immediately without buffering
new_term_settings.c_lflag &= (~ICANON & ~ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &new_term_settings);
while (server_active) {
unsigned char key = getchar();
pthread_t a;
switch (key) {
case 'Q':
case 'q':
endGame();
break;
case 'c':
addCollectibleRandomly(&world, COIN);
break;
case 't':
addCollectibleRandomly(&world, TREASURE);
break;
case 'T':
addCollectibleRandomly(&world, LARGE_TREASURE);
break;
case 'B':
case 'b':
// The SIGUSR1 signal is handled as "add a new beast" in the beast
// manager process
kill(beast_handler_pid, SIGUSR1);
break;
default:
putchar(key);
break;
}
usleep(1000);
}
tcsetattr(STDIN_FILENO, TCSANOW, &old_term_settings);
pthread_exit(NULL);
}
// Runs in game_thread
void gameLoop() {
while (server_active) {
// Logic
for (int i = 0; i < MAX_PLAYERS + MAX_BEASTS; i++) {
// Combined for players and beasts so we don't have to write this code twice
Player* entity;
if (i < MAX_PLAYERS) {
entity = world.players[i];
} else {
entity = world.beasts[i - MAX_PLAYERS];
}
if (entity == NULL) {
continue;
}
if (entity->bush_timer != 0) {
entity->bush_timer++;
}
if (entity->queued_action == NULL) {
continue;
}
movePlayer(world, entity, entity->queued_action->direction);
// Player was moved to their desired location, so remove the action from the
// queue
entity->queued_action = NULL;
}
// Render
system("clear");
printWorld(world);
printf(
"\nTick: %d \tWorld size: %dx%d\nPlayers: %d/%d\tBeasts: %d/%d\n\n",
tick,
world.size_y,
world.size_x,
countPlayers(world),
MAX_PLAYERS,
countBeasts(world),
MAX_BEASTS
);
printPlayerDetails(world);
// Post render
tick++;
sleep(1);
}
}
// Packs the map, players and beasts into a seriazible format and sends to client
long sendMapData(int sock, Player* player) {
char data[MAP_DATA_SIZE(world)];
// Serialize tiles
for (int y = 0; y < world.size_y; y++) {
for (int x = 0; x < world.size_x; x++) {
int index = world.size_x * y + x;
if (inVisionRange(player->pos_y, player->pos_x, y, x)) {
data[index] = (char)world.tiles[y][x].id;
} else {
data[index] = (char)0;
}
}
}
// Player position
int position_offset = world.size_y * world.size_x;
data[position_offset] = (char)player->pos_y;
data[position_offset + 1] = (char)player->pos_x;
// Serialize players
int players_offset = position_offset + 2;
for (int i = 0; i < MAX_PLAYERS; i++) {
int index = players_offset + i * 4 + i * ((int)sizeof(int) * 2);
if (world.players[i] == NULL || !inVisionRange(
player->pos_y,
player->pos_x,
world.players[i]->pos_y,
world.players[i]->pos_x
)) {
// Send -1/-1 y/x to indicate that this data is invalid and the requester
// can't see this player
data[index] = (char)-1;
data[index + 1] = (char)-1;
continue;
}
data[index] = (char)world.players[i]->pos_y;
data[index + 1] = (char)world.players[i]->pos_x;
data[index + 2] = (char)world.players[i]->type;
if (player == world.players[i]) {
// This data is only available to the player which requests it
// carrying, budget and deaths are unknown to other players
data[index + 3] = (char)world.players[i]->deaths;
// Memcpy nicely copies raw bytes of an int to a char
// Which is necessary here, as we often handle values larger than 128
memcpy(&data[index + 4], &world.players[i]->carrying, sizeof(int));
memcpy(
&data[index + 4 + sizeof(int)], &world.players[i]->budget, sizeof(int)
);
} else {
// -1 are later printed as '?' to let players know they don't have access to
// this info
int data_unavail = -1;
data[index + 3] = (char)data_unavail;
memcpy(&data[index + 4], &data_unavail, sizeof(int));
memcpy(&data[index + 4 + sizeof(int)], &data_unavail, sizeof(int));
}
}
// Serialize beasts
int beasts_offset = players_offset + MAX_PLAYERS * (4 + ((int)sizeof(int)) * 2);
for (int i = 0; i < MAX_BEASTS; i++) {
int index = beasts_offset + i * 2;
if (world.beasts[i] == NULL || !inVisionRange(
player->pos_y,
player->pos_x,
world.beasts[i]->pos_y,
world.beasts[i]->pos_x
)) {
data[index] = (char)-1;
data[index + 1] = (char)-1;
continue;
} else {
data[index] = (char)world.beasts[i]->pos_y;
data[index + 1] = (char)world.beasts[i]->pos_x;
}
}
return send(sock, data, sizeof(data), 0);
}
// Runs in waiter_thread
void waitForClientConnection() {
struct sockaddr_in client;
socklen_t client_len = sizeof(client);
// Accept new connection
int client_socket = accept(server_socket, (struct sockaddr*)&client, &client_len);
if (client_socket == -1) {
pthread_exit(NULL);
}
// Get client connection info
char hoststr[NI_MAXHOST];
char portstr[NI_MAXSERV];
getnameinfo(
(struct sockaddr*)&client,
client_len,
hoststr,
sizeof(hoststr),
portstr,
sizeof(portstr),
NI_NUMERICHOST | NI_NUMERICSERV
);
int port = (int)strtol(portstr, NULL, 10);
ClientInfo client_info = {
.socket = client_socket,
.port = port,
};
pthread_exit(&client_info);
}
// Runs in player->thread, processes client commands
void handleClientConnection(ClientInfo* client_info) {
char buffer[1024] = { 0 };
bool connected = true;
Player* player;
while (connected) {
// Wait for a message, the client always knocks first and only then we
// sendResponse
long bytes_received = recv(client_info->socket, buffer, sizeof(buffer), 0);
if (bytes_received <= 0) {
// The transport was dropped for some reason (client was killed or
// connection interrupted) In this case, we remove the player if they had
// already joined and terminate the handler
if (player != NULL) {
removePlayer(&world, player);
}
pthread_exit(NULL);
}
// Read command from the first byte and argument from the second byte of the
// client's message
Command command = (Command)buffer[0];
int argument = (int)buffer[1];
memset(&buffer, 0, sizeof(buffer));
switch ((Command)command) {
case JOIN: {
switch ((PlayerType)argument) {
case HUMAN:
case CPU:
if (countPlayers(world) >= MAX_PLAYERS) {
// Server is full, sendResponse with SERVER_FULL and close
// the connection
sendResponse(client_info->socket, SERVER_FULL);
connected = false;
} else {
int y, x;
randomFreeCoordinates(world, &y, &x);
// Arguments for JOIN are always of type PlayerType
player = addPlayer(&world, y, x, (PlayerType)argument);
player->port = client_info->port;
player->thread = client_info->thread;
sendResponse(client_info->socket, JOINED);
}
break;
case BEAST:
if (countBeasts(world) >= MAX_BEASTS) {
sendResponse(client_info->socket, SERVER_FULL);
connected = false;
} else {
int y, x;
randomFreeCoordinates(world, &y, &x);
player = addBeast(&world, y, x);
player->port = client_info->port;
player->thread = client_info->thread;
sendResponse(client_info->socket, JOINED);
}
break;
}
break;
}
case LEAVE:
if (player->type == BEAST) {
removeBeast(&world, player);
} else {
removePlayer(&world, player);
}
sendResponse(client_info->socket, REMOVED);
connected = false;
break;
case MOVE:
player->queued_action = &(Action){ .direction = (Direction)argument };
sendResponse(client_info->socket, MOVED);
break;
case WORLD_SIZE: {
// Sends just char[2] - no Response here
// Subtract 1, so we can send the max size of the world without adding
// extra bytes
char size_data[2] = { (char)(world.size_y - 1),
(char)(world.size_x - 1) };
send(client_info->socket, size_data, sizeof(size_data), 0);
break;
}
case MAP:
// Sends just map data - no Response here
sendMapData(client_info->socket, player);
break;
default:
break;
}
}
}
int main() {
beast_handler_pid = fork();
if (beast_handler_pid == 0) {
beastManager();
exit(EXIT_SUCCESS);
}
server_socket = getServerSocket(PORT);
if (server_socket == -1) {
ERROR("Error opening socket")
} else if (server_socket == -2) {
ERROR("Error binding to socket")
}
Image map_image = loadPPM(MAP_IMAGE);
world = emptyWorld(map_image.height, map_image.width);
populateWorld(&world, map_image);
free(map_image.pixels);
// Add some random collectibles around the map
srand(time(NULL) * 100);
for (int i = 0; i < 50; i++) {
addCollectibleRandomly(&world, (TileID)rand() % 3 + 3);
}
// Game loop
pthread_create(&game_thread, NULL, (void*)gameLoop, NULL);
// Keyboard handler
pthread_create(&keyboard_thread, NULL, (void*)keyboardHandler, NULL);
// This loop waits for a new client connection, then:
// - Rejects it if the server is full (countPlayers(world) >= MAX_PLAYERS)
// - Accepts it and starts a new client handler thread2
while (server_active) {
// Create a waiter thread and wait for new client connections
ClientInfo* client_info;
pthread_create(&waiter_thread, NULL, (void*)waitForClientConnection, NULL);
pthread_join(waiter_thread, (void**)&client_info);
if (client_info == NULL) {
printf("Connection on port %d failed\n", client_info->port);
continue;
}
// Double check if the server is still server_active
// This prevents a SEGFAULT during server shutdown
if (server_active) {
pthread_create(
&client_info->thread, NULL, (void*)handleClientConnection, client_info
);
}
}
// Wait until beast handler exits
wait(NULL);
// Clean up
close(server_socket);
printf("Server exit\n");
return 0;
}