From 53dd3222844271f64e9902b74ad1148839b89366 Mon Sep 17 00:00:00 2001 From: "jazz (gitea)" Date: Sun, 8 Mar 2026 12:52:18 -0500 Subject: [PATCH] refactor pt 2 --- chat.cc | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++---- chat.h | 6 ++--- main.cc | 20 ++++++++++++++++ 3 files changed, 89 insertions(+), 9 deletions(-) diff --git a/chat.cc b/chat.cc index 345116f..b51de7c 100644 --- a/chat.cc +++ b/chat.cc @@ -5,9 +5,12 @@ #include #include +#include #include +#include #include #include +#include #include #include @@ -91,11 +94,11 @@ await_client(void *a) delete args; - return client_poll(); + return poll_client(); } void * -client_poll(void) +poll_client(void) { char msg[1024]; for (;;) { @@ -191,15 +194,15 @@ client_stop(void) } void * -server_poll(void *args) +poll_server(void *args) { - wclient_args_t *a = static_cast(args); + wclient_args_t *a = static_cast(args); int sock_desc = (int)a->aux; char msg[1024]; for (;;) { memset(&msg, 0, sizeof(msg)); - bytes_read += recv(sock_desc, (char*)&msg, sizeof(msg), 0); + bytes_read += recv(sock_desc, (char *)&msg, sizeof(msg), 0); log_append(log_path, msg); @@ -209,3 +212,62 @@ server_poll(void *args) file_disp(log_path, line_position, LOG_LENGTH); } } + +void +client_start(void) +{ + char msg[1024]; + int bread; + int bwrit; + int rc; + sockaddr_in send_addr; + + struct timeval time_start; + struct timeval time_end; + struct hostent *host = gethostbyname(listen_ip_address.c_str()); + + auto *a = new wclient_args_t{}; + + bzero((char *)&send_addr, sizeof(send_addr)); + send_addr.sin_family = AF_INET; + send_addr.sin_addr.s_addr = inet_addr(inet_ntoa(*(struct in_addr *)*host->h_addr_list)); + send_addr.sin_port = htons(listen_port); + socket_descriptor_client = socket(AF_INET, SOCK_STREAM, 0); + +attempt_client_connection: + int stat = connect(socket_descriptor_client, (sockaddr *)&send_addr, sizeof(send_addr)); + if (stat < 0) { + log_append(log_path, "Error connecting to socket, retrying..."); + goto attempt_client_connection; + } + + log_append(log_path, "Connected to the server!"); + bread = bwrit = 0; + + gettimeofday(&time_start, NULL); + + a->aux = socket_descriptor_client; + rc = pthread_create(&client_wait_thread, nullptr, poll_server, a); + pthread_detach(client_wait_thread); +} + +int +log_append(std::string p, std::string ln) +{ + std::ofstream f; + + f.open(p, std::ios_base::app); + if (!f.is_open()) + return 1; + + f << ln << std::endl; + + f.close(); + + // "this probably would help but theres too much broken stuff right now to be sure" - scott + // bool inclnum should be added to function if we do decide to use this block: + //if (inclnum) + // line_position++; + + return 0; +} diff --git a/chat.h b/chat.h index a8d3e4b..0c542e3 100644 --- a/chat.h +++ b/chat.h @@ -27,8 +27,6 @@ const int CURSOR_DEFAULT_POSITION_Y = 12; const int LOG_LENGTH = 10; int line_position = 0; -chatmode_t mode = NO_MODE; - struct timeval time_start; struct timeval time_end; int bytes_read; @@ -50,10 +48,10 @@ int count_lines(std::string p); int file_disp(std::string p, int lnstart, int lncount = 10); int log_append(std::string p, std::string ln); int log_linect(std::string p); -void *client_poll(void); void client_start(void); void client_stop(void); -void *server_poll(void *a); +void *poll_client(void); +void *poll_server(void *a); void server_start(int port); void server_stop(void); diff --git a/main.cc b/main.cc index 11434be..dbbf36e 100644 --- a/main.cc +++ b/main.cc @@ -1,8 +1,28 @@ #include #include +#include #include +#include #include #include "chat.h" +chatmode_t mode = NO_MODE; + +int +main(int argc, char *argv[]) +{ + char *usr_in = new char[1024]; + bool quit = false; + + if (argc > 1 && (!strcmp(argv[1], "client"))) + mode = CLIENT_MODE; + else + mode = SERVER_MODE; + + initscr(); + use_default_colors(); + + line_position = count_lines(log_path) - LOG_LENGTH + 1; +}