• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

GetTime.cpp

Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 #if defined(_WIN32) && !defined(_XBOX) && !defined(X360)
00009 #include "WindowsIncludes.h"
00010 // To call timeGetTime
00011 // on Code::Blocks, this needs to be libwinmm.a instead
00012 #pragma comment(lib, "Winmm.lib")
00013 #endif
00014 
00015 #include "GetTime.h"
00016 #if defined(_XBOX) || defined(X360)
00017                             
00018 #endif
00019 #if defined(_WIN32)
00020 DWORD mProcMask;
00021 DWORD mSysMask;
00022 HANDLE mThread;
00023 static LARGE_INTEGER yo;
00024 #elif defined(_PS3) || defined(__PS3__) || defined(SN_TARGET_PS3)
00025                                                                                                                                                                                                   
00026 #else
00027 #include <sys/time.h>
00028 #include <unistd.h>
00029 static timeval tp;
00030 RakNetTimeUS initialTime;
00031 #endif
00032 
00033 static bool initialized=false;
00034 
00035 #if defined(GET_TIME_SPIKE_LIMIT) && GET_TIME_SPIKE_LIMIT>0
00036 #include "SimpleMutex.h"
00037 RakNetTimeUS lastNormalizedReturnedValue=0;
00038 RakNetTimeUS lastNormalizedInputValue=0;
00042 RakNetTimeUS NormalizeTime(RakNetTimeUS timeIn)
00043 {
00044         RakNetTimeUS diff, lastNormalizedReturnedValueCopy;
00045         static SimpleMutex mutex;
00046         
00047         mutex.Lock();
00048         if (timeIn>lastNormalizedInputValue)
00049         {
00050                 diff = timeIn-lastNormalizedInputValue;
00051                 if (diff > GET_TIME_SPIKE_LIMIT)
00052                         lastNormalizedReturnedValue+=GET_TIME_SPIKE_LIMIT;
00053                 else
00054                         lastNormalizedReturnedValue+=diff;
00055         }
00056 
00057         lastNormalizedInputValue=timeIn;
00058         lastNormalizedReturnedValueCopy=lastNormalizedReturnedValue;
00059         mutex.Unlock();
00060 
00061         return lastNormalizedReturnedValueCopy;
00062 }
00063 #endif // #if defined(GET_TIME_SPIKE_LIMIT) && GET_TIME_SPIKE_LIMIT>0
00064 RakNetTime RakNet::GetTime( void )
00065 {
00066         return (RakNetTime)(GetTimeNS()/1000);
00067 }
00068 #if defined(_PS3) || defined(__PS3__) || defined(SN_TARGET_PS3)
00069                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
00070 #elif defined(_XBOX) || defined(X360)
00071                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
00072 #elif defined(_WIN32) && !defined(_XBOX) && !defined(X360)
00073 RakNetTimeUS GetTimeUS_Windows( void )
00074 {
00075         if ( initialized == false)
00076         {
00077                 initialized = true;
00078 
00079                 // Save the current process
00080 #if !defined(_WIN32_WCE)
00081                 HANDLE mProc = GetCurrentProcess();
00082 
00083                 // Get the current Affinity
00084 #if _MSC_VER >= 1400 && defined (_M_X64)
00085                 GetProcessAffinityMask(mProc, (PDWORD_PTR)&mProcMask, (PDWORD_PTR)&mSysMask);
00086 #else
00087                 GetProcessAffinityMask(mProc, &mProcMask, &mSysMask);
00088 #endif
00089                 mThread = GetCurrentThread();
00090 
00091 #endif // _WIN32_WCE
00092                 QueryPerformanceFrequency( &yo );
00093         }       
00094 
00095         RakNetTimeUS curTime;
00096         LARGE_INTEGER PerfVal;
00097         QueryPerformanceCounter( &PerfVal );
00098         __int64 quotient, remainder;
00099         quotient=((PerfVal.QuadPart) / yo.QuadPart);
00100         remainder=((PerfVal.QuadPart) % yo.QuadPart);
00101         curTime = (RakNetTimeUS) quotient*(RakNetTimeUS)1000000 + (remainder*(RakNetTimeUS)1000000 / yo.QuadPart);
00102 
00103 #if defined(GET_TIME_SPIKE_LIMIT) && GET_TIME_SPIKE_LIMIT>0
00104         return NormalizeTime(curTime);
00105 #else
00106         return curTime;
00107 #endif // #if defined(GET_TIME_SPIKE_LIMIT) && GET_TIME_SPIKE_LIMIT>0
00108 }
00109 #elif (defined(__GNUC__)  || defined(__GCCXML__))
00110 RakNetTimeUS GetTimeUS_Linux( void )
00111 {
00112         if ( initialized == false)
00113         {
00114                 gettimeofday( &tp, 0 );
00115                 initialized=true;
00116                 // I do this because otherwise RakNetTime in milliseconds won't work as it will underflow when dividing by 1000 to do the conversion
00117                 initialTime = ( tp.tv_sec ) * (RakNetTimeUS) 1000000 + ( tp.tv_usec );
00118         }
00119 
00120         // GCC
00121         RakNetTimeUS curTime;
00122         gettimeofday( &tp, 0 );
00123 
00124         curTime = ( tp.tv_sec ) * (RakNetTimeUS) 1000000 + ( tp.tv_usec );
00125 
00126 #if defined(GET_TIME_SPIKE_LIMIT) && GET_TIME_SPIKE_LIMIT>0
00127         return NormalizeTime(curTime - initialTime);
00128 #else
00129         return curTime - initialTime;
00130 #endif // #if defined(GET_TIME_SPIKE_LIMIT) && GET_TIME_SPIKE_LIMIT>0
00131 }
00132 #endif
00133 
00134 RakNetTimeUS RakNet::GetTimeNS( void )
00135 {
00136 #if defined(_PS3) || defined(__PS3__) || defined(SN_TARGET_PS3)
00137                         
00138 #elif defined(_XBOX) || defined(X360)
00139                         
00140 #elif defined(_WIN32)
00141         return GetTimeUS_Windows();
00142 #else
00143         return GetTimeUS_Linux();
00144 #endif
00145 }

Generated on Thu Sep 30 2010 01:27:24 for RakNet by  doxygen 1.7.1