From 76864937f3f72ac1fb7bc29ea92864d31c05227d Mon Sep 17 00:00:00 2001 From: GeckoEidechse <40122905+GeckoEidechse@users.noreply.github.com> Date: Thu, 16 Jun 2022 01:37:29 +0200 Subject: Attempt reconnect in case of DUPLICATE_SERVER (#187) * Attempt reconnect in case of DUPLICATE_SERVER If gameserver receives DUPLICATE_SERVER response it will retry the auth request after a timeout of 10 seconds. Retries will stop after 10 attempts. Co-authored-by: Emma Miler <27428383+emma-miler@users.noreply.github.com> * Fix formatting * Move comment So that it refers to the right line * Retry 10 times instead of just once * Only check retrycounter for DUPLICATE_SERVER error Co-authored-by: Emma Miler <27428383+emma-miler@users.noreply.github.com> --- NorthstarDedicatedTest/masterserver.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/NorthstarDedicatedTest/masterserver.cpp b/NorthstarDedicatedTest/masterserver.cpp index 8bb74668..d5ef2d9f 100644 --- a/NorthstarDedicatedTest/masterserver.cpp +++ b/NorthstarDedicatedTest/masterserver.cpp @@ -14,6 +14,7 @@ #include #include #include "version.h" +#include // NOTE for anyone reading this: we used to use httplib for requests here, but it had issues, so we're moving to curl now for masterserver // requests so httplib is used exclusively for server stuff now @@ -836,6 +837,10 @@ void MasterServerManager::AddSelfToServerList( std::thread requestThread( [this, port, authPort, strName, strDescription, strMap, strPlaylist, maxPlayers, strPassword] { + // Keep track of attempted connects in case of DUPLICATE_SERVER response + int retry_counter = 0; + + START_REQUEST: m_ownServerId[0] = 0; m_ownServerAuthToken[0] = 0; @@ -915,6 +920,29 @@ void MasterServerManager::AddSelfToServerList( { spdlog::error("Failed reading masterserver response: got fastify error response"); spdlog::error(readBuffer); + + // Check for enum member in JSON + if (serverAddedJson["error"].HasMember("enum")) + { + // Check for DUPLICATE_SERVER error response, stop if we tried 10 times + if (strncmp(serverAddedJson["error"]["enum"].GetString(), "DUPLICATE_SERVER", 17) == 0 && retry_counter < 10) + { + + spdlog::info("Retrying request in 10 seconds."); + // Incremement retry counter + retry_counter++; + + // Sleep for 10 seconds + std::this_thread::sleep_for(std::chrono::seconds(10)); + + // curl cleanup to retry request + curl_easy_cleanup(curl); + curl_mime_free(mime); + + // go to beginning and retry + goto START_REQUEST; + } + } goto REQUEST_END_CLEANUP; } @@ -937,6 +965,8 @@ void MasterServerManager::AddSelfToServerList( strncpy(m_ownServerAuthToken, serverAddedJson["serverAuthToken"].GetString(), sizeof(m_ownServerAuthToken)); m_ownServerAuthToken[sizeof(m_ownServerAuthToken) - 1] = 0; + spdlog::info("Succesfully added server to the master server."); + // heartbeat thread // ideally this should actually be done in main thread, rather than on it's own thread, so it'd stop if server freezes std::thread heartbeatThread( -- cgit v1.2.3