Skip to content
This repository has been archived by the owner on Jan 13, 2022. It is now read-only.

RakNet TimeZone, TimeVal and gettimeofday #97

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 3 additions & 60 deletions Source/GetTime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,11 @@
#include "GetTime.h"




#if defined(_WIN32)
//DWORD mProcMask;
//DWORD mSysMask;
//HANDLE mThread;









#else
#include <sys/time.h>
#include <unistd.h>
Expand Down Expand Up @@ -89,53 +79,6 @@ RakNet::TimeMS RakNet::GetTimeMS( void )
return (RakNet::TimeMS)(GetTimeUS()/1000);
}
















































#if defined(_WIN32)
RakNet::TimeUS GetTimeUS_Windows( void )
{
Expand Down Expand Up @@ -180,18 +123,18 @@ 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 );

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small remark: It would appear raknet uses tabs instead of spaces. We should stick to their guidelines.

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 );
}

// GCC
RakNet::TimeUS curTime;
gettimeofday( &tp, 0 );
RakNet::gettimeofday( &tp, 0 );

curTime = ( tp.tv_sec ) * (RakNet::TimeUS) 1000000 + ( tp.tv_usec );

Expand Down
1 change: 1 addition & 0 deletions Source/GetTime.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "Export.h"
#include "RakNetTime.h" // For RakNet::TimeMS
#include "gettimeofday.h"

namespace RakNet
{
Expand Down
7 changes: 3 additions & 4 deletions Source/PacketLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include "Itoa.h"
#include <time.h>
#include "SocketIncludes.h"
#include "gettimeofday.h"

#ifdef _MSC_VER
#pragma warning( push )
Expand Down Expand Up @@ -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;

Expand Down
36 changes: 3 additions & 33 deletions Source/RakPeer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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();

Expand All @@ -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
}
Expand Down
7 changes: 4 additions & 3 deletions Source/RakSleep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -36,7 +37,7 @@ using namespace ThreadEmulation;
void RakSleep(unsigned int ms)
{
#ifdef _WIN32
Sleep(ms);
::Sleep(ms);



Expand All @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions Source/SignaledEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "SignaledEvent.h"
#include "RakAssert.h"
#include "RakSleep.h"
#include "GetTime.h"

#if defined(__GNUC__)
#include <sys/time.h>
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion Source/TCPInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "Itoa.h"
#include "SocketLayer.h"
#include "SocketDefines.h"
#include "GetTime.h"
#if (defined(__GNUC__) || defined(__GCCXML__)) && !defined(__WIN32__)
#include <netdb.h>
#endif
Expand Down Expand Up @@ -963,7 +964,7 @@ RAK_THREAD_DECLARATION(RakNet::UpdateTCPInterfaceLoop)
int selectResult;


timeval tv;
RakNet::TimeVal tv;
tv.tv_sec=0;
tv.tv_usec=30000;

Expand Down
36 changes: 25 additions & 11 deletions Source/gettimeofday.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -21,14 +19,31 @@
#else
#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
#endif
#endif

#include <time.h>

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;
Expand Down Expand Up @@ -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

65 changes: 11 additions & 54 deletions Source/gettimeofday.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <sys/time.h>

#include <unistd.h>

// 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