From 29584196eea8ba4612f9a870e5d54aa6b1f02f62 Mon Sep 17 00:00:00 2001 From: iWas-Coder Date: Mon, 1 Apr 2024 06:01:47 +0200 Subject: [PATCH] Separated offline and online modes into local functions --- src/sk_client.c | 104 ++++++++++++++++++++++++++---------------------- 1 file changed, 56 insertions(+), 48 deletions(-) diff --git a/src/sk_client.c b/src/sk_client.c index 92da536..cc23245 100644 --- a/src/sk_client.c +++ b/src/sk_client.c @@ -31,59 +31,67 @@ #include #include +static void offline_mode(void) { + SK_LOG_WARN("Running in offline mode"); + sk_state state = sk_state_create_offline(); + sk_renderer_create(); + sk_renderer_loop { + sk_renderer_update(&state); + sk_renderer_draw(&state); + } + sk_player_destroy(&state.player); + sk_renderer_destroy(); +} + +static i8 online_mode(const char *ip) { + const struct sockaddr_in server_addr = { + .sin_family = AF_INET, + .sin_port = htons(SK_SERVER_PORT), + .sin_addr.s_addr = inet_addr(ip) + }; + i32 sock_fd = sk_server_socket_create(); + if (sock_fd == -1) return -1; + SK_LOG_INFO("Connecting to `%s` ...", ip); + if (sendto(sock_fd, + SK_SERVER_MSG_HELLO, + strlen(SK_SERVER_MSG_HELLO), + 0, + (const struct sockaddr *) &server_addr, + sizeof(server_addr)) == -1) { + SK_LOG_ERROR("sendto(2) :: %s", strerror(errno)); + close(sock_fd); + return -1; + } + char howdy_msg[SK_SERVER_MSG_MAX_SIZE]; + i32 howdy_msg_n = recv(sock_fd, howdy_msg, sizeof(howdy_msg), MSG_WAITALL); + if (howdy_msg_n == -1) { + SK_LOG_ERROR("recv(2) :: %s", strerror(errno)); + close(sock_fd); + return -1; + } + howdy_msg[howdy_msg_n] = 0; + SK_LOG_DEBUG(howdy_msg); + // TODO: hardcoded now to 0/0 ... change this to save the received IDs + if (strcmp(howdy_msg, TextFormat(SK_SERVER_MSG_HOWDY, 0, 0))) { + SK_LOG_ERROR("Unable to communicate with `%s`. Exiting...", ip); + close(sock_fd); + return -1; + } + SK_LOG_INFO("Connected successfully to `%s`", ip); + SK_LOG_WARN("Exit due to not being implemented yet"); + sk_server_socket_destroy(sock_fd); + return 0; +} + u8 sk_client_run(const char *ip) { SK_LOG_INFO("Initializing %s", SK_CLIENT_NAME); if (!ChangeDirectory(GetApplicationDirectory())) { SK_LOG_WARN("Could not change CWD to the game's root directory"); } - if (!ip) { - SK_LOG_WARN("Running in offline mode"); - sk_state state = sk_state_create_offline(); - sk_renderer_create(); - sk_renderer_loop { - sk_renderer_update(&state); - sk_renderer_draw(&state); - } - sk_player_destroy(&state.player); - sk_renderer_destroy(); - } - else { - const struct sockaddr_in server_addr = { - .sin_family = AF_INET, - .sin_port = htons(SK_SERVER_PORT), - .sin_addr.s_addr = inet_addr(ip) - }; - i32 sock_fd = sk_server_socket_create(); - if (sock_fd == -1) return 1; - SK_LOG_INFO("Connecting to `%s` ...", ip); - if (sendto(sock_fd, - SK_SERVER_MSG_HELLO, - strlen(SK_SERVER_MSG_HELLO), - 0, - (const struct sockaddr *) &server_addr, - sizeof(server_addr)) == -1) { - SK_LOG_ERROR("sendto(2) :: %s", strerror(errno)); - close(sock_fd); - return 1; - } - char howdy_msg[SK_SERVER_MSG_MAX_SIZE]; - i32 howdy_msg_n = recv(sock_fd, howdy_msg, sizeof(howdy_msg), MSG_WAITALL); - if (howdy_msg_n == -1) { - SK_LOG_ERROR("recv(2) :: %s", strerror(errno)); - close(sock_fd); - return 1; - } - howdy_msg[howdy_msg_n] = 0; - SK_LOG_DEBUG(howdy_msg); - // TODO: hardcoded now to 0/0 ... change this to save the received IDs - if (strcmp(howdy_msg, TextFormat(SK_SERVER_MSG_HOWDY, 0, 0))) { - SK_LOG_ERROR("Unable to communicate with `%s`. Exiting...", ip); - close(sock_fd); - return 1; - } - SK_LOG_INFO("Connected successfully to `%s`", ip); - SK_LOG_WARN("Exit due to not being implemented yet"); - sk_server_socket_destroy(sock_fd); + if (!ip) offline_mode(); + else if (online_mode(ip) == -1) { + SK_LOG_ERROR("%s closed abruptly due to errors", SK_CLIENT_NAME); + return 1; } SK_LOG_INFO("%s closed successfully", SK_CLIENT_NAME); return 0;