-
Notifications
You must be signed in to change notification settings - Fork 0
/
tris_server.c
852 lines (676 loc) · 26.1 KB
/
tris_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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "common.h"
#include "client_list.h"
#include "log.h"
#define BACKLOG 5
#define update_maxfds(n) maxfds = (maxfds < (n) ? (n) : maxfds)
#define monitor_socket_r(sock) { FD_SET(sock, &readfds); update_maxfds(sock); }
#define monitor_socket_w(sock) { FD_SET(sock, &writefds); update_maxfds(sock); }
#define unmonitor_socket_r(sock) FD_CLR(sock, &readfds)
#define unmonitor_socket_w(sock) FD_CLR(sock, &writefds)
/* ===[ Client handlers ]==================================================== */
void cancel_request(struct client_node*);
void get_username(struct client_node*);
void idle_free(struct client_node*);
void idle_play(struct client_node*);
void send_data(struct client_node*);
void get_play_resp(struct client_node*);
/* ===[ Helpers ]============================================================ */
void accept_connection(void);
void client_disconnected(struct client_node*);
void inactive(struct client_node*);
void prepare_byte(struct client_node *client, uint8_t byte);
void prepare_client_list(struct client_node*);
void prepare_client_contact(struct client_node *to, struct client_node *contact);
void prepare_play_request(struct client_node *from, struct client_node *to);
void server_shell(void);
void start_match(struct client_node*, struct client_node*);
/* ===[ Data ]=============================================================== */
char buffer[BUFFER_SIZE];
fd_set readfds, writefds;
int maxfds = -1;
int sock_listen;
struct log_file *console;
/* ===[ Main ]=============================================================== */
int main (int argc, char **argv) {
fd_set _readfds, _writefds;
struct sockaddr_in myhost;
int y = 1, s, i;
/* Set log files */
console = new_log(stdout, LOG_CONSOLE | LOG_INFO | LOG_ERROR, FALSE);
open_log("logs/tris_server.log", LOG_ALL);
if ( argc != 3 ) {
flog_message(LOG_CONSOLE, "Usage: %s <host> <listening_port>", argv[0]);
exit(EXIT_FAILURE);
}
if ( inet_pton(AF_INET, argv[1], &(myhost.sin_addr)) != 1 ) {
log_message(LOG_CONSOLE, "Invalid host address");
exit(EXIT_FAILURE);
}
myhost.sin_family = AF_INET;
myhost.sin_port = htons((uint16_t) atoi(argv[2])); /*FIXME check cast */
memset(myhost.sin_zero, 0, sizeof(myhost.sin_zero));
if ( (sock_listen = socket(myhost.sin_family, SOCK_STREAM, 0)) == -1 ) {
log_error("Error socket()");
exit(EXIT_FAILURE);
}
if ( setsockopt(sock_listen, SOL_SOCKET, SO_REUSEADDR, &y, sizeof(int)) ) {
log_error("Error setsockopt()");
exit(EXIT_FAILURE);
}
if ( bind(sock_listen, (struct sockaddr*) &myhost, sizeof(myhost)) ) {
log_error("Error bind()");
exit(EXIT_FAILURE);
}
if ( listen(sock_listen, BACKLOG) ) {
log_error("Error listen()");
exit(EXIT_FAILURE);
}
inet_ntop(AF_INET, &(myhost.sin_addr), buffer, INET_ADDRSTRLEN);
flog_message(LOG_INFO, "Server listening on %s:%hu", buffer,
ntohs(myhost.sin_port));
FD_ZERO(&readfds);
FD_ZERO(&writefds);
monitor_socket_r(sock_listen);
monitor_socket_r(STDIN_FILENO);
_readfds = readfds;
_writefds = writefds;
console->prompt = '>';
console->auto_prompt = TRUE;
log_prompt(console);
while ( (s = select(maxfds + 1, &_readfds, &_writefds, NULL, NULL)) > 0 ) {
for ( i = 0; i <= maxfds; i++ ) {
if ( FD_ISSET(i, &_readfds) ) {
if ( i == sock_listen ) {
accept_connection();
} else if ( i == STDIN_FILENO ) {
server_shell();
} else {
struct client_node *client;
int sock_client = i;
client = get_client_by_socket(sock_client);
if ( client != NULL && client->read_dispatch != NULL ) {
if ( client->muted ) inactive(client);
else client->read_dispatch(client);
} else { /*FIXME non dovrebbe mai succedere */
flog_message(LOG_WARNING,
"Unexpected event on line %d, got read event on socket descriptor %d",
__LINE__, i);
unmonitor_socket_r(sock_client);
unmonitor_socket_w(sock_client);
close(sock_client);
}
}
break; /* goto select() */
} else if ( FD_ISSET(i, &_writefds) ) {
struct client_node *client;
int sock_client = i;
client = get_client_by_socket(sock_client);
if ( client != NULL && client->write_dispatch != NULL ) {
client->write_dispatch(client);
} else { /*FIXME non dovrebbe mai succedere */
flog_message(LOG_WARNING,
"Unexpected event on line %d, got write event on socket descriptor %d",
__LINE__, i);
unmonitor_socket_r(sock_client);
unmonitor_socket_w(sock_client);
close(sock_client);
}
break; /* goto select() */
}
}
_readfds = readfds;
_writefds = writefds;
}
log_error("Error select()");
for ( i = 0; i <= maxfds; i++ ) {
if ( i != STDIN_FILENO &&
( FD_ISSET(i, &readfds) || FD_ISSET(i, &writefds) ) ) {
close(i);
}
}
exit(EXIT_FAILURE);
}
/* ========================================================================== */
void accept_connection() {
int sock_client;
struct sockaddr_in clienthost;
struct client_node *client;
socklen_t addrlen = sizeof(clienthost);
sock_client = accept(sock_listen, (struct sockaddr*) &clienthost, &addrlen);
if ( sock_client < 0 ) {
/*FIXME Potrebbe verificarsi ECONNABORTED, vorrei ritentare */
log_error("Error accept()");
close(sock_listen);
exit(EXIT_FAILURE);
}
client = create_client_node();
add_client_node(client);
client->addr = clienthost;
client->socket = sock_client;
client->state = CONNECTED;
client->read_dispatch = &get_username;
flog_message(LOG_INFO, "Incoming connection from %s",
client_sockaddr_p(client));
log_statechange(client);
monitor_socket_r(sock_client);
}
void get_username(struct client_node *client) {
uint8_t cmd;
int received;
struct client_node *dbl;
received = recv(client->socket, &cmd, 1, 0);
if ( received != 1 ) {
flog_message(LOG_DEBUG, "Received=%d on line %d from %s", received,
__LINE__, client_canon_p(client));
client_disconnected(client);
return;
}
if ( cmd != REQ_LOGIN ) {
flog_message(LOG_WARNING, "Got BADREQ on line %d, cmd=%s from %s",
__LINE__, magic_name(cmd), client_canon_p(client));
if ( cmd != RESP_BADREQ ) prepare_byte(client, RESP_BADREQ);
return;
}
flog_message(LOG_DEBUG, "Got REQ_LOGIN from %s", client_sockaddr_p(client));
received = recv(client->socket, &(client->username_len), 1, 0);
if ( received != 1 ) {
flog_message(LOG_WARNING, "Received=%d on line %d from %s", received,
__LINE__, client_canon_p(client));
client_disconnected(client);
return;
}
if ( client->username_len < MIN_USERNAME_LENGTH ||
client->username_len > MAX_USERNAME_LENGTH ) {
flog_message(LOG_INFO_VERBOSE,
"Client %s tried to login with an invalid username",
client_sockaddr_p(client));
prepare_byte(client, RESP_BADUSR);
return;
}
received = recv(client->socket, buffer, client->username_len + 2, 0);
if ( received != client->username_len + 2 ) {
flog_message(LOG_WARNING, "Received=%d on line %d from %s",
received, __LINE__, client_canon_p(client));
client_disconnected(client);
return;
}
unpack(buffer, "sw", client->username_len, &(client->username),
&(client->udp_port));
if ( !username_is_valid(client->username, client->username_len) ) {
/*TODO print escaped username string */
flog_message(LOG_INFO_VERBOSE,
"Client %s tried to login with invalid username",
client_sockaddr_p(client));
prepare_byte(client, RESP_BADUSR);
return;
}
dbl = get_client_by_username(client->username);
if ( dbl != NULL ) {
flog_message(LOG_INFO_VERBOSE,
"Client %s tried to login with existing username=%s",
client_sockaddr_p(client), dbl->username);
prepare_byte(client, RESP_EXIST);
return;
}
inet_ntop(AF_INET, &(client->addr.sin_addr), buffer, INET_ADDRSTRLEN);
flog_message(LOG_INFO, "Client %s has username [%s]",
client_sockaddr_p(client), client->username);
flog_message(LOG_INFO, "[%s] Listening on %s:%hu (udp)",
client->username, buffer, client->udp_port);
client->state = FREE;
/* client->read_dispatch = &idle_free; */
log_statechange(client);
prepare_byte(client, RESP_OK_LOGIN);
}
void idle_free(struct client_node *client) {
uint8_t cmd, length;
int received;
struct client_node *opp;
received = recv(client->socket, &cmd, 1, 0);
if ( received != 1 ) {
flog_message(LOG_DEBUG, "Received=%d on line %d from %s", received,
__LINE__, client_canon_p(client));
client_disconnected(client);
return;
}
flog_message(LOG_DEBUG, "Got cmd=%s from %s in idle_free", magic_name(cmd),
client_canon_p(client));
switch ( cmd ) {
case REQ_WHO:
prepare_client_list(client);
break;
case REQ_PLAY:
received = recv(client->socket, &length, 1, 0);
if ( received != 1 ) {
flog_message(LOG_WARNING, "Received=%d on line %d from %s",
received, __LINE__, client_canon_p(client));
client_disconnected(client);
return;
}
if ( length < MIN_USERNAME_LENGTH ||
length > MAX_USERNAME_LENGTH ) {
flog_message(LOG_WARNING,
"[%s] requested to play with nonexistent player (too long)",
client->username);
prepare_byte(client, RESP_NONEXIST);
return;
}
received = recv(client->socket, buffer, length, 0);
if ( received != length ) {
flog_message(LOG_WARNING, "Received=%d on line %d from %s",
received, __LINE__, client_canon_p(client));
client_disconnected(client);
return;
}
buffer[length] = '\0';
opp = get_client_by_username(buffer);
if ( opp == NULL ) {
flog_message(LOG_INFO_VERBOSE,
"[%s] requested to play with nonexistent player",
client->username);
prepare_byte(client, RESP_NONEXIST);
} else if ( opp == client ) {
flog_message(LOG_INFO_VERBOSE,
"[%s] requested to play with himself", client->username);
prepare_byte(client, RESP_NONEXIST);
} else if ( opp->state != FREE ) {
flog_message(LOG_INFO_VERBOSE,
"[%s] requested to play with non-FREE player %s",
client->username, client_canon_p(opp));
prepare_byte(client, RESP_BUSY);
} else prepare_play_request(client, opp);
break;
default:
flog_message(LOG_WARNING, "Unexpected request from %s in idle_free",
client_canon_p(client));
if ( cmd != RESP_BADREQ ) prepare_byte(client, RESP_BADREQ);
}
}
void idle_play(struct client_node *client) {
uint8_t cmd;
int received;
received = recv(client->socket, &cmd, 1, 0);
if ( received != 1 ) {
flog_message(LOG_DEBUG, "Received=%d on line %d from %s", received,
__LINE__, client_canon_p(client));
client_disconnected(client);
return;
}
flog_message(LOG_DEBUG, "Got cmd=%s from %s in idle_play", magic_name(cmd),
client_canon_p(client));
switch ( cmd ) {
case REQ_WHO:
prepare_client_list(client);
break;
case REQ_END:
if ( client->play_with != NULL ) {
flog_message(LOG_INFO, "[%s] stopped playing with [%s]",
client->username, client->play_with->username);
client->play_with->play_with = NULL;
client->play_with = NULL;
} else if ( client->req_to != NULL ) {
/* cancel_request() */
flog_message(LOG_INFO, "[%s] cancelled playing with [%s]",
client->username, client->req_to->username);
client->req_to->req_from = NULL;
client->req_to = NULL;
} else
flog_message(LOG_INFO, "[%s] stopped playing",
client->username);
client->state = FREE;
log_statechange(client);
prepare_byte(client, RESP_OK_FREE);
break;
default:
flog_message(LOG_WARNING, "Unexpected request from %s in idle_play",
client_canon_p(client));
if ( cmd != RESP_BADREQ ) prepare_byte(client, RESP_BADREQ);
}
}
void cancel_request(struct client_node *client) {
uint8_t cmd;
int received;
received = recv(client->socket, &cmd, 1, 0);
if ( received != 1 ) {
flog_message(LOG_DEBUG, "Received=%d on line %d from %s", received,
__LINE__, client_canon_p(client));
client_disconnected(client);
return;
}
flog_message(LOG_DEBUG, "Got cmd=%s from %s in cancel_request",
magic_name(cmd), client_canon_p(client));
if ( cmd == REQ_END ) {
if ( client->req_to != NULL ) {
flog_message(LOG_INFO, "[%s] cancelled playing with [%s]",
client->username, client->req_to->username);
client->req_to->req_from = NULL;
client->req_to = NULL;
}
client->state = FREE;
log_statechange(client);
prepare_byte(client, RESP_OK_FREE);
} else if ( cmd != RESP_BADREQ )
prepare_byte(client, RESP_BADREQ);
}
void client_disconnected(struct client_node *client) {
struct client_node *opp;
if ( client->state == BUSY ) {
if ( client->req_from != NULL ) {
opp = client->req_from;
flog_message(LOG_INFO_VERBOSE, "[%s] had a play request from [%s]",
client->username, opp->username);
prepare_byte(opp, RESP_NONEXIST);
opp->req_to = NULL;
opp->state = FREE;
log_statechange(opp);
} else if ( client->req_to != NULL ) {
opp = client->req_to;
flog_message(LOG_INFO_VERBOSE,
"[%s] had requested to play with [%s]", client->username,
opp->username);
/* opp->state == BUSY || opp->state == PLAY */
opp->req_from = NULL;
/* opp->read_dispatch = &idle_free; */
unmonitor_socket_w(opp->socket);
monitor_socket_r(opp->socket); /*FIXME inutile? */
} else {
/* in case client->req_from or req_to are set to NULL by
cancel_request() or idle_play() */
/*TODO use a better log message */
flog_message(LOG_INFO_VERBOSE, "[%s] had a play request",
client_canon_p(client), __LINE__);
}
} else if ( client->state == PLAY ) {
opp = client->play_with;
if ( opp != NULL ) {
flog_message(LOG_INFO_VERBOSE, "[%s] was playing with [%s]",
client->username, opp->username);
opp->play_with = NULL;
} else {
flog_message(LOG_INFO_VERBOSE, "[%s] was playing with [[unknown]]",
client->username);
}
}
if ( client->state != NONE && client->state != CONNECTED )
flog_message(LOG_INFO, "[%s] disconnected", client->username);
else
flog_message(LOG_INFO, "[[unknown]] %s disconnected",
client_sockaddr_p(client));
unmonitor_socket_r(client->socket);
unmonitor_socket_w(client->socket);
close(client->socket);
remove_client_node(client);
destroy_client_node(client);
}
void prepare_byte(struct client_node *client, uint8_t byte) {
flog_message(LOG_DEBUG, "Preparing to send %s to %s", magic_name(byte),
client_canon_p(client));
client->byte_resp = byte;
client->data = NULL;
client->data_count = 1;
client->data_cursor = 0;
client->write_dispatch = &send_data;
/* client->read_dispatch = &inactive; */
client->muted = TRUE;
monitor_socket_w(client->socket);
}
void send_data(struct client_node *client) {
int sent;
if (client->data == NULL)
sent = send(client->socket, &(client->byte_resp), 1, 0);
else
sent = send(client->socket, client->data + client->data_cursor,
client->data_count - client->data_cursor, 0);
if ( sent < 0 ) {
log_error("Error send()");
client_disconnected(client);
return;
}
client->data_cursor += sent;
if ( client->data_cursor == client->data_count ) {
flog_message(LOG_DEBUG, "Finished sending %d bytes of data to %s",
client->data_count, client_canon_p(client));
if ( client->data != NULL ) {
free(client->data);
client->data = NULL;
}
flog_message(LOG_DEBUG, "%s is %s", client_canon_p(client),
state_name(client->state));
switch ( client->state ) {
case CONNECTED: client->read_dispatch = &get_username; break;
case FREE: client->read_dispatch = &idle_free; break;
case BUSY: client->read_dispatch = &get_play_resp; break;
case PLAY: client->read_dispatch = &idle_play; break;
default:
client_disconnected(client);
return;
}
client->muted = FALSE;
unmonitor_socket_w(client->socket);
}
}
void server_shell() {
get_line(buffer, BUFFER_SIZE);
log_message(LOG_USERINPUT, buffer);
if ( strcmp(buffer, "help" ) == 0 ) { /* ------------------------- > help */
log_message(LOG_CONSOLE, "Commands: help, who, playing, exit");
} else if ( strcmp(buffer, "who") == 0 ) { /* --------------------- > who */
struct client_node *cn;
if (client_list.count == 0) {
log_message(LOG_CONSOLE, "There are no connected clients.");
} else {
console->prompt = FALSE;
flog_message(LOG_CONSOLE, "There are %u connected clients:",
client_list.count);
for ( cn = client_list.head; cn != NULL; cn = cn->next ) {
if ( cn->state == NONE || cn->state == CONNECTED )
flog_message(LOG_CONSOLE,
"[[unknown]] Host %s, not logged in",
client_sockaddr_p(cn));
else
flog_message(LOG_CONSOLE, "[%s] Host %s listening on %hu",
cn->username, client_sockaddr_p(cn), cn->udp_port);
}
console->prompt = '>';
log_prompt(console);
}
} else if ( strcmp(buffer, "playing") == 0 ) { /* ------------- > playing */
struct client_node *cn, *opp;
bool playing = FALSE;
console->prompt = FALSE;
for ( cn = client_list.head; cn != NULL; cn = cn->next ) {
if ( cn->state == PLAY && cn->play_with != NULL ) {
opp = cn->play_with;
if ( opp->state == PLAY && opp->play_with == cn ) {
playing = TRUE;
if ( strcmp(cn->username, opp->username) < 0 )
flog_message(LOG_CONSOLE, "[%s] is playing with [%s]",
cn->username, opp->username);
}
}
}
if ( !playing ) log_message(LOG_CONSOLE, "No one is playing");
console->prompt = '>';
log_prompt(console);
} else if ( strcmp(buffer, "exit") == 0 ) { /* ------------------- > exit */
int i;
flog_message(LOG_INFO_VERBOSE, "Closing %d client connections...",
client_list.count);
for ( i = 0; i <= maxfds; i++ ) {
if ( i == STDIN_FILENO ) continue;
if ( FD_ISSET(i, &readfds) || FD_ISSET(i, &writefds) ) {
close(i);
}
}
destroy_client_list(client_list.head);
console->auto_prompt = FALSE;
log_message(LOG_CONSOLE, "Exiting...");
exit(EXIT_SUCCESS);
} else if ( strcmp(buffer, "") == 0 ) { /* ---------------------------- > */
log_prompt(console);
} else {
log_message(LOG_CONSOLE, "Unknown command. Type 'help' for list of commands");
}
}
void prepare_client_list(struct client_node *client) {
uint32_t count = 0;
int total_length = 1 + 4;
struct client_node *cn;
flog_message(LOG_INFO_VERBOSE,
"[%s] requested the list of connected clients", client->username);
for (cn = client_list.head; cn != NULL; cn = cn->next ) {
if ( cn->state != NONE && cn->state != CONNECTED ) {
count++;
total_length += 2 + cn->username_len;
}
}
client->data = malloc(total_length);
check_alloc(client->data);
client->data_cursor = 0;
pack(client->data, "bl", RESP_WHO, count);
client->data_count = 5;
for (cn = client_list.head; cn != NULL; cn = cn->next ) {
if ( cn->state != NONE && cn->state != CONNECTED ) {
pack(client->data + client->data_count, "bsb", cn->username_len,
cn->username, state_encode(cn->state));
client->data_count += 2 + cn->username_len;
}
}
flog_message(LOG_DEBUG, "Preparing to send RESP_WHO data to [%s]",
client->username);
client->write_dispatch = &send_data;
monitor_socket_w(client->socket);
/* client->read_dispatch = &inactive; */
client->muted = TRUE;
}
void prepare_client_contact(struct client_node *to, struct client_node *cntc) {
to->data_count = 1 + sizeof(cntc->addr.sin_addr) + sizeof(cntc->udp_port);
to->data = malloc(to->data_count);
check_alloc(to->data);
pack(to->data, "blw", RESP_OK_PLAY, cntc->addr.sin_addr, cntc->udp_port);
to->data_cursor = 0;
flog_message(LOG_DEBUG, "Preparing to send RESP_OK_PLAY data to [%s]",
to->username);
to->write_dispatch = &send_data;
monitor_socket_w(to->socket);
to->muted = TRUE;
}
void prepare_play_request(struct client_node *from, struct client_node *to) {
flog_message(LOG_INFO, "[%s] requested to play with [%s]", from->username,
to->username);
from->req_to = to;
to->req_from = from;
from->state = to->state = BUSY;
log_statechange(from);
log_statechange(to);
to->data_count = 2 + from->username_len;
to->data = malloc(to->data_count);
check_alloc(to->data);
pack(to->data, "bbs", REQ_PLAY, from->username_len, from->username);
to->data_cursor = 0;
flog_message(LOG_DEBUG, "Preparing to send REQ_PLAY data to [%s]",
to->username);
to->write_dispatch = &send_data;
monitor_socket_w(to->socket);
to->muted = TRUE;
from->read_dispatch = &cancel_request;
}
void get_play_resp(struct client_node *client) {
uint8_t resp;
int received;
struct client_node *opp = client->req_from;
received = recv(client->socket, &resp, 1, 0);
if ( received != 1 ) {
flog_message(LOG_DEBUG, "Received=%d on line %d from %s", received,
__LINE__, client_canon_p(client));
client_disconnected(client);
return;
}
if ( resp == RESP_OK_PLAY ) {
if ( opp == NULL ) {
/* opp si è disconnesso prima che client rispondesse. Facciamo
comunque finta che sia ancora collegato, perché non c'è modo di
comunicarlo a client */
flog_message(LOG_INFO_VERBOSE, "[%s] accepted to play",
client->username);
/*TODO oppure opp ha inviato REQ_END prima della play response */
} else {
flog_message(LOG_INFO, "[%s] accepted to play with [%s]",
client->username, opp->username);
prepare_client_contact(opp, client);
}
start_match(client, opp);
} else {
/* FIXME May be some other valid command such as REQ_WHO or REQ_PLAY */
/* RESP_REFUSE o, per sbaglio, anche RESP_BUSY / PERCHÉ?! */
if ( resp != RESP_REFUSE )
flog_message(LOG_WARNING, "Got %s in get_play_resp from %s",
magic_name(resp), client_canon_p(client));
if ( opp == NULL ) {
flog_message(LOG_INFO_VERBOSE, "[%s] refused to play",
client->username);
/*TODO oppure opp ha inviato REQ_END prima della play response */
} else {
flog_message(LOG_INFO, "[%s] refused to play with [%s]",
client->username, opp->username);
prepare_byte(opp, RESP_REFUSE);
opp->state = FREE;
opp->req_to = NULL;
log_statechange(opp);
}
client->state = FREE;
client->req_from = NULL;
client->read_dispatch = &idle_free;
log_statechange(client);
}
}
void start_match(struct client_node *a, struct client_node *b) {
/* Symmetric */
if ( a != NULL ) {
a->state = PLAY;
a->play_with = b;
a->req_from = a->req_to = NULL;
a->read_dispatch = &idle_play;
log_statechange(a);
}
if ( b != NULL ) {
b->state = PLAY;
b->play_with = a;
b->req_from = b->req_to = NULL;
b->read_dispatch = &idle_play;
log_statechange(b);
}
if ( a != NULL && b != NULL )
flog_message(LOG_INFO, "[%s] is playing with [%s]", a->username,
b->username);
else if ( a != NULL )
flog_message(LOG_INFO_VERBOSE, "[%s] is playing", a->username);
else if ( b != NULL )
flog_message(LOG_INFO_VERBOSE, "[%s] is playing", b->username);
else
flog_message(LOG_WARNING,
"Unexpected event on line %d, both clients are NULL", __LINE__);
}
void inactive(struct client_node *client) {
int received = recv(client->socket, buffer, BUFFER_SIZE, 0);
if ( received == 0 )
client_disconnected(client);
else if ( received == 1 )
flog_message(LOG_WARNING, "Got %s in inactive from %s",
magic_name(buffer[0]), client_canon_p(client));
else
flog_message(LOG_WARNING, "Got %d bytes in inactive from %s", received,
client_canon_p(client));
/*FIXME what to do? */
}