-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
468 lines (408 loc) · 11.8 KB
/
server.cpp
File metadata and controls
468 lines (408 loc) · 11.8 KB
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#include <stdio.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <stdlib.h>
#include <string.h>
#include <poll.h>
#include <vector>
#include <assert.h>
#include <unistd.h>
#include <algorithm>
#include "net_protocol.hpp"
#include "game.hpp"
using namespace std;
// TODO Handle SIGFPIPE
#define arraysize(array) (sizeof(array)/sizeof(array[0]))
int listen_fd = -1;
extern FILE *dbg;
// ulimit -n
// sysctl -w fs.file-max=100000
// cat /proc/sys/fs/file-nr
// getrlimit/setrlimit RLIMIT_NOFILE
struct pollfd fds[100]; // TODO MAX FD PER PROCESSUS?
size_t sz = 0;
struct player
{
int fd;
char username[10];
};
bool operator == (struct player l, struct player r)
{
return l.fd == r.fd && strncmp(l.username, r.username, sizeof(l.username)) == 0;
}
vector<struct player> player_list;
vector<struct player> waiting_list;
template<typename T>
void appendUnique(vector<T>& vec, T nelt)
{
for(auto elt : vec)
if(elt == nelt)
return;
vec.push_back(nelt);
}
enum color last_color = color::black;
struct srv_game
{
struct game game;
int white_fd = -1, black_fd = -1;
};
struct srv_game current_game;
// void match_players()
// {
// while(waiting_list.size() > 1)
// {
// int white_fd = waiting_list[0];
// int black_fd = waiting_list[1];
// struct srv_game new_game;
// new_game.game.pieces = initial_board;
// new_game.white_fd = white_fd;
// new_game.black_fd = black_fd;
// current_game = new_game;
// struct new_game_msg new_game_msg;
// new_game_msg.msg_type = msg_type::new_game_msg;
// new_game_msg.player_color = color::white;
// ssize_t n = send(white_fd, &new_game_msg, sizeof(new_game_msg),0);
// if(n == -1)
// {
// perror("send()");
// assert(false);
// exit(EXIT_FAILURE);
// }
// new_game_msg.player_color = color::black;
// n = send(black_fd, &new_game_msg, sizeof(new_game_msg),0);
// if(n == -1)
// {
// perror("send()");
// assert(false);
// exit(EXIT_FAILURE);
// }
// waiting_list.erase(waiting_list.begin(), waiting_list.begin()+2);
// }
// }
void process_login(int fd, struct login *login)
{
printf("login={msg type=%d, username=%.*s}\n",
login->msg_type,
(int)sizeof(login->username), login->username);
// TODO POLLOUT or not POLLOUT?
struct login_ack login_ack;
login_ack.msg_type = msg_type::login_ack;
struct player player;
player.fd = fd;
memcpy(player.username, login->username, sizeof(login->username));
static_assert(sizeof(player.username) == sizeof(login->username), "");
player_list.push_back(player);
int n = send(fd, &login_ack, sizeof(login_ack), 0);
if(n == -1)
{
perror("send()");
assert(false);
exit(EXIT_FAILURE);
}
printf("login ack sent!\n");
fflush(stdout);
}
int find_opponent_fd(int fd)
{
for(size_t i = 0; i < sz; i++)
{
if(fds[i].fd == listen_fd)
continue;
if(fds[i].fd == fd)
continue;
return fds[i].fd;
}
assert(false);
return -1;
}
void process_move(int fd, struct move_msg *move_msg)
{
printf("move_msg={src={row=%d,col=%d},dst={row=%d,col=%d}}\n",
move_msg->src_row, move_msg->src_col,
move_msg->dst_row, move_msg->dst_col);
assert(current_game.white_fd != -1 && current_game.black_fd != -1);
int opponent_fd = -1;
if(fd == current_game.white_fd)
opponent_fd = current_game.black_fd;
else if(fd == current_game.black_fd)
opponent_fd = current_game.white_fd;
else
assert(false);
assert(opponent_fd != -1);
struct move candidate_move = {{move_msg->src_row, move_msg->src_col},
{move_msg->dst_row, move_msg->dst_col},
piece_type::none};
vector<struct move> valid_moves = next_valid_moves(current_game.game);
auto found = find(valid_moves.begin(), valid_moves.end(), candidate_move);
fprintf(dbg, "candidate:\n");
print_move(candidate_move);
fprintf(dbg, "valid moves:\n");
for(auto move : valid_moves)
{
print_move(move);
}
if(found != valid_moves.end())
{
current_game.game = apply_move(current_game.game, candidate_move);
ssize_t n = send(opponent_fd, move_msg, sizeof(*move_msg), 0);
if(n == -1)
{
perror("send()");
assert(false);
exit(EXIT_FAILURE);
}
if(next_valid_moves(current_game.game).size() == 0)
{
update_king_statuses(current_game.game);
if(current_game.game.cur_player == color::white && current_game.game.is_white_king_checked)
printf("WHITE IS CHECKMATED!!\n");
else if(current_game.game.cur_player == color::black && current_game.game.is_black_king_checked)
printf("BLACK IS CHECKMATED!!\n");
else
printf("STALEMATE!!\n");
// TODO The game end here. Mark it into the game. As a
// result when we receive a new move from one of the
// opponent we must check the party is still open and
// reject any move.
}
}
else
{
printf("reject move.\n");
struct reject_move_msg reject_move_msg;
reject_move_msg.msg_type = msg_type::reject_move_msg;
ssize_t n = send(fd, &reject_move_msg, sizeof(reject_move_msg), 0);
if(n == -1)
{
perror("send()");
assert(false);
exit(EXIT_FAILURE);
}
}
}
void process_play_online(int fd, struct play_online_msg *play_online_msg __attribute__((unused)))
{
printf("process_play_online(fd=%d,...)\n", fd);
struct players_list_msg msg;
msg.msg_type = msg_type::players;
msg.players_list[0] = '\0';
for(auto player : player_list)
{
if(player.fd == fd)
{
appendUnique(waiting_list, player);
break;
}
}
// TODO Verify the size of the msg.players_list field.
for(auto player : waiting_list)
{
if(player.fd == fd)
continue;
strcat(msg.players_list, player.username);
strcat(msg.players_list, ",");
}
ssize_t n = send(fd, &msg, sizeof(msg), 0);
if(n == -1)
{
perror("send()");
assert(false);
exit(EXIT_FAILURE);
}
}
void process_request_for_game(int fd, struct request_for_game *request_for_game_msg)
{
for(auto player : waiting_list)
{
if(strncmp(player.username, request_for_game_msg->opponent_username,
sizeof(player.username)) == 0)
{
struct new_game_msg msg;
msg.msg_type = msg_type::new_game_msg;
msg.player_color = color::white;
ssize_t n = send(fd, &msg, sizeof(msg), 0);
if(n == -1)
{
perror("send()");
assert(false);
exit(EXIT_FAILURE);
}
msg.player_color = color::black;
n = send(player.fd, &msg, sizeof(msg), 0);
if(n == -1)
{
perror("send()");
assert(false);
exit(EXIT_FAILURE);
}
struct srv_game new_game;
new_game.game.pieces = initial_board;
new_game.white_fd = fd;
new_game.black_fd = player.fd;
current_game = new_game;
}
}
}
void process_listen_fd(int fd)
{
// TODO Read man page for accept4(). And possible errors.
int new_fd = accept(fd, nullptr, nullptr);
if(new_fd == -1)
{
perror("accept()");
assert(false);
exit(EXIT_FAILURE);
}
printf("accepted fd = %d.\n", new_fd);
fds[sz++] = {new_fd, POLLIN, 0};
}
void process_player_fd(int i, struct pollfd pollfd)
{
assert(pollfd.revents & (POLLIN));
int fd = pollfd.fd;
char buf[1024];
// TODO Replace with while(n = recv(...)) {...}
int n = recv(fd, buf, sizeof(buf), 0);
if(n == -1)
{
perror("recv()");
assert(false);
exit(EXIT_FAILURE);
}
else if(n == 0)
{
printf("close fd = %d.\n", fd);
int res = close(fd);
if(res == -1)
{
perror("close()");
assert(false);
exit(EXIT_FAILURE);
}
int opponent_fd = -1;
if(fd == current_game.white_fd)
{
opponent_fd = current_game.black_fd;
}
else if(fd == current_game.black_fd)
{
opponent_fd = current_game.white_fd;
}
if(opponent_fd != -1)
{
struct game_evt_msg msg;
msg.msg_type = msg_type::game_evt_msg;
msg.game_evt_type = game_evt_type::opponent_resigned;
ssize_t n = send(opponent_fd, &msg, sizeof(msg), 0);
if(n == -1)
{
perror("send()");
assert(false);
exit(EXIT_FAILURE);
}
}
fds[i] = {-1,0,0};
current_game.white_fd = -1;
current_game.black_fd = -1;
}
else
{
enum msg_type type = (enum msg_type)buf[0];
printf("recv=%d, msg_type=%d.\n", n, type);
switch(type)
{
case msg_type::login:
process_login(fd, (struct login*)buf);
break;
case msg_type::move_msg:
process_move(fd, (struct move_msg*)buf);
break;
case msg_type::play_online:
process_play_online(fd, (struct play_online_msg*)buf);
break;
case msg_type::request_for_game:
process_request_for_game(fd, (struct request_for_game*)buf);
break;
default:
printf("Unknown msg type=%d.\n", type);
break;
}
}
}
int main()
{
listen_fd = socket(AF_INET, SOCK_STREAM, 0);
if(listen_fd == -1)
{
perror("socket()");
assert(false);
exit(EXIT_FAILURE);
}
printf("listen fd = %d.\n", listen_fd);
fds[sz++] = { listen_fd, POLLIN, 0 };
int optval = 1;
int res = setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR,
&optval, sizeof(optval));
if(res == -1)
{
perror("setsockopt(SO_REUSEADDR)");
assert(false);
exit(EXIT_FAILURE);
}
res = setsockopt(listen_fd, IPPROTO_TCP, TCP_NODELAY,
&optval, sizeof(optval));
if(res == -1)
{
perror("setsockopt(TCP_NODELAY)");
assert(false);
exit(EXIT_FAILURE);
}
struct sockaddr_in listen_addr;
memset(&listen_addr, 0, sizeof(listen_addr));
listen_addr.sin_family = AF_INET;
listen_addr.sin_port = htons(55555);
listen_addr.sin_addr.s_addr = INADDR_ANY;
res = bind(listen_fd, (sockaddr*)&listen_addr, sizeof(listen_addr));
if(res == -1)
{
perror("bind()");
assert(false);
exit(EXIT_FAILURE);
}
res = listen(listen_fd, 10/*backlog*/);
if(res == -1)
{
perror("listen()");
assert(false);
exit(EXIT_FAILURE);
}
while(true)
{
int nfds = poll(&fds[0], sz, -1/*timeout*/);
if(nfds == -1)
{
perror("poll()");
assert(false);
exit(EXIT_FAILURE);
}
for(size_t i = 0; i < sz; i++)
{
struct pollfd pollfd = fds[i];
int fd = pollfd.fd;
if(pollfd.revents != 0)
{
if(fd == listen_fd)
{
process_listen_fd(fd);
}
else
{
process_player_fd(i, pollfd);
}
nfds--;
}
}
// remove(fds, fds+sz, fd);
// sz--;
}
}