From 79e4651d5b162177f5d78b3dfa4465aca4c9918c Mon Sep 17 00:00:00 2001 From: Learath2 Date: Sun, 3 Nov 2019 01:07:10 +0100 Subject: [PATCH] Only update serverinfo when needed --- src/engine/server.h | 1 + src/engine/server/server.cpp | 47 ++++++++++++++++++++------------- src/engine/server/server.h | 4 ++- src/game/server/gamecontext.cpp | 4 +++ 4 files changed, 37 insertions(+), 19 deletions(-) diff --git a/src/engine/server.h b/src/engine/server.h index 918341c2f..a386f47e7 100644 --- a/src/engine/server.h +++ b/src/engine/server.h @@ -188,6 +188,7 @@ public: virtual void SetTimeoutProtected(int ClientID) = 0; virtual void SetErrorShutdown(const char *pReason) = 0; + virtual void ExpireServerInfo() = 0; virtual char *GetMapName() = 0; }; diff --git a/src/engine/server/server.cpp b/src/engine/server/server.cpp index a8c72f00f..9d99311df 100644 --- a/src/engine/server/server.cpp +++ b/src/engine/server/server.cpp @@ -1265,6 +1265,7 @@ void CServer::ProcessClientPacket(CNetChunk *pPacket) Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", aBuf); m_aClients[ClientID].m_State = CClient::STATE_INGAME; GameServer()->OnClientEnter(ClientID); + ExpireServerInfo(); } } else if(Msg == NETMSG_INPUT) @@ -1546,6 +1547,8 @@ void CServer::CCache::Clear() void CServer::CacheServerInfo(CCache *pCache, int Type, bool SendClients) { + pCache->Clear(); + // One chance to improve the protocol! CPacker p; char aBuf[128]; @@ -1786,17 +1789,30 @@ void CServer::SendServerInfo(const NETADDR *pAddr, int Token, int Type, bool Sen } } -void CServer::UpdateServerInfo() +void CServer::ExpireServerInfo() { - for(int i = 0; i < MAX_CLIENTS; ++i) - { - if(m_aClients[i].m_State != CClient::STATE_EMPTY) - { - SendServerInfo(m_NetServer.ClientAddr(i), -1, SERVERINFO_INGAME, false); - } - } + m_ServerInfoNeedsUpdate = true; } +void CServer::UpdateServerInfo(bool Resend) +{ + for(int i = 0; i < 3; i++) + for(int j = 0; j < 2; j++) + CacheServerInfo(&m_ServerInfoCache[i * 2 + j], i, j); + + if(Resend) + { + for(int i = 0; i < MAX_CLIENTS; ++i) + { + if(m_aClients[i].m_State != CClient::STATE_EMPTY) + { + SendServerInfo(m_NetServer.ClientAddr(i), -1, SERVERINFO_INGAME, false); + } + } + } + + m_ServerInfoNeedsUpdate = false; +} void CServer::PumpNetwork() { @@ -2001,6 +2017,7 @@ int CServer::Run() m_Lastheartbeat = 0; m_GameStartTime = time_get(); + UpdateServerInfo(); while(m_RunServer) { if(NonActive) @@ -2041,7 +2058,7 @@ int CServer::Run() { break; } - UpdateServerInfo(); + UpdateServerInfo(true); } else { @@ -2145,14 +2162,8 @@ int CServer::Run() // master server stuff m_Register.RegisterUpdate(m_NetServer.NetType()); - for(int i = 0; i < 3; i++) - { - for(int j = 0; j < 2; j++) - { - m_ServerInfoCache[i * 2 + j].Clear(); - CacheServerInfo(&m_ServerInfoCache[i * 2 + j], i, j); - } - } + if(m_ServerInfoNeedsUpdate) + UpdateServerInfo(); if(!NonActive) PumpNetwork(); @@ -2792,7 +2803,7 @@ void CServer::ConchainSpecialInfoupdate(IConsole::IResult *pResult, void *pUserD { pfnCallback(pResult, pCallbackUserData); if(pResult->NumArguments()) - ((CServer *)pUserData)->UpdateServerInfo(); + ((CServer *)pUserData)->UpdateServerInfo(true); } void CServer::ConchainMaxclientsperipUpdate(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData) diff --git a/src/engine/server/server.h b/src/engine/server/server.h index 3c3e7c65f..7e3896ded 100644 --- a/src/engine/server/server.h +++ b/src/engine/server/server.h @@ -309,11 +309,13 @@ public: void Clear(); }; CCache m_ServerInfoCache[3 * 2]; + bool m_ServerInfoNeedsUpdate; + void ExpireServerInfo(); void CacheServerInfo(CCache *pCache, int Type, bool SendClients); void SendServerInfo(const NETADDR *pAddr, int Token, int Type, bool SendClients); void SendServerInfoConnless(const NETADDR *pAddr, int Token, int Type); - void UpdateServerInfo(); + void UpdateServerInfo(bool Resend = false); void PumpNetwork(); diff --git a/src/game/server/gamecontext.cpp b/src/game/server/gamecontext.cpp index 167645cc3..525c77f1f 100644 --- a/src/game/server/gamecontext.cpp +++ b/src/game/server/gamecontext.cpp @@ -1837,6 +1837,8 @@ void CGameContext::OnMessage(int MsgID, CUnpacker *pUnpacker, int ClientID) pPlayer->m_TeeInfos.m_UseCustomColor = pMsg->m_UseCustomColor; pPlayer->m_TeeInfos.m_ColorBody = pMsg->m_ColorBody; pPlayer->m_TeeInfos.m_ColorFeet = pMsg->m_ColorFeet; + + Server()->ExpireServerInfo(); } else if (MsgID == NETMSGTYPE_CL_EMOTICON && !m_World.m_Paused) { @@ -1955,6 +1957,8 @@ void CGameContext::OnMessage(int MsgID, CUnpacker *pUnpacker, int ClientID) CNetMsg_Sv_ReadyToEnter m; Server()->SendPackMsg(&m, MSGFLAG_VITAL|MSGFLAG_FLUSH, ClientID); } + + Server()->ExpireServerInfo(); } }