-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.c
668 lines (553 loc) · 24.9 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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <sys/types.h>
#include "customSTD.h"
#define MAX_CLIENTS 10
#define MAX_ROOMS 5
#define BUFFER_SZ 2048
#define NAME_LEN 64
static _Atomic unsigned int cli_count = 0;
static int uid = 10;
static int roomUid = 1;
int posicoes[9][2] = {{2, 0}, {2, 1}, {2, 2}, {1, 0}, {1, 1}, {1, 2}, {0, 0}, {0, 1}, {0, 2}};
// client structure
typedef struct {
struct sockaddr_in address;
int sockfd;
int uid;
char name[NAME_LEN];
} client_t;
typedef struct {
char tabuleiro[3][3];
int estadoDeJogo;
int rodada;
int turnoDoJogador;
} game_t;
// room structure
typedef struct {
client_t *player1;
client_t *player2;
unsigned int uid;
char state[NAME_LEN];
game_t *game;
} room_t;
client_t *clients[MAX_CLIENTS];
room_t *rooms[MAX_ROOMS];
pthread_mutex_t clients_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t rooms_mutex = PTHREAD_MUTEX_INITIALIZER;
#include "queueManager.h"
void send_message(char *message, int uid)
{
pthread_mutex_lock(&clients_mutex);
for (int i = 0; i < MAX_CLIENTS; i++)
{
if (clients[i])
{
if (clients[i]->uid == uid)
{
if (write(clients[i]->sockfd, message, strlen(message)) < 0)
{
printf("ERROR: write to descriptor failed\n");
break;
}
}
}
}
pthread_mutex_unlock(&clients_mutex);
}
void *handle_client(void *arg)
{
char buffer[BUFFER_SZ];
char command[BUFFER_SZ];
int number;
char name[NAME_LEN];
int leave_flag = 0;
int flag = 0;
client_t *cli = (client_t*)arg;
// name
if (recv(cli->sockfd, name, NAME_LEN, 0) <= 0 || strlen(name) < 2 || strlen(name) >= NAME_LEN -1)
{
printf("Enter the name correctly\n");
leave_flag = 1;
} else {
strcpy(cli->name, name);
sprintf(buffer, "> %s has joined\n", cli->name);
printf("%s", buffer);
bzero(buffer, BUFFER_SZ);
sprintf(buffer, "ok");
send_message(buffer, cli->uid);
}
bzero(buffer, BUFFER_SZ);
while(leave_flag == 0)
{
command[0] = '\x00';
number = 0;
int receive = recv(cli->sockfd, buffer, BUFFER_SZ, 0);
if (receive > 0)
{
if (strlen(buffer) > 0)
{
// send_message(buffer, cli->uid);
trim_lf(buffer, strlen(buffer));
printf("> client: '%s' has been send '%s' command\n", cli->name, buffer);
sscanf(buffer, "%s %i", &command[0], &number);
if (strcmp(command, "create") == 0)
{
flag = 0;
pthread_mutex_lock(&rooms_mutex);
for (int i = 0; i < MAX_ROOMS; i++)
{
if (rooms[i])
{
if (rooms[i]->player1->uid == cli->uid)
{
bzero(buffer, BUFFER_SZ);
sprintf(buffer, "[SERVER] voce ja esta em uma sala\n");
send_message(buffer, cli->uid);
flag = 1;
break;
}
if (rooms[i]->player2 != 0)
{
if (rooms[i]->player2->uid == cli->uid)
{
bzero(buffer, BUFFER_SZ);
sprintf(buffer, "[SERVER] voce ja esta em uma sala\n");
send_message(buffer, cli->uid);
flag = 1;
break;
}
}
}
}
pthread_mutex_unlock(&rooms_mutex);
if (flag != 1) {
// clients settings
room_t *room = (room_t *)malloc(sizeof(room_t));
room->player1 = cli;
room->player2 = 0;
room->uid = roomUid;
strcpy(room->state, "waiting for secound player");
// add room to queue
queue_add_room(room);
bzero(buffer, BUFFER_SZ);
sprintf(buffer, "[SERVER] voce criou o novo room de numero %i\n", roomUid);
send_message(buffer, cli->uid);
roomUid++;
}
}
else if (strcmp(command, "join") == 0)
{
int researched = 0;
int already = 0;
pthread_mutex_lock(&rooms_mutex);
for (int j = 0; j < MAX_ROOMS; j++)
{
if (rooms[j])
{
if (rooms[j]->player1->uid == cli->uid)
{
already = 1;
bzero(buffer, BUFFER_SZ);
sprintf(buffer, "[SERVER] voce ja esta no room de numero: %i\n", rooms[j]->uid);
send_message(buffer, cli->uid);
break;
}
}
}
pthread_mutex_unlock(&rooms_mutex);
if (already == 1)
{
continue;
}
pthread_mutex_lock(&rooms_mutex);
for (int i = 0; i < MAX_ROOMS; i++)
{
if (rooms[i])
{
if (rooms[i]->uid == number)
{
researched = 1;
if (rooms[i]->player2 != 0)
{
if (rooms[i]->player2->uid == cli->uid)
{
bzero(buffer, BUFFER_SZ);
send_message("[SERVER] voce ja esta nesta sala\n", cli->uid);
break;
}
bzero(buffer, BUFFER_SZ);
sprintf(buffer, "[SERVER] room de numero: %i, ja esta cheio\n", rooms[i]->uid);
send_message(buffer, cli->uid);
break;
}
rooms[i]->player2 = cli;
strcpy(rooms[i]->state, "waiting start");
bzero(buffer, BUFFER_SZ);
printf("%s entrou no room de numero: %i\n", cli->name, number);
sprintf(buffer, "[SERVER] '%s' entrou em sua sala\n", cli->name);
send_message(buffer, rooms[i]->player1->uid);
bzero(buffer, BUFFER_SZ);
sprintf(buffer, "[SERVER] voce entrou no room de numero: %i\n", number);
send_message(buffer, cli->uid);
break;
}
}
}
pthread_mutex_unlock(&rooms_mutex);
if (researched == 0)
{
sprintf(buffer, "[SERVER] nao foi possivel encontrar a sala de numero %i\n", number);
send_message(buffer, cli->uid);
}
}
else if (strcmp(command, "list") == 0)
{
pthread_mutex_lock(&rooms_mutex);
for (int i = 0; i < MAX_ROOMS; i++)
{
if (rooms[i])
{
char *list = (char *)malloc(BUFFER_SZ*sizeof(char));
if (rooms[i]->player2 != 0)
{
sprintf(list, "%i)\n room state: %s\n player1: %s\n player2: %s\n", rooms[i]->uid, rooms[i]->state, rooms[i]->player1->name, rooms[i]->player2->name);
}
else
{
sprintf(list, "%i)\n room state: %s\n player1: %s\n", rooms[i]->uid, rooms[i]->state, rooms[i]->player1->name);
}
send_message(list, cli->uid);
free(list);
}
}
pthread_mutex_unlock(&rooms_mutex);
}
else if (strcmp(command, "leave") == 0)
{
int remove_room = 0;
int room_number = 0;
pthread_mutex_lock(&rooms_mutex);
for (int i = 0; i < MAX_ROOMS; i++)
{
if (rooms[i])
{
if (rooms[i]->player1->uid == cli->uid)
{
if (rooms[i]->player2 != 0) {
bzero(buffer, BUFFER_SZ);
sprintf(buffer, "[SERVER] %s saio da sala, agora voce e o dono\n", rooms[i]->player1->name);
send_message(buffer, rooms[i]->player2->uid);
rooms[i]->player1 = rooms[i]->player2;
rooms[i]->player2 = 0;
strcpy(rooms[i]->state, "waiting for secound player");
}
else
{
remove_room = 1;
room_number = rooms[i]->uid;
}
bzero(buffer, BUFFER_SZ);
sprintf(buffer, "[SERVER] voce saio da sala %i\n", rooms[i]->uid);
send_message(buffer, cli->uid);
break;
}
else if (rooms[i]->player2->uid == cli->uid)
{
bzero(buffer, BUFFER_SZ);
sprintf(buffer, "[SERVER] %s saio de sua sala\n", rooms[i]->player1->name);
send_message(buffer, rooms[i]->player1->uid);
rooms[i]->player2 = 0;
strcpy(rooms[i]->state, "waiting for secound player");
bzero(buffer, BUFFER_SZ);
sprintf(buffer, "[SERVER] voce saio da sala %i\n", rooms[i]->uid);
send_message(buffer, cli->uid);
break;
}
}
}
pthread_mutex_unlock(&rooms_mutex);
if (remove_room == 1)
{
queue_remove_room(room_number);
roomUid--;
}
}
else if (strcmp(command, "start") == 0)
{
int startgame = 0;
room_t *room_game;
pthread_mutex_lock(&rooms_mutex);
for (int j = 0; j < MAX_ROOMS; j++)
{
if (rooms[j])
{
if (rooms[j]->player1->uid == cli->uid)
{
if (rooms[j]->player2 != 0)
{
startgame = 1;
room_game = rooms[j];
break;
}
bzero(buffer, BUFFER_SZ);
sprintf(buffer, "[SERVER] e nescessario 2 players para poder iniciar a partida\n");
send_message(buffer, cli->uid);
break;
}
else if (rooms[j]->player2->uid == cli->uid)
{
bzero(buffer, BUFFER_SZ);
sprintf(buffer, "[SERVER] somente o dono da sala pode iniciar\n");
send_message(buffer, cli->uid);
break;
}
}
}
pthread_mutex_unlock(&rooms_mutex);
if (startgame == 1) {
room_game->game = (game_t *)malloc(sizeof(game_t));
room_game->game->estadoDeJogo = 1;
room_game->game->rodada = 0;
room_game->game->turnoDoJogador = room_game->player1->uid;
strcpy(room_game->state, "playing now");
for (int linha = 0; linha < 3; linha++)
{
for (int coluna = 0; coluna < 3; coluna++)
{
room_game->game->tabuleiro[linha][coluna] = '-';
}
}
sleep(1);
bzero(buffer, BUFFER_SZ);
sprintf(buffer, "start game\n");
send_message(buffer, room_game->player1->uid);
sleep(0.1);
bzero(buffer, BUFFER_SZ);
sprintf(buffer, "start game2\n");
send_message(buffer, room_game->player2->uid);
sleep(1);
bzero(buffer, BUFFER_SZ);
sprintf(buffer, "%s\n", room_game->player2->name);
send_message(buffer, room_game->player1->uid);
sleep(0.1);
bzero(buffer, BUFFER_SZ);
sprintf(buffer, "%s\n", room_game->player1->name);
send_message(buffer, room_game->player2->uid);
sleep(1);
bzero(buffer, BUFFER_SZ);
sprintf(buffer, "vez1\n");
send_message(buffer, room_game->player1->uid);
sleep(0.2);
bzero(buffer, BUFFER_SZ);
sprintf(buffer, "vez2\n");
send_message(buffer, room_game->player2->uid);
}
}
else if (strcmp(command, "play") == 0)
{
pthread_mutex_lock(&rooms_mutex);
for (int j = 0; j < MAX_ROOMS; j++)
{
if (rooms[j])
{
if (rooms[j]->player1->uid == cli->uid || rooms[j]->player2->uid == cli->uid)
{
if (rooms[j]->game->estadoDeJogo == 0)
{
bzero(buffer, BUFFER_SZ);
sprintf(buffer, "[SERVER] esta partida ja acabou\n");
send_message(buffer, cli->uid);
break;
}
setbuf(stdin, 0);
bzero(buffer, BUFFER_SZ);
sprintf(buffer, "%i", number);
int linhaJogada = posicoes[number - 1][0];
int colunaJogada = posicoes[number - 1][1];
if (rooms[j]->game->turnoDoJogador == rooms[j]->player1->uid)
{
send_message(buffer, rooms[j]->player2->uid);
rooms[j]->game->tabuleiro[linhaJogada][colunaJogada] = 'X';
rooms[j]->game->turnoDoJogador = rooms[j]->player2->uid;
}
else if (rooms[j]->game->turnoDoJogador == rooms[j]->player2->uid)
{
send_message(buffer, rooms[j]->player1->uid);
rooms[j]->game->tabuleiro[linhaJogada][colunaJogada] = 'O';
rooms[j]->game->turnoDoJogador = rooms[j]->player1->uid;
}
for (int iterator = 0; iterator < 3; iterator++)
{
if (
(
(rooms[j]->game->tabuleiro[iterator][0] == rooms[j]->game->tabuleiro[iterator][1]) && (rooms[j]->game->tabuleiro[iterator][1] == rooms[j]->game->tabuleiro[iterator][2]) && rooms[j]->game->tabuleiro[iterator][0] != '-'
)
||
(
(rooms[j]->game->tabuleiro[0][iterator] == rooms[j]->game->tabuleiro[1][iterator]) && (rooms[j]->game->tabuleiro[1][iterator] == rooms[j]->game->tabuleiro[2][iterator]) && rooms[j]->game->tabuleiro[0][iterator] != '-'
)
)
{
rooms[j]->game->estadoDeJogo = 0;
}
}
if (
(
(rooms[j]->game->tabuleiro[0][0] == rooms[j]->game->tabuleiro[1][1]) && (rooms[j]->game->tabuleiro[1][1] == rooms[j]->game->tabuleiro[2][2]) && rooms[j]->game->tabuleiro[0][0] != '-'
)
||
(
(rooms[j]->game->tabuleiro[0][2] == rooms[j]->game->tabuleiro[1][1]) && (rooms[j]->game->tabuleiro[1][1] == rooms[j]->game->tabuleiro[2][0]) && rooms[j]->game->tabuleiro[0][2] != '-'
)
)
{
rooms[j]->game->estadoDeJogo = 0;
}
sleep(1);
if (rooms[j]->game->estadoDeJogo == 0)
{
strcpy(rooms[j]->state, "waiting start");
if (rooms[j]->game->turnoDoJogador == rooms[j]->player1->uid)
{
bzero(buffer, BUFFER_SZ);
sprintf(buffer, "win1\n");
send_message(buffer, rooms[j]->player1->uid);
sleep(0.5);
send_message(buffer, rooms[j]->player2->uid);
}
else if (rooms[j]->game->turnoDoJogador == rooms[j]->player2->uid)
{
bzero(buffer, BUFFER_SZ);
sprintf(buffer, "win2\n");
send_message(buffer, rooms[j]->player1->uid);
sleep(0.5);
send_message(buffer, rooms[j]->player2->uid);
}
bzero(buffer, BUFFER_SZ);
sprintf(buffer, "ok");
send_message(buffer, rooms[j]->player1->uid);
sleep(0.5);
send_message(buffer, rooms[j]->player2->uid);
break;
}
if (rooms[j]->game->turnoDoJogador == rooms[j]->player1->uid)
{
bzero(buffer, BUFFER_SZ);
sprintf(buffer, "vez1\n");
send_message(buffer, rooms[j]->player1->uid);
sleep(0.5);
bzero(buffer, BUFFER_SZ);
sprintf(buffer, "vez2\n");
send_message(buffer, rooms[j]->player2->uid);
}
else if (rooms[j]->game->turnoDoJogador == rooms[j]->player2->uid)
{
bzero(buffer, BUFFER_SZ);
sprintf(buffer, "vez2\n");
send_message(buffer, rooms[j]->player1->uid);
sleep(0.5);
bzero(buffer, BUFFER_SZ);
sprintf(buffer, "vez1\n");
send_message(buffer, rooms[j]->player2->uid);
}
rooms[j]->game->rodada++;
}
break;
}
}
pthread_mutex_unlock(&rooms_mutex);
}
}
}
else if (receive == 0 || strcmp(buffer, "exit") == 0)
{
sprintf(buffer, "%s has left\n", cli->name);
printf("%s", buffer);
// send_message(buffer, cli->uid);
leave_flag = 1;
}
else
{
printf("ERROR: -1\n");
leave_flag = 1;
}
bzero(buffer, BUFFER_SZ);
}
bzero(buffer, BUFFER_SZ);
sprintf(buffer, "bye");
send_message(buffer, cli->uid);
close(cli->sockfd);
queue_remove_client(cli->uid);
free(cli);
cli_count--;
pthread_detach(pthread_self());
return NULL;
}
int main(int argc, char **argv)
{
if (argc != 2) {
printf("Usage: %s <port>\n", argv[0]);
return EXIT_FAILURE;
}
char *ip = "127.0.0.1";
int port = atoi(argv[1]);
int option = 1;
int listenfd = 0, connfd = 0;
struct sockaddr_in serv_addr;
struct sockaddr_in cli_addr;
pthread_t tid;
// Socket settings
listenfd = socket(AF_INET, SOCK_STREAM, 0);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(ip);
serv_addr.sin_port = htons(port);
// Signals
signal(SIGPIPE, SIG_IGN);
if (setsockopt(listenfd, SOL_SOCKET, (SO_REUSEPORT | SO_REUSEADDR), (char*)&option, sizeof(option)) < 0) {
printf("ERROR: setsockopt\n");
return EXIT_FAILURE;
}
if (bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
printf("Error bind\n");
return EXIT_FAILURE;
}
// listen
if (listen(listenfd, 10) < 0) {
printf("ERROR: listen\n");
return EXIT_FAILURE;
}
flashScreen();
printf("############################################");
printf("\n# Tic-Tac-Toe Server running on port: %i #", port);
printf("\n############################################\n\n");
while(1) {
socklen_t clilen = sizeof(cli_addr);
connfd = accept(listenfd, (struct sockaddr*)&cli_addr, &clilen);
// check dor max clients
if ((cli_count + 1) == MAX_CLIENTS) {
printf("Maximun of clients are connected, Connection rejected");
close(connfd);
continue;
}
// clients settings
client_t *cli = (client_t *)malloc(sizeof(client_t));
cli->address = cli_addr;
cli->sockfd = connfd;
cli->uid = uid++;
// add client to queue
queue_add_client(cli);
pthread_create(&tid, NULL, &handle_client, (void*)cli);
// reduce CPU usage
sleep(1);
}
return EXIT_SUCCESS;
}