Properly shut down the networking API.

This commit is contained in:
2025-12-16 12:43:47 +01:00
parent 167c8a3ce3
commit 76e5b3b282
3 changed files with 19 additions and 13 deletions

View File

@@ -46,6 +46,7 @@ bool game_instance_host_session(Game_Instance *instance, const Game_Session_Sett
uint16_t host_player_id = 0xFFFF;
if(!networking_try_host(&instance->game_networking, instance->game_session, settings, &host_player_id)) {
printf("Failed to host session.\n");
free(instance->game_session);
instance->game_session = NULL;
return false;
@@ -82,10 +83,11 @@ bool game_instance_join_session(Game_Instance *instance, const char *session_id)
return false;
}
instance->game_session = (Game_Session *)calloc(1, sizeof(Game_Session)); // LEAK
instance->game_session = (Game_Session *)calloc(1, sizeof(Game_Session));
if(!networking_try_join(&instance->game_networking, instance->game_session, session_id)) {
free(instance->game_session);
instance->game_session = NULL;
return false;
}

View File

@@ -87,12 +87,7 @@ static void state_render(Presentation_State *state) {
ctx->mode = Main_Menu_Mode_Multiplayer_Join;
memset(&ctx->session_id_input, 0, sizeof(ctx->session_id_input));
ctx->session_id_input[0] = 'A';
ctx->session_id_input[1] = 'B';
ctx->session_id_input[2] = 'C';
ctx->session_id_input[3] = 'D';
ctx->session_id_input[4] = 'E';
ctx->session_id_input[5] = 'F';
// snprintf(&ctx->session_id_input[0], sizeof(ctx->session_id_input) + 1, "ABCDEF");
}
if (GuiButton((Rectangle){ 64, 64 + (BUTTON_HEIGHT * 2), 128, BUTTON_HEIGHT }, "Back to Main Menu")) {
ctx->mode = Main_Menu_Mode_Home;
@@ -134,7 +129,7 @@ static void state_render(Presentation_State *state) {
int pressed_button_index = GuiTextInputBox(
(Rectangle) { 64, 64, 256, 128 }, // Bounds
"Session-ID", // Title
NULL, // Message
"6 characters expected. (ABCDEF)", // Message
"Join", // Buttons
&ctx->session_id_input[0], // Text
sizeof(ctx->session_id_input) + 1, // Text max size

View File

@@ -82,6 +82,17 @@ static inline bool setup_api(Game_Networking *networking) {
return true;
}
static inline bool shutdown_api(Game_Networking *networking) {
if(networking->api == NULL) {
return false;
}
mp_api_destroy(networking->api);
networking->api = NULL;
return true;
}
static bool start_listening(Game_Networking *networking) {
assert(networking != NULL);
assert(networking->api != NULL);
@@ -130,7 +141,7 @@ bool networking_try_host(Game_Networking *networking, Game_Session *session, Gam
);
if(host_result != MP_API_OK) {
printf("Failed to host; Got result: %i.\n", host_result);
// TODO: SS - Shutdown API.
shutdown_api(networking);
return false;
}
@@ -192,7 +203,7 @@ bool networking_try_join(Game_Networking *networking, Game_Session *session, con
&received_json // Received JSON-data.
);
if(join_result != MP_API_OK) {
// TODO: SS - Shutdown API.
shutdown_api(networking);
printf("Failed to join; Got result: %i.\n", join_result);
return false;
}
@@ -215,7 +226,6 @@ bool networking_try_join(Game_Networking *networking, Game_Session *session, con
json_decref(received_json);
}
// TODO: SS - Stop listening.
assert(start_listening(networking));
// TODO: SS - Set up session based on what we got in the json.
@@ -242,8 +252,7 @@ bool networking_stop(Game_Networking *networking) {
assert(stop_listening(networking));
networking->listener_id = 0;
mp_api_destroy(networking->api);
networking->api = NULL;
shutdown_api(networking);
networking->game_session = NULL;
networking->is_host = false;