ready to test
This commit is contained in:
108
server03.c
108
server03.c
@@ -30,7 +30,7 @@ int main() {
|
||||
handle_args_t *args = malloc(sizeof(handle_args_t));
|
||||
args->client = client;
|
||||
args->connection = connection_number++;
|
||||
args->connections = *connections;
|
||||
args->connections = connections;
|
||||
pthread_t thread;
|
||||
pthread_create(&thread, nullptr, handle_connection, args);
|
||||
}
|
||||
@@ -46,50 +46,67 @@ void *handle_connection(void *args) {
|
||||
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');
|
||||
if (username == NULL) {
|
||||
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);
|
||||
if (!username_is_valid(username)) {
|
||||
printf("{%d} Invalid username\n", handleArgs->connection);
|
||||
connections_remove(&handleArgs->connections, handleArgs->client);
|
||||
free(handleArgs);
|
||||
pthread_exit(NULL);
|
||||
exit(3);
|
||||
}
|
||||
pthread_mutex_lock(&mutex);
|
||||
sprintf(message, "* The room contains: ");
|
||||
for (int i = 0; i < handleArgs->connections.len; i++) {
|
||||
strcat(message, handleArgs->connections.clients[i].username);
|
||||
strcat(message, " ");
|
||||
}
|
||||
strcat(message, "\n");
|
||||
send(handleArgs->client, message, strlen(message), 0);
|
||||
pthread_mutex_unlock(&mutex);
|
||||
|
||||
connections_append(&handleArgs->connections, handleArgs->client, username);
|
||||
sprintf(message, "* %s has entered the room\n", username);
|
||||
broadcast(&handleArgs->connections, &handleArgs->client, message);
|
||||
|
||||
printf("{%d} Username: %s\n", handleArgs->connection, username);
|
||||
|
||||
char *username = malloc(sizeof(char)*1024);
|
||||
while ((bytesReceived = recv(handleArgs->client, buffer, sizeof(buffer), 0)) > 0) {
|
||||
printf("{%d} Client sent: |%d| \n", handleArgs->connection, bytesReceived);
|
||||
char_array_append(data, buffer, bytesReceived);
|
||||
char *request;
|
||||
while ((request = char_array_get_until_char(data, '\n')) != NULL) {
|
||||
sprintf(message, "[%s] %s\n", username, request);
|
||||
switch (status) {
|
||||
case CONNECTED: {
|
||||
if ((username = char_array_get_until_char(data, '\n')) != NULL) {
|
||||
printf("{%d} Username raw: %s\n", handleArgs->connection, username);
|
||||
if (username == NULL) {
|
||||
printf("{%d} Failed to parse username\n", handleArgs->connection);
|
||||
closesocket(handleArgs->client);
|
||||
free(handleArgs);
|
||||
pthread_exit(NULL);
|
||||
exit(3);
|
||||
}
|
||||
username = trim(username);
|
||||
printf("{%d} Username after trim: %s\n", handleArgs->connection, username);
|
||||
if (!username_is_valid(username)) {
|
||||
printf("{%d} Invalid username\n", handleArgs->connection);
|
||||
closesocket(handleArgs->client);
|
||||
free(handleArgs);
|
||||
pthread_exit(NULL);
|
||||
exit(3);
|
||||
}
|
||||
pthread_mutex_lock(&mutex);
|
||||
sprintf(message, "* The room contains: ");
|
||||
for (int i = 0; i < handleArgs->connections->len; i++) {
|
||||
strcat(message, handleArgs->connections->clients[i].username);
|
||||
strcat(message, " ");
|
||||
}
|
||||
strcat(message, "\n");
|
||||
send(handleArgs->client, message, strlen(message), 0);
|
||||
pthread_mutex_unlock(&mutex);
|
||||
|
||||
connections_append(handleArgs->connections, handleArgs->client, username);
|
||||
sprintf(message, "* %s has entered the room\n", username);
|
||||
broadcast(handleArgs->connections, &handleArgs->client, message);
|
||||
|
||||
printf("{%d} Username: %s\n", handleArgs->connection, username);
|
||||
status = IDENTIFIED;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case IDENTIFIED: {
|
||||
char *request;
|
||||
while ((request = char_array_get_until_char(data, '\n')) != NULL) {
|
||||
sprintf(message, "[%s] %s\n", username, request);
|
||||
broadcast(handleArgs->connections, &handleArgs->client, message);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
memset(buffer, 0, sizeof(buffer));
|
||||
}
|
||||
sprintf(message, "* %s has left the room\n", username);
|
||||
broadcast(handleArgs->connections, &handleArgs->client, message);
|
||||
connections_remove(handleArgs->connections, handleArgs->client);
|
||||
printf("{%d} Client disconnected\n", handleArgs->connection);
|
||||
|
||||
free(message);
|
||||
free(args);
|
||||
return NULL;
|
||||
@@ -120,8 +137,8 @@ void connections_append(connections_t *connections, SOCKET client, char *usernam
|
||||
printf("Failed to allocate memory for array\n");
|
||||
}
|
||||
}
|
||||
connections->clients[connections->size].client = client;
|
||||
connections->clients[connections->size].username = username;
|
||||
connections->clients[connections->len].client = client;
|
||||
connections->clients[connections->len].username = username;
|
||||
connections->len = new_len;
|
||||
printf("{%llu} Connection added\n", connections->size);
|
||||
pthread_mutex_unlock(&mutex);
|
||||
@@ -129,7 +146,7 @@ void connections_append(connections_t *connections, SOCKET client, char *usernam
|
||||
|
||||
};
|
||||
void connections_remove(connections_t *connections, SOCKET client) {
|
||||
if (connections == NULL) {
|
||||
if (connections == NULL || connections->clients == NULL) {
|
||||
exit(1);
|
||||
}
|
||||
pthread_mutex_lock(&mutex);
|
||||
@@ -141,15 +158,20 @@ void connections_remove(connections_t *connections, SOCKET client) {
|
||||
for (int j = i; j < connections->size; j++) {
|
||||
connections->clients[j] = connections->clients[j+1];
|
||||
}
|
||||
connections->size--;
|
||||
connections->len--;
|
||||
printf("{%llu} Connection removed\n", connections->size);
|
||||
pthread_mutex_unlock(&mutex);
|
||||
return;
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&mutex);
|
||||
printf("{%llu} Connection not found\n", connections->size);
|
||||
}
|
||||
|
||||
void broadcast(const connections_t *connections, const SOCKET *source, const char *message) {
|
||||
if (connections == NULL || connections->clients == NULL) {
|
||||
exit(1);
|
||||
}
|
||||
pthread_mutex_lock(&mutex);
|
||||
for (size_t i = 0; i < connections->size; i++) {
|
||||
if (connections->clients[i].client != *source) {
|
||||
|
||||
Reference in New Issue
Block a user