diff --git a/Server/Common/Base/TimeInfo.cpp b/Server/Common/Base/TimeInfo.cpp index 5438a90..9f4cb3b 100644 --- a/Server/Common/Base/TimeInfo.cpp +++ b/Server/Common/Base/TimeInfo.cpp @@ -14,9 +14,8 @@ TimeInfo::TimeInfo() m_nLastSecond = now.sec; m_nLastWeek = now.weekday; - m_uLastSysRunTime = 0; + m_uLastSysRunTime = TimeUtil::TickCount();; m_nAnsiTime = time(NULL); - m_uLastTime = TimeUtil::TickCount(); Update(); } @@ -31,8 +30,8 @@ void TimeInfo::Update() m_nAnsiTime = time(NULL); - m_uTimeElapse = nowMs - m_uLastTime; - m_uLastSysRunTime += m_uTimeElapse; + m_uTimeElapse = nowMs - m_uLastSysRunTime; + m_uLastSysRunTime = nowMs; m_bDiffYear = (m_nLastYear != (now.real_year)); m_bDiffMonth = (m_bDiffYear || m_nLastMonth != now.mon); @@ -42,7 +41,6 @@ void TimeInfo::Update() m_bDiffSecond = (m_bDiffMinute || m_nLastSecond != now.sec); m_bDiffWeek = (m_nLastWeek == 0 && now.weekday == 1); - m_uLastTime = nowMs; m_nLastYear = now.real_year; m_nLastMonth = now.mon; m_nLastDay = now.day; diff --git a/Server/Common/Base/TimeInfo.h b/Server/Common/Base/TimeInfo.h index 2196aef..18c403e 100644 --- a/Server/Common/Base/TimeInfo.h +++ b/Server/Common/Base/TimeInfo.h @@ -20,7 +20,6 @@ class TimeInfo uint32_t Elapse() const {return m_uTimeElapse;} uint32_t SysRunTime()const {return m_uLastSysRunTime;} private: - uint32_t m_uLastTime; uint32_t m_uLastSysRunTime; uint32_t m_uTimeElapse; diff --git a/Server/Common/Base/TimeManager.cpp b/Server/Common/Base/TimeManager.cpp index 5b35dd3..1dc5558 100644 --- a/Server/Common/Base/TimeManager.cpp +++ b/Server/Common/Base/TimeManager.cpp @@ -23,9 +23,9 @@ __LEAVE_FUNCTION bool TimeManager::Init( ) { __ENTER_FUNCTION + m_StartTime = TimeUtil::TickCount(); + m_CurrentTime = TimeUtil::TickCount(); - m_CurrentTime = m_StartTime = TimeUtil::Now(); - m_CurrentTickCount = m_StartTickCount = TimeUtil::TickCount(); return true ; __LEAVE_FUNCTION @@ -33,282 +33,35 @@ bool TimeManager::Init( ) return false ; } -uint32_t TimeManager::SysTick() +uint32_t TimeManager::CurrentTime() { - return (TimeUtil::TickCount( ) - m_StartTickCount); -} - -uint32_t TimeManager::RunTick() -{ - uint32_t _CurrentTick = CurrentTick( ); - return (_CurrentTick - m_StartTickCount); -} - -TIME64 TimeManager::StartTime() -{ - return m_StartTime; -} + m_CurrentTime = TimeUtil::TickCount(); - -int64_t TimeManager::StartTimeToInt64() -{ - TIME64 startTime = StartTime(); - return TimeUtil::ToInt64(startTime); + return m_CurrentTime; } -uint32_t TimeManager::StartTick() +uint32_t TimeManager::RunTime() { - return m_StartTickCount; + CurrentTime(); + if( m_CurrentTime < m_StartTime ) + { + return ((uint32_t)0xffffffff-m_StartTime) + m_CurrentTime; + } + return m_CurrentTime - m_StartTime; } -uint32_t TimeManager::CurrentTick() +uint32_t TimeManager::StartTime() { - AutoLock_T _lock(m_Lock); - m_CurrentTickCount = TimeUtil::TickCount(); - - return m_CurrentTickCount; + return m_StartTime; } -TIME64 TimeManager::CurrentTime() +void TimeManager::SetTime() { - AutoLock_T _lock(m_Lock); - m_CurrentTime = TimeUtil::Now(); - return m_CurrentTime; -} + time_t nowTime = time(NULL); + tm curTm; + localtime_r(&curTm, &nowTime); -int64_t TimeManager::CurrentTimeToInt64() -{ - TIME64 currentTime = CurrentTime(); - return TimeUtil::ToInt64(currentTime); + m_SetTime = nowTime; + memcpy(&m_TM, &curTm, sizeof(tm)); } -// -//uint32_t TimeManager::CurrentTime( ) -//{ -//__ENTER_FUNCTION -// -//#if defined(__WINDOWS__) -// m_CurrentTime = GetTickCount() ; -//#elif defined(__LINUX__) -// gettimeofday(&_tend,&tz); -// double t1, t2; -// t1 = (double)_tstart.tv_sec*1000 + (double)_tstart.tv_usec/1000; -// t2 = (double)_tend.tv_sec*1000 + (double)_tend.tv_usec/1000; -// m_CurrentTime = (uint32_t)(t2-t1); -//#endif -// -// return m_CurrentTime ; -// -// -//__LEAVE_FUNCTION -// -// return 0 ; -//} -// -//uint32_t TimeManager::CurrentDate() -//{ -// __ENTER_FUNCTION -// -// SetTime( ) ; -// uint32_t Date; -// ConvertTU(&m_TM,Date); -// -// return Date; -// -// __LEAVE_FUNCTION -// -// return 0; -//} -// -//uint32_t TimeManager::RunTime( ) -//{ -// __ENTER_FUNCTION -// CurrentTime( ) ; -// return (m_CurrentTime-m_StartTime); -// __LEAVE_FUNCTION -// return 0; -//} -// -//uint16_t TimeManager::RunTick( ) -//{ -// __ENTER_FUNCTION -// CurrentTime(); -// return uint16_t(m_CurrentTime-m_StartTime); -// __LEAVE_FUNCTION -// return 0; -//} -// -//void TimeManager::SetTime( ) -//{ -//__ENTER_FUNCTION -// -// time( &m_SetTime ) ; -// tm _tm; -// localtime_r( &_tm, &m_SetTime) ; -// m_TM = _tm ; -// -//__LEAVE_FUNCTION -//} -// -//// 得到标准时间 -//time_t TimeManager::GetANSITime( ) -//{ -//__ENTER_FUNCTION -// -// SetTime(); -// -//__LEAVE_FUNCTION -// -// return m_SetTime; -//} -// -//uint32_t TimeManager::Time2DWORD( ) -//{ -//__ENTER_FUNCTION -// -// SetTime( ) ; -// -// uint32_t uRet=0 ; -// -// uRet += GetYear( ) ; -// uRet -= 2000 ; -// uRet =uRet*100 ; -// -// uRet += GetMonth( )+1 ; -// uRet =uRet*100 ; -// -// uRet += GetDay( ) ; -// uRet =uRet*100 ; -// -// uRet += GetHour( ) ; -// uRet =uRet*100 ; -// -// uRet += GetMinute( ) ; -// -// return uRet ; -// -//__LEAVE_FUNCTION -// -// return 0 ; -//} -// -//uint32_t TimeManager::DiffTime( uint32_t Date1, uint32_t Date2 ) -//{ -//__ENTER_FUNCTION -// -// tm S_D1, S_D2 ; -// ConvertUT( Date1, &S_D1 ) ; -// ConvertUT( Date2, &S_D2 ) ; -// time_t t1,t2 ; -// t1 = mktime(&S_D1) ; -// t2 = mktime(&S_D2) ; -// uint32_t dif = (uint32_t)(difftime(t2,t1)*1000) ; -// return dif ; -// -//__LEAVE_FUNCTION -// -// return 0 ; -//} -// -//void TimeManager::ConvertUT( uint32_t Date, tm* TM ) const -//{ -//__ENTER_FUNCTION -// -// Assert(TM) ; -// memset( TM, 0, sizeof(tm) ) ; -// TM->tm_year = (Date>>26)&0xf ; -// TM->tm_mon = (Date>>22)&0xf ; -// TM->tm_mday = (Date>>17)&0x1f ; -// TM->tm_hour = (Date>>12)&0x1f ; -// TM->tm_min = (Date>>6) &0x3f ; -// TM->tm_sec = (Date) &0x3f ; -// -//__LEAVE_FUNCTION -//} -// -//void TimeManager::ConvertTU( tm* TM, uint32_t& Date ) const -//{ -//__ENTER_FUNCTION -// -// Assert( TM ) ; -// Date = 0 ; -// Date += (TM->tm_yday%10) & 0xf ; -// Date = (Date<<4) ; -// Date += TM->tm_mon & 0xf ; -// Date = (Date<<4) ; -// Date += TM->tm_mday & 0x1f ; -// Date = (Date<<5) ; -// Date += TM->tm_hour & 0x1f ; -// Date = (Date<<5) ; -// Date += TM->tm_min & 0x3f ; -// Date = (Date<<6) ; -// Date += TM->tm_sec & 0x3f ; -// -//__LEAVE_FUNCTION -//} -// -//uint32_t TimeManager::GetDayTime( ) -//{ -//__ENTER_FUNCTION -// -// time_t st ; -// time( &st ) ; -// tm* ptm = localtime( &m_SetTime ) ; -// -// uint32_t uRet=0 ; -// -// uRet = (ptm->tm_year-100)*1000 ; -// uRet += ptm->tm_yday ; -// -// return uRet ; -// -//__LEAVE_FUNCTION -// -// return 0 ; -//} -// -//uint16_t TimeManager::GetTodayTime() -//{ -//__ENTER_FUNCTION -// time_t st ; -// time( &st ) ; -// tm* ptm = localtime( &m_SetTime ) ; -// -// uint16_t uRet=0 ; -// -// uRet = ptm->tm_hour*100 ; -// uRet += ptm->tm_min ; -// -// return uRet ; -// -//__LEAVE_FUNCTION -// -//return 0 ; -//} -// -//bool TimeManager::FormatTodayTime(uint16_t& nTime) -//{ -//__ENTER_FUNCTION -// bool ret = false; -// -// uint16_t wHour = nTime / 100; -// uint16_t wMin = nTime % 100; -// uint16_t wAddHour = 0; -// if( wMin > 59 ) -// { -// wAddHour = wMin / 60; -// wMin = wMin % 60; -// } -// wHour += wAddHour; -// if( wHour > 23 ) -// { -// ret = true; -// wHour = wHour % 60; -// } -// -// return ret; -// -//__LEAVE_FUNCTION -// -//return false ; -//} diff --git a/Server/Common/Base/TimeManager.h b/Server/Common/Base/TimeManager.h index 3194276..2bbbfd8 100644 --- a/Server/Common/Base/TimeManager.h +++ b/Server/Common/Base/TimeManager.h @@ -11,22 +11,24 @@ public : TimeManager( ) ; ~TimeManager( ) ; public: - uint32_t SysTick(); - uint32_t RunTick(); - TIME64 StartTime(); - int64_t StartTimeToInt64(); - TIME64 CurrentTime(); - int64_t CurrentTimeToInt64(); - uint32_t StartTick(); - uint32_t CurrentTick(); + uint32_t CurrentTime(); + uint32_t RunTime(); + uint32_t StartTime(); + void SetTime(); private: bool Init( ) ; private: - MyLock m_Lock; - uint32_t m_StartTickCount; - uint32_t m_CurrentTickCount; - TIME64 m_StartTime; - TIME64 m_CurrentTime; + uint32_t m_StartTime; + uint32_t m_CurrentTime; + time_t m_SetTime; + tm m_TM; +#ifdef __LINUX__ + struct timeval _tstart; + struct timezone _tz; + { + + }; +#endif }; extern TimeManager g_TimeManager; diff --git a/Server/Common/Base/Timer.cpp b/Server/Common/Base/Timer.cpp index af0835f..1bca774 100644 --- a/Server/Common/Base/Timer.cpp +++ b/Server/Common/Base/Timer.cpp @@ -410,3 +410,11 @@ void TimeUtil::ToTM(const TIME64* t, struct tm* m) m->tm_year = t->year; } } + +time_t TimeUtil::AnsiTime() +{ + time_t ansiTime; + time(&ansiTime); + return ansiTime; +} + diff --git a/Server/Common/Base/Timer.h b/Server/Common/Base/Timer.h index 0596703..ed659de 100644 --- a/Server/Common/Base/Timer.h +++ b/Server/Common/Base/Timer.h @@ -83,6 +83,8 @@ static void Format(char* buf, int32_t len, const char* fmt); */ static int32_t FromDateTime(TIME64 *t, const char *buf, int32_t len); static void ToTM(const TIME64* t, struct tm* m); + +static time_t AnsiTime(); }; #endif /* __TIMER_H__*/ diff --git a/Server/Server/Global/InstanceModule.cpp b/Server/Server/Global/InstanceModule.cpp index 1deb110..0097240 100644 --- a/Server/Server/Global/InstanceModule.cpp +++ b/Server/Server/Global/InstanceModule.cpp @@ -13,11 +13,11 @@ bool InstanceManager::Init() bool bRet = g_Config.Init(__argv[0]); Assert(bRet); - srand(g_TimeManager.CurrentTick()); + srand((uint32_t)TimeUtil::AnsiTime()); LOG_DEBUG(ServerDebug, "main..." ) ; - LOG_DEBUG(ServerDebug, "Login Starting... (%lld)(%d)", - g_TimeManager.CurrentTimeToInt64(), g_TimeManager.CurrentTick() ) ; + LOG_DEBUG(ServerDebug, "Login Starting... (%u)(%u)", + g_TimeManager.CurrentTime(), g_TimeManager.RunTime() ) ; CHAR currentDir[_MAX_PATH] = {0}; tgetcwd(currentDir, _MAX_PATH); diff --git a/Server/Server/Player/Player.cpp b/Server/Server/Player/Player.cpp index aeb7d13..04a9617 100644 --- a/Server/Server/Player/Player.cpp +++ b/Server/Server/Player/Player.cpp @@ -65,8 +65,8 @@ __ENTER_FUNCTION uint32_t ret = m_SocketInputStream.Fill( ) ; if( (int32_t)ret <= SOCKET_ERROR ) { - LOG_ERROR(ServerError, "[%lld] m_pSocketInputStream->Fill ret:%d %s", - g_TimeManager.CurrentTimeToInt64(), (int32_t)ret, MySocketError() ) ; + LOG_ERROR(ServerError, "[%u] m_pSocketInputStream->Fill ret:%d %s", + g_TimeManager.CurrentTime(), (int32_t)ret, MySocketError() ) ; return false ; } } @@ -266,8 +266,8 @@ __ENTER_FUNCTION uint32_t ret = m_SocketOutputStream.Flush( ) ; if( (int32_t)ret <= SOCKET_ERROR ) { - LOG_ERROR(ServerError,"[%lld] m_pSocketOutputStream->Flush ret:%d %s", - g_TimeManager.CurrentTimeToInt64(), (int32_t)ret, MySocketError() ) ; + LOG_ERROR(ServerError,"[%u] m_pSocketOutputStream->Flush ret:%d %s", + g_TimeManager.CurrentTime(), (int32_t)ret, MySocketError() ) ; return false ; } }