-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.c
174 lines (161 loc) · 8.21 KB
/
connection.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
#include "server.h"
void send_packet(response msg_type, int index) {
memset(clients[index].send, '\0', PACKET_SIZE);
switch (msg_type) {
case WELCOME: sprintf(clients[index].send, "WELCOME,%s", clients[index].client_id); break;
case START: sprintf(clients[index].send, "START,%d,%d", game->players, game->start_lives); break;
case PASS: sprintf(clients[index].send, "%s,PASS", clients[index].client_id); break;
case FAIL: sprintf(clients[index].send, "%s,FAIL", clients[index].client_id); break;
case ELIM: sprintf(clients[index].send, "%s,ELIM", clients[index].client_id); break;
case VICT: sprintf(clients[index].send, "VICT"); break;
case REJECT: sprintf(clients[index].send, "REJECT"); break;
case CANCEL: sprintf(clients[index].send, "CANCEL"); break;
default: fprintf(stderr, "Unexpected msg_type\n"); exit(EXIT_FAILURE);
}
}
void connection_listener() {
// Listen to connections while the game is waiting or playing
while (game->status == WAITING || game->status == PLAYING) {
struct sockaddr_in client;
socklen_t client_len = sizeof(client);
// Block until a connection is made or if it times out
struct timeval timeout = {0, (int) (1E6 / POLLING_RATE)};
fd_set client_fd_set;
FD_ZERO(&client_fd_set);
FD_SET(server_fd, &client_fd_set);
if(select(server_fd + 1, &client_fd_set, NULL, NULL, &timeout) == 0){
continue;
}
int client_fd = accept(server_fd, (struct sockaddr *) &client, &client_len);
if (client_fd < 0) {
fprintf(stderr, "Could not establish connection with new client\n");
continue;
}
switch (fork()) {
case -1: {
fprintf(stderr, "Fork failed\n");
exit(EXIT_FAILURE);
} case 0: {
char *buf = allocate_memory(PACKET_SIZE, sizeof(char));
// Try to read from the incoming client
int read = recv(client_fd, buf, PACKET_SIZE, 0);
if (read < 0) {
fprintf(stderr, "Client read failed\n");
free(buf);
close(client_fd);
exit(EXIT_FAILURE);
}
// If new connection is not an INIT packet ignore
if (strcmp(buf, "INIT") != 0) {
close(client_fd);
free(buf);
exit(EXIT_FAILURE);
}
// Reject connections whilst the server is playing
if (game->status == PLAYING) {
if (send(client_fd, "REJECT", strlen("REJECT"), 0) < 0) {
perror(__func__);
fprintf(stderr, "Failed to send packet (REJECT) to new client\n");
}
printf("New client connection rejected as game as started!\n");
close(client_fd);
free(buf);
exit(EXIT_FAILURE);
}
// Add the client to the global clients list
int index = add_client(client_fd);
// Loop that handles rec and send packets to the client
while (true) {
// Check if the client is the winner
if (game->status == FINISHED && clients[index].client_fd != -1) {
send_packet(VICT, index);
if (send(client_fd, clients[index].send, strlen(clients[index].send), 0) < 0) {
perror(__func__);
fprintf(stderr, "Failed to send packet (%s) to client %s\n", clients[index].send, clients[index].client_id);
}
break;
}
// Check if the client is eliminated
if (clients[index].client_fd == -1) {
send_packet(ELIM, index);
if (send(client_fd, clients[index].send, strlen(clients[index].send), 0) < 0) {
perror(__func__);
fprintf(stderr, "Failed to send packet (%s) to client %s\n", clients[index].send, clients[index].client_id);
}
break;
}
// Checks for when the game can't start
if (game->status == EXIT) {
send_packet(CANCEL, index);
if (send(client_fd, clients[index].send, strlen(clients[index].send), 0) < 0) {
perror(__func__);
fprintf(stderr, "Failed to send packet (%s) to client %s\n", clients[index].send, clients[index].client_id);
}
break;
}
// Checks for packets to send
if (clients[index].send[0] != '\0') {
if (send(client_fd, clients[index].send, strlen(clients[index].send), 0) < 0) {
perror(__func__);
fprintf(stderr, "Failed to send packet (%s) to client %s\n", clients[index].send, clients[index].client_id);
}
memset(clients[index].send, '\0', PACKET_SIZE);
}
// Set a timeout for recv so it is not constantly blocked
struct pollfd fd;
fd.fd = client_fd;
fd.events = POLLIN;
switch (poll(&fd, 1, (int) (1E3 / POLLING_RATE))) {
case -1: {
fprintf(stderr, "Poll failed\n");
break;
} case 0: {
continue;
}
}
memset(buf, '\0', PACKET_SIZE);
read = recv(client_fd, buf, PACKET_SIZE, 0);
if (read < 0) {
fprintf(stderr, "Failed to read from client %s\n", clients[index].client_id);
game->players--;
eliminate_client(index);
} else if (read == 0) { // Client disconnected
if (game->status == WAITING) {
disconnect_client(index);
break;
} else if (clients[index].client_fd != -1) {
printf("Client %s has left the game\n", clients[index].client_id);
game->players--;
eliminate_client(index);
break;
}
} else { // Received a packet
if (clients[index].rec[0] != '\0') { // Not expecting packet
if (++(clients[index].unexpected) == MAX_PACKET_OVERFLOW) {
printf("Client %s sent too many unexpected packets\n", clients[index].client_id);
game->players--;
eliminate_client(index);
// Send elimination packet instantly
if (send(client_fd, clients[index].send, strlen(clients[index].send), 0) < 0) {
perror(__func__);
fprintf(stderr, "Failed to send packet (%s) to client %s\n", clients[index].send, clients[index].client_id);
}
break;
} else {
printf("Client %s sent an unexcepted packet\n", clients[index].client_id);
}
while (clients[index].rec[0] != '\0') {
usleep((int) (1E6 / POLLING_RATE));
}
}
strcpy(clients[index].rec, buf);
}
usleep((int) (1E6 / POLLING_RATE));
}
close(client_fd);
free(buf);
exit(EXIT_SUCCESS);
}
}
}
}