From d451030ad045d2dd8cf06c2edb75ce4fdd954de0 Mon Sep 17 00:00:00 2001 From: ENGO150 Date: Fri, 19 Apr 2024 16:31:45 +0200 Subject: [PATCH] handling sigint signal i'm really trying alex, i am wicked game --- src/chat/main/client.c | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/chat/main/client.c b/src/chat/main/client.c index cbbb3b1..accc5e3 100644 --- a/src/chat/main/client.c +++ b/src/chat/main/client.c @@ -19,6 +19,7 @@ along with this program. If not, see . #include #include #include +#include #include #include #include @@ -30,15 +31,32 @@ along with this program. If not, see . #include #include +#include #include #include +why2_bool exited = 0; //USER ALREADY REQUESTED EXIT +int listen_socket; //THE SERVER SOCKET + +void exit_client(WHY2_UNUSED int i) //guess what +{ + if (exited) return; + exited = 1; + + char *exit_cmd = why2_chat_client_get_server_exit_cmd(); + + why2_send_socket(exit_cmd, NULL, listen_socket); + why2_deallocate(exit_cmd); +} + int main(void) { + signal(SIGINT, exit_client); //HANDLE ^C + why2_chat_init_client_config(); //CREATE client.toml CONFIGURATION why2_chat_init_keys(); //CREATE RSA KEYS - int listen_socket = socket(AF_INET, SOCK_STREAM, 0); //CREATE SERVER SOCKET + listen_socket = socket(AF_INET, SOCK_STREAM, 0); //CREATE SERVER SOCKET char *line = NULL; void *return_line = NULL; size_t line_length = 0; @@ -111,11 +129,7 @@ int main(void) if (strcmp(line, WHY2_CHAT_COMMAND_PREFIX WHY2_CHAT_COMMAND_EXIT "\n") == 0) //USER REQUESTED PROGRAM EXIT { printf("Exiting...\n"); - - char *exit_cmd = why2_chat_client_get_server_exit_cmd(); - - why2_send_socket(exit_cmd, NULL, listen_socket); - why2_deallocate(exit_cmd); + exit_client(0); } else if (strcmp(line, WHY2_CHAT_COMMAND_PREFIX WHY2_CHAT_COMMAND_HELP "\n") == 0) { printf