-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
445 lines (339 loc) · 11 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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#include <gtk/gtk.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/select.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <chipmunk/chipmunk.h>
#include <string.h>
#include <stdbool.h>
#include <assert.h>
#include "specs/common.h"
#include "specs/physics.h"
#include "specs/networking.h"
#include "specs/protocols.h"
#define NEW_PLAYER 30001 // Port used to listen for new players
#define TIMESTEP 1.0/60.0
#define EXTRA_TIME 10000
/*
server.c
the central file for the server. Has the main function. Contains a world_struct
and a chipmunk space; it will accept new body messages and level change messages
from a client, update accordingly its world_struct and chipmunk space accordingly,
all while updating all clients of updates to body positions.
*/
/*
broadcast_info struct
contains all the necessary info of a current game status
*/
typedef struct {
bool skip_sender_fd;
int sender_fd;
char *message;
fd_set *master_readfds;
int nbytes;
int new_player_listener;
int fdmax;
int game_started;
int num_players;
int try_number;
int level;
world_status *world;
} broadcast_info;
//function prototypes
static broadcast_info *new_broadcast_info(fd_set *master_readfds, int new_player_listener, int fdmax);
static void server_select(struct timeval *tv, broadcast_info *info);
static void server_broadcast_message(broadcast_info *info);
static void server_world_step(broadcast_info *info);
static void server_add_body(broadcast_info *info);
static void server_send_initial_bodies(cpBody *body, broadcast_info *info);
static void free_message(broadcast_info *info);
static void server_send_world_info(broadcast_info *info);
static void server_switch_level(broadcast_info *info, int level, bool win);
/*
initializes a new broadcast_info
parameters: file descriptor, player listener, max file descriptor
returns: pointer to the broadcast_info struct
*/
static broadcast_info *
new_broadcast_info(fd_set *master_readfds, int new_player_listener, int fdmax) {
broadcast_info *info = (broadcast_info *) malloc(sizeof(broadcast_info));
assert(info);
info -> skip_sender_fd = false;
info -> message = NULL;
info -> master_readfds = master_readfds;
info -> nbytes = 0;
info -> game_started = false;
info -> new_player_listener = new_player_listener;
info -> fdmax = fdmax;
info -> num_players = 0;
info -> try_number = 0;
info -> sender_fd = 0;
info -> level = 1;
info -> world = NULL;
return info;
}
/*
checks and processes messages from clients
parameters: timeval struct, broadcast info structs
returns: nothing
*/
static void
server_select(struct timeval *tv, broadcast_info *info) {
fd_set readfds;
FD_ZERO(&readfds);
readfds = *(info -> master_readfds);
char buf[MAXLINE] = {0};
// Look for sockets ready to be read
if (select(info -> fdmax + 1, &readfds, NULL, NULL, tv) == -1) {
perror("select");
exit(4);
}
// Check through the file descriptors for data
for (int i = 0; i <= info -> fdmax; i++) {
if (FD_ISSET(i, &readfds)) {
// Check if a new player is going to be added.
if (i == info -> new_player_listener) {
// If so, accept the player's connection
int new_player_fd = server_accept_connection(info -> new_player_listener);
info -> game_started = true;
if(new_player_fd != -1) {
// Update fdmax if necessary
if(new_player_fd > info -> fdmax)
info -> fdmax = new_player_fd;
// Add fd to the set of fds to check from next loop on
FD_SET(new_player_fd, info -> master_readfds);
info -> num_players++;
server_send_world_info(info);
}
// Problem in accepting player's connection
else
perror("accept");
}
// There is either a chat or a shape to process.
else {
info -> nbytes = recvall(buf, i, MAXLINE);
if(info -> nbytes != MAXLINE + 2) {
// Message from client to add new body
if(buf[ID_INDEX] == NEW_BODY_MESSAGE + '0') {
free_message(info);
info -> message = buf;
info -> skip_sender_fd = false;
server_add_body(info);
info -> try_number++;
}
// Message from client that is a chat to be sent out to other players
else if(buf[ID_INDEX] == CHAT_MESSAGE + '0') {
free_message(info);
info -> skip_sender_fd = true;
info -> sender_fd = i;
info -> message = buf;
server_broadcast_message(info);
usleep(EXTRA_TIME);
}
// Message from client to change the level
else if(buf[ID_INDEX] == LEVEL_MESSAGE + '0') {
free_message(info);
info -> message = buf;
info -> skip_sender_fd = false;
level_info *level_switch_info = protocol_decode_level(buf);
server_switch_level(info, level_switch_info -> level, false);
}
}
// Errors or client dropped
else {
// Connection closed
if (info -> nbytes == 0) {
printf("selectserver: socket %d hung up\n", i);
}
else
perror("recv");
info -> num_players--;
FD_CLR(i, info -> master_readfds); // remove from master set
close(i); // bye!
}
}
}
}
}
/*
broadcasts the broadcast info
parameters: broadcast info
returns: nothing
*/
static void
server_broadcast_message(broadcast_info *info) {
for(int j = 0; j <= info -> fdmax + 1; j++) {
// Loop through file descriptors to send info to each one
if (FD_ISSET(j, info -> master_readfds)) {
// Except the new player listener and the sender if a chat
if (j != info -> new_player_listener && !(info -> skip_sender_fd && info -> sender_fd == j)) {
if (sendall(j, info -> message, MAXLINE) == -1)
perror("send");
}
}
}
}
/*
calls physics functions to timestep the world
parameters: broadcast_info struct pointer
returns: nothing
*/
static void
server_world_step(broadcast_info *info) {
world_update(info -> world);
info -> message = protocol_send_coords(info -> world -> space);
info -> skip_sender_fd = false;
server_broadcast_message(info);
//if a new level is desired, switches level
if (info -> world->status == 1) {
usleep(EXTRA_TIME*2);
server_switch_level(info, info->level, true);
usleep(EXTRA_TIME*2);
}
}
/*
adds the body received from client
parameters: broadcast_info struct pointer
returns: nothing
*/
static void
server_add_body(broadcast_info *info) {
//if the player is out of tries, restart the level
if(info -> try_number >= 3) {
server_switch_level(info, info -> level, false);
info -> try_number = -1;
return;
}
polygon_struct *body_info = protocol_extract_body(info -> message);
body_info -> body_id = create_user_object((cpVect *)body_info -> vectors ->
data, body_info -> vector_count, conv_color(body_info -> color),
info -> world, PLAYER_BOX_COLLISION_NUMBER, 1, body_info->mass);
cpVect average = cpv(0,0);
cpVect *vect = (cpVect *) (body_info->vectors->data);
// sum up the x and y values
for (int i = 0; i < body_info->vector_count; i++) {
average.x += vect[i].x;
average.y += vect[i].y;
}
// divide by the vector count to get the average
average.x /= body_info->vector_count;
average.y /= body_info->vector_count;
cpVect vertices[body_info->vector_count];
for (int i = 0; i < body_info->vector_count; i++) {
vertices[i] = cpvsub (vect[i], average);
}
info -> message = protocol_new_body(body_info -> color, vertices, body_info -> vector_count,
body_info -> body_id, 1);
server_broadcast_message(info);
usleep(EXTRA_TIME * 5);
}
/*
send the body of the level to the clients
parameters: body, broadcast_info pointer
returns: nothing
*/
static void
server_send_initial_bodies(cpBody *body, broadcast_info *info) {
polygon_struct *body_info = polygon_from_body(body);
info -> message = protocol_new_body(body_info -> color, (cpVect *)body_info -> vectors-> data,
body_info -> vector_count, body_info -> body_id, 1);
server_broadcast_message(info);
free_message(info);
usleep(EXTRA_TIME * 5);
}
/*
frees the message in broadcast info
parameters: broadcast_info pointer
returns: nothing
*/
static void
free_message(broadcast_info *info) {
if(info -> message) {
free(info -> message);
info -> message = NULL;
}
}
/*
sends the initial world info, using server_send_initial_bodies
on each body within the initial level cpSpace
parameters: broadcast_info pointer
returns: nothing
*/
static void
server_send_world_info(broadcast_info *info) {
server_send_initial_bodies(world_get_ground(info -> world -> space), info);
cpSpaceEachBody(info -> world -> space, (cpSpaceBodyIteratorFunc)
server_send_initial_bodies, info);
if (info -> world->drawing_box) {
free_message(info);
info->message = protocol_send_zone(info -> world->drawing_box_x1,
info -> world->drawing_box_y1, info -> world->drawing_box_x2,
info -> world->drawing_box_y2);
server_broadcast_message(info);
}
}
/*
switches the level depending on if won or not
parameters: broadcast_info pointer, current level, win boolean
returns: nothing
*/
static void
server_switch_level(broadcast_info *info, int level, bool win) {
if(info->world) {
world_free(info -> world);
info->world = NULL;
}
// Check which level should be loaded
if(win)
info->level = info->level % 10 + 1;
else if (level >= 1) {
info -> level = level;
}
info->try_number = 0;
// Create new world
info -> world = world_new(info -> level, TIMESTEP);
// Send instruction to kill current world and send info to populate new world
info -> message = protocol_send_level(info -> level, info -> try_number);
server_broadcast_message(info);
usleep(EXTRA_TIME * 2);
server_send_world_info(info);
}
/*
main function, starts up the server and sends
updates to any connected clients.
*/
int
main(int argc, char **argv) {
fd_set master_readfds;
FD_ZERO(&master_readfds);
int fdmax = 0; // Keeps track of highest file descriptor
// Socket to figure out if people are joining the game.
int new_player_listener_fd = server_create_socket(NEW_PLAYER);
FD_SET(new_player_listener_fd, &master_readfds);
fdmax = new_player_listener_fd + 1;
// Start game: create world_new and send level information to all clients
broadcast_info *info = new_broadcast_info(&master_readfds,
new_player_listener_fd, fdmax);
server_switch_level(info, 1, false);
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 500000;
while(!info -> game_started || info -> num_players > 0) {
server_world_step(info);
server_select(&tv, info);
usleep(5000);
}
// Close connections
for(int i = 0; i < fdmax + 1; i++) {
if(FD_ISSET(i, &master_readfds))
close(i);
}
// Freeing the world
world_free(info -> world);
free(info);
return EXIT_SUCCESS;
}