From ddbe89907111cae59dee06cc9069e5c45b1aa058 Mon Sep 17 00:00:00 2001 From: ENGO150 Date: Sun, 21 Jan 2024 13:44:31 +0100 Subject: [PATCH] added message thread cleanup for server also swaped the function names cause I am so smart --- include/chat/misc.h | 3 ++- src/chat/main/server.c | 5 ++++- src/chat/misc.c | 34 +++++++++++++++++++++++++++------- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/include/chat/misc.h b/include/chat/misc.h index 491d172..8d673cd 100644 --- a/include/chat/misc.h +++ b/include/chat/misc.h @@ -25,7 +25,8 @@ void why2_send_socket(char *text, char *username, int socket); //send socket.... char *why2_read_socket(int socket); //read lol void *why2_communicate_thread(void *arg); //COMMUNICATION THREAD void *why2_accept_thread(void *socket); //LOOP ACCEPTING CONNECTIONS -void why2_clean_threads(void); //CLOSE EVERY RUNNING THREAD +void why2_clean_connections(void); //CLOSE EVERY CONNECTION +void why2_clean_threads(void); //CLOSE EVERY RUNNING MESSAGE THREAD void *why2_listen_server(void *socket); //LISTEN FOR OTHER's USERS MESSAGES void *why2_getline_thread(WHY2_UNUSED void* arg); //START getline IN SEPARATE THREAD diff --git a/src/chat/main/server.c b/src/chat/main/server.c index 33c9d0b..5674b01 100644 --- a/src/chat/main/server.c +++ b/src/chat/main/server.c @@ -70,8 +70,11 @@ int main(void) } } + //QUIT COMMUNICATION + why2_clean_connections(); //STOP LISTENING; DISCONNECT ALL USERS + why2_clean_threads(); //STOP WAITING FOR MESSAGES + //DEALLOCATION - why2_clean_threads(); free(line_buffer); close(listen_socket); pthread_cancel(thread); diff --git a/src/chat/misc.c b/src/chat/misc.c index c8bd672..448ee28 100644 --- a/src/chat/misc.c +++ b/src/chat/misc.c @@ -452,25 +452,45 @@ void *why2_accept_thread(void *socket) return NULL; } -void why2_clean_threads(void) +void why2_clean_connections(void) { - if (connection_list.head == NULL) return; //EMPTY LIST + why2_node_t *head = connection_list.head; + if (head == NULL) return; //EMPTY LIST - why2_node_t *node_buffer; + why2_node_t *node_buffer = head; + why2_node_t *node_buffer_2; connection_node_t connection_buffer; do //GO TROUGH LIST { - node_buffer = connection_list.head; + node_buffer_2 = node_buffer; + node_buffer = node_buffer -> next; - connection_buffer = *(connection_node_t*) node_buffer -> value; + connection_buffer = *(connection_node_t*) node_buffer_2 -> value; // TODO: TADY SE TO SERE why2_send_socket(WHY2_CHAT_CODE_SSQC, WHY2_CHAT_SERVER_USERNAME, connection_buffer.connection); close(connection_buffer.connection); + why2_list_remove(&connection_list, node_buffer_2); //REMOVE + } while (node_buffer != NULL); +} - why2_list_remove(&connection_list, node_buffer); //REMOVE - } while (connection_list.head != NULL); +void why2_clean_threads(void) +{ + why2_node_t *head = waiting_list.head; + if (head == NULL) return; //EMPTY LIST + + why2_node_t *node_buffer = head; + why2_node_t *node_buffer_2; + + do //GO TROUGH LIST + { + node_buffer_2 = node_buffer; + node_buffer = node_buffer -> next; + + pthread_cancel(**(pthread_t**)(node_buffer_2 -> value)); + why2_list_remove(&waiting_list, node_buffer_2); //REMOVE + } while (node_buffer != NULL); } void *why2_listen_server(void *socket)