From 447cace77f2533e616f82e42787c14f7fcbc039b Mon Sep 17 00:00:00 2001 From: Jack <66967891+ASpoonPlaysGames@users.noreply.github.com> Date: Sat, 20 Jan 2024 22:40:27 +0000 Subject: Add and use ScopeGuard (#643) Use a scope guard instead of `GOTO` statements for curl cleanup --- primedev/masterserver/masterserver.cpp | 114 +++++++++++++++++---------------- 1 file changed, 59 insertions(+), 55 deletions(-) (limited to 'primedev/masterserver/masterserver.cpp') diff --git a/primedev/masterserver/masterserver.cpp b/primedev/masterserver/masterserver.cpp index aa248464..c4cc22ef 100644 --- a/primedev/masterserver/masterserver.cpp +++ b/primedev/masterserver/masterserver.cpp @@ -7,6 +7,7 @@ #include "engine/r2engine.h" #include "mods/modmanager.h" #include "shared/misccommands.h" +#include "util/utils.h" #include "util/version.h" #include "server/auth/bansystem.h" #include "dedicated/dedicated.h" @@ -118,6 +119,13 @@ void MasterServerManager::AuthenticateOriginWithMasterServer(const char* uid, co curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); CURLcode result = curl_easy_perform(curl); + ScopeGuard cleanup( + [&] + { + m_bOriginAuthWithMasterServerInProgress = false; + m_bOriginAuthWithMasterServerDone = true; + curl_easy_cleanup(curl); + }); if (result == CURLcode::CURLE_OK) { @@ -131,13 +139,13 @@ void MasterServerManager::AuthenticateOriginWithMasterServer(const char* uid, co spdlog::error( "Failed reading origin auth info response: encountered parse error \"{}\"", rapidjson::GetParseError_En(originAuthInfo.GetParseError())); - goto REQUEST_END_CLEANUP; + return; } if (!originAuthInfo.IsObject() || !originAuthInfo.HasMember("success")) { spdlog::error("Failed reading origin auth info response: malformed response object {}", readBuffer); - goto REQUEST_END_CLEANUP; + return; } if (originAuthInfo["success"].IsTrue() && originAuthInfo.HasMember("token") && originAuthInfo["token"].IsString()) @@ -174,12 +182,6 @@ void MasterServerManager::AuthenticateOriginWithMasterServer(const char* uid, co spdlog::error("Failed performing northstar origin auth: error {}", curl_easy_strerror(result)); m_bSuccessfullyConnected = false; } - - // we goto this instead of returning so we always hit this - REQUEST_END_CLEANUP: - m_bOriginAuthWithMasterServerInProgress = false; - m_bOriginAuthWithMasterServerDone = true; - curl_easy_cleanup(curl); }); requestThread.detach(); @@ -213,6 +215,13 @@ void MasterServerManager::RequestServerList() curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); CURLcode result = curl_easy_perform(curl); + ScopeGuard cleanup( + [&] + { + m_bRequestingServerList = false; + m_bScriptRequestingServerList = false; + curl_easy_cleanup(curl); + }); if (result == CURLcode::CURLE_OK) { @@ -226,20 +235,20 @@ void MasterServerManager::RequestServerList() spdlog::error( "Failed reading masterserver response: encountered parse error \"{}\"", rapidjson::GetParseError_En(serverInfoJson.GetParseError())); - goto REQUEST_END_CLEANUP; + return; } if (serverInfoJson.IsObject() && serverInfoJson.HasMember("error")) { spdlog::error("Failed reading masterserver response: got fastify error response"); spdlog::error(readBuffer); - goto REQUEST_END_CLEANUP; + return; } if (!serverInfoJson.IsArray()) { spdlog::error("Failed reading masterserver response: root object is not an array"); - goto REQUEST_END_CLEANUP; + return; } rapidjson::GenericArray serverArray = serverInfoJson.GetArray(); @@ -251,7 +260,7 @@ void MasterServerManager::RequestServerList() if (!serverObj.IsObject()) { spdlog::error("Failed reading masterserver response: member of server array is not an object"); - goto REQUEST_END_CLEANUP; + return; } // todo: verify json props are fine before adding to m_remoteServers @@ -342,12 +351,6 @@ void MasterServerManager::RequestServerList() spdlog::error("Failed requesting servers: error {}", curl_easy_strerror(result)); m_bSuccessfullyConnected = false; } - - // we goto this instead of returning so we always hit this - REQUEST_END_CLEANUP: - m_bRequestingServerList = false; - m_bScriptRequestingServerList = false; - curl_easy_cleanup(curl); }); requestThread.detach(); @@ -374,6 +377,7 @@ void MasterServerManager::RequestMainMenuPromos() curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); CURLcode result = curl_easy_perform(curl); + ScopeGuard cleanup([&] { curl_easy_cleanup(curl); }); if (result == CURLcode::CURLE_OK) { @@ -387,20 +391,20 @@ void MasterServerManager::RequestMainMenuPromos() spdlog::error( "Failed reading masterserver main menu promos response: encountered parse error \"{}\"", rapidjson::GetParseError_En(mainMenuPromoJson.GetParseError())); - goto REQUEST_END_CLEANUP; + return; } if (!mainMenuPromoJson.IsObject()) { spdlog::error("Failed reading masterserver main menu promos response: root object is not an object"); - goto REQUEST_END_CLEANUP; + return; } if (mainMenuPromoJson.HasMember("error")) { spdlog::error("Failed reading masterserver response: got fastify error response"); spdlog::error(readBuffer); - goto REQUEST_END_CLEANUP; + return; } if (!mainMenuPromoJson.HasMember("newInfo") || !mainMenuPromoJson["newInfo"].IsObject() || @@ -428,7 +432,7 @@ void MasterServerManager::RequestMainMenuPromos() !mainMenuPromoJson["smallButton2"]["ImageIndex"].IsNumber()) { spdlog::error("Failed reading masterserver main menu promos response: malformed json object"); - goto REQUEST_END_CLEANUP; + return; } m_sMainMenuPromoData.newInfoTitle1 = mainMenuPromoJson["newInfo"]["Title1"].GetString(); @@ -455,10 +459,6 @@ void MasterServerManager::RequestMainMenuPromos() spdlog::error("Failed requesting main menu promos: error {}", curl_easy_strerror(result)); m_bSuccessfullyConnected = false; } - - REQUEST_END_CLEANUP: - // nothing lol - curl_easy_cleanup(curl); }); requestThread.detach(); @@ -495,6 +495,21 @@ void MasterServerManager::AuthenticateWithOwnServer(const char* uid, const char* curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); CURLcode result = curl_easy_perform(curl); + ScopeGuard cleanup( + [&] + { + m_bAuthenticatingWithGameServer = false; + m_bScriptAuthenticatingWithGameServer = false; + + if (m_bNewgameAfterSelfAuth) + { + // pretty sure this is threadsafe? + Cbuf_AddText(Cbuf_GetCurrentPlayer(), "ns_end_reauth_and_leave_to_lobby", cmd_source_t::kCommandSrcCode); + m_bNewgameAfterSelfAuth = false; + } + + curl_easy_cleanup(curl); + }); if (result == CURLcode::CURLE_OK) { @@ -508,13 +523,13 @@ void MasterServerManager::AuthenticateWithOwnServer(const char* uid, const char* spdlog::error( "Failed reading masterserver authentication response: encountered parse error \"{}\"", rapidjson::GetParseError_En(authInfoJson.GetParseError())); - goto REQUEST_END_CLEANUP; + return; } if (!authInfoJson.IsObject()) { spdlog::error("Failed reading masterserver authentication response: root object is not an object"); - goto REQUEST_END_CLEANUP; + return; } if (authInfoJson.HasMember("error")) @@ -529,13 +544,13 @@ void MasterServerManager::AuthenticateWithOwnServer(const char* uid, const char* else m_sAuthFailureReason = "No error message provided"; - goto REQUEST_END_CLEANUP; + return; } if (!authInfoJson["success"].IsTrue()) { spdlog::error("Authentication with masterserver failed: \"success\" is not true"); - goto REQUEST_END_CLEANUP; + return; } if (!authInfoJson.HasMember("success") || !authInfoJson.HasMember("id") || !authInfoJson["id"].IsString() || @@ -543,7 +558,7 @@ void MasterServerManager::AuthenticateWithOwnServer(const char* uid, const char* !authInfoJson.HasMember("persistentData") || !authInfoJson["persistentData"].IsArray()) { spdlog::error("Failed reading masterserver authentication response: malformed json object"); - goto REQUEST_END_CLEANUP; + return; } RemoteAuthData newAuthData {}; @@ -561,7 +576,7 @@ void MasterServerManager::AuthenticateWithOwnServer(const char* uid, const char* if (!byte.IsUint() || byte.GetUint() > 255) { spdlog::error("Failed reading masterserver authentication response: malformed json object"); - goto REQUEST_END_CLEANUP; + return; } newAuthData.pdata[i++] = static_cast(byte.GetUint()); @@ -581,19 +596,6 @@ void MasterServerManager::AuthenticateWithOwnServer(const char* uid, const char* m_bSuccessfullyAuthenticatedWithGameServer = false; m_bScriptAuthenticatingWithGameServer = false; } - - REQUEST_END_CLEANUP: - m_bAuthenticatingWithGameServer = false; - m_bScriptAuthenticatingWithGameServer = false; - - if (m_bNewgameAfterSelfAuth) - { - // pretty sure this is threadsafe? - Cbuf_AddText(Cbuf_GetCurrentPlayer(), "ns_end_reauth_and_leave_to_lobby", cmd_source_t::kCommandSrcCode); - m_bNewgameAfterSelfAuth = false; - } - - curl_easy_cleanup(curl); }); requestThread.detach(); @@ -651,6 +653,13 @@ void MasterServerManager::AuthenticateWithServer(const char* uid, const char* pl } CURLcode result = curl_easy_perform(curl); + ScopeGuard cleanup( + [&] + { + m_bAuthenticatingWithGameServer = false; + m_bScriptAuthenticatingWithGameServer = false; + curl_easy_cleanup(curl); + }); if (result == CURLcode::CURLE_OK) { @@ -664,13 +673,13 @@ void MasterServerManager::AuthenticateWithServer(const char* uid, const char* pl spdlog::error( "Failed reading masterserver authentication response: encountered parse error \"{}\"", rapidjson::GetParseError_En(connectionInfoJson.GetParseError())); - goto REQUEST_END_CLEANUP; + return; } if (!connectionInfoJson.IsObject()) { spdlog::error("Failed reading masterserver authentication response: root object is not an object"); - goto REQUEST_END_CLEANUP; + return; } if (connectionInfoJson.HasMember("error")) @@ -685,13 +694,13 @@ void MasterServerManager::AuthenticateWithServer(const char* uid, const char* pl else m_sAuthFailureReason = "No error message provided"; - goto REQUEST_END_CLEANUP; + return; } if (!connectionInfoJson["success"].IsTrue()) { spdlog::error("Authentication with masterserver failed: \"success\" is not true"); - goto REQUEST_END_CLEANUP; + return; } if (!connectionInfoJson.HasMember("success") || !connectionInfoJson.HasMember("ip") || @@ -700,7 +709,7 @@ void MasterServerManager::AuthenticateWithServer(const char* uid, const char* pl !connectionInfoJson["authToken"].IsString()) { spdlog::error("Failed reading masterserver authentication response: malformed json object"); - goto REQUEST_END_CLEANUP; + return; } m_pendingConnectionInfo.ip.S_un.S_addr = inet_addr(connectionInfoJson["ip"].GetString()); @@ -725,11 +734,6 @@ void MasterServerManager::AuthenticateWithServer(const char* uid, const char* pl m_bSuccessfullyAuthenticatedWithGameServer = false; m_bScriptAuthenticatingWithGameServer = false; } - - REQUEST_END_CLEANUP: - m_bAuthenticatingWithGameServer = false; - m_bScriptAuthenticatingWithGameServer = false; - curl_easy_cleanup(curl); }); requestThread.detach(); -- cgit v1.2.3