From 8c3837987bd5f74563790c15a1d52755383135ae Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Wed, 14 Aug 2013 10:24:34 +0200 Subject: Player counts are now properly handled. Fixes #80 --- source/Server.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'source/Server.cpp') diff --git a/source/Server.cpp b/source/Server.cpp index 31a1925a8..c01222e5a 100644 --- a/source/Server.cpp +++ b/source/Server.cpp @@ -172,10 +172,34 @@ void cServer::ClientMovedToWorld(const cClientHandle * a_Client) +void cServer::PlayerCreated(const cPlayer * a_Player) +{ + // To avoid deadlocks, the player count is not handled directly, but rather posted onto the tick thread + cCSLock Lock(m_CSPlayerCountDiff); + m_PlayerCountDiff += 1; +} + + + + + +void cServer::PlayerDestroyed(const cPlayer * a_Player) +{ + // To avoid deadlocks, the player count is not handled directly, but rather posted onto the tick thread + cCSLock Lock(m_CSPlayerCountDiff); + m_PlayerCountDiff -= 1; +} + + + + + bool cServer::InitServer(cIniFile & a_SettingsIni) { m_Description = a_SettingsIni.GetValue ("Server", "Description", "MCServer! - In C++!").c_str(); m_MaxPlayers = a_SettingsIni.GetValueI("Server", "MaxPlayers", 100); + m_PlayerCount = 0; + m_PlayerCountDiff = 0; if (m_bIsConnected) { @@ -254,6 +278,16 @@ bool cServer::InitServer(cIniFile & a_SettingsIni) +int cServer::GetNumPlayers(void) +{ + cCSLock Lock(m_CSPlayerCount); + return m_PlayerCount; +} + + + + + void cServer::PrepareKeys(void) { // TODO: Save and load key for persistence across sessions @@ -310,6 +344,17 @@ void cServer::OnConnectionAccepted(cSocket & a_Socket) bool cServer::Tick(float a_Dt) { + // Apply the queued playercount adjustments (postponed to avoid deadlocks) + int PlayerCountDiff = 0; + { + cCSLock Lock(m_CSPlayerCountDiff); + std::swap(PlayerCountDiff, m_PlayerCountDiff); + } + { + cCSLock Lock(m_CSPlayerCount); + m_PlayerCount += PlayerCountDiff; + } + cRoot::Get()->TickCommands(); TickClients(a_Dt); -- cgit v1.2.3