From 024ae36003f262462d3aebb6bb250df90793c3de Mon Sep 17 00:00:00 2001 From: MysterD Date: Mon, 9 Aug 2021 22:27:52 -0700 Subject: [PATCH] Retry Discord lobby creation on failure 5 times before giving up --- src/pc/network/discord/discord.c | 6 ++++++ src/pc/network/discord/lobby.c | 26 ++++++++++++++++++++++++++ src/pc/network/discord/lobby.h | 5 +++++ 3 files changed, 37 insertions(+) diff --git a/src/pc/network/discord/discord.c b/src/pc/network/discord/discord.c index 81085a8f..9a1380c8 100644 --- a/src/pc/network/discord/discord.c +++ b/src/pc/network/discord/discord.c @@ -131,6 +131,7 @@ static void register_launch_command(void) { static void ns_discord_update(void) { if (!gDiscordInitialized) { return; } + discord_lobby_update(); DISCORD_REQUIRE(app.core->run_callbacks(app.core)); discord_network_flush(); } @@ -155,6 +156,11 @@ static bool ns_discord_initialize(enum NetworkType networkType) { params.activity_events = discord_activity_initialize(); params.lobby_events = discord_lobby_initialize(); + gCurLobbyId = 0; + gLobbyCreateRetry = false; + gLobbyCreateAttempts = 0; + gLobbyCreateAttemptElapsed = 0; + int rc = DiscordCreate(DISCORD_VERSION, ¶ms, &app.core); if (app.core != NULL) { app.core->set_log_hook(app.core, DiscordLogLevel_Debug, NULL, discord_sdk_log_callback); diff --git a/src/pc/network/discord/lobby.c b/src/pc/network/discord/lobby.c index 6858644f..d9d378da 100644 --- a/src/pc/network/discord/lobby.c +++ b/src/pc/network/discord/lobby.c @@ -2,12 +2,38 @@ #include "activity.h" #include "discord_network.h" #include "pc/logfile.h" +#include "pc/utils/misc.h" + +#define MAX_LOBBY_RETRY 5 +#define MAX_LOBBY_RETRY_WAIT_TIME 6 static bool isHosting = false; DiscordLobbyId gCurLobbyId = 0; +bool gLobbyCreateRetry = false; +u8 gLobbyCreateAttempts = 0; +f32 gLobbyCreateAttemptElapsed = 0; + +void discord_lobby_update(void) { + if (gCurLobbyId != 0) { return; } + if (!gLobbyCreateRetry) { return; } + f32 timeUntilRetry = (clock_elapsed() - gLobbyCreateAttemptElapsed); + if (timeUntilRetry < MAX_LOBBY_RETRY_WAIT_TIME) { return; } + gLobbyCreateRetry = false; + discord_lobby_create(); +} + static void on_lobby_create_callback(UNUSED void* data, enum EDiscordResult result, struct DiscordLobby* lobby) { LOGFILE_INFO(LFT_DISCORD, "> on_lobby_create returned %d", (int)result); + + if (result != DiscordResult_Ok && gLobbyCreateAttempts < MAX_LOBBY_RETRY) { + LOGFILE_INFO(LFT_DISCORD, "rescheduling lobby creation"); + gLobbyCreateRetry = true; + gLobbyCreateAttempts++; + gLobbyCreateAttemptElapsed = clock_elapsed(); + return; + } + DISCORD_REQUIRE(result); LOGFILE_INFO(LFT_DISCORD, "Lobby id: %lld", lobby->id); LOGFILE_INFO(LFT_DISCORD, "Lobby type: %u", lobby->type); diff --git a/src/pc/network/discord/lobby.h b/src/pc/network/discord/lobby.h index 3dec9a3d..4e0d32d2 100644 --- a/src/pc/network/discord/lobby.h +++ b/src/pc/network/discord/lobby.h @@ -4,6 +4,11 @@ extern DiscordLobbyId gCurLobbyId; +extern bool gLobbyCreateRetry; +extern u8 gLobbyCreateAttempts; +extern f32 gLobbyCreateAttemptElapsed; + +void discord_lobby_update(void); void discord_lobby_create(void); void discord_lobby_leave(void); struct IDiscordLobbyEvents* discord_lobby_initialize(void);