From 0774ebf9fff91edaf20297457ec713a589077668 Mon Sep 17 00:00:00 2001 From: Amos <48657826+Mauler125@users.noreply.github.com> Date: Sun, 27 Feb 2022 19:47:29 +0100 Subject: [PATCH] Fix exception during SDK shutdown Run in a fixed interval manner within same thread instead of separate threads. --- r5dev/core/init.cpp | 8 +++---- r5dev/engine/host_state.cpp | 43 ++++++++++++++++++++----------------- r5dev/engine/host_state.h | 3 ++- 3 files changed, 29 insertions(+), 25 deletions(-) diff --git a/r5dev/core/init.cpp b/r5dev/core/init.cpp index b1db0fe3..16c02730 100644 --- a/r5dev/core/init.cpp +++ b/r5dev/core/init.cpp @@ -92,7 +92,7 @@ void Systems_Init() int nError = ::WSAStartup(MAKEWORD(2, 2), &wsaData); if (nError != 0) { - std::cerr << "Failed to start Winsock via WSAStartup: (" << NET_ErrorString(WSAGetLastError()) << ")." << std::endl; + std::cerr << "Failed to start Winsock via WSAStartup: (" << NET_ErrorString(WSAGetLastError()) << ")" << std::endl; } // Begin the detour transaction to hook the the process @@ -102,7 +102,7 @@ void Systems_Init() // Hook functions IApplication_Attach(); #ifdef DEDICATED - PRX_Attach(); + //PRX_Attach(); #endif // DEDICATED CBaseClient_Attach(); CBaseFileSystem_Attach(); @@ -172,7 +172,7 @@ void Systems_Shutdown() int nError = ::WSACleanup(); if (nError != 0) { - std::cerr << "Failed to stop winsock via WSACleanup: (" << NET_ErrorString(WSAGetLastError()) << ")." << std::endl; + std::cerr << "Failed to stop winsock via WSACleanup: (" << NET_ErrorString(WSAGetLastError()) << ")" << std::endl; } // Begin the detour transaction to unhook the the process @@ -182,7 +182,7 @@ void Systems_Shutdown() // Unhook functions IApplication_Detach(); #ifdef DEDICATED - PRX_Detach(); + //PRX_Detach(); #endif // DEDICATED CBaseClient_Detach(); CBaseFileSystem_Detach(); diff --git a/r5dev/engine/host_state.cpp b/r5dev/engine/host_state.cpp index 1317d99c..72771094 100644 --- a/r5dev/engine/host_state.cpp +++ b/r5dev/engine/host_state.cpp @@ -28,6 +28,9 @@ #include "public/include/bansystem.h" #include "game/server/gameinterface.h" +std::chrono::time_point tpPylonStartClock = std::chrono::steady_clock::now(); +std::chrono::time_point tpBanListStartClock = std::chrono::steady_clock::now(); + //----------------------------------------------------------------------------- // Purpose: state machine's main processing loop //----------------------------------------------------------------------------- @@ -55,7 +58,6 @@ FORCEINLINE void CHostState::FrameUpdate(void* rcx, void* rdx, float time) else { *g_ServerAbortServer = true; - do { Cbuf_Execute(); @@ -82,6 +84,7 @@ FORCEINLINE void CHostState::FrameUpdate(void* rcx, void* rdx, float time) } case HostStates_t::HS_RUN: { + g_pHostState->Think(); State_RunFn(&g_pHostState->m_iCurrentState, nullptr, time); break; } @@ -120,7 +123,6 @@ FORCEINLINE void CHostState::FrameUpdate(void* rcx, void* rdx, float time) } while ((oldState != HostStates_t::HS_RUN || g_pHostState->m_iNextState == HostStates_t::HS_LOAD_GAME && g_pCVar->FindVar("single_frame_shutdown_for_reload")->GetBool()) && oldState != HostStates_t::HS_SHUTDOWN && oldState != HostStates_t::HS_RESTART); - } } @@ -141,24 +143,6 @@ FORCEINLINE void CHostState::Setup(void) const ConCommandBase* disconnect = (ConCommandBase*)g_pCVar->FindCommand("disconnect"); disconnect->AddFlags(FCVAR_SERVER_CAN_EXECUTE); // Make sure server is not restricted to this. - static std::thread PylonThread([]() // Pylon request thread. - { - while (true) - { - KeepAliveToPylon(); - std::this_thread::sleep_for(std::chrono::milliseconds(5000)); - } - }); - - static std::thread BanlistThread([]() - { - while (true) - { - g_pBanSystem->BanListCheck(); - std::this_thread::sleep_for(std::chrono::milliseconds(1000)); - } - }); - if (net_userandomkey->GetBool()) { HNET_GenerateKey(); @@ -167,6 +151,25 @@ FORCEINLINE void CHostState::Setup(void) const g_pCVar->FindVar("net_usesocketsforloopback")->SetValue(1); } +//----------------------------------------------------------------------------- +// Purpose: think +//----------------------------------------------------------------------------- +FORCEINLINE void CHostState::Think(void) const +{ + std::chrono::time_point tpCrrentClock = std::chrono::steady_clock::now(); + + if (std::chrono::duration_cast(tpCrrentClock - tpBanListStartClock).count() >= 1) + { + g_pBanSystem->BanListCheck(); + tpBanListStartClock = std::chrono::steady_clock::now(); + } + if (std::chrono::duration_cast(tpCrrentClock - tpPylonStartClock).count() >= 5) + { + KeepAliveToPylon(); + tpPylonStartClock = std::chrono::steady_clock::now(); + } +} + //----------------------------------------------------------------------------- // Purpose: load and execute configuration files //----------------------------------------------------------------------------- diff --git a/r5dev/engine/host_state.h b/r5dev/engine/host_state.h index 64a8993e..de2ad21a 100644 --- a/r5dev/engine/host_state.h +++ b/r5dev/engine/host_state.h @@ -17,9 +17,10 @@ class CHostState { public: FORCEINLINE static void FrameUpdate(void* rcx, void* rdx, float time); + FORCEINLINE void LoadConfig(void) const; FORCEINLINE void Setup(void) const; - FORCEINLINE void LoadConfig(void) const; + FORCEINLINE void Think(void) const; FORCEINLINE void State_NewGame(void); FORCEINLINE void GameShutDown(void);