Fix Pylon backend not working on dedicated server

Also fix dual thread for backend, and main thread pausing when request is slow
This commit is contained in:
Kawe Mazidjatari 2022-03-26 18:06:54 +01:00
parent 4e9ae9c350
commit 4cbb4746bd
7 changed files with 56 additions and 44 deletions

View File

@ -94,7 +94,6 @@ FORCEINLINE void CHostState::FrameUpdate(void* rcx, void* rdx, float time)
} }
case HostStates_t::HS_RUN: case HostStates_t::HS_RUN:
{ {
g_pHostState->Think();
State_RunFn(&g_pHostState->m_iCurrentState, nullptr, time); State_RunFn(&g_pHostState->m_iCurrentState, nullptr, time);
break; break;
} }
@ -153,6 +152,9 @@ FORCEINLINE void CHostState::Setup(void) const
g_pRConClient->Init(); g_pRConClient->Init();
#endif // DEDICATED #endif // DEDICATED
std::thread t1(&CHostState::Think, this);
t1.detach();
*reinterpret_cast<bool*>(m_bRestrictServerCommands) = true; // Restrict commands. *reinterpret_cast<bool*>(m_bRestrictServerCommands) = true; // Restrict commands.
ConCommandBase* disconnect = g_pCVar->FindCommandBase("disconnect"); ConCommandBase* disconnect = g_pCVar->FindCommandBase("disconnect");
disconnect->AddFlags(FCVAR_SERVER_CAN_EXECUTE); // Make sure server is not restricted to this. disconnect->AddFlags(FCVAR_SERVER_CAN_EXECUTE); // Make sure server is not restricted to this.
@ -182,12 +184,15 @@ FORCEINLINE void CHostState::Think(void) const
static CFastTimer statsTimer; static CFastTimer statsTimer;
static ConVar* hostname = g_pCVar->FindVar("hostname"); static ConVar* hostname = g_pCVar->FindVar("hostname");
for (;;) // Loop running at 20-tps.
{
if (!bInitialized) // Initialize clocks. if (!bInitialized) // Initialize clocks.
{ {
banListTimer.Start(); banListTimer.Start();
#ifdef DEDICATED
pylonTimer.Start(); pylonTimer.Start();
#endif // DEDICATED
statsTimer.Start(); statsTimer.Start();
bInitialized = true; bInitialized = true;
} }
@ -196,11 +201,13 @@ FORCEINLINE void CHostState::Think(void) const
g_pBanSystem->BanListCheck(); g_pBanSystem->BanListCheck();
banListTimer.Start(); banListTimer.Start();
} }
#ifdef DEDICATED
if (pylonTimer.GetDurationInProgress().GetSeconds() > 5.0) if (pylonTimer.GetDurationInProgress().GetSeconds() > 5.0)
{ {
KeepAliveToPylon(); KeepAliveToPylon();
pylonTimer.Start(); pylonTimer.Start();
} }
#endif // DEDICATED
if (statsTimer.GetDurationInProgress().GetSeconds() > 1.0) if (statsTimer.GetDurationInProgress().GetSeconds() > 1.0)
{ {
std::string svCurrentPlaylist = KeyValues_GetCurrentPlaylist(); std::string svCurrentPlaylist = KeyValues_GetCurrentPlaylist();
@ -210,6 +217,8 @@ FORCEINLINE void CHostState::Think(void) const
hostname->GetString(), nPlayerCount, g_ServerGlobalVariables->m_nMaxClients, svCurrentPlaylist.c_str(), m_levelName).c_str()); hostname->GetString(), nPlayerCount, g_ServerGlobalVariables->m_nMaxClients, svCurrentPlaylist.c_str(), m_levelName).c_str());
statsTimer.Start(); statsTimer.Start();
} }
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------

View File

