From 87bf911ba2de7763e711cc0de7c3ddfa10d880f2 Mon Sep 17 00:00:00 2001 From: ENGO150 Date: Wed, 5 Apr 2023 10:12:12 +0200 Subject: [PATCH 1/8] created WHY2_INVALID_POINTER macro it is really bik number --- include/chat/flags.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/chat/flags.h b/include/chat/flags.h index 281e0ff..65ba001 100644 --- a/include/chat/flags.h +++ b/include/chat/flags.h @@ -27,4 +27,6 @@ along with this program. If not, see . #define WHY2_CLEAR_AND_GO_UP "\33[2K\r\033[A" //i mean read the name +#define WHY2_INVALID_POINTER (void*) 0xffffffffffffffff + #endif \ No newline at end of file From 28b2baae3ca827f8c890f843d67b37257f65022e Mon Sep 17 00:00:00 2001 From: ENGO150 Date: Wed, 5 Apr 2023 10:16:53 +0200 Subject: [PATCH 2/8] fixed infinite waiting in communication loop yeah I used signal.h (alert) and it works... yay --- src/chat/misc.c | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/src/chat/misc.c b/src/chat/misc.c index b3db261..b59a330 100644 --- a/src/chat/misc.c +++ b/src/chat/misc.c @@ -21,9 +21,9 @@ along with this program. If not, see . #include #include #include -#include #include #include +#include #include @@ -33,6 +33,9 @@ along with this program. If not, see . #include #include +//TIMEOUT STUFF +pthread_t thread_timeout_buffer; + //LINKED LIST STUFF (BIT CHANGED memory.c'S VERSION) typedef struct node { @@ -116,8 +119,6 @@ node_t *get_node(int connection) return buffer; } - - char *get_string_from_json(struct json_object *json, char *string) { struct json_object *object; @@ -216,6 +217,11 @@ char *read_socket_raw(int socket) return content_buffer; } +void *read_socket_raw_thread(void *socket) +{ + return read_socket_raw(*(int*) socket); +} + char *read_socket_from_raw(char *raw) { char *final_message; @@ -250,6 +256,11 @@ void remove_json_syntax_characters(char *text) } } +void alarm_handler() +{ + pthread_cancel(thread_timeout_buffer); +} + //GLOBAL void why2_send_socket(char *text, char *username, int socket) { @@ -308,19 +319,27 @@ void *why2_communicate_thread(void *arg) push_to_list(connection, pthread_self()); //ADD TO LIST - time_t start_time = time(NULL); char *received = NULL; char *raw = NULL; + void *raw_ptr = NULL; char *decoded_buffer; pthread_t thread_buffer; why2_bool exiting = 0; - while ((time(NULL) - start_time) < WHY2_COMMUNICATION_TIME && !exiting) //KEEP COMMUNICATION ALIVE FOR 5 MINUTES [RESET TIMER AT MESSAGE SENT] //TODO: Fix stuck + while (!exiting) //KEEP COMMUNICATION ALIVE FOR 5 MINUTES [RESET TIMER AT MESSAGE SENT] //TODO: Fix stuck { //READ - raw = read_socket_raw(connection); + pthread_create(&thread_timeout_buffer, NULL, read_socket_raw_thread, &connection); - if (raw == NULL) break; //QUIT COMMUNICATION IF INVALID PACKET WAS RECEIVED + //SET TIMEOUT (DEFAULT IS 5 MINUTES) + signal(SIGALRM, alarm_handler); + alarm(WHY2_COMMUNICATION_TIME); + + pthread_join(thread_timeout_buffer, &raw_ptr); + + if (raw_ptr == WHY2_INVALID_POINTER || raw_ptr == NULL) break; //QUIT COMMUNICATION IF INVALID PACKET WAS RECEIVED + + raw = (char*) raw_ptr; //REMOVE CONTROL CHARACTERS FROM raw for (size_t i = 0; i < strlen(raw); i++) @@ -346,14 +365,15 @@ void *why2_communicate_thread(void *arg) pthread_create(&thread_buffer, NULL, send_to_all, raw); pthread_join(thread_buffer, NULL); - //RESET TIMER - start_time = time(NULL); - deallocation: why2_deallocate(received); why2_deallocate(raw); + why2_deallocate(raw_ptr); why2_deallocate(decoded_buffer); + + //RESET VARIABLES + raw_ptr = NULL; } printf("User exited.\t%d\n", connection); From c78a82e38189667bbc60dbc8c06c3c72636868d2 Mon Sep 17 00:00:00 2001 From: ENGO150 Date: Wed, 5 Apr 2023 10:17:56 +0200 Subject: [PATCH 3/8] removed stuck TODO --- src/chat/misc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/chat/misc.c b/src/chat/misc.c index b59a330..3b820b4 100644 --- a/src/chat/misc.c +++ b/src/chat/misc.c @@ -326,7 +326,7 @@ void *why2_communicate_thread(void *arg) pthread_t thread_buffer; why2_bool exiting = 0; - while (!exiting) //KEEP COMMUNICATION ALIVE FOR 5 MINUTES [RESET TIMER AT MESSAGE SENT] //TODO: Fix stuck + while (!exiting) //KEEP COMMUNICATION ALIVE FOR 5 MINUTES [RESET TIMER AT MESSAGE SENT] { //READ pthread_create(&thread_timeout_buffer, NULL, read_socket_raw_thread, &connection); From aeed3cc5ee1ba1cba4d24e883b75074bdf40ba69 Mon Sep 17 00:00:00 2001 From: ENGO150 Date: Wed, 5 Apr 2023 10:18:57 +0200 Subject: [PATCH 4/8] added client timeout todo --- src/chat/misc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/chat/misc.c b/src/chat/misc.c index 3b820b4..6ab1dd9 100644 --- a/src/chat/misc.c +++ b/src/chat/misc.c @@ -383,7 +383,7 @@ void *why2_communicate_thread(void *arg) close(connection); remove_node(get_node(connection)); - return NULL; + return NULL; //TODO: Fix client segfault on timeout } char *why2_read_socket(int socket) From 2991b1f2c3aa9ac80f3cc879fe4fd6c7403ed1b5 Mon Sep 17 00:00:00 2001 From: ENGO150 Date: Wed, 5 Apr 2023 10:20:38 +0200 Subject: [PATCH 5/8] removed server crash bug todo --- src/chat/misc.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/chat/misc.c b/src/chat/misc.c index 6ab1dd9..0040075 100644 --- a/src/chat/misc.c +++ b/src/chat/misc.c @@ -460,6 +460,4 @@ void *why2_listen_server(void *socket) why2_deallocate(read); } -} - -//BUG: SERVER SOMETIMES CRASHES - I HAVE NO FUCKING IDEA WHY \ No newline at end of file +} \ No newline at end of file From cdcc9e53b4d4749c6fcca9350994fdb161343430 Mon Sep 17 00:00:00 2001 From: ENGO150 Date: Wed, 5 Apr 2023 10:21:47 +0200 Subject: [PATCH 6/8] removed all why2_listen_server formatting todos --- src/chat/misc.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/chat/misc.c b/src/chat/misc.c index 0040075..b7c2fa7 100644 --- a/src/chat/misc.c +++ b/src/chat/misc.c @@ -448,14 +448,14 @@ void *why2_listen_server(void *socket) { char *read = NULL; - printf(">>> "); //TODO: Make this smart + printf(">>> "); fflush(stdout); for (;;) { - read = why2_read_socket(*((int*) socket)); //TODO: Fix other user message formatting + read = why2_read_socket(*((int*) socket)); printf(WHY2_CLEAR_AND_GO_UP); - printf("%s\n\n>>> ", read); //TODO: wtf is the output + printf("%s\n\n>>> ", read); fflush(stdout); why2_deallocate(read); From 278e880e62892c712e52d32dacd6e83882208f6c Mon Sep 17 00:00:00 2001 From: ENGO150 Date: Wed, 5 Apr 2023 11:12:01 +0200 Subject: [PATCH 7/8] created waiting llist it contains only function for push --- src/chat/misc.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/chat/misc.c b/src/chat/misc.c index b7c2fa7..a0c719b 100644 --- a/src/chat/misc.c +++ b/src/chat/misc.c @@ -44,7 +44,14 @@ typedef struct node struct node *next; } node_t; //SINGLE LINKED LIST +typedef struct waiting_node +{ + pthread_t thread; + struct waiting_node *next; +} waiting_node_t; //SINGLE LINKED LIST + node_t *head = NULL; +waiting_node_t *waiting_head = NULL; void push_to_list(int connection, pthread_t thread) { @@ -67,6 +74,26 @@ void push_to_list(int connection, pthread_t thread) } } +void waiting_push_to_list(pthread_t thread) +{ + //CREATE NODE + waiting_node_t *new_node = malloc(sizeof(waiting_node_t)); + waiting_node_t *buffer = waiting_head; + + new_node -> thread = thread; + new_node -> next = NULL; + + if (waiting_head == NULL) //INIT LIST + { + waiting_head = new_node; + } else + { + while (buffer -> next != NULL) buffer = buffer -> next; //GET TO THE END OF LIST + + buffer -> next = new_node; //LINK + } +} + void remove_node(node_t *node) { if (node == NULL) return; //NULL NODE From 6a4034668b1fd1a29e2d9bc74fbe7d68f7b2d801 Mon Sep 17 00:00:00 2001 From: ENGO150 Date: Wed, 5 Apr 2023 11:21:21 +0200 Subject: [PATCH 8/8] implemented waiting llist in alarm_handler --- src/chat/misc.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/chat/misc.c b/src/chat/misc.c index a0c719b..0fb83dd 100644 --- a/src/chat/misc.c +++ b/src/chat/misc.c @@ -33,10 +33,7 @@ along with this program. If not, see . #include #include -//TIMEOUT STUFF -pthread_t thread_timeout_buffer; - -//LINKED LIST STUFF (BIT CHANGED memory.c'S VERSION) +//LINKED LIST STUFF (BIT CHANGED memory.c'S VERSION) //TODO: Move llist in some single separate file typedef struct node { int connection; @@ -285,7 +282,14 @@ void remove_json_syntax_characters(char *text) void alarm_handler() { - pthread_cancel(thread_timeout_buffer); + //CANCEL OLDEST THREAD + pthread_cancel(waiting_head -> thread); + + //REMOVE FIRST NODE + waiting_node_t *buffer = waiting_head; //BUFFER + + waiting_head = buffer -> next; //UNLINK + free(waiting_head); } //GLOBAL @@ -356,13 +360,14 @@ void *why2_communicate_thread(void *arg) while (!exiting) //KEEP COMMUNICATION ALIVE FOR 5 MINUTES [RESET TIMER AT MESSAGE SENT] { //READ - pthread_create(&thread_timeout_buffer, NULL, read_socket_raw_thread, &connection); + pthread_create(&thread_buffer, NULL, read_socket_raw_thread, &connection); + waiting_push_to_list(thread_buffer); //SET TIMEOUT (DEFAULT IS 5 MINUTES) signal(SIGALRM, alarm_handler); alarm(WHY2_COMMUNICATION_TIME); - pthread_join(thread_timeout_buffer, &raw_ptr); + pthread_join(thread_buffer, &raw_ptr); if (raw_ptr == WHY2_INVALID_POINTER || raw_ptr == NULL) break; //QUIT COMMUNICATION IF INVALID PACKET WAS RECEIVED