-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
473 lines (378 loc) · 15 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
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <pthread.h>
#include <strings.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdbool.h>
#include <signal.h>
size_t MAX_CONNECTIONS = 5;
size_t LISTEN_THREADS = 5;
bool debug = true;
pthread_t* recv_threads = NULL;
pthread_t* send_threads = NULL;
typedef struct _server_state{
int server_socket_fd; // File descriptor for servers socket
int* client_socket_fds; // List of file descriptors for client connection
char** client_names; // List of names for clients
size_t current_connections; // Current number of connections
char* message; // message to transmit
int originator; // Client who sent message
int messages_sent; // number of messages sent
bool* has_sent; // Array to keep track of messages sent
bool canceled;
pthread_mutex_t msg_lock; // lock to prevent multiple messages being written
pthread_cond_t msg_cond;
pthread_cond_t all_sent;
} server_state;
server_state* s = NULL;
typedef struct _sender_state{
server_state* s_state;
int connection_handled;
} sender_state;
void end_connection(int client_fd){
char zero = 0;
write(client_fd, &zero, 1);
close(client_fd);
// If client cannot connect, sends null message and closes connection
}
void initialize_server_state(server_state* s, int socketfd){
s ->server_socket_fd = socketfd;
int* connection_socket_fds = malloc(sizeof(int)*MAX_CONNECTIONS);
bzero(connection_socket_fds, MAX_CONNECTIONS*sizeof(int));
s ->client_socket_fds = connection_socket_fds;
s ->current_connections = 0;
char** client_names = malloc(MAX_CONNECTIONS*sizeof(char*));
bzero(client_names, MAX_CONNECTIONS*sizeof(char*));
s ->client_names = client_names;
s ->message = NULL;
s ->originator = -1;
s ->messages_sent = 0;
pthread_mutex_t lock;
pthread_mutex_init(&lock, NULL);
pthread_cond_t cond;
pthread_cond_init(&cond, NULL);
pthread_cond_t all_sent;
pthread_cond_init(&all_sent, NULL);
s ->msg_lock = lock;
s ->msg_cond = cond;
s ->all_sent = all_sent;
bool* block = malloc(sizeof(bool)*MAX_CONNECTIONS);
bzero(block, sizeof(bool)*MAX_CONNECTIONS);
s ->has_sent = block;
s -> canceled = false;
}
void destroy_server_state(server_state* s){
printf("Destroying state\n");
if(s -> client_names != NULL){
for(int i = 0; i<MAX_CONNECTIONS; i++){
if(s -> client_names[i] != NULL){
end_connection(s -> client_socket_fds[i]);
free(s -> client_names[i]);
s -> client_names[i] = NULL;
}
}
}
s -> canceled = true;
free(s -> client_names);
s -> client_names = NULL;
free(s -> has_sent);
s -> has_sent = NULL;
free(s -> client_socket_fds);
s -> client_socket_fds = NULL;
pthread_mutex_unlock(&s -> msg_lock);
pthread_mutex_destroy(&s -> msg_lock);
pthread_cond_broadcast(&s -> msg_cond);
pthread_cond_destroy(&s -> msg_cond);
pthread_cond_broadcast(&s -> all_sent);
pthread_cond_destroy(&s -> all_sent);
printf("Destroyed state\n");
}
void interrupt(int signum){
if(s != NULL){
s -> canceled = true;
}
if(recv_threads != NULL){
for(int i = 0; i < LISTEN_THREADS; i++){
pthread_cancel(recv_threads[i]);
}
}
if(send_threads != NULL){
for(int i = 0; i < MAX_CONNECTIONS; i++){
pthread_cancel(send_threads[i]);
}
}
if(s != NULL){
destroy_server_state(s);
}
}
void* handle_connection(void* server_state_ptr){
// pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
server_state* state = (server_state*)server_state_ptr;
int client_fd = accept(state -> server_socket_fd, NULL, NULL);
bool new_connection = true;
while(client_fd != -1){
if(debug) printf("Client connected\n");
if(new_connection && state->current_connections == MAX_CONNECTIONS){
end_connection(client_fd);
client_fd = accept(state -> server_socket_fd, NULL, NULL);
continue;
// If client cannot connect, sends null message and closes connection
}
if(state -> canceled){
end_connection(client_fd);
break;
}
new_connection = false;
int msg_len;
if(debug) printf("Reading from fd\n");
int init_read = read(client_fd, (void*)&msg_len, 4);
if(init_read != 4){
end_connection(client_fd);
state -> current_connections --;
new_connection = true;
client_fd = accept(state -> server_socket_fd, NULL, NULL);
continue;
}
msg_len = ntohs(msg_len);
if(debug) printf("Message is %d bytes long\n", msg_len);
char* msg = malloc(msg_len);
size_t recv_read = read(client_fd, msg, msg_len);
if(recv_read != msg_len){
free(msg);
close(client_fd);
state -> current_connections --;
new_connection = true;
client_fd = accept(state -> server_socket_fd, NULL, NULL);
continue;
}
// First byte will be n if it is name connection
if(msg[0] == 'n'){
char* name = malloc(msg_len-1);
printf("Name len is %d\n", msg_len - 1);
printf("Message is %s\n", msg);
memcpy(name, msg+1, msg_len - 1);
if(debug) printf("Name of client is %s\n", name);
bool found_spot = false;
int client_id = -1;
for(int i = 0; i < MAX_CONNECTIONS; i++){
if(state->client_names[i] == NULL){
client_id = i;
state->client_names[i] = name;
state->client_socket_fds[i] = client_fd;
found_spot = true;
break;
}
}
printf("%s's client id is %d\n", name, client_id);
if(!found_spot){
free(name);
free(msg);
end_connection(client_fd);
continue;
}
char resp[8];
*((int*)resp) = htons(4);
*((int*)resp + 1) = htons(client_id);
if(debug) printf("Sending response\n");
state -> current_connections ++;
write(client_fd, resp, 8);
if(debug) printf("Send response\n");
char* template_str = "Client has connected.";
char* send_str = malloc(strlen(template_str) + strlen(name) + 1);
sprintf(send_str, "Client %s has connected.", name);
pthread_mutex_lock(&state ->msg_lock);
while(state -> message != NULL){
pthread_cond_wait(&state -> all_sent, &state -> msg_lock);
}
// Send message like Client [name] has connected.
printf("Notifying that %s has connected.\n", name);
state -> message = send_str;
state -> originator = client_id;
pthread_cond_broadcast(&state -> msg_cond);
pthread_mutex_unlock(&state -> msg_lock);
} else if(strncmp(msg, "c", 1) == 0){ // Close connection
int client_id;
client_id = ntohs(*(int*)(msg + 1));
char* client_name = state -> client_names[client_id];
printf("Client %d closes\n", client_id);
char* template = "Client has disconnected.";
char* discon_message = malloc(strlen(client_name) + strlen(template) + 1);
sprintf(discon_message, "Client %s has disconnected.", client_name);
printf("Created discon str: %s\n", discon_message);
if(state->client_names[client_id] != NULL){
free(state->client_names[client_id]);
}
state -> client_names[client_id] = NULL;
state->client_socket_fds[client_id] = 0;
state -> current_connections --;
close(client_fd);
printf("Freed data\n");
pthread_mutex_lock(&state -> msg_lock);
while(state -> message != NULL){
pthread_cond_wait(&state -> all_sent, &state -> msg_lock);
}
printf("Setting message: %s from %d\n", discon_message, client_id);
state -> message = discon_message;
state -> originator = client_id;
pthread_cond_broadcast(&state -> msg_cond);
pthread_mutex_unlock(&state -> msg_lock);
client_fd = accept(state -> server_socket_fd, NULL, NULL);
} else if(msg[0] == 'm'){ // Recieving Message, first byte is client id
int client_id;
printf("Determining client id\n");
client_id = ntohs(*((int*)(msg + 1)));
char* client_name = state->client_names[client_id];
printf("Client id is %d, name is %s\n", client_id, client_name);
char* client_message = malloc(msg_len - 5);
strcpy(client_message, msg + 5);
printf("Recieved message from %s: %s\n", client_name, client_message);
pthread_mutex_lock(&state -> msg_lock);
while(state -> message != NULL){
pthread_cond_wait(&state -> all_sent, &state -> msg_lock);
}
state -> message = client_message;
state -> originator = client_id;
pthread_cond_broadcast(&state -> msg_cond);
pthread_mutex_unlock(&state -> msg_lock);
} else{
printf("%s is invalid\n", msg);
}
free(msg);
}
pthread_exit(NULL);
}
void* send_message(void* sender_state_ptr){
//pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
sender_state* send_state = sender_state_ptr;
int client_responsible_id = send_state -> connection_handled;
while(1){
pthread_mutex_lock(&send_state -> s_state -> msg_lock);
while(
!send_state -> s_state -> canceled &&
send_state -> s_state -> client_names[client_responsible_id] == NULL ||
send_state -> s_state -> message == NULL ||
send_state -> s_state -> has_sent[client_responsible_id] ||
send_state -> s_state -> originator == client_responsible_id
){
pthread_cond_wait(&send_state -> s_state ->msg_cond, &send_state -> s_state -> msg_lock);
if(
send_state -> s_state -> current_connections == 0 ||
(send_state -> s_state -> client_names[client_responsible_id] != NULL &&
send_state -> s_state -> originator == client_responsible_id &&
send_state -> s_state -> current_connections == 1)
){
free(send_state -> s_state -> message);
send_state -> s_state -> message = NULL;
}
}
if(send_state -> s_state -> canceled){
pthread_mutex_unlock(&send_state -> s_state -> msg_lock);
break;
}
send_state -> s_state ->messages_sent ++;
send_state -> s_state -> has_sent[client_responsible_id] = true;
pthread_mutex_unlock(&send_state->s_state->msg_lock);
int client_socket_fd = send_state -> s_state -> client_socket_fds[client_responsible_id];
char* client_name = send_state -> s_state -> client_names[send_state -> s_state -> originator];
if(client_name == NULL){
client_name = "Server";
}
char* message = send_state -> s_state -> message;
//Format like [name] says: [message]
size_t message_len = strlen(client_name) + strlen(": ") + strlen(message) + 1;
char* send_message = malloc(4 + message_len);
*((int*)send_message) = htons(message_len);
sprintf(send_message+4, "%s: %s", client_name, message);
printf("Sending message %s from %s to client number %d\n", send_message + 4, client_name, client_responsible_id);
write(client_socket_fd, send_message, 4 + message_len);
printf("Successfully sent message #%d\n", send_state -> s_state -> messages_sent);
pthread_mutex_lock(&send_state -> s_state -> msg_lock);
printf("Determining if message to %d is last message\n", client_responsible_id);
if(
send_state -> s_state -> messages_sent >= send_state -> s_state -> current_connections - 1
){
printf("Message to %d is the last one, deleting data\n", client_responsible_id);
free(send_state -> s_state -> message);
send_state -> s_state -> message = NULL;
send_state -> s_state -> originator = -1;
send_state -> s_state -> messages_sent = 0;
bzero(send_state -> s_state -> has_sent, sizeof(bool)*MAX_CONNECTIONS);
pthread_cond_broadcast(&send_state -> s_state -> all_sent);
}
pthread_mutex_unlock(&send_state -> s_state -> msg_lock);
free(send_message);
}
pthread_exit(NULL);
}
int main(int argc, char** argv){
signal(SIGINT, interrupt);
int socketfd = socket(AF_INET, SOCK_STREAM, 0);
struct in_addr local_addr;
int c;
char* local_ip = "127.0.0.1";
int local_port = 65535;
while((c = getopt(argc, argv, "h:p:")) != -1){
switch(c){
case 'h':
if(optarg != NULL){
local_ip = optarg;
}
break;
case 'p':
if(optarg != NULL){
local_port = atoi(optarg);
}
break;
}
}
printf("IP %s port %d\n", local_ip, local_port);
int ip_cast = inet_aton(local_ip, &local_addr);
if(ip_cast == 0){
perror("ip failed to translate");
return 1;
}
struct sockaddr_in sock_bind;
sock_bind.sin_family = AF_INET;
sock_bind.sin_port = local_port;
sock_bind.sin_addr = local_addr;
int bindstatus = bind(socketfd, (const struct sockaddr*)&sock_bind, sizeof(struct sockaddr_in));
if(bindstatus == -1){
perror("bind failed");
}
int listenstatus = listen(socketfd, MAX_CONNECTIONS);
if(listenstatus == -1){
perror("socket listed failed");
}
server_state state;
initialize_server_state(&state, socketfd);
s = &state;
pthread_t threads[LISTEN_THREADS];
recv_threads = threads;
for(int i = 0; i < LISTEN_THREADS; i++){
pthread_t thread;
pthread_create(&thread, NULL, &handle_connection, (void*)&state);
threads[i] = thread;
}
sender_state sender_states[MAX_CONNECTIONS];
for(int i = 0; i < MAX_CONNECTIONS; i++){
sender_state s;
s.s_state = &state;
s.connection_handled = i;
sender_states[i] = s;
}
pthread_t sender_threads[MAX_CONNECTIONS];
send_threads = sender_threads;
for(int i = 0; i < MAX_CONNECTIONS; i++){
pthread_t thread;
pthread_create(&thread, NULL, &send_message, (void*)&sender_states[i]);
sender_threads[i] = thread;
}
pthread_join(threads[0], NULL);
pthread_join(recv_threads[0], NULL);
return 0;
}