@ -640,16 +640,18 @@ void IBrowser::UpdateHostingStatus(void)
void IBrowser::SendHostingPostRequest(void) void IBrowser::SendHostingPostRequest(void)
{ {
#ifndef GAMECLIENTONLY #ifndef GAMECLIENTONLY
static ConVar* hostport = g_pCVar->FindVar("hostport");
static ConVar* mp_gamemode = g_pCVar->FindVar("mp_gamemode");
m_szHostToken = std::string(); m_szHostToken = std::string();
DevMsg(eDLL_T::CLIENT, "Sending PostServerHost request\n");
bool result = g_pR5net->PostServerHost(m_szHostRequestMessage, m_szHostToken, bool result = g_pR5net->PostServerHost(m_szHostRequestMessage, m_szHostToken,
ServerListing ServerListing
{ {
m_Server.svServerName, m_Server.svServerName.c_str(),
std::string(g_pHostState->m_levelName), std::string(g_pHostState->m_levelName),
"", "",
g_pCVar->FindVar("hostport")->GetString(), hostport->GetString(),
g_pCVar->FindVar("mp_gamemode")->GetString(), mp_gamemode->GetString(),
m_Server.bHidden, m_Server.bHidden,
std::to_string(*g_nClientRemoteChecksum), std::to_string(*g_nClientRemoteChecksum),

View File

@ -29,7 +29,6 @@ void KeepAliveToPylon()
std::string m_szHostToken = std::string(); std::string m_szHostToken = std::string();
std::string m_szHostRequestMessage = std::string(); std::string m_szHostRequestMessage = std::string();
DevMsg(eDLL_T::SERVER, "Sending PostServerHost request\n");
bool result = g_pR5net->PostServerHost(m_szHostRequestMessage, m_szHostToken, bool result = g_pR5net->PostServerHost(m_szHostRequestMessage, m_szHostToken,
ServerListing{ ServerListing{
hostname->GetString(), hostname->GetString(),

View File

@ -21,17 +21,18 @@ std::vector<ServerListing> R5Net::Client::GetServersList(std::string& svOutMessa
{ {
std::vector<ServerListing> vslList{}; std::vector<ServerListing> vslList{};
nlohmann::json jsReqBody = nlohmann::json::object(); nlohmann::json jsRequestBody = nlohmann::json::object();
jsReqBody["version"] = GetSDKVersion(); jsRequestBody["version"] = GetSDKVersion();
std::string reqBodyStr = jsReqBody.dump(); std::string svRequestBody = jsRequestBody.dump(4);
if (r5net_show_debug->GetBool()) if (r5net_show_debug->GetBool())
{ {
DevMsg(eDLL_T::ENGINE, "Sending GetServerList post.\n"); DevMsg(eDLL_T::ENGINE, "Sending GetServerList post.\n");
DevMsg(eDLL_T::ENGINE, "%s - Sending server list request to comp-server:\n%s\n", __FUNCTION__, svRequestBody.c_str());
} }
httplib::Result htResults = m_HttpClient.Post("/servers", jsReqBody.dump().c_str(), jsReqBody.dump().length(), "application/json"); httplib::Result htResults = m_HttpClient.Post("/servers", jsRequestBody.dump().c_str(), jsRequestBody.dump().length(), "application/json");
if (htResults && r5net_show_debug->GetBool()) if (htResults && r5net_show_debug->GetBool())
{ {
@ -122,18 +123,18 @@ bool R5Net::Client::PostServerHost(std::string& svOutMessage, std::string& svOut
jsRequestBody["encKey"] = slServerListing.svEncryptionKey; jsRequestBody["encKey"] = slServerListing.svEncryptionKey;
jsRequestBody["hidden"] = slServerListing.bHidden; jsRequestBody["hidden"] = slServerListing.bHidden;
std::string svRequestBody = jsRequestBody.dump(); std::string svRequestBody = jsRequestBody.dump(4);
if (r5net_show_debug->GetBool()) if (r5net_show_debug->GetBool())
{ {
DevMsg(eDLL_T::ENGINE, "Sending PostServerHost post '%s'.\n", svRequestBody.c_str()); DevMsg(eDLL_T::ENGINE, "%s - Sending post host request to comp-server:\n%s\n", __FUNCTION__, svRequestBody.c_str());
} }
httplib::Result htResults = m_HttpClient.Post("/servers/add", svRequestBody.c_str(), svRequestBody.length(), "application/json"); httplib::Result htResults = m_HttpClient.Post("/servers/add", svRequestBody.c_str(), svRequestBody.length(), "application/json");
if (htResults && r5net_show_debug->GetBool()) if (htResults && r5net_show_debug->GetBool())
{ {
DevMsg(eDLL_T::ENGINE, "PostServerHost replied with '%d'.\n", htResults->status); DevMsg(eDLL_T::ENGINE, "%s - Comp-server replied with '%d'\n", __FUNCTION__, htResults->status);
} }
if (htResults && htResults->status == 200) // STATUS_OK if (htResults && htResults->status == 200) // STATUS_OK
@ -209,19 +210,20 @@ bool R5Net::Client::PostServerHost(std::string& svOutMessage, std::string& svOut
bool R5Net::Client::GetServerByToken(ServerListing& slOutServer, std::string& svOutMessage, const std::string svToken) bool R5Net::Client::GetServerByToken(ServerListing& slOutServer, std::string& svOutMessage, const std::string svToken)
{ {
nlohmann::json jsRequestBody = nlohmann::json::object(); nlohmann::json jsRequestBody = nlohmann::json::object();
jsRequestBody["token"] = svToken; jsRequestBody["token"] = svToken;
std::string svRequestBody = jsRequestBody.dump(4);
if (r5net_show_debug->GetBool()) if (r5net_show_debug->GetBool())
{ {
DevMsg(eDLL_T::ENGINE, "Sending GetServerByToken post.\n"); DevMsg(eDLL_T::ENGINE, "%s - Sending token connect request to comp-server:\n%s\n", __FUNCTION__, svRequestBody.c_str());
} }
httplib::Result htResults = m_HttpClient.Post("/server/byToken", jsRequestBody.dump().c_str(), jsRequestBody.dump().length(), "application/json"); httplib::Result htResults = m_HttpClient.Post("/server/byToken", jsRequestBody.dump().c_str(), jsRequestBody.dump().length(), "application/json");
if (r5net_show_debug->GetBool()) if (r5net_show_debug->GetBool())
{ {
DevMsg(eDLL_T::ENGINE, "GetServerByToken replied with '%d'\n", htResults->status); DevMsg(eDLL_T::ENGINE, "%s - Comp-server replied with '%d'\n", __FUNCTION__, htResults->status);
} }
if (htResults && htResults->status == 200) // STATUS_OK if (htResults && htResults->status == 200) // STATUS_OK

View File

@ -140,7 +140,7 @@ void ConVar::Init(void) const
r5net_show_debug = new ConVar("r5net_show_debug" , "1" , FCVAR_DEVELOPMENTONLY, "Shows debug output for R5Net.", false, 0.f, false, 0.f, nullptr, nullptr); r5net_show_debug = new ConVar("r5net_show_debug" , "1" , FCVAR_DEVELOPMENTONLY, "Shows debug output for R5Net.", false, 0.f, false, 0.f, nullptr, nullptr);
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
// RTECH API | // RTECH API |
//-------------------------------------------------------------------------
// RUI | // RUI |
#ifndef DEDICATED #ifndef DEDICATED
rui_drawEnable = new ConVar("rui_drawEnable", "1", FCVAR_RELEASE, "Draws the RUI, 1 = Draw, 0 = No Draw.", false, 0.f, false, 0.f, nullptr, nullptr); rui_drawEnable = new ConVar("rui_drawEnable", "1", FCVAR_RELEASE, "Draws the RUI, 1 = Draw, 0 = No Draw.", false, 0.f, false, 0.f, nullptr, nullptr);
@ -505,7 +505,7 @@ void ConVar::ChangeStringValue(const char* pszTempVal, float flOldValue)
m_Value.m_pszString = NULL; m_Value.m_pszString = NULL;
} }
pszOldValue = 0; pszOldValue = NULL;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------

View File

@ -97,7 +97,7 @@ ConVar* r5net_matchmaking_hostname = nullptr;
ConVar* r5net_show_debug = nullptr; ConVar* r5net_show_debug = nullptr;
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// RTECH API | // RTECH API |
//-----------------------------------------------------------------------------
// RUI | // RUI |
#ifndef DEDICATED #ifndef DEDICATED
ConVar* rui_drawEnable = nullptr; ConVar* rui_drawEnable = nullptr;

View File

@ -107,7 +107,7 @@ extern ConVar* r5net_matchmaking_hostname;
extern ConVar* r5net_show_debug; extern ConVar* r5net_show_debug;
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// RTECH API | // RTECH API |
//-----------------------------------------------------------------------------
// RUI | // RUI |
#ifndef DEDICATED #ifndef DEDICATED
extern ConVar* rui_drawEnable; extern ConVar* rui_drawEnable;