CPylon error handling code deduplication

Deduplicate code.
This commit is contained in:
Kawe Mazidjatari 2023-04-24 00:32:27 +02:00
parent 59b685b7ee
commit aff4b6125e
2 changed files with 68 additions and 102 deletions

View File

@ -25,15 +25,17 @@ vector<NetGameServer_t> 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<NetGameServer_t> CPylon::GetServerList(string& svOutMessage) const
}
else
{
if (jsResultBody["error"].is_string())
{
svOutMessage = jsResultBody["error"].get<string>();
}
else
{
svOutMessage = Format("Unknown error with status: %d", static_cast<int>(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<string>();
}
else
{
svOutMessage = Format("Server list error: %d", static_cast<int>(status));
}
return vslList;
}
svOutMessage = Format("Failed comp-server query: %d", static_cast<int>(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<string>();
}
else
{
svOutMessage = Format("Unknown error with status: %d", static_cast<int>(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<string>();
}
else
{
svOutMessage = Format("Server not found: %d", static_cast<int>(status));
}
return false;
}
svOutMessage = Format("Failed comp-server query: %d", static_cast<int>(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<string>();
}
else
{
svOutMessage = Format("Unknown error with status: %d", static_cast<int>(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<string>();
}
else
{
svOutMessage = Format("Server host error: %d", static_cast<int>(status));
}
svOutToken.clear();
return false;
}
svOutMessage = Format("Failed comp-server query: %d", static_cast<int>(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<string>();
}
else
{
if (!errorText)
{
errorText = "Unknown error with status";
}
outMessage = Format("%s: %d", errorText, static_cast<int>(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<int>(status));
}
else
{
outMessage = Format("Failed to reach comp-server: %s", "connection timed-out");
}
}
///////////////////////////////////////////////////////////////////////////////
CPylon* g_pMasterServer(new CPylon());

View File

@ -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);