-
Notifications
You must be signed in to change notification settings - Fork 2
/
SERVER.C
371 lines (288 loc) · 9.27 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
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int player_count = 0;
pthread_mutex_t mutexcount;
void error(const char *msg)
{
perror(msg);
pthread_exit(NULL);
}
void write_client_int(int cli_sockfd, int msg)
{
int n = write(cli_sockfd, &msg, sizeof(int));
if (n < 0)
error("ERROR writing int to client socket");
}
void write_clients_msg(int * cli_sockfd, char * msg)
{
write_client_msg(cli_sockfd[0], msg);
write_client_msg(cli_sockfd[1], msg);
}
void write_clients_int(int * cli_sockfd, int msg)
{
write_client_int(cli_sockfd[0], msg);
write_client_int(cli_sockfd[1], msg);
}
int setup_listener(int portno)
{
int sockfd;
struct sockaddr_in serv_addr;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening listener socket.");
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
error("ERROR binding listener socket.");
#ifdef DEBUG
printf("[DEBUG] Listener set.\n");
#endif
return sockfd;
}
int recv_int(int cli_sockfd)
{
int msg = 0;
int n = read(cli_sockfd, &msg, sizeof(int));
if (n < 0 || n != sizeof(int)) return -1;
printf("[DEBUG] Received int: %d\n", msg);
return msg;
}
void write_client_msg(int cli_sockfd, char * msg)
{
int n = write(cli_sockfd, msg, strlen(msg));
if (n < 0)
error("ERROR writing msg to client socket");
}
void get_clients(int lis_sockfd, int * cli_sockfd)
{
socklen_t clilen;
struct sockaddr_in serv_addr, cli_addr;
#ifdef DEBUG
printf("[DEBUG] Listening for clients...\n");
#endif
int num_conn = 0;
while(num_conn < 2)
{
listen(lis_sockfd, 253 - player_count);
memset(&cli_addr, 0, sizeof(cli_addr));
clilen = sizeof(cli_addr);
cli_sockfd[num_conn] = accept(lis_sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (cli_sockfd[num_conn] < 0)
error("ERROR accepting a connection from a client.");
#ifdef DEBUG
printf("[DEBUG] Accepted connection from client %d\n", num_conn);
#endif
write(cli_sockfd[num_conn], &num_conn, sizeof(int));
#ifdef DEBUG
printf("[DEBUG] Sent client %d it's ID.\n", num_conn);
#endif
pthread_mutex_lock(&mutexcount);
player_count++;
printf("Number of players is now %d.\n", player_count);
pthread_mutex_unlock(&mutexcount);
if (num_conn == 0) {
write_client_msg(cli_sockfd[0],"HLD");
#ifdef DEBUG
printf("[DEBUG] Told client 0 to hold.\n");
#endif
}
num_conn++;
}
}
int get_player_move(int cli_sockfd)
{
#ifdef DEBUG
printf("[DEBUG] Getting player move...\n");
#endif
write_client_msg(cli_sockfd, "TRN");
return recv_int(cli_sockfd);
}
int check_move(char board[][3], int move, int player_id)
{
if ((move == 9) || (board[move/3][move%3] == ' ')) {
#ifdef DEBUG
printf("[DEBUG] Player %d's move was valid.\n", player_id);
#endif
return 1;
}
else { #ifdef DEBUG
printf("[DEBUG] Player %d's move was invalid.\n", player_id);
#endif
return 0;
}
}
void update_board(char board[][3], int move, int player_id)
{
board[move/3][move%3] = player_id ? 'X' : 'O';
#ifdef DEBUG
printf("[DEBUG] Board updated.\n");
#endif
}
void draw_board(char board[][3])
{
printf(" %c | %c | %c \n", board[0][0], board[0][1], board[0][2]);
printf("-----------\n");
printf(" %c | %c | %c \n", board[1][0], board[1][1], board[1][2]);
printf("-----------\n");
printf(" %c | %c | %c \n", board[2][0], board[2][1], board[2][2]);
}
void send_update(int * cli_sockfd, int move, int player_id)
{
#ifdef DEBUG
printf("[DEBUG] Sending update...\n");
#endif
write_clients_msg(cli_sockfd, "UPD");
write_clients_int(cli_sockfd, player_id);
write_clients_int(cli_sockfd, move);
#ifdef DEBUG
printf("[DEBUG] Update sent.\n");
#endif
}
void send_player_count(int cli_sockfd)
{
write_client_msg(cli_sockfd, "CNT");
write_client_int(cli_sockfd, player_count);
#ifdef DEBUG
printf("[DEBUG] Player Count Sent.\n");
#endif
}
int check_board(char board[][3], int last_move)
{
#ifdef DEBUG
printf("[DEBUG] Checking for a winner...\n");
#endif
int row = last_move/3;
int col = last_move%3;
if ( board[row][0] == board[row][1] && board[row][1] == board[row][2] ) {
#ifdef DEBUG
printf("[DEBUG] Win by row %d.\n", row);
#endif
return 1;
}
else if ( board[0][col] == board[1][col] && board[1][col] == board[2][col] ) {
#ifdef DEBUG
printf("[DEBUG] Win by column %d.\n", col);
#endif
return 1;
}
else if (!(last_move % 2)) { if ( (last_move == 0 || last_move == 4 || last_move == 8) && (board[1][1] == board[0][0] && board[1][1] == board[2][2]) ) { #ifdef DEBUG
printf("[DEBUG] Win by backslash diagonal.\n");
#endif
return 1;
}
if ( (last_move == 2 || last_move == 4 || last_move == 6) && (board[1][1] == board[0][2] && board[1][1] == board[2][0]) ) { #ifdef DEBUG
printf("[DEBUG] Win by frontslash diagonal.\n");
#endif
return 1;
}
}
#ifdef DEBUG
printf("[DEBUG] No winner, yet.\n");
#endif
return 0;
}
void *run_game(void *thread_data)
{
int *cli_sockfd = (int*)thread_data;
char board[3][3] = { {' ', ' ', ' '},
{' ', ' ', ' '},
{' ', ' ', ' '} };
printf("Game on!\n");
write_clients_msg(cli_sockfd, "SRT");
#ifdef DEBUG
printf("[DEBUG] Sent start message.\n");
#endif
draw_board(board);
int prev_player_turn = 1;
int player_turn = 0;
int game_over = 0;
int turn_count = 0;
while(!game_over) {
if (prev_player_turn != player_turn)
write_client_msg(cli_sockfd[(player_turn + 1) % 2], "WAT");
int valid = 0;
int move = 0;
while(!valid) { move = get_player_move(cli_sockfd[player_turn]);
if (move == -1) break;
printf("Player %d played position %d\n", player_turn, move);
valid = check_move(board, move, player_turn);
if (!valid) {
printf("Move was invalid. Let's try this again...\n");
write_client_msg(cli_sockfd[player_turn], "INV");
}
}
if (move == -1) {
printf("Player disconnected.\n");
break;
}
else if (move == 9) {
prev_player_turn = player_turn;
send_player_count(cli_sockfd[player_turn]);
}
else {
update_board(board, move, player_turn);
send_update( cli_sockfd, move, player_turn );
draw_board(board);
game_over = check_board(board, move);
if (game_over == 1) {
write_client_msg(cli_sockfd[player_turn], "WIN");
write_client_msg(cli_sockfd[(player_turn + 1) % 2], "LSE");
printf("Player %d won.\n", player_turn);
}
else if (turn_count == 8) { printf("Draw.\n");
write_clients_msg(cli_sockfd, "DRW");
game_over = 1;
}
prev_player_turn = player_turn;
player_turn = (player_turn + 1) % 2;
turn_count++;
}
}
printf("Game over.\n");
close(cli_sockfd[0]);
close(cli_sockfd[1]);
pthread_mutex_lock(&mutexcount);
player_count--;
printf("Number of players is now %d.", player_count);
player_count--;
printf("Number of players is now %d.", player_count);
pthread_mutex_unlock(&mutexcount);
free(cli_sockfd);
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
int lis_sockfd = setup_listener(atoi(argv[1]));
pthread_mutex_init(&mutexcount, NULL);
while (1) {
if (player_count <= 252) {
int *cli_sockfd = (int*)malloc(2*sizeof(int));
memset(cli_sockfd, 0, 2*sizeof(int));
get_clients(lis_sockfd, cli_sockfd);
#ifdef DEBUG
printf("[DEBUG] Starting new game thread...\n");
#endif
pthread_t thread;int result = pthread_create(&thread, NULL, run_game, (void *)cli_sockfd);
if (result){
printf("Thread creation failed with return code %d\n", result);
exit(-1);
}
printf("[DEBUG] New game thread started.\n");
}
}
close(lis_sockfd);
pthread_mutex_destroy(&mutexcount);
pthread_exit(NULL);
}