ready to test

This commit is contained in:
2025-07-30 08:59:21 +01:00
parent a14f053132
commit 88fa2fa2a3
2 changed files with 17 additions and 9 deletions

View File

@@ -42,9 +42,11 @@ void *handle_connection(void *args) {
handle_args_t *handleArgs = args;
char buffer[1024] = {0};
int bytesReceived;
char *message[1024];
status_t status = CONNECTED;
char *message = malloc(sizeof(char)*1024);
char_array_t *data = char_array_create(1024);
send(handleArgs->client, WELCOME, sizeof(WELCOME), 0);
bytesReceived = recv(handleArgs->client, buffer, sizeof(buffer), 0);
char_array_append(data, buffer, bytesReceived);
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);
connections_remove(&handleArgs->connections, handleArgs->client);
free(handleArgs);
pthread_exit(NULL);
exit(3);
}
username = trim(username);
@@ -59,15 +62,16 @@ void *handle_connection(void *args) {
printf("{%d} Invalid username\n", handleArgs->connection);
connections_remove(&handleArgs->connections, handleArgs->client);
free(handleArgs);
pthread_exit(NULL);
exit(3);
}
pthread_mutex_lock(&mutex);
strcat(message, "* The room contains: ");
for (int i = 0; i < handleArgs->connections.size; i++) {
sprintf(message, "* The room contains: ");
for (int i = 0; i < handleArgs->connections.len; i++) {
strcat(message, handleArgs->connections.clients[i].username);
strcat(message, " ");
}
message[strlen(message)-1] = '\n';
strcat(message, "\n");
send(handleArgs->client, message, strlen(message), 0);
pthread_mutex_unlock(&mutex);
@@ -86,7 +90,7 @@ void *handle_connection(void *args) {
}
memset(buffer, 0, sizeof(buffer));
}
free(message);
free(args);
return NULL;
}
@@ -94,6 +98,7 @@ void *handle_connection(void *args) {
connections_t *connections_create(const int size) {
connections_t *connections = malloc(sizeof(connections_t));
connections->size = size;
connections->len = 0;
connections->clients = calloc(size, sizeof(client_connection_t));
return connections;
}
@@ -106,8 +111,8 @@ void connections_append(connections_t *connections, SOCKET client, char *usernam
exit(1);
}
pthread_mutex_lock(&mutex);
size_t new_size = connections->size + 1;
if (new_size > connections->size) {
size_t new_len = connections->len + 1;
if (new_len > connections->size) {
connections->size = connections->size+64;
client_connection_t *new_array = realloc(connections->clients, connections->size * sizeof(client_connection_t));
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].username = username;
connections->size = new_size;
connections->len = new_len;
printf("{%llu} Connection added\n", connections->size);
pthread_mutex_unlock(&mutex);

View File

@@ -27,7 +27,10 @@ typedef struct handleArgs {
connections_t connections;
} handle_args_t;
typedef enum Status {
CONNECTED,
IDENTIFIED
} status_t;
void *handle_connection(void *args);