diff --git a/r5dev/networksystem/pylon.cpp b/r5dev/networksystem/pylon.cpp index fd9dfc67..367add68 100644 --- a/r5dev/networksystem/pylon.cpp +++ b/r5dev/networksystem/pylon.cpp @@ -25,15 +25,17 @@ vector CPylon::GetServerList(string& svOutMessage) const nlohmann::json jsRequestBody = nlohmann::json::object(); jsRequestBody["version"] = SDK_VERSION; - string svRequestBody = jsRequestBody.dump(4); - string svResponse; + const string svRequestBody = jsRequestBody.dump(4); + const bool bDebug = pylon_showdebuginfo->GetBool(); - if (pylon_showdebuginfo->GetBool()) + if (bDebug) { DevMsg(eDLL_T::ENGINE, "%s - Sending server list request to comp-server:\n%s\n", __FUNCTION__, svRequestBody.c_str()); } + string svResponse; CURLINFO status; + if (!QueryMasterServer(pylon_matchmaking_hostname->GetString(), "/servers", svRequestBody, svResponse, svOutMessage, status)) { return vslList; @@ -72,41 +74,12 @@ vector CPylon::GetServerList(string& svOutMessage) const } else { - if (jsResultBody["error"].is_string()) - { - svOutMessage = jsResultBody["error"].get(); - } - else - { - svOutMessage = Format("Unknown error with status: %d", static_cast(status)); - } + ExtractError(jsResultBody, svOutMessage, status); } } else { - if (status) - { - if (!svResponse.empty()) - { - nlohmann::json jsResultBody = nlohmann::json::parse(svResponse); - - if (jsResultBody["error"].is_string()) - { - svOutMessage = jsResultBody["error"].get(); - } - else - { - svOutMessage = Format("Server list error: %d", static_cast(status)); - } - - return vslList; - } - - svOutMessage = Format("Failed comp-server query: %d", static_cast(status)); - return vslList; - } - - svOutMessage = Format("Failed to reach comp-server: %s", "connection timed-out"); + ExtractError(svResponse, svOutMessage, status, "Server list error"); return vslList; } } @@ -130,9 +103,7 @@ bool CPylon::GetServerByToken(NetGameServer_t& slOutServer, string& svOutMessage nlohmann::json jsRequestBody = nlohmann::json::object(); jsRequestBody["token"] = svToken; - string svRequestBody = jsRequestBody.dump(4); - string svResponseBuf; - + const string svRequestBody = jsRequestBody.dump(4); const bool bDebugLog = pylon_showdebuginfo->GetBool(); if (bDebugLog) @@ -140,7 +111,9 @@ bool CPylon::GetServerByToken(NetGameServer_t& slOutServer, string& svOutMessage DevMsg(eDLL_T::ENGINE, "%s - Sending token connect request to comp-server:\n%s\n", __FUNCTION__, svRequestBody.c_str()); } + string svResponseBuf; CURLINFO status; + if (!QueryMasterServer(pylon_matchmaking_hostname->GetString(), "/server/byToken", svRequestBody, svResponseBuf, svOutMessage, status)) { return false; @@ -187,43 +160,13 @@ bool CPylon::GetServerByToken(NetGameServer_t& slOutServer, string& svOutMessage } else { - if (jsResultBody["error"].is_string()) - { - svOutMessage = jsResultBody["error"].get(); - } - else - { - svOutMessage = Format("Unknown error with status: %d", static_cast(status)); - } - + ExtractError(jsResultBody, svOutMessage, status); return false; } } else { - if (status) - { - if (!svResponseBuf.empty()) - { - nlohmann::json jsResultBody = nlohmann::json::parse(svResponseBuf); - - if (jsResultBody["error"].is_string()) - { - svOutMessage = jsResultBody["error"].get(); - } - else - { - svOutMessage = Format("Server not found: %d", static_cast(status)); - } - - return false; - } - - svOutMessage = Format("Failed comp-server query: %d", static_cast(status)); - return false; - } - - svOutMessage = Format("Failed to reach comp-server: %s", "connection timed-out"); + ExtractError(svResponseBuf, svOutMessage, status, "Server not found"); return false; } } @@ -309,45 +252,17 @@ bool CPylon::PostServerHost(string& svOutMessage, string& svOutToken, const NetG } else { - if (jsResultBody["error"].is_string()) - { - svOutMessage = jsResultBody["error"].get(); - } - else - { - svOutMessage = Format("Unknown error with status: %d", static_cast(status)); - } + ExtractError(jsResultBody, svOutMessage, status); + svOutToken.clear(); + return false; } } else { - if (status) - { - if (!svResponseBuf.empty()) - { - nlohmann::json jsResultBody = nlohmann::json::parse(svResponseBuf); - - if (jsResultBody["error"].is_string()) - { - svOutMessage = jsResultBody["error"].get(); - } - else - { - svOutMessage = Format("Server host error: %d", static_cast(status)); - } - - svOutToken.clear(); - return false; - } - - svOutMessage = Format("Failed comp-server query: %d", static_cast(status)); - svOutToken.clear(); - return false; - } - - svOutMessage = Format("Failed to reach comp-server: %s", "connection timed-out"); + ExtractError(svResponseBuf, svOutMessage, status, "Server host error"); svOutToken.clear(); + return false; } } @@ -500,5 +415,53 @@ bool CPylon::QueryMasterServer(const string& svHostName, const string& svApi, co return true; } +//----------------------------------------------------------------------------- +// Purpose: Extracts the error from the result body. +// Input : &resultBody - +// &outMessage - +// status - +// *errorText - +//----------------------------------------------------------------------------- +void CPylon::ExtractError(const nlohmann::json& resultBody, string& outMessage, CURLINFO status, const char* errorText) const +{ + if (resultBody["error"].is_string()) + { + outMessage = resultBody["error"].get(); + } + else + { + if (!errorText) + { + errorText = "Unknown error with status"; + } + + outMessage = Format("%s: %d", errorText, static_cast(status)); + } +} + +//----------------------------------------------------------------------------- +// Purpose: Extracts the error from the response buffer. +// Input : &resultBody - +// &outMessage - +// status - +// *errorText - +//----------------------------------------------------------------------------- +void CPylon::ExtractError(const string& responseBuffer, string& outMessage, CURLINFO status, const char* errorText) const +{ + if (!responseBuffer.empty()) + { + nlohmann::json resultBody = nlohmann::json::parse(responseBuffer); + ExtractError(resultBody, outMessage, status, errorText); + } + else if (status) + { + outMessage = Format("Failed comp-server query: %d", static_cast(status)); + } + else + { + outMessage = Format("Failed to reach comp-server: %s", "connection timed-out"); + } +} + /////////////////////////////////////////////////////////////////////////////// CPylon* g_pMasterServer(new CPylon()); diff --git a/r5dev/networksystem/pylon.h b/r5dev/networksystem/pylon.h index e9d7c918..d6565198 100644 --- a/r5dev/networksystem/pylon.h +++ b/r5dev/networksystem/pylon.h @@ -11,6 +11,9 @@ public: bool CheckForBan(const string& svIpAddress, const uint64_t nNucleusID, string& svOutReason) const; bool QueryMasterServer(const string& svHostName, const string& svApi, const string& svRequest, string& svResponse, string& svOutMessage, CURLINFO& status) const; + void ExtractError(const nlohmann::json& resultBody, string& outMessage, CURLINFO status, const char* errorText = nullptr) const; + void ExtractError(const string& responseBuffer, string& outMessage, CURLINFO status, const char* messageText = nullptr) const; + #ifdef DEDICATED bool KeepAlive(const NetGameServer_t& netGameServer);