[raknet] Implement GetTime and GetTimeNS

This commit is contained in:
RD42 2023-10-31 21:05:49 +08:00
parent 7992273f86
commit 5550c20f37
5 changed files with 161 additions and 0 deletions

6
raknet/Export.h Normal file
View File

@ -0,0 +1,6 @@
// Fuck it, GCC won't compile with exports. Someone else can fix this if they want
#if defined(_WIN32) && !(defined(__GNUC__) || defined(__GCCXML__)) && !defined(_RAKNET_LIB) && defined(_RAKNET_DLL)
#define RAK_DLL_EXPORT __declspec(dllexport)
#else
#define RAK_DLL_EXPORT
#endif

105
raknet/GetTime.cpp Normal file
View File

@ -0,0 +1,105 @@
/// \file
///
/// This file is part of RakNet Copyright 2003 Kevin Jenkins.
///
/// Usage of RakNet is subject to the appropriate license agreement.
/// Creative Commons Licensees are subject to the
/// license found at
/// http://creativecommons.org/licenses/by-nc/2.5/
/// Single application licensees are subject to the license found at
/// http://www.rakkarsoft.com/SingleApplicationLicense.html
/// Custom license users are subject to the terms therein.
/// GPL license users are subject to the GNU General Public
/// License as published by the Free
/// Software Foundation; either version 2 of the License, or (at your
/// option) any later version.
#include "GetTime.h"
#ifdef _COMPATIBILITY_1
#include "Compatibility1Includes.h" // Developers of a certain platform will know what to do here.
#elif defined(_WIN32)
#include <windows.h>
#elif defined(_COMPATIBILITY_2)
#include "Compatibility2Includes.h"
#include <sys/time.h>
#include <unistd.h>
#else
#include <sys/time.h>
#include <unistd.h>
#endif
static bool initialized=false;
#ifdef _WIN32
static LARGE_INTEGER yo;
#else
static timeval tp, initialTime;
#endif
RakNetTime RakNet::GetTime( void )
{
if ( initialized == false )
{
#ifdef _WIN32
QueryPerformanceFrequency( &yo );
// The original code shifted right 10 bits
//counts = yo.QuadPart >> 10;
// It gives the wrong value since 2^10 is not 1000
// counts = yo.QuadPart;// / 1000;
#else
gettimeofday( &initialTime, 0 );
#endif
initialized = true;
}
#ifdef _WIN32
LARGE_INTEGER PerfVal;
QueryPerformanceCounter( &PerfVal );
return (RakNetTime)(PerfVal.QuadPart*1000 / yo.QuadPart);
#else
gettimeofday( &tp, 0 );
// Seconds to ms and microseconds to ms
return ( tp.tv_sec - initialTime.tv_sec ) * 1000 + ( tp.tv_usec - initialTime.tv_usec ) / 1000;
#endif
}
RakNetTimeNS RakNet::GetTimeNS( void )
{
if ( initialized == false )
{
#ifdef _WIN32
QueryPerformanceFrequency( &yo );
// The original code shifted right 10 bits
//counts = yo.QuadPart >> 10;
// It gives the wrong value since 2^10 is not 1000
// counts = yo.QuadPart;// / 1000;
#else
gettimeofday( &initialTime, 0 );
#endif
initialized = true;
}
#ifdef _WIN32
LARGE_INTEGER PerfVal;
QueryPerformanceCounter( &PerfVal );
__int64 quotient, remainder;
quotient=((PerfVal.QuadPart*1000) / yo.QuadPart);
remainder=((PerfVal.QuadPart*1000) % yo.QuadPart);
//return (PerfVal.QuadPart*1000 / (yo.QuadPart/1000));
return quotient*1000 + (remainder*1000 / yo.QuadPart);
#else
gettimeofday( &tp, 0 );
return ( tp.tv_sec - initialTime.tv_sec ) * (RakNetTimeNS) 1000000 + ( tp.tv_usec - initialTime.tv_usec );
#endif
}

33
raknet/GetTime.h Normal file
View File

@ -0,0 +1,33 @@
/// \file
/// \brief Returns the value from QueryPerformanceCounter. This is the function RakNet uses to represent time.
///
/// This file is part of RakNet Copyright 2003 Kevin Jenkins.
///
/// Usage of RakNet is subject to the appropriate license agreement.
/// Creative Commons Licensees are subject to the
/// license found at
/// http://creativecommons.org/licenses/by-nc/2.5/
/// Single application licensees are subject to the license found at
/// http://www.rakkarsoft.com/SingleApplicationLicense.html
/// Custom license users are subject to the terms therein.
/// GPL license users are subject to the GNU General Public
/// License as published by the Free
/// Software Foundation; either version 2 of the License, or (at your
/// option) any later version.
#ifndef __GET_TIME_H
#define __GET_TIME_H
#include "Export.h"
#include "NetworkTypes.h" // For RakNetTime
/// The namespace RakNet is not consistently used. It's only purpose is to avoid compiler errors for classes whose names are very common.
/// For the most part I've tried to avoid this simply by using names very likely to be unique for my classes.
namespace RakNet
{
/// Returns the value from QueryPerformanceCounter. This is the function RakNet uses to represent time.
RakNetTime RAK_DLL_EXPORT GetTime( void );
RakNetTimeNS RAK_DLL_EXPORT GetTimeNS( void );
}
#endif

1
raknet/NetworkTypes.cpp Normal file
View File

@ -0,0 +1 @@
// TODO: Implement NetworkTypes.cpp

16
raknet/NetworkTypes.h Normal file
View File

@ -0,0 +1,16 @@
// TODO: Implement NetworkTypes.h
#ifndef __NETWORK_TYPES_H
#define __NETWORK_TYPES_H
// Define __GET_TIME_64BIT if you want to use large types for GetTime (takes more bandwidth when you transmit time though!)
// You would want to do this if your system is going to run long enough to overflow the millisecond counter (over a month)
#ifdef __GET_TIME_64BIT
typedef long long RakNetTime;
typedef long long RakNetTimeNS;
#else
typedef unsigned int RakNetTime;
typedef long long RakNetTimeNS;
#endif
#endif