diff --git a/Source/GetTime.cpp b/Source/GetTime.cpp index 073c1f929..c81aaec6b 100644 --- a/Source/GetTime.cpp +++ b/Source/GetTime.cpp @@ -26,21 +26,11 @@ #include "GetTime.h" - - #if defined(_WIN32) //DWORD mProcMask; //DWORD mSysMask; //HANDLE mThread; - - - - - - - - #else #include #include @@ -89,53 +79,6 @@ RakNet::TimeMS RakNet::GetTimeMS( void ) return (RakNet::TimeMS)(GetTimeUS()/1000); } - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #if defined(_WIN32) RakNet::TimeUS GetTimeUS_Windows( void ) { @@ -180,10 +123,10 @@ RakNet::TimeUS GetTimeUS_Windows( void ) #elif defined(__GNUC__) || defined(__GCCXML__) || defined(__S3E__) RakNet::TimeUS GetTimeUS_Linux( void ) { - timeval tp; + RakNet::TimeVal tp; if ( initialized == false) { - gettimeofday( &tp, 0 ); + RakNet::gettimeofday( &tp, 0 ); initialized=true; // I do this because otherwise RakNet::Time in milliseconds won't work as it will underflow when dividing by 1000 to do the conversion initialTime = ( tp.tv_sec ) * (RakNet::TimeUS) 1000000 + ( tp.tv_usec ); @@ -191,7 +134,7 @@ RakNet::TimeUS GetTimeUS_Linux( void ) // GCC RakNet::TimeUS curTime; - gettimeofday( &tp, 0 ); + RakNet::gettimeofday( &tp, 0 ); curTime = ( tp.tv_sec ) * (RakNet::TimeUS) 1000000 + ( tp.tv_usec ); diff --git a/Source/GetTime.h b/Source/GetTime.h index 8dee237f8..e88d7b709 100644 --- a/Source/GetTime.h +++ b/Source/GetTime.h @@ -18,6 +18,7 @@ #include "Export.h" #include "RakNetTime.h" // For RakNet::TimeMS +#include "gettimeofday.h" namespace RakNet { diff --git a/Source/PacketLogger.cpp b/Source/PacketLogger.cpp index 6fa3224bf..6123c18ba 100644 --- a/Source/PacketLogger.cpp +++ b/Source/PacketLogger.cpp @@ -25,7 +25,6 @@ #include "Itoa.h" #include #include "SocketIncludes.h" -#include "gettimeofday.h" #ifdef _MSC_VER #pragma warning( push ) @@ -435,10 +434,10 @@ void PacketLogger::GetLocalTime(char buffer[128]) { #if defined(_WIN32) && !defined(__GNUC__) && !defined(__GCCXML__) time_t rawtime; - struct timeval tv; + RakNet::TimeVal tv; // If you get an arror about an incomplete type, just delete this file - struct timezone tz; - gettimeofday(&tv, &tz); + RakNet::TimeZone tz; + RakNet::gettimeofday(&tv, &tz); // time ( &rawtime ); rawtime=tv.tv_sec; diff --git a/Source/RakPeer.cpp b/Source/RakPeer.cpp index 09718ded0..98de5cc85 100644 --- a/Source/RakPeer.cpp +++ b/Source/RakPeer.cpp @@ -48,7 +48,7 @@ #include "RakAssert.h" #include "RakNetVersion.h" #include "NetworkIDManager.h" -#include "gettimeofday.h" +#include "GetTime.h" #include "SignaledEvent.h" #include "SuperFastHash.h" #include "RakAlloca.h" @@ -4465,36 +4465,6 @@ union Buff6AndBuff8 uint64_t RakPeerInterface::Get64BitUniqueRandomNumber(void) { // Mac address is a poor solution because you can't have multiple connections from the same system - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #if defined(_WIN32) uint64_t g=RakNet::GetTimeUS(); @@ -4516,8 +4486,8 @@ uint64_t RakPeerInterface::Get64BitUniqueRandomNumber(void) return g; #else - struct timeval tv; - gettimeofday(&tv, NULL); + RakNet::TimeVal tv; + RakNet::gettimeofday(&tv, NULL); return tv.tv_usec + tv.tv_sec * 1000000; #endif } diff --git a/Source/RakSleep.cpp b/Source/RakSleep.cpp index 560ba6584..e1285f86b 100644 --- a/Source/RakSleep.cpp +++ b/Source/RakSleep.cpp @@ -26,6 +26,7 @@ pthread_cond_t fakeCond = PTHREAD_COND_INITIALIZER; #endif #include "RakSleep.h" +#include "GetTime.h" #if defined(WINDOWS_PHONE_8) || defined(WINDOWS_STORE_RT) @@ -36,7 +37,7 @@ using namespace ThreadEmulation; void RakSleep(unsigned int ms) { #ifdef _WIN32 - Sleep(ms); + ::Sleep(ms); @@ -48,10 +49,10 @@ void RakSleep(unsigned int ms) //Single thread sleep code thanks to Furquan Shaikh, http://somethingswhichidintknow.blogspot.com/2009/09/sleep-in-pthread.html //Modified slightly from the original struct timespec timeToWait; - struct timeval now; + RakNet::TimeVal now; int rt; - gettimeofday(&now,NULL); + RakNet::gettimeofday(&now,NULL); long seconds = ms/1000; long nanoseconds = (ms - seconds * 1000) * 1000000; diff --git a/Source/SignaledEvent.cpp b/Source/SignaledEvent.cpp index 5b6121e5d..ec6a45b0d 100644 --- a/Source/SignaledEvent.cpp +++ b/Source/SignaledEvent.cpp @@ -11,6 +11,7 @@ #include "SignaledEvent.h" #include "RakAssert.h" #include "RakSleep.h" +#include "GetTime.h" #if defined(__GNUC__) #include @@ -206,8 +207,8 @@ void SignaledEvent::WaitOnEvent(int timeoutMs) struct timespec ts; int rc; - struct timeval tp; - rc = gettimeofday(&tp, NULL); + RakNet::TimeVal tp; + rc = RakNet::gettimeofday(&tp, NULL); ts.tv_sec = tp.tv_sec; ts.tv_nsec = tp.tv_usec * 1000; // #endif diff --git a/Source/TCPInterface.cpp b/Source/TCPInterface.cpp index 71f8fdfd3..007a6f59b 100644 --- a/Source/TCPInterface.cpp +++ b/Source/TCPInterface.cpp @@ -39,6 +39,7 @@ #include "Itoa.h" #include "SocketLayer.h" #include "SocketDefines.h" +#include "GetTime.h" #if (defined(__GNUC__) || defined(__GCCXML__)) && !defined(__WIN32__) #include #endif @@ -963,7 +964,7 @@ RAK_THREAD_DECLARATION(RakNet::UpdateTCPInterfaceLoop) int selectResult; - timeval tv; + RakNet::TimeVal tv; tv.tv_sec=0; tv.tv_usec=30000; diff --git a/Source/gettimeofday.cpp b/Source/gettimeofday.cpp index 4081abdbc..e01b02ddc 100644 --- a/Source/gettimeofday.cpp +++ b/Source/gettimeofday.cpp @@ -7,12 +7,10 @@ * of patent rights can be found in the PATENTS file in the same directory. * */ - -#if defined(_WIN32) && !defined(__GNUC__) &&!defined(__GCCXML__) - #include "gettimeofday.h" // From http://www.openasthra.com/c-tidbits/gettimeofday-function-for-windows/ +#if defined(_MSC_VER) #include "WindowsIncludes.h" @@ -21,14 +19,31 @@ #else #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL #endif +#endif + +#include -int gettimeofday(struct timeval *tv, struct timezone *tz) +int RakNet::gettimeofday(TimeVal *tv, TimeZone *tz) { #if defined(WINDOWS_PHONE_8) || defined(WINDOWS_STORE_RT) - // _tzset not supported - (void) tv; - (void) tz; -#else + if (tv) + { + SYSTEMTIME wtm; + GetLocalTime(&wtm); + + struct tm tTm; + tTm.tm_year = wtm.wYear - 1900; + tTm.tm_mon = wtm.wMonth - 1; + tTm.tm_mday = wtm.wDay; + tTm.tm_hour = wtm.wHour; + tTm.tm_min = wtm.wMinute; + tTm.tm_sec = wtm.wSecond; + tTm.tm_isdst = -1; + + tv->tv_sec = (long)mktime(&tTm); // time_t is 64-bit on win32 + tv->tv_usec = wtm.wMilliseconds * 1000; + } +#elif defined(_WIN32) FILETIME ft; unsigned __int64 tmpres = 0; @@ -59,11 +74,10 @@ int gettimeofday(struct timeval *tv, struct timezone *tz) tz->tz_minuteswest = _timezone / 60; tz->tz_dsttime = _daylight; } - +#else + return ::gettimeofday((struct timeval*)tv, (struct timezone*)tz); #endif return 0; } -#endif - diff --git a/Source/gettimeofday.h b/Source/gettimeofday.h index 7cab89cf0..2c748dbe1 100644 --- a/Source/gettimeofday.h +++ b/Source/gettimeofday.h @@ -11,64 +11,21 @@ #ifndef __GET_TIME_OF_DAY_H #define __GET_TIME_OF_DAY_H -#if defined(_WIN32) && !defined(__GNUC__) &&!defined(__GCCXML__) -#include < time.h > -struct timezone -{ - int tz_minuteswest; /* minutes W of Greenwich */ - int tz_dsttime; /* type of dst correction */ -}; - -#if defined(WINDOWS_STORE_RT) -struct timeval { - long tv_sec; - long tv_usec; -}; -#endif - -int gettimeofday(struct timeval *tv, struct timezone *tz); -#else +namespace RakNet { + struct TimeZone + { + int tz_minuteswest; /* minutes W of Greenwich */ + int tz_dsttime; /* type of dst correction */ + }; + struct TimeVal { + long tv_sec; + long tv_usec; + }; - - -#include - -#include - -// Uncomment this if you need to -/* -// http://www.halcode.com/archives/2008/08/26/retrieving-system-time-gettimeofday/ -struct timezone -{ - int tz_minuteswest; - int tz_dsttime; -}; - -#ifdef __cplusplus - -void GetSystemTimeAsFileTime(FILETIME*); - -inline int gettimeofday(struct timeval* p, void* tz ) -{ - union { - long long ns100; // time since 1 Jan 1601 in 100ns units - FILETIME ft; - } now; - - GetSystemTimeAsFileTime( &(now.ft) ); - p->tv_usec=(long)((now.ns100 / 10LL) % 1000000LL ); - p->tv_sec= (long)((now.ns100-(116444736000000000LL))/10000000LL); - return 0; + int gettimeofday(TimeVal *tv, TimeZone *tz); } - -#else - int gettimeofday(struct timeval* p, void* tz ); -#endif -*/ - -#endif #endif