diff options
author | Tiger Wang <ziwei.tiger@hotmail.co.uk> | 2014-10-20 19:59:40 +0200 |
---|---|---|
committer | Tiger Wang <ziwei.tiger@hotmail.co.uk> | 2014-10-20 19:59:40 +0200 |
commit | bde99d684e0bb51adaa053a240abe61cf4af07fb (patch) | |
tree | ad1111d21ee606dd3cd60439189a92b1b51e2dea /src | |
parent | Migrated random generators to std::random (diff) | |
download | cuberite-bde99d684e0bb51adaa053a240abe61cf4af07fb.tar cuberite-bde99d684e0bb51adaa053a240abe61cf4af07fb.tar.gz cuberite-bde99d684e0bb51adaa053a240abe61cf4af07fb.tar.bz2 cuberite-bde99d684e0bb51adaa053a240abe61cf4af07fb.tar.lz cuberite-bde99d684e0bb51adaa053a240abe61cf4af07fb.tar.xz cuberite-bde99d684e0bb51adaa053a240abe61cf4af07fb.tar.zst cuberite-bde99d684e0bb51adaa053a240abe61cf4af07fb.zip |
Diffstat (limited to '')
-rw-r--r-- | src/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/ClientHandle.cpp | 18 | ||||
-rw-r--r-- | src/ClientHandle.h | 10 | ||||
-rw-r--r-- | src/DeadlockDetect.cpp | 8 | ||||
-rw-r--r-- | src/DeadlockDetect.h | 2 | ||||
-rw-r--r-- | src/Entities/Player.cpp | 12 | ||||
-rw-r--r-- | src/Entities/Player.h | 2 | ||||
-rw-r--r-- | src/Globals.h | 1 | ||||
-rw-r--r-- | src/OSSupport/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/OSSupport/IsThread.h | 2 | ||||
-rw-r--r-- | src/OSSupport/Sleep.cpp | 19 | ||||
-rw-r--r-- | src/OSSupport/Sleep.h | 7 | ||||
-rw-r--r-- | src/OSSupport/Timer.cpp | 37 | ||||
-rw-r--r-- | src/OSSupport/Timer.h | 32 | ||||
-rw-r--r-- | src/Root.cpp | 12 | ||||
-rw-r--r-- | src/Server.cpp | 16 | ||||
-rw-r--r-- | src/World.cpp | 27 | ||||
-rw-r--r-- | src/main.cpp | 5 |
18 files changed, 45 insertions, 168 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9d0e2cede..e322b842b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -112,7 +112,6 @@ SET (HDRS MapManager.h Matrix4.h MemoryLeak.h - MersenneTwister.h MobCensus.h MobFamilyCollecter.h MobProximityCounter.h diff --git a/src/ClientHandle.cpp b/src/ClientHandle.cpp index 7d0f2de06..3dcbaa6a9 100644 --- a/src/ClientHandle.cpp +++ b/src/ClientHandle.cpp @@ -16,7 +16,6 @@ #include "Mobs/Monster.h" #include "ChatColor.h" #include "OSSupport/Socket.h" -#include "OSSupport/Timer.h" #include "Items/ItemHandler.h" #include "Blocks/BlockHandler.h" #include "Blocks/BlockSlab.h" @@ -63,8 +62,6 @@ cClientHandle::cClientHandle(const cSocket * a_Socket, int a_ViewDistance) : m_TimeSinceLastPacket(0), m_Ping(1000), m_PingID(1), - m_PingStartTime(0), - m_LastPingTime(1000), m_BlockDigAnimStage(-1), m_BlockDigAnimSpeed(0), m_BlockDigAnimX(0), @@ -87,9 +84,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; - - cTimer t1; - m_LastPingTime = t1.GetNowTime(); + m_LastPingTime = std::chrono::steady_clock::now(); LOGD("New ClientHandle created at %p", this); } @@ -383,8 +378,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 - cTimer t1; - m_LastPingTime = t1.GetNowTime() + 3000; // Send the first KeepAlive packet in 3 seconds + m_LastPingTime = std::chrono::steady_clock::now() + std::chrono::seconds(3); // Send the first KeepAlive packet in 3 seconds cRoot::Get()->GetPluginManager()->CallHookPlayerSpawned(*m_Player); } @@ -1694,8 +1688,7 @@ void cClientHandle::HandleKeepAlive(int a_KeepAliveID) { if (a_KeepAliveID == m_PingID) { - cTimer t1; - m_Ping = (short)((t1.GetNowTime() - m_PingStartTime) / 2); + m_Ping = std::chrono::steady_clock::now() - m_PingStartTime; } } @@ -1919,11 +1912,10 @@ void cClientHandle::Tick(float a_Dt) // Send a ping packet: if (m_State == csPlaying) { - cTimer t1; - if ((m_LastPingTime + cClientHandle::PING_TIME_MS <= t1.GetNowTime())) + if ((m_LastPingTime + cClientHandle::PING_TIME_MS <= std::chrono::steady_clock::now())) { m_PingID++; - m_PingStartTime = t1.GetNowTime(); + 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 9733ff32d..ef7974efd 100644 --- a/src/ClientHandle.h +++ b/src/ClientHandle.h @@ -210,7 +210,7 @@ public: const AString & GetUsername(void) const; void SetUsername( const AString & a_Username); - inline short GetPing(void) const { return m_Ping; } + inline short GetPing(void) const { return static_cast<short>(std::chrono::duration_cast<std::chrono::milliseconds>(m_Ping).count()); } void SetViewDistance(int a_ViewDistance); int GetViewDistance(void) const { return m_ViewDistance; } @@ -367,11 +367,11 @@ private: /** Seconds since the last packet data was received (updated in Tick(), reset in DataReceived()) */ float m_TimeSinceLastPacket; - short m_Ping; + std::chrono::steady_clock::duration m_Ping; int m_PingID; - long long m_PingStartTime; - long long m_LastPingTime; - static const unsigned short PING_TIME_MS = 1000; // Vanilla sends 1 per 20 ticks (1 second or every 1000 ms) + 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 diff --git a/src/DeadlockDetect.cpp b/src/DeadlockDetect.cpp index 7f703416c..81a328af8 100644 --- a/src/DeadlockDetect.cpp +++ b/src/DeadlockDetect.cpp @@ -13,8 +13,8 @@ -/// Number of milliseconds per cycle -const int CYCLE_MILLISECONDS = 100; +/** Number of milliseconds per cycle */ +#define CYCLE_MILLISECONDS 100 @@ -87,7 +87,7 @@ void cDeadlockDetect::Execute(void) } Checker(this); cRoot::Get()->ForEachWorld(Checker); - cSleep::MilliSleep(CYCLE_MILLISECONDS); + std::this_thread::sleep_for(std::chrono::milliseconds(CYCLE_MILLISECONDS)); } // while (should run) } @@ -119,7 +119,7 @@ void cDeadlockDetect::CheckWorldAge(const AString & a_WorldName, Int64 a_Age) if (WorldAge.m_Age == a_Age) { WorldAge.m_NumCyclesSame += 1; - if (WorldAge.m_NumCyclesSame > (1000 * m_IntervalSec) / CYCLE_MILLISECONDS) + if (WorldAge.m_NumCyclesSame > (m_IntervalSec * 1000) / CYCLE_MILLISECONDS) { DeadlockDetected(); } diff --git a/src/DeadlockDetect.h b/src/DeadlockDetect.h index 6aa98acbb..57027e923 100644 --- a/src/DeadlockDetect.h +++ b/src/DeadlockDetect.h @@ -28,7 +28,7 @@ class cDeadlockDetect : public: cDeadlockDetect(void); - /// Starts the detection. Hides cIsThread's Start, because we need some initialization + /** Starts the detection. Hides cIsThread's Start, because we need some initialization */ bool Start(int a_IntervalSec); protected: diff --git a/src/Entities/Player.cpp b/src/Entities/Player.cpp index 6bd0a3d20..24daba048 100644 --- a/src/Entities/Player.cpp +++ b/src/Entities/Player.cpp @@ -11,7 +11,6 @@ #include "../BlockEntities/BlockEntity.h" #include "../BlockEntities/EnderChestEntity.h" #include "../Root.h" -#include "../OSSupport/Timer.h" #include "../Chunk.h" #include "../Items/ItemHandler.h" #include "../Vector3.h" @@ -27,7 +26,7 @@ #define PLAYER_INVENTORY_SAVE_INTERVAL 6000 // 1000 = once per second -#define PLAYER_LIST_TIME_MS 1000 +#define PLAYER_LIST_TIME_MS std::chrono::milliseconds(1000) @@ -91,9 +90,7 @@ cPlayer::cPlayer(cClientHandle* a_Client, const AString & a_PlayerName) : SetMaxHealth(MAX_HEALTH); m_Health = MAX_HEALTH; - cTimer t1; - m_LastPlayerListTime = t1.GetNowTime(); - + m_LastPlayerListTime = std::chrono::steady_clock::now(); m_PlayerName = a_PlayerName; cWorld * World = NULL; @@ -264,11 +261,10 @@ void cPlayer::Tick(float a_Dt, cChunk & a_Chunk) m_Inventory.UpdateItems(); // Send Player List (Once per m_LastPlayerListTime/1000 ms) - cTimer t1; - if (m_LastPlayerListTime + PLAYER_LIST_TIME_MS <= t1.GetNowTime()) + if (m_LastPlayerListTime + PLAYER_LIST_TIME_MS <= std::chrono::steady_clock::now()) { m_World->BroadcastPlayerListUpdatePing(*this); - m_LastPlayerListTime = t1.GetNowTime(); + m_LastPlayerListTime = std::chrono::steady_clock::now(); } if (IsFlying()) diff --git a/src/Entities/Player.h b/src/Entities/Player.h index 22d6a2ae2..8dd5bdb19 100644 --- a/src/Entities/Player.h +++ b/src/Entities/Player.h @@ -516,7 +516,7 @@ protected: /** The item being dragged by the cursor while in a UI window */ cItem m_DraggingItem; - long long m_LastPlayerListTime; + std::chrono::steady_clock::time_point m_LastPlayerListTime; cClientHandle * m_ClientHandle; diff --git a/src/Globals.h b/src/Globals.h index 6e6bb9c92..b90542af0 100644 --- a/src/Globals.h +++ b/src/Globals.h @@ -257,7 +257,6 @@ template class SizeChecker<UInt16, 2>; #ifndef TEST_GLOBALS // Common headers (part 1, without macros): #include "StringUtils.h" - #include "OSSupport/Sleep.h" #include "OSSupport/CriticalSection.h" #include "OSSupport/Semaphore.h" #include "OSSupport/Event.h" diff --git a/src/OSSupport/CMakeLists.txt b/src/OSSupport/CMakeLists.txt index 1c8da5ddd..45df0c1a3 100644 --- a/src/OSSupport/CMakeLists.txt +++ b/src/OSSupport/CMakeLists.txt @@ -13,7 +13,6 @@ SET (SRCS IsThread.cpp ListenThread.cpp Semaphore.cpp - Sleep.cpp Socket.cpp SocketThreads.cpp Timer.cpp) @@ -28,7 +27,6 @@ SET (HDRS ListenThread.h Queue.h Semaphore.h - Sleep.h Socket.h SocketThreads.h Timer.h) diff --git a/src/OSSupport/IsThread.h b/src/OSSupport/IsThread.h index 8dfad84cb..ba6a48898 100644 --- a/src/OSSupport/IsThread.h +++ b/src/OSSupport/IsThread.h @@ -33,7 +33,7 @@ protected: volatile bool m_ShouldTerminate; public: - cIsThread(const AString & iThreadName); + cIsThread(const AString & a_ThreadName); virtual ~cIsThread(); /// Starts the thread; returns without waiting for the actual start diff --git a/src/OSSupport/Sleep.cpp b/src/OSSupport/Sleep.cpp deleted file mode 100644 index 297d668d7..000000000 --- a/src/OSSupport/Sleep.cpp +++ /dev/null @@ -1,19 +0,0 @@ - -#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules - -#ifndef _WIN32 - #include <unistd.h> -#endif - - - - - -void cSleep::MilliSleep( unsigned int a_MilliSeconds) -{ -#ifdef _WIN32 - Sleep(a_MilliSeconds); // Don't tick too much -#else - usleep(a_MilliSeconds*1000); -#endif -} diff --git a/src/OSSupport/Sleep.h b/src/OSSupport/Sleep.h deleted file mode 100644 index 57d29682c..000000000 --- a/src/OSSupport/Sleep.h +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once - -class cSleep -{ -public: - static void MilliSleep( unsigned int a_MilliSeconds); -}; diff --git a/src/OSSupport/Timer.cpp b/src/OSSupport/Timer.cpp deleted file mode 100644 index fd838dd0d..000000000 --- a/src/OSSupport/Timer.cpp +++ /dev/null @@ -1,37 +0,0 @@ - -#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules - -#include "Timer.h" - - - - - - -cTimer::cTimer(void) -{ - #ifdef _WIN32 - QueryPerformanceFrequency(&m_TicksPerSecond); - #endif -} - - - - - -long long cTimer::GetNowTime(void) -{ - #ifdef _WIN32 - LARGE_INTEGER now; - QueryPerformanceCounter(&now); - return ((now.QuadPart * 1000) / m_TicksPerSecond.QuadPart); - #else - struct timeval now; - gettimeofday(&now, NULL); - return (long long)now.tv_sec * 1000 + (long long)now.tv_usec / 1000; - #endif -} - - - - diff --git a/src/OSSupport/Timer.h b/src/OSSupport/Timer.h deleted file mode 100644 index a059daa41..000000000 --- a/src/OSSupport/Timer.h +++ /dev/null @@ -1,32 +0,0 @@ - -// Timer.h - -// Declares the cTimer class representing an OS-independent of retrieving current time with msec accuracy - - - - - -#pragma once - - - - - -class cTimer -{ -public: - cTimer(void); - - // Returns the current time expressed in milliseconds - long long GetNowTime(void); -private: - - #ifdef _WIN32 - LARGE_INTEGER m_TicksPerSecond; - #endif -} ; - - - - diff --git a/src/Root.cpp b/src/Root.cpp index c8a268a78..1271e8648 100644 --- a/src/Root.cpp +++ b/src/Root.cpp @@ -16,7 +16,6 @@ #include "Protocol/ProtocolRecognizer.h" // for protocol version constants #include "CommandOutput.h" #include "DeadlockDetect.h" -#include "OSSupport/Timer.h" #include "LoggerListeners.h" #include "BuildInfo.h" @@ -118,9 +117,7 @@ void cRoot::Start(void) m_bStop = false; while (!m_bStop) { - cTimer Time; - long long mseconds = Time.GetNowTime(); - + auto BeginTime = std::chrono::steady_clock::now(); m_bRestart = false; LoadGlobalSettings(); @@ -200,17 +197,14 @@ void cRoot::Start(void) } #endif - long long finishmseconds = Time.GetNowTime(); - finishmseconds -= mseconds; - - LOG("Startup complete, took %lld ms!", finishmseconds); + LOG("Startup complete, took %lld ms!", std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - BeginTime).count()); #ifdef _WIN32 EnableMenuItem(hmenu, SC_CLOSE, MF_ENABLED); // Re-enable close button #endif while (!m_bStop && !m_bRestart && !m_TerminateEventRaised) // These are modified by external threads { - cSleep::MilliSleep(1000); + std::this_thread::sleep_for(std::chrono::seconds(1)); } if (m_TerminateEventRaised) diff --git a/src/Server.cpp b/src/Server.cpp index c5f4f9042..f29683b21 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -4,7 +4,6 @@ #include "Server.h" #include "ClientHandle.h" -#include "OSSupport/Timer.h" #include "Mobs/Monster.h" #include "OSSupport/Socket.h" #include "Root.h" @@ -73,22 +72,19 @@ cServer::cTickThread::cTickThread(cServer & a_Server) : void cServer::cTickThread::Execute(void) { - cTimer Timer; - - long long msPerTick = 50; - long long LastTime = Timer.GetNowTime(); + auto LastTime = std::chrono::steady_clock::now(); + static const auto msPerTick = std::chrono::milliseconds(50); while (!m_ShouldTerminate) { - long long NowTime = Timer.GetNowTime(); - float DeltaTime = (float)(NowTime-LastTime); - m_ShouldTerminate = !m_Server.Tick(DeltaTime); - long long TickTime = Timer.GetNowTime() - NowTime; + auto NowTime = std::chrono::steady_clock::now(); + m_ShouldTerminate = !m_Server.Tick(std::chrono::duration_cast<std::chrono::milliseconds>(NowTime - LastTime).count()); + auto TickTime = std::chrono::steady_clock::now() - NowTime; if (TickTime < msPerTick) { // Stretch tick time until it's at least msPerTick - cSleep::MilliSleep((unsigned int)(msPerTick - TickTime)); + std::this_thread::sleep_for(msPerTick -TickTime); } LastTime = NowTime; diff --git a/src/World.cpp b/src/World.cpp index 56f0d6ce5..010fc0d87 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -11,7 +11,6 @@ #include "inifile/iniFile.h" #include "ChunkMap.h" #include "Generating/ChunkDesc.h" -#include "OSSupport/Timer.h" #include "SetChunkData.h" // Serializers @@ -109,7 +108,7 @@ protected: // Wait for 2 sec, but be "reasonably wakeable" when the thread is to finish for (int i = 0; i < 20; i++) { - cSleep::MilliSleep(100); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); if (m_ShouldTerminate) { return; @@ -159,7 +158,7 @@ protected: // Wait for 2 sec, but be "reasonably wakeable" when the thread is to finish for (int i = 0; i < 20; i++) { - cSleep::MilliSleep(100); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); if (m_ShouldTerminate) { return; @@ -167,8 +166,7 @@ protected: } } // for (-ever) } - -} ; +}; @@ -201,23 +199,20 @@ cWorld::cTickThread::cTickThread(cWorld & a_World) : void cWorld::cTickThread::Execute(void) { - cTimer Timer; - - const Int64 msPerTick = 50; - Int64 LastTime = Timer.GetNowTime(); + auto LastTime = std::chrono::steady_clock::now(); + static const auto msPerTick = std::chrono::milliseconds(50); + auto TickTime = std::chrono::steady_clock::duration(50); - Int64 TickDuration = 50; while (!m_ShouldTerminate) { - Int64 NowTime = Timer.GetNowTime(); - float DeltaTime = (float)(NowTime - LastTime); - m_World.Tick(DeltaTime, (int)TickDuration); - TickDuration = Timer.GetNowTime() - NowTime; + auto NowTime = std::chrono::steady_clock::now(); + m_World.Tick(std::chrono::duration_cast<std::chrono::milliseconds>(NowTime - LastTime).count(), std::chrono::duration_cast<std::chrono::duration<int>>(TickTime).count()); + TickTime = std::chrono::steady_clock::now() - NowTime; - if (TickDuration < msPerTick) + if (TickTime < msPerTick) { // Stretch tick time until it's at least msPerTick - cSleep::MilliSleep((unsigned int)(msPerTick - TickDuration)); + std::this_thread::sleep_for(msPerTick -TickTime); } LastTime = NowTime; diff --git a/src/main.cpp b/src/main.cpp index 86ecd4000..b1cfd6976 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -160,7 +160,10 @@ BOOL CtrlHandler(DWORD fdwCtrlType) if (fdwCtrlType == CTRL_CLOSE_EVENT) // Console window closed via 'x' button, Windows will try to close immediately, therefore... { - while (!g_ServerTerminated) { cSleep::MilliSleep(100); } // Delay as much as possible to try to get the server to shut down cleanly + while (!g_ServerTerminated) + { + std::this_thread::sleep_for(std::chrono::milliseconds(50)); // Delay as much as possible to try to get the server to shut down cleanly + } } return TRUE; |