2022-02-19 02:31:16 +01:00
|
|
|
//=====================================================================================//
|
|
|
|
//
|
|
|
|
// Purpose: Implementation of the pylon server backend.
|
|
|
|
//
|
|
|
|
// $NoKeywords: $
|
|
|
|
//=====================================================================================//
|
|
|
|
|
|
|
|
#include <core/stdafx.h>
|
2022-04-09 16:16:40 +02:00
|
|
|
#include <tier1/cvar.h>
|
2023-01-26 20:06:48 +01:00
|
|
|
#include <tier2/curlutils.h>
|
2022-02-19 02:31:16 +01:00
|
|
|
#include <networksystem/pylon.h>
|
2022-08-28 17:40:03 +02:00
|
|
|
#ifndef CLIENT_DLL
|
|
|
|
#include <engine/server/server.h>
|
|
|
|
#endif // !CLIENT_DLL
|
2022-02-19 02:31:16 +01:00
|
|
|
|
2022-07-01 10:29:27 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
2023-04-24 01:55:37 +02:00
|
|
|
// Purpose: gets a vector of hosted servers.
|
|
|
|
// Input : &outMessage -
|
2023-01-26 20:06:48 +01:00
|
|
|
// Output : vector<NetGameServer_t>
|
2022-07-01 10:29:27 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
2023-04-24 01:55:37 +02:00
|
|
|
vector<NetGameServer_t> CPylon::GetServerList(string& outMessage) const
|
2022-07-01 10:29:27 +02:00
|
|
|
{
|
2023-04-24 01:55:37 +02:00
|
|
|
vector<NetGameServer_t> vecServers;
|
2022-07-01 10:29:27 +02:00
|
|
|
|
2023-04-24 01:55:37 +02:00
|
|
|
nlohmann::json requestJson = nlohmann::json::object();
|
|
|
|
requestJson["version"] = SDK_VERSION;
|
2022-07-01 10:29:27 +02:00
|
|
|
|
2023-04-24 01:55:37 +02:00
|
|
|
string responseBody;
|
2023-01-26 20:06:48 +01:00
|
|
|
CURLINFO status;
|
2023-04-24 00:32:27 +02:00
|
|
|
|
2023-04-24 01:55:37 +02:00
|
|
|
if (!QueryAPI("/servers", requestJson.dump(4), responseBody, outMessage, status))
|
2022-07-01 10:29:27 +02:00
|
|
|
{
|
2023-04-24 01:55:37 +02:00
|
|
|
return vecServers;
|
2022-07-01 10:29:27 +02:00
|
|
|
}
|
|
|
|
|
2022-08-28 17:12:58 +02:00
|
|
|
try
|
2022-07-01 10:29:27 +02:00
|
|
|
{
|
2023-01-26 20:06:48 +01:00
|
|
|
if (status == 200) // STATUS_OK
|
2022-07-01 10:29:27 +02:00
|
|
|
{
|
2023-04-24 01:55:37 +02:00
|
|
|
nlohmann::json resultJson = nlohmann::json::parse(responseBody);
|
|
|
|
if (resultJson["success"].is_boolean() && resultJson["success"].get<bool>())
|
2022-07-01 10:29:27 +02:00
|
|
|
{
|
2023-04-24 01:55:37 +02:00
|
|
|
for (auto& obj : resultJson["servers"])
|
2022-08-28 17:12:58 +02:00
|
|
|
{
|
2023-04-24 01:55:37 +02:00
|
|
|
vecServers.push_back(
|
2022-08-28 17:12:58 +02:00
|
|
|
NetGameServer_t
|
|
|
|
{
|
|
|
|
obj.value("name",""),
|
|
|
|
obj.value("description",""),
|
|
|
|
obj.value("hidden","false") == "true",
|
|
|
|
obj.value("map",""),
|
|
|
|
obj.value("playlist",""),
|
|
|
|
obj.value("ip",""),
|
|
|
|
obj.value("port", ""),
|
|
|
|
obj.value("key",""),
|
|
|
|
obj.value("checksum",""),
|
|
|
|
obj.value("version", SDK_VERSION),
|
|
|
|
obj.value("playerCount", ""),
|
|
|
|
obj.value("maxPlayers", ""),
|
|
|
|
obj.value("timeStamp", 0),
|
|
|
|
obj.value("publicRef", ""),
|
2022-09-09 19:47:31 +02:00
|
|
|
obj.value("cachedId", ""),
|
2022-08-28 17:12:58 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2022-07-01 10:29:27 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-24 01:55:37 +02:00
|
|
|
ExtractError(resultJson, outMessage, status);
|
2022-08-28 17:12:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-24 01:55:37 +02:00
|
|
|
ExtractError(responseBody, outMessage, status, "Server list error");
|
|
|
|
return vecServers;
|
2022-07-01 10:29:27 +02:00
|
|
|
}
|
2022-08-28 17:12:58 +02:00
|
|
|
}
|
|
|
|
catch (const std::exception& ex)
|
|
|
|
{
|
|
|
|
Warning(eDLL_T::ENGINE, "%s - Exception while parsing comp-server response:\n%s\n", __FUNCTION__, ex.what());
|
2022-07-01 10:29:27 +02:00
|
|
|
}
|
|
|
|
|
2023-04-24 01:55:37 +02:00
|
|
|
return vecServers;
|
2022-07-01 10:29:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-08-30 12:07:09 +02:00
|
|
|
// Purpose: Gets the server by token string.
|
2023-04-24 01:55:37 +02:00
|
|
|
// Input : &outGameServer -
|
|
|
|
// &outMessage -
|
|
|
|
// &token -
|
2022-07-01 10:29:27 +02:00
|
|
|
// Output : Returns true on success, false on failure.
|
|
|
|
//-----------------------------------------------------------------------------
|
2023-04-24 01:55:37 +02:00
|
|
|
bool CPylon::GetServerByToken(NetGameServer_t& outGameServer,
|
|
|
|
string& outMessage, const string& token) const
|
2022-07-01 10:29:27 +02:00
|
|
|
{
|
2023-04-24 01:55:37 +02:00
|
|
|
nlohmann::json requestJson = nlohmann::json::object();
|
|
|
|
requestJson["token"] = token;
|
2023-01-26 21:11:32 +01:00
|
|
|
|
2023-04-24 01:55:37 +02:00
|
|
|
string responseBody;
|
2023-01-26 20:06:48 +01:00
|
|
|
CURLINFO status;
|
2023-04-24 00:32:27 +02:00
|
|
|
|
2023-04-24 01:55:37 +02:00
|
|
|
if (!QueryAPI("/server/byToken", requestJson.dump(4), responseBody, outMessage, status))
|
2023-01-26 20:06:48 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2022-08-27 18:57:56 +02:00
|
|
|
|
2022-08-28 17:12:58 +02:00
|
|
|
try
|
2022-07-01 10:29:27 +02:00
|
|
|
{
|
2023-01-26 20:06:48 +01:00
|
|
|
if (status == 200) // STATUS_OK
|
2022-07-01 10:29:27 +02:00
|
|
|
{
|
2023-04-24 01:55:37 +02:00
|
|
|
nlohmann::json responseJson = nlohmann::json::parse(responseBody);
|
2023-02-04 19:18:18 +01:00
|
|
|
|
2023-04-24 01:55:37 +02:00
|
|
|
if (pylon_showdebuginfo->GetBool())
|
2022-07-01 10:29:27 +02:00
|
|
|
{
|
2023-04-24 01:55:37 +02:00
|
|
|
responseBody = responseJson.dump(4);
|
|
|
|
DevMsg(eDLL_T::ENGINE, "%s: Response body:\n%s\n",
|
|
|
|
__FUNCTION__, responseBody.c_str());
|
2023-02-04 19:18:18 +01:00
|
|
|
}
|
2022-07-01 10:29:27 +02:00
|
|
|
|
2023-04-24 01:55:37 +02:00
|
|
|
if (responseJson["success"].is_boolean() && responseJson["success"])
|
2023-02-04 19:18:18 +01:00
|
|
|
{
|
2023-04-24 01:55:37 +02:00
|
|
|
outGameServer = NetGameServer_t
|
2022-07-01 10:29:27 +02:00
|
|
|
{
|
2023-04-24 01:55:37 +02:00
|
|
|
responseJson["server"].value("name",""),
|
|
|
|
responseJson["server"].value("description",""),
|
|
|
|
responseJson["server"].value("hidden","false") == "true",
|
|
|
|
responseJson["server"].value("map",""),
|
|
|
|
responseJson["server"].value("playlist",""),
|
|
|
|
responseJson["server"].value("ip",""),
|
|
|
|
responseJson["server"].value("port", ""),
|
|
|
|
responseJson["server"].value("key",""),
|
|
|
|
responseJson["server"].value("checksum",""),
|
|
|
|
responseJson["server"].value("version", SDK_VERSION),
|
|
|
|
responseJson["server"].value("playerCount", ""),
|
|
|
|
responseJson["server"].value("maxPlayers", ""),
|
|
|
|
responseJson["server"].value("timeStamp", 0),
|
|
|
|
responseJson["server"].value("publicRef", ""),
|
|
|
|
responseJson["server"].value("cachedId", ""),
|
2023-02-04 19:18:18 +01:00
|
|
|
};
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-24 01:55:37 +02:00
|
|
|
ExtractError(responseJson, outMessage, status);
|
2023-02-04 19:18:18 +01:00
|
|
|
return false;
|
2022-08-28 17:12:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-24 01:55:37 +02:00
|
|
|
ExtractError(responseBody, outMessage, status, "Server not found");
|
2022-07-01 10:29:27 +02:00
|
|
|
return false;
|
|
|
|
}
|
2022-08-28 17:12:58 +02:00
|
|
|
}
|
|
|
|
catch (const std::exception& ex)
|
|
|
|
{
|
|
|
|
Warning(eDLL_T::ENGINE, "%s - Exception while parsing comp-server response:\n%s\n", __FUNCTION__, ex.what());
|
2022-07-01 10:29:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-08-30 12:07:09 +02:00
|
|
|
// Purpose: Sends host server POST request.
|
2023-04-24 01:55:37 +02:00
|
|
|
// Input : &outMessage -
|
|
|
|
// &outToken -
|
2023-01-26 20:06:48 +01:00
|
|
|
// &netGameServer -
|
2022-07-01 10:29:27 +02:00
|
|
|
// Output : Returns true on success, false on failure.
|
|
|
|
//-----------------------------------------------------------------------------
|
2023-04-24 01:55:37 +02:00
|
|
|
bool CPylon::PostServerHost(string& outMessage, string& outToken, const NetGameServer_t& netGameServer) const
|
2022-07-01 10:29:27 +02:00
|
|
|
{
|
2023-04-24 01:55:37 +02:00
|
|
|
nlohmann::json requestJson = nlohmann::json::object();
|
|
|
|
requestJson["name"] = netGameServer.m_svHostName;
|
|
|
|
requestJson["description"] = netGameServer.m_svDescription;
|
|
|
|
requestJson["hidden"] = netGameServer.m_bHidden;
|
|
|
|
requestJson["map"] = netGameServer.m_svHostMap;
|
|
|
|
requestJson["playlist"] = netGameServer.m_svPlaylist;
|
|
|
|
requestJson["ip"] = netGameServer.m_svIpAddress;
|
|
|
|
requestJson["port"] = netGameServer.m_svGamePort;
|
|
|
|
requestJson["key"] = netGameServer.m_svEncryptionKey;
|
|
|
|
requestJson["checksum"] = netGameServer.m_svRemoteChecksum;
|
|
|
|
requestJson["version"] = netGameServer.m_svSDKVersion;
|
|
|
|
requestJson["playerCount"] = netGameServer.m_svPlayerCount;
|
|
|
|
requestJson["maxPlayers"] = netGameServer.m_svMaxPlayers;
|
|
|
|
requestJson["timeStamp"] = netGameServer.m_nTimeStamp;
|
|
|
|
requestJson["publicRef"] = netGameServer.m_svPublicRef;
|
|
|
|
requestJson["cachedId"] = netGameServer.m_svCachedId;
|
|
|
|
|
|
|
|
string responseBody;
|
2023-01-26 20:06:48 +01:00
|
|
|
CURLINFO status;
|
2022-07-01 10:29:27 +02:00
|
|
|
|
2023-04-24 01:55:37 +02:00
|
|
|
if (!QueryAPI("/servers/add", requestJson.dump(4), responseBody, outMessage, status))
|
2022-07-01 10:29:27 +02:00
|
|
|
{
|
2023-04-24 01:55:37 +02:00
|
|
|
return false;
|
2022-07-01 10:29:27 +02:00
|
|
|
}
|
|
|
|
|
2022-08-28 17:12:58 +02:00
|
|
|
try
|
2022-07-01 10:29:27 +02:00
|
|
|
{
|
2023-01-26 20:06:48 +01:00
|
|
|
if (status == 200) // STATUS_OK
|
2022-07-01 10:29:27 +02:00
|
|
|
{
|
2023-04-24 01:55:37 +02:00
|
|
|
nlohmann::json responseJson = nlohmann::json::parse(responseBody);
|
2023-02-04 19:18:18 +01:00
|
|
|
|
2023-04-24 01:55:37 +02:00
|
|
|
if (pylon_showdebuginfo->GetBool())
|
2023-02-04 19:18:18 +01:00
|
|
|
{
|
2023-04-24 01:55:37 +02:00
|
|
|
responseBody = responseJson.dump(4);
|
|
|
|
DevMsg(eDLL_T::ENGINE, "%s: Response body:\n%s\n",
|
|
|
|
__FUNCTION__, responseBody.c_str());
|
2023-02-04 19:18:18 +01:00
|
|
|
}
|
|
|
|
|
2023-04-24 01:55:37 +02:00
|
|
|
if (responseJson["success"].is_boolean() && responseJson["success"].get<bool>())
|
2022-07-01 10:29:27 +02:00
|
|
|
{
|
2023-04-24 01:55:37 +02:00
|
|
|
if (responseJson["token"].is_string())
|
2022-07-01 10:29:27 +02:00
|
|
|
{
|
2023-04-24 01:55:37 +02:00
|
|
|
outToken = responseJson["token"].get<string>();
|
2022-07-01 10:29:27 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-24 01:55:37 +02:00
|
|
|
outMessage = Format("Invalid response with status: %d", static_cast<int>(status));
|
|
|
|
outToken.clear();
|
2022-08-30 12:07:09 +02:00
|
|
|
}
|
2022-07-01 10:29:27 +02:00
|
|
|
|
2022-08-30 12:07:09 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-24 01:55:37 +02:00
|
|
|
ExtractError(responseJson, outMessage, status);
|
|
|
|
outToken.clear();
|
2023-04-24 00:32:27 +02:00
|
|
|
|
2022-08-30 12:07:09 +02:00
|
|
|
return false;
|
2022-07-01 10:29:27 +02:00
|
|
|
}
|
|
|
|
}
|
2022-08-28 17:12:58 +02:00
|
|
|
else
|
2022-07-01 10:29:27 +02:00
|
|
|
{
|
2023-04-24 01:55:37 +02:00
|
|
|
ExtractError(responseBody, outMessage, status, "Server host error");
|
|
|
|
outToken.clear();
|
2023-04-24 00:32:27 +02:00
|
|
|
|
2022-07-01 10:29:27 +02:00
|
|
|
return false;
|
|
|
|
}
|
2022-08-28 17:12:58 +02:00
|
|
|
}
|
|
|
|
catch (const std::exception& ex)
|
|
|
|
{
|
|
|
|
Warning(eDLL_T::ENGINE, "%s - Exception while parsing comp-server response:\n%s\n", __FUNCTION__, ex.what());
|
2022-07-01 10:29:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-02-04 19:18:18 +01:00
|
|
|
#ifdef DEDICATED
|
2022-08-30 12:07:09 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: Send keep alive request to Pylon Master Server.
|
|
|
|
// Input : &netGameServer -
|
|
|
|
// Output : Returns true on success, false otherwise.
|
|
|
|
//-----------------------------------------------------------------------------
|
2023-02-04 19:18:18 +01:00
|
|
|
bool CPylon::KeepAlive(const NetGameServer_t& netGameServer)
|
2022-08-30 12:07:09 +02:00
|
|
|
{
|
2023-02-04 19:18:18 +01:00
|
|
|
if (!g_pServer->IsActive() || !sv_pylonVisibility->GetBool()) // Check for active game.
|
2022-08-30 12:07:09 +02:00
|
|
|
{
|
2023-02-04 19:18:18 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-04-24 01:55:37 +02:00
|
|
|
string errorMsg;
|
|
|
|
string hostToken;
|
2022-08-30 12:07:09 +02:00
|
|
|
|
2023-04-24 01:55:37 +02:00
|
|
|
const bool result = PostServerHost(errorMsg, hostToken, netGameServer);
|
2023-02-04 19:18:18 +01:00
|
|
|
if (!result)
|
|
|
|
{
|
2023-04-24 01:55:37 +02:00
|
|
|
if (!errorMsg.empty() && m_ErrorMsg.compare(errorMsg) != NULL)
|
2023-02-04 19:18:18 +01:00
|
|
|
{
|
2023-04-24 01:55:37 +02:00
|
|
|
m_ErrorMsg = errorMsg;
|
|
|
|
Error(eDLL_T::SERVER, NO_ERROR, "%s\n", errorMsg.c_str());
|
2023-02-04 19:18:18 +01:00
|
|
|
}
|
2022-08-30 12:07:09 +02:00
|
|
|
}
|
2023-02-04 19:18:18 +01:00
|
|
|
else // Attempt to log the token, if there is one.
|
|
|
|
{
|
2023-04-24 01:55:37 +02:00
|
|
|
if (!hostToken.empty() && m_Token.compare(hostToken) != NULL)
|
2023-02-04 19:18:18 +01:00
|
|
|
{
|
2023-04-24 01:55:37 +02:00
|
|
|
m_Token = hostToken;
|
2023-02-04 19:18:18 +01:00
|
|
|
DevMsg(eDLL_T::SERVER, "Published server with token: %s'%s%s%s'\n",
|
|
|
|
g_svReset.c_str(), g_svGreyB.c_str(),
|
2023-04-24 01:55:37 +02:00
|
|
|
hostToken.c_str(), g_svReset.c_str());
|
2023-02-04 19:18:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2022-08-30 12:07:09 +02:00
|
|
|
}
|
2023-02-04 19:18:18 +01:00
|
|
|
#endif // DEDICATED
|
2022-08-30 12:07:09 +02:00
|
|
|
|
2022-07-01 10:29:27 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: Checks if client is banned on the comp server.
|
2023-04-24 01:55:37 +02:00
|
|
|
// Input : &ipAddress -
|
|
|
|
// nucleusId -
|
|
|
|
// &outReason - <- contains banned reason if any.
|
2022-07-01 10:29:27 +02:00
|
|
|
// Output : Returns true if banned, false if not banned.
|
|
|
|
//-----------------------------------------------------------------------------
|
2023-04-24 01:55:37 +02:00
|
|
|
bool CPylon::CheckForBan(const string& ipAddress, const uint64_t nucleusId, string& outReason) const
|
2022-07-01 10:29:27 +02:00
|
|
|
{
|
2023-04-24 01:55:37 +02:00
|
|
|
nlohmann::json requestJson = nlohmann::json::object();
|
|
|
|
requestJson["id"] = nucleusId;
|
|
|
|
requestJson["ip"] = ipAddress;
|
2022-07-01 10:29:27 +02:00
|
|
|
|
2023-04-24 01:55:37 +02:00
|
|
|
string responseBody;
|
|
|
|
string outMessage;
|
2023-01-26 20:06:48 +01:00
|
|
|
CURLINFO status;
|
|
|
|
|
2023-04-24 01:55:37 +02:00
|
|
|
if (!QueryAPI("/banlist/isBanned", requestJson.dump(4), responseBody, outMessage, status))
|
2023-01-26 20:06:48 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2022-08-27 18:57:56 +02:00
|
|
|
|
2023-04-22 22:00:20 +02:00
|
|
|
if (status != 200)
|
|
|
|
{
|
|
|
|
Error(eDLL_T::ENGINE, NO_ERROR, "%s - Failed to query comp-server: status code = %d\n", __FUNCTION__, status);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-08-28 17:12:58 +02:00
|
|
|
try
|
2022-07-01 10:29:27 +02:00
|
|
|
{
|
2023-04-24 01:55:37 +02:00
|
|
|
nlohmann::json responseJson = nlohmann::json::parse(responseBody);
|
2023-01-26 21:11:32 +01:00
|
|
|
|
2023-04-24 01:55:37 +02:00
|
|
|
if (pylon_showdebuginfo->GetBool())
|
2023-04-22 22:00:20 +02:00
|
|
|
{
|
2023-04-24 01:55:37 +02:00
|
|
|
responseBody = responseJson.dump(4);
|
|
|
|
DevMsg(eDLL_T::ENGINE, "%s: Response body:\n%s\n",
|
|
|
|
__FUNCTION__, responseBody.c_str());
|
2023-04-22 22:00:20 +02:00
|
|
|
}
|
2023-01-26 21:11:32 +01:00
|
|
|
|
2023-04-24 01:55:37 +02:00
|
|
|
if (responseJson["success"].is_boolean() && responseJson["success"].get<bool>())
|
2023-04-22 22:00:20 +02:00
|
|
|
{
|
2023-04-24 01:55:37 +02:00
|
|
|
if (responseJson["banned"].is_boolean() && responseJson["banned"].get<bool>())
|
2022-07-01 10:29:27 +02:00
|
|
|
{
|
2023-04-24 01:55:37 +02:00
|
|
|
outReason = responseJson.value("reason", "#DISCONNECT_BANNED");
|
2023-04-22 22:00:20 +02:00
|
|
|
return true;
|
2022-07-01 10:29:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-28 17:12:58 +02:00
|
|
|
catch (const std::exception& ex)
|
|
|
|
{
|
|
|
|
Warning(eDLL_T::ENGINE, "%s - Exception while parsing comp-server response:\n%s\n", __FUNCTION__, ex.what());
|
|
|
|
}
|
2023-04-22 22:00:20 +02:00
|
|
|
|
2022-07-01 10:29:27 +02:00
|
|
|
return false;
|
|
|
|
}
|
2023-01-26 20:06:48 +01:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: Sends query to master server.
|
2023-04-24 01:55:37 +02:00
|
|
|
// Input : &apiName -
|
|
|
|
// &request -
|
|
|
|
// &outResponse -
|
|
|
|
// &outMessage - <- contains an error message if any.
|
|
|
|
// &outStatus -
|
|
|
|
// Output : Returns true if successful, false otherwise.
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
bool CPylon::QueryAPI(const string& apiName, const string& request,
|
|
|
|
string& outResponse, string& outMessage, CURLINFO& outStatus) const
|
|
|
|
{
|
|
|
|
const bool showDebug = pylon_showdebuginfo->GetBool();
|
|
|
|
const char* hostName = pylon_matchmaking_hostname->GetString();
|
|
|
|
|
|
|
|
if (showDebug)
|
|
|
|
{
|
|
|
|
DevMsg(eDLL_T::ENGINE, "%s: Sending '%s' request to '%s':\n%s\n",
|
|
|
|
__FUNCTION__, apiName.c_str(), hostName, request.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!QueryMasterServer(pylon_matchmaking_hostname->GetString(), apiName,
|
|
|
|
request, outResponse, outMessage, outStatus))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (showDebug)
|
|
|
|
{
|
|
|
|
DevMsg(eDLL_T::ENGINE, "%s: Host '%s' replied with status: '%d'\n",
|
|
|
|
__FUNCTION__, hostName, outStatus);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: Sends query to master server.
|
|
|
|
// Input : &hostName -
|
|
|
|
// &apiName -
|
|
|
|
// &request -
|
|
|
|
// &outResponse -
|
|
|
|
// &outMessage - <- contains an error message if any.
|
|
|
|
// &outStatus -
|
2023-01-26 20:06:48 +01:00
|
|
|
// Output : Returns true if successful, false otherwise.
|
|
|
|
//-----------------------------------------------------------------------------
|
2023-04-24 01:55:37 +02:00
|
|
|
bool CPylon::QueryMasterServer(const string& hostName, const string& apiName,
|
|
|
|
const string& request, string& outResponse, string& outMessage,
|
|
|
|
CURLINFO& outStatus) const
|
2023-01-26 20:06:48 +01:00
|
|
|
{
|
2023-04-24 01:55:37 +02:00
|
|
|
string finalUrl;
|
|
|
|
CURLFormatUrl(finalUrl, hostName, apiName);
|
2023-01-26 20:06:48 +01:00
|
|
|
|
|
|
|
curl_slist* sList = nullptr;
|
2023-04-24 01:55:37 +02:00
|
|
|
CURL* curl = CURLInitRequest(finalUrl, request, outResponse, sList);
|
2023-01-26 20:06:48 +01:00
|
|
|
if (!curl)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CURLcode res = CURLSubmitRequest(curl, sList);
|
2023-04-24 01:55:37 +02:00
|
|
|
if (!CURLHandleError(curl, res, outMessage))
|
2023-01-26 20:06:48 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
outStatus = CURLRetrieveInfo(curl);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-04-24 00:32:27 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
2023-04-24 01:55:37 +02:00
|
|
|
// Purpose: Extracts the error from the result json.
|
|
|
|
// Input : &resultJson -
|
2023-04-24 00:32:27 +02:00
|
|
|
// &outMessage -
|
2023-04-24 01:55:37 +02:00
|
|
|
// status -
|
|
|
|
// *errorText -
|
2023-04-24 00:32:27 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
2023-04-24 01:55:37 +02:00
|
|
|
void CPylon::ExtractError(const nlohmann::json& resultJson, string& outMessage,
|
|
|
|
CURLINFO status, const char* errorText) const
|
2023-04-24 00:32:27 +02:00
|
|
|
{
|
2023-04-24 01:55:37 +02:00
|
|
|
if (resultJson["error"].is_string())
|
2023-04-24 00:32:27 +02:00
|
|
|
{
|
2023-04-24 01:55:37 +02:00
|
|
|
outMessage = resultJson["error"].get<string>();
|
2023-04-24 00:32:27 +02:00
|
|
|
}
|
|
|
|
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.
|
2023-04-24 01:55:37 +02:00
|
|
|
// Input : &response -
|
2023-04-24 00:32:27 +02:00
|
|
|
// &outMessage -
|
2023-04-24 01:55:37 +02:00
|
|
|
// status -
|
|
|
|
// *errorText -
|
2023-04-24 00:32:27 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
2023-04-24 01:55:37 +02:00
|
|
|
void CPylon::ExtractError(const string& response, string& outMessage,
|
|
|
|
CURLINFO status, const char* errorText) const
|
2023-04-24 00:32:27 +02:00
|
|
|
{
|
2023-04-24 01:55:37 +02:00
|
|
|
if (!response.empty())
|
2023-04-24 00:32:27 +02:00
|
|
|
{
|
2023-04-24 01:55:37 +02:00
|
|
|
nlohmann::json resultBody = nlohmann::json::parse(response);
|
2023-04-24 00:32:27 +02:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-01 10:29:27 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2022-08-27 18:57:56 +02:00
|
|
|
CPylon* g_pMasterServer(new CPylon());
|