Skip to content

Commit

Permalink
Separated offline and online modes into local functions
Browse files Browse the repository at this point in the history
  • Loading branch information
iWas-Coder committed Apr 1, 2024
1 parent c9c088d commit 2958419
Showing 1 changed file with 56 additions and 48 deletions.
104 changes: 56 additions & 48 deletions src/sk_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,59 +31,67 @@
#include <sys/socket.h>
#include <sk_renderer.h>

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;
Expand Down

0 comments on commit 2958419

Please sign in to comment.