ready to test
This commit is contained in:
21
server03.c
21
server03.c
@@ -42,9 +42,11 @@ void *handle_connection(void *args) {
|
|||||||
handle_args_t *handleArgs = args;
|
handle_args_t *handleArgs = args;
|
||||||
char buffer[1024] = {0};
|
char buffer[1024] = {0};
|
||||||
int bytesReceived;
|
int bytesReceived;
|
||||||
char *message[1024];
|
status_t status = CONNECTED;
|
||||||
|
char *message = malloc(sizeof(char)*1024);
|
||||||
char_array_t *data = char_array_create(1024);
|
char_array_t *data = char_array_create(1024);
|
||||||
send(handleArgs->client, WELCOME, sizeof(WELCOME), 0);
|
send(handleArgs->client, WELCOME, sizeof(WELCOME), 0);
|
||||||
|
|
||||||
bytesReceived = recv(handleArgs->client, buffer, sizeof(buffer), 0);
|
bytesReceived = recv(handleArgs->client, buffer, sizeof(buffer), 0);
|
||||||
char_array_append(data, buffer, bytesReceived);
|
char_array_append(data, buffer, bytesReceived);
|
||||||
char *username = char_array_get_until_char(data, '\n');
|
char *username = char_array_get_until_char(data, '\n');
|
||||||
@@ -52,6 +54,7 @@ void *handle_connection(void *args) {
|
|||||||
printf("{%d} Failed to parse username\n", handleArgs->connection);
|
printf("{%d} Failed to parse username\n", handleArgs->connection);
|
||||||
connections_remove(&handleArgs->connections, handleArgs->client);
|
connections_remove(&handleArgs->connections, handleArgs->client);
|
||||||
free(handleArgs);
|
free(handleArgs);
|
||||||
|
pthread_exit(NULL);
|
||||||
exit(3);
|
exit(3);
|
||||||
}
|
}
|
||||||
username = trim(username);
|
username = trim(username);
|
||||||
@@ -59,15 +62,16 @@ void *handle_connection(void *args) {
|
|||||||
printf("{%d} Invalid username\n", handleArgs->connection);
|
printf("{%d} Invalid username\n", handleArgs->connection);
|
||||||
connections_remove(&handleArgs->connections, handleArgs->client);
|
connections_remove(&handleArgs->connections, handleArgs->client);
|
||||||
free(handleArgs);
|
free(handleArgs);
|
||||||
|
pthread_exit(NULL);
|
||||||
exit(3);
|
exit(3);
|
||||||
}
|
}
|
||||||
pthread_mutex_lock(&mutex);
|
pthread_mutex_lock(&mutex);
|
||||||
strcat(message, "* The room contains: ");
|
sprintf(message, "* The room contains: ");
|
||||||
for (int i = 0; i < handleArgs->connections.size; i++) {
|
for (int i = 0; i < handleArgs->connections.len; i++) {
|
||||||
strcat(message, handleArgs->connections.clients[i].username);
|
strcat(message, handleArgs->connections.clients[i].username);
|
||||||
strcat(message, " ");
|
strcat(message, " ");
|
||||||
}
|
}
|
||||||
message[strlen(message)-1] = '\n';
|
strcat(message, "\n");
|
||||||
send(handleArgs->client, message, strlen(message), 0);
|
send(handleArgs->client, message, strlen(message), 0);
|
||||||
pthread_mutex_unlock(&mutex);
|
pthread_mutex_unlock(&mutex);
|
||||||
|
|
||||||
@@ -86,7 +90,7 @@ void *handle_connection(void *args) {
|
|||||||
}
|
}
|
||||||
memset(buffer, 0, sizeof(buffer));
|
memset(buffer, 0, sizeof(buffer));
|
||||||
}
|
}
|
||||||
|
free(message);
|
||||||
free(args);
|
free(args);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -94,6 +98,7 @@ void *handle_connection(void *args) {
|
|||||||
connections_t *connections_create(const int size) {
|
connections_t *connections_create(const int size) {
|
||||||
connections_t *connections = malloc(sizeof(connections_t));
|
connections_t *connections = malloc(sizeof(connections_t));
|
||||||
connections->size = size;
|
connections->size = size;
|
||||||
|
connections->len = 0;
|
||||||
connections->clients = calloc(size, sizeof(client_connection_t));
|
connections->clients = calloc(size, sizeof(client_connection_t));
|
||||||
return connections;
|
return connections;
|
||||||
}
|
}
|
||||||
@@ -106,8 +111,8 @@ void connections_append(connections_t *connections, SOCKET client, char *usernam
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
pthread_mutex_lock(&mutex);
|
pthread_mutex_lock(&mutex);
|
||||||
size_t new_size = connections->size + 1;
|
size_t new_len = connections->len + 1;
|
||||||
if (new_size > connections->size) {
|
if (new_len > connections->size) {
|
||||||
connections->size = connections->size+64;
|
connections->size = connections->size+64;
|
||||||
client_connection_t *new_array = realloc(connections->clients, connections->size * sizeof(client_connection_t));
|
client_connection_t *new_array = realloc(connections->clients, connections->size * sizeof(client_connection_t));
|
||||||
connections->clients = new_array;
|
connections->clients = new_array;
|
||||||
@@ -117,7 +122,7 @@ void connections_append(connections_t *connections, SOCKET client, char *usernam
|
|||||||
}
|
}
|
||||||
connections->clients[connections->size].client = client;
|
connections->clients[connections->size].client = client;
|
||||||
connections->clients[connections->size].username = username;
|
connections->clients[connections->size].username = username;
|
||||||
connections->size = new_size;
|
connections->len = new_len;
|
||||||
printf("{%llu} Connection added\n", connections->size);
|
printf("{%llu} Connection added\n", connections->size);
|
||||||
pthread_mutex_unlock(&mutex);
|
pthread_mutex_unlock(&mutex);
|
||||||
|
|
||||||
|
|||||||
@@ -27,7 +27,10 @@ typedef struct handleArgs {
|
|||||||
connections_t connections;
|
connections_t connections;
|
||||||
} handle_args_t;
|
} handle_args_t;
|
||||||
|
|
||||||
|
typedef enum Status {
|
||||||
|
CONNECTED,
|
||||||
|
IDENTIFIED
|
||||||
|
} status_t;
|
||||||
|
|
||||||
void *handle_connection(void *args);
|
void *handle_connection(void *args);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user