Browse Source

Don't trust the local machine's system time.

Co-authored-by: Adam Johnson <AdamJohnso@gmail.com>
tickets/52/52/1
Edmond Mondor 1 year ago
parent
commit
13ea0a98de
  1. 2
      Sources/Plasma/PubUtilLib/plNetClient/plNetCliAgeJoiner.cpp
  2. 47
      Sources/Plasma/PubUtilLib/plNetClient/plNetClientMgr.cpp
  3. 5
      Sources/Plasma/PubUtilLib/plNetClient/plNetClientMgr.h

2
Sources/Plasma/PubUtilLib/plNetClient/plNetCliAgeJoiner.cpp

@ -211,7 +211,7 @@ void plNCAgeJoiner::Start () {
plNetClientMgr * nc = plNetClientMgr::GetInstance(); plNetClientMgr * nc = plNetClientMgr::GetInstance();
nc->SetFlagsBit(plNetClientMgr::kPlayingGame, false); nc->SetFlagsBit(plNetClientMgr::kPlayingGame, false);
nc->fServerTimeOffset = 0; // reset since we're connecting to a new server nc->ResetServerTimeOffset();
nc->fRequiredNumInitialSDLStates = 0; nc->fRequiredNumInitialSDLStates = 0;
nc->fNumInitialSDLStates = 0; nc->fNumInitialSDLStates = 0;
nc->SetFlagsBit(plNetClientApp::kNeedInitialAgeStateCount); nc->SetFlagsBit(plNetClientApp::kNeedInitialAgeStateCount);

47
Sources/Plasma/PubUtilLib/plNetClient/plNetClientMgr.cpp

@ -133,9 +133,7 @@ plNetClientMgr::plNetClientMgr() :
// fProgressBar( nil ), // fProgressBar( nil ),
fTaskProgBar( nil ), fTaskProgBar( nil ),
fMsgRecorder(nil), fMsgRecorder(nil),
fServerTimeOffset(0), fLastLocalTime(),
fTimeSamples(0),
fLastTimeUpdate(0),
fListenListMode(kListenList_Distance), fListenListMode(kListenList_Distance),
fAgeSDLObjectKey(nil), fAgeSDLObjectKey(nil),
fExperimentalLevel(0), fExperimentalLevel(0),
@ -480,34 +478,23 @@ void plNetClientMgr::StartLinkInFX()
// //
void plNetClientMgr::UpdateServerTimeOffset(plNetMessage* msg) void plNetClientMgr::UpdateServerTimeOffset(plNetMessage* msg)
{ {
if ((hsTimer::GetSysSeconds() - fLastTimeUpdate) > 5) if (!msg->GetHasTimeSent())
{ return;
fLastTimeUpdate = hsTimer::GetSysSeconds(); if (msg->GetTimeSent().AtEpoch())
return;
const plUnifiedTime& msgSentUT = msg->GetTimeSent();
if (!msgSentUT.AtEpoch())
{
double diff = plUnifiedTime::GetTimeDifference(msgSentUT, plClientUnifiedTime::GetCurrentTime());
if (fServerTimeOffset == 0) double localTime = hsTimer::GetSeconds();
{ if (localTime - fLastLocalTime < 1.0)
fServerTimeOffset = diff; return;
}
else
{
fServerTimeOffset = fServerTimeOffset + ((diff - fServerTimeOffset) / ++fTimeSamples);
}
DebugMsg("Setting server time offset to %f", fServerTimeOffset); fLastServerTime = msg->GetTimeSent();
} fLastLocalTime = localTime;
}
} }
void plNetClientMgr::ResetServerTimeOffset() void plNetClientMgr::ResetServerTimeOffset()
{ {
fServerTimeOffset = 0; fLastServerTime.ToEpoch();
fTimeSamples = 0; fLastLocalTime = 0.0;
fLastTimeUpdate = 0;
} }
// //
@ -515,14 +502,12 @@ void plNetClientMgr::ResetServerTimeOffset()
// //
plUnifiedTime plNetClientMgr::GetServerTime() const plUnifiedTime plNetClientMgr::GetServerTime() const
{ {
if ( fServerTimeOffset==0 ) // offline mode or before connecting/calibrating to a server if (fLastServerTime.AtEpoch()) {
WarningMsg("Someone asked for the server time, but we don't know it yet!");
return plUnifiedTime::GetCurrentTime(); return plUnifiedTime::GetCurrentTime();
}
plUnifiedTime serverUT; return fLastServerTime + plUnifiedTime(hsTimer::GetSeconds() - fLastLocalTime);
if (fServerTimeOffset<0)
return plUnifiedTime::GetCurrentTime() - plUnifiedTime(fabs(fServerTimeOffset));
else
return plUnifiedTime::GetCurrentTime() + plUnifiedTime(fServerTimeOffset);
} }
// //

5
Sources/Plasma/PubUtilLib/plNetClient/plNetClientMgr.h

@ -166,9 +166,8 @@ private:
std::string fSPDesiredPlayerName; // SP: the player we want to load from vault. std::string fSPDesiredPlayerName; // SP: the player we want to load from vault.
// server info // server info
double fServerTimeOffset; // diff between our unified time and server's unified time plUnifiedTime fLastServerTime; // Last received time update from the server
UInt32 fTimeSamples; double fLastLocalTime; // Last monotonic time (in seconds) when the above update was received
double fLastTimeUpdate;
UInt8 fJoinOrder; // returned by the server UInt8 fJoinOrder; // returned by the server

Loading…
Cancel
Save