diff options
-rw-r--r-- | Tools/ProtoProxy/Connection.cpp | 3 | ||||
-rw-r--r-- | src/ClientHandle.cpp | 12 | ||||
-rw-r--r-- | src/ClientHandle.h | 11 | ||||
-rw-r--r-- | src/FastRandom.cpp | 32 | ||||
-rw-r--r-- | src/FastRandom.h | 19 | ||||
-rw-r--r-- | src/OSSupport/IsThread.cpp | 20 | ||||
-rw-r--r-- | src/Server.cpp | 3 | ||||
-rw-r--r-- | src/World.cpp | 4 |
8 files changed, 59 insertions, 45 deletions
diff --git a/Tools/ProtoProxy/Connection.cpp b/Tools/ProtoProxy/Connection.cpp index fe61b37d1..fb2d40e5b 100644 --- a/Tools/ProtoProxy/Connection.cpp +++ b/Tools/ProtoProxy/Connection.cpp @@ -436,7 +436,8 @@ bool cConnection::RelayFromClient(void) double cConnection::GetRelativeTime(void) { - return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - m_BeginTick).count() / 1000; + Int64 msec = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - m_BeginTick).count(); + return static_cast<double>(msec) / 1000; } diff --git a/src/ClientHandle.cpp b/src/ClientHandle.cpp index d246f6da2..c4a620565 100644 --- a/src/ClientHandle.cpp +++ b/src/ClientHandle.cpp @@ -38,6 +38,11 @@ /** Maximum number of block change interactions a player can perform per tick - exceeding this causes a kick */ #define MAX_BLOCK_CHANGE_INTERACTIONS 20 +/** The interval for sending pings to clients. +Vanilla sends one ping every 1 second. */ +static const std::chrono::milliseconds PING_TIME_MS = std::chrono::milliseconds(1000); + + @@ -86,7 +91,7 @@ cClientHandle::cClientHandle(const cSocket * a_Socket, int a_ViewDistance) : s_ClientCount++; // Not protected by CS because clients are always constructed from the same thread m_UniqueID = s_ClientCount; - m_LastPingTime = std::chrono::steady_clock::now(); + m_PingStartTime = std::chrono::steady_clock::now(); LOGD("New ClientHandle created at %p", this); } @@ -384,7 +389,7 @@ void cClientHandle::Authenticate(const AString & a_Name, const AString & a_UUID, // Delay the first ping until the client "settles down" // This should fix #889, "BadCast exception, cannot convert bit to fm" error in client - m_LastPingTime = std::chrono::steady_clock::now() + std::chrono::seconds(3); // Send the first KeepAlive packet in 3 seconds + m_PingStartTime = std::chrono::steady_clock::now() + std::chrono::seconds(3); // Send the first KeepAlive packet in 3 seconds cRoot::Get()->GetPluginManager()->CallHookPlayerSpawned(*m_Player); } @@ -1990,12 +1995,11 @@ void cClientHandle::Tick(float a_Dt) // Send a ping packet: if (m_State == csPlaying) { - if ((m_LastPingTime + cClientHandle::PING_TIME_MS <= std::chrono::steady_clock::now())) + if ((m_PingStartTime + PING_TIME_MS <= std::chrono::steady_clock::now())) { m_PingID++; m_PingStartTime = std::chrono::steady_clock::now(); m_Protocol->SendKeepAlive(m_PingID); - m_LastPingTime = m_PingStartTime; } } diff --git a/src/ClientHandle.h b/src/ClientHandle.h index 3431e3a71..495348ac3 100644 --- a/src/ClientHandle.h +++ b/src/ClientHandle.h @@ -378,12 +378,15 @@ private: /** Seconds since the last packet data was received (updated in Tick(), reset in DataReceived()) */ float m_TimeSinceLastPacket; + /** Duration of the last completed client ping. */ std::chrono::steady_clock::duration m_Ping; - int m_PingID; + + /** ID of the last ping request sent to the client. */ + int m_PingID; + + /** Time of the last ping request sent to the client. */ std::chrono::steady_clock::time_point m_PingStartTime; - std::chrono::steady_clock::time_point m_LastPingTime; - std::chrono::milliseconds PING_TIME_MS = std::chrono::milliseconds(1000); // Vanilla sends 1 per 20 ticks (1 second or every 1000 ms) - + // Values required for block dig animation int m_BlockDigAnimStage; // Current stage of the animation; -1 if not digging int m_BlockDigAnimSpeed; // Current speed of the animation (units ???) diff --git a/src/FastRandom.cpp b/src/FastRandom.cpp index cfafc7486..515dc25ea 100644 --- a/src/FastRandom.cpp +++ b/src/FastRandom.cpp @@ -96,8 +96,8 @@ cFastRandom::cFastRandom(void) : int cFastRandom::NextInt(int a_Range) { - m_IntDistribution = std::uniform_int_distribution<>(0, a_Range - 1); - return m_IntDistribution(m_LinearRand); + std::uniform_int_distribution<> distribution(0, a_Range - 1); + return distribution(m_LinearRand); } @@ -108,8 +108,8 @@ int cFastRandom::NextInt(int a_Range) int cFastRandom::NextInt(int a_Range, int a_Salt) { m_LinearRand.seed(a_Salt); - m_IntDistribution = std::uniform_int_distribution<>(0, a_Range - 1); - return m_IntDistribution(m_LinearRand); + std::uniform_int_distribution<> distribution(0, a_Range - 1); + return distribution(m_LinearRand); } @@ -119,8 +119,8 @@ int cFastRandom::NextInt(int a_Range, int a_Salt) float cFastRandom::NextFloat(float a_Range) { - m_FloatDistribution = std::uniform_real_distribution<float>(0, a_Range - 1); - return m_FloatDistribution(m_LinearRand); + std::uniform_real_distribution<float> distribution(0, a_Range); + return distribution(m_LinearRand); } @@ -131,8 +131,8 @@ float cFastRandom::NextFloat(float a_Range) float cFastRandom::NextFloat(float a_Range, int a_Salt) { m_LinearRand.seed(a_Salt); - m_FloatDistribution = std::uniform_real_distribution<float>(0, a_Range - 1); - return m_FloatDistribution(m_LinearRand); + std::uniform_real_distribution<float> distribution(0, a_Range); + return distribution(m_LinearRand); } @@ -142,8 +142,8 @@ float cFastRandom::NextFloat(float a_Range, int a_Salt) int cFastRandom::GenerateRandomInteger(int a_Begin, int a_End) { - m_IntDistribution = std::uniform_int_distribution<>(a_Begin, a_End - 1); - return m_IntDistribution(m_LinearRand); + std::uniform_int_distribution<> distribution(a_Begin, a_End); + return distribution(m_LinearRand); } @@ -164,8 +164,8 @@ MTRand::MTRand() : int MTRand::randInt(int a_Range) { - m_IntDistribution = std::uniform_int_distribution<>(0, a_Range); - return m_IntDistribution(m_MersenneRand); + std::uniform_int_distribution<> distribution(0, a_Range); + return distribution(m_MersenneRand); } @@ -174,8 +174,8 @@ int MTRand::randInt(int a_Range) int MTRand::randInt() { - m_IntDistribution = std::uniform_int_distribution<>(0, std::numeric_limits<int>::max()); - return m_IntDistribution(m_MersenneRand); + std::uniform_int_distribution<> distribution(0, std::numeric_limits<int>::max()); + return distribution(m_MersenneRand); } @@ -184,8 +184,8 @@ int MTRand::randInt() double MTRand::rand(double a_Range) { - m_DoubleDistribution = std::uniform_real_distribution<>(0, a_Range); - return m_DoubleDistribution(m_MersenneRand); + std::uniform_real_distribution<> distribution(0, a_Range); + return distribution(m_MersenneRand); } diff --git a/src/FastRandom.h b/src/FastRandom.h index 7eecc698a..64a087c97 100644 --- a/src/FastRandom.h +++ b/src/FastRandom.h @@ -34,16 +34,16 @@ public: cFastRandom(void); - /// Returns a random int in the range [0 .. a_Range - 1]; a_Range must be less than 1M + /** Returns a random int in the range [0 .. a_Range - 1]; a_Range must be less than 1M */ int NextInt(int a_Range); - /// Returns a random int in the range [0 .. a_Range - 1]; a_Range must be less than 1M; a_Salt is additional source of randomness + /** Returns a random int in the range [0 .. a_Range - 1]; a_Range must be less than 1M; a_Salt is additional source of randomness */ int NextInt(int a_Range, int a_Salt); - /// Returns a random float in the range [0 .. a_Range]; a_Range must be less than 1M + /** Returns a random float in the range [0 .. a_Range]; a_Range must be less than 1M */ float NextFloat(float a_Range); - /// Returns a random float in the range [0 .. a_Range]; a_Range must be less than 1M; a_Salt is additional source of randomness + /** Returns a random float in the range [0 .. a_Range]; a_Range must be less than 1M; a_Salt is additional source of randomness */ float NextFloat(float a_Range, int a_Salt); /** Returns a random float between 0 and 1. */ @@ -55,8 +55,6 @@ public: private: std::minstd_rand m_LinearRand; - std::uniform_int_distribution<> m_IntDistribution; - std::uniform_real_distribution<float> m_FloatDistribution; }; @@ -69,15 +67,20 @@ public: MTRand(void); + /** Returns a random integer in the range [0 .. a_Range]. */ int randInt(int a_Range); + /** Returns a random integer in the range [0 .. MAX_INT]. */ int randInt(void); + /** Returns a random floating point number in the range [0 .. a_Range]. */ double rand(double a_Range); private: std::mt19937 m_MersenneRand; - std::uniform_int_distribution<> m_IntDistribution; - std::uniform_real_distribution<> m_DoubleDistribution; }; + + + + diff --git a/src/OSSupport/IsThread.cpp b/src/OSSupport/IsThread.cpp index 9c62c89bf..94bed1f56 100644 --- a/src/OSSupport/IsThread.cpp +++ b/src/OSSupport/IsThread.cpp @@ -85,7 +85,7 @@ bool cIsThread::Start(void) } catch (std::system_error & a_Exception) { - LOGERROR("cIsThread::Wait (std::thread) error %i: could not construct thread %s; %s", a_Exception.code().value(), m_ThreadName.c_str(), a_Exception.what()); + LOGERROR("cIsThread::Start error %i: could not construct thread %s; %s", a_Exception.code().value(), m_ThreadName.c_str(), a_Exception.code().message().c_str()); return false; } } @@ -96,6 +96,12 @@ bool cIsThread::Start(void) void cIsThread::Stop(void) { + if (!m_Thread.joinable()) + { + // The thread hasn't been started or has already been joined + return; + } + m_ShouldTerminate = true; Wait(); } @@ -106,10 +112,7 @@ void cIsThread::Stop(void) bool cIsThread::Wait(void) { - #ifdef LOGD // ProtoProxy doesn't have LOGD - LOGD("Waiting for thread %s to finish", m_ThreadName.c_str()); - #endif // LOGD - + LOGD("Waiting for thread %s to finish", m_ThreadName.c_str()); if (m_Thread.joinable()) { try @@ -119,15 +122,12 @@ bool cIsThread::Wait(void) } catch (std::system_error & a_Exception) { - LOGERROR("cIsThread::Wait (std::thread) error %i: could not join thread %s; %s", a_Exception.code().value(), m_ThreadName.c_str(), a_Exception.what()); + LOGERROR("cIsThread::Wait error %i: could not join thread %s; %s", a_Exception.code().value(), m_ThreadName.c_str(), a_Exception.code().message().c_str()); return false; } } - #ifdef LOGD // ProtoProxy doesn't have LOGD - LOGD("Thread %s finished", m_ThreadName.c_str()); - #endif - + LOGD("Thread %s finished", m_ThreadName.c_str()); return true; } diff --git a/src/Server.cpp b/src/Server.cpp index db1522602..d6163df7e 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -78,7 +78,8 @@ void cServer::cTickThread::Execute(void) while (!m_ShouldTerminate) { auto NowTime = std::chrono::steady_clock::now(); - m_ShouldTerminate = !m_Server.Tick(static_cast<float>(std::chrono::duration_cast<std::chrono::milliseconds>(NowTime - LastTime).count())); + auto msec = std::chrono::duration_cast<std::chrono::milliseconds>(NowTime - LastTime).count(); + m_ShouldTerminate = !m_Server.Tick(static_cast<float>(msec)); auto TickTime = std::chrono::steady_clock::now() - NowTime; if (TickTime < msPerTick) diff --git a/src/World.cpp b/src/World.cpp index 14010b8b1..91e1199ee 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -239,7 +239,9 @@ void cWorld::cTickThread::Execute(void) while (!m_ShouldTerminate) { auto NowTime = std::chrono::steady_clock::now(); - m_World.Tick(static_cast<float>(std::chrono::duration_cast<std::chrono::milliseconds>(NowTime - LastTime).count()), std::chrono::duration_cast<std::chrono::duration<int>>(TickTime).count()); + auto msec = std::chrono::duration_cast<std::chrono::milliseconds>(NowTime - LastTime).count(); + auto LastTickMsec = std::chrono::duration_cast<std::chrono::duration<int>>(TickTime).count(); + m_World.Tick(static_cast<float>(msec), LastTickMsec); TickTime = std::chrono::steady_clock::now() - NowTime; if (TickTime < msPerTick) |