make rest of the code not use C4TimeMilliseconds-pointers but instead use the infinity field.

stable-5.4
Tobias Zwick 2013-12-09 00:04:19 +07:00
parent 71dbd5bcfb
commit e9eea97864
10 changed files with 63 additions and 109 deletions

View File

@ -33,7 +33,7 @@
C4GameControlNetwork::C4GameControlNetwork(C4GameControl *pnParent)
: fEnabled(false), fRunning(false), iClientID(C4ClientIDUnknown),
fActivated(false), iTargetTick(-1),
iControlPreSend(1), tWaitStart(NULL), iAvgControlSendTime(0), iTargetFPS(38),
iControlPreSend(1), tWaitStart(C4TimeMilliseconds::PositiveInfinity), iAvgControlSendTime(0), iTargetFPS(38),
iControlSent(0), iControlReady(0),
pCtrlStack(NULL),
tNextControlRequest(0),
@ -64,11 +64,7 @@ bool C4GameControlNetwork::Init(int32_t inClientID, bool fnHost, int32_t iStartT
fEnabled = true; fRunning = false;
iTargetFPS = 38;
tNextControlRequest = C4TimeMilliseconds::Now() + C4ControlRequestInterval;
if(tWaitStart)
{
delete tWaitStart;
tWaitStart = NULL;
}
tWaitStart = C4TimeMilliseconds::PositiveInfinity;
return true;
}
@ -94,8 +90,7 @@ void C4GameControlNetwork::Execute() // by main thread
return;
// Save time the control tick was reached
if (!tWaitStart)
tWaitStart = new C4TimeMilliseconds(C4TimeMilliseconds::Now());
tWaitStart = C4TimeMilliseconds::Now();
// Execute any queued sync control
ExecQueuedSyncCtrl();
@ -122,11 +117,7 @@ bool C4GameControlNetwork::GetControl(C4Control *pCtrl, int32_t iTick) // by mai
pCtrl->Append(pPkt->getControl());
// calc performance
CalcPerformance(iTick);
if(tWaitStart)
{
delete tWaitStart;
tWaitStart = NULL;
}
tWaitStart = C4TimeMilliseconds::PositiveInfinity;
// ok
return true;
}
@ -429,9 +420,9 @@ void C4GameControlNetwork::CalcPerformance(int32_t iCtrlTick)
// Performance statistics
// find control (may not be found, if we only got the complete ctrl)
C4GameControlPacket *pCtrl = getCtrl(pClient->getClientID(), iCtrlTick);
if (!pCtrl || !tWaitStart) continue;
if (!pCtrl || tWaitStart.IsInfinite()) continue;
// calc stats
pClient->AddPerf(pCtrl->getTime() - *tWaitStart);
pClient->AddPerf(pCtrl->getTime() - tWaitStart);
}
// Now do PreSend-calcs based on ping times
int32_t iControlSendTime;
@ -774,7 +765,7 @@ C4GameControlPacket *C4GameControlNetwork::PackCompleteCtrl(int32_t iTick)
{
// async mode: wait n extra frames for slow clients
const int iMaxWait = (Config.Network.AsyncMaxWait * 1000) / iTargetFPS;
if (eMode != CNM_Async || !tWaitStart || C4TimeMilliseconds::Now() <= *tWaitStart + iMaxWait)
if (eMode != CNM_Async || C4TimeMilliseconds::Now() <= tWaitStart + iMaxWait)
return NULL;
}

View File

@ -66,8 +66,8 @@ protected:
// statistics
// time started to wait. NULL if not set yet
C4TimeMilliseconds *tWaitStart;
// time started to wait.
C4TimeMilliseconds tWaitStart;
int32_t iAvgControlSendTime;
int32_t iTargetFPS; // used for PreSend-colculation

View File

@ -1795,7 +1795,7 @@ C4NetIOUDP::C4NetIOUDP()
pPeerList(NULL),
fSavePacket(false),
fDelayedLoopbackTest(false),
tNextCheck(NULL),
tNextCheck(C4TimeMilliseconds::PositiveInfinity),
OPackets(iMaxOPacketBacklog),
iOPacketCounter(0),
iBroadcastRate(0)
@ -1805,7 +1805,6 @@ C4NetIOUDP::C4NetIOUDP()
C4NetIOUDP::~C4NetIOUDP()
{
delete tNextCheck;
Close();
}
@ -1831,8 +1830,7 @@ bool C4NetIOUDP::Init(uint16_t inPort)
fInit = true;
fMultiCast = false;
if(!tNextCheck) tNextCheck = new C4TimeMilliseconds();
*tNextCheck = C4TimeMilliseconds::Now() + iCheckInterval;
tNextCheck = C4TimeMilliseconds::Now() + iCheckInterval;
// ok, that's all for now.
// call InitBroadcast for more initialization fun
@ -2013,9 +2011,8 @@ bool C4NetIOUDP::Execute(int iMaxTime, pollfd *) // (mt-safe)
return false;
// connection check needed?
if (tNextCheck)
if (*tNextCheck <= C4TimeMilliseconds::Now())
DoCheck();
if (tNextCheck <= C4TimeMilliseconds::Now())
DoCheck();
// client timeout?
for (Peer *pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
if (!pPeer->Closed())
@ -2102,14 +2099,14 @@ bool C4NetIOUDP::SetBroadcast(const addr_t &addr, bool fSet) // (mt-safe)
C4TimeMilliseconds C4NetIOUDP::GetNextTick(C4TimeMilliseconds tNow) // (mt-safe)
{
// maximum time: check interval
C4TimeMilliseconds tTiming = tNextCheck ? Max(tNow, *tNextCheck): tNow;
C4TimeMilliseconds tTiming = tNextCheck.IsInfinite() ? tNow : Max(tNow, tNextCheck);
// client timeouts (e.g. connection timeout)
CStdShareLock PeerListLock(&PeerListCSec);
for (Peer *pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
if (!pPeer->Closed())
if (pPeer->GetTimeout())
tTiming = Min(tTiming, *pPeer->GetTimeout());
if (!pPeer->GetTimeout().IsInfinite())
tTiming = Min(tTiming, pPeer->GetTimeout());
// return timing value
return tTiming;
}
@ -2490,7 +2487,7 @@ C4NetIOUDP::Peer::Peer(const sockaddr_in &naddr, C4NetIOUDP *pnParent)
iIPacketCounter(0), iRIPacketCounter(0),
iIMCPacketCounter(0), iRIMCPacketCounter(0),
iMCAckPacketCounter(0),
tNextReCheck(NULL),
tNextReCheck(C4TimeMilliseconds::PositiveInfinity),
iIRate(0), iORate(0), iLoss(0)
{
ZeroMem(&addr2, sizeof(addr2));
@ -2499,8 +2496,6 @@ C4NetIOUDP::Peer::Peer(const sockaddr_in &naddr, C4NetIOUDP *pnParent)
C4NetIOUDP::Peer::~Peer()
{
delete tNextReCheck;
delete tTimeout;
// send close-packet
Close("deleted");
}
@ -2540,7 +2535,7 @@ bool C4NetIOUDP::Peer::Check(bool fForceCheck)
if (eStatus != CS_Works) return true;
// prevent re-check (check floods)
// instead, ask for other packets that are missing until recheck is allowed
bool fNoReCheck = !!tNextReCheck && *tNextReCheck > C4TimeMilliseconds::Now();
bool fNoReCheck = tNextReCheck > C4TimeMilliseconds::Now();
if (!fNoReCheck) iLastPacketAsked = iLastMCPacketAsked = 0;
unsigned int iStartAt = fNoReCheck ? Max(iLastPacketAsked + 1, iIPacketCounter) : iIPacketCounter;
unsigned int iStartAtMC = fNoReCheck ? Max(iLastMCPacketAsked + 1, iIMCPacketCounter) : iIMCPacketCounter;
@ -2559,8 +2554,10 @@ bool C4NetIOUDP::Peer::Check(bool fForceCheck)
// no re-check limit? set it
if (!fNoReCheck)
{
if(!tNextReCheck) tNextReCheck = new C4TimeMilliseconds();
*tNextReCheck = iEAskCnt ? C4TimeMilliseconds::Now() + iReCheckInterval : 0;
if (iEAskCnt)
tNextReCheck = C4TimeMilliseconds::Now() + iReCheckInterval;
else
tNextReCheck = C4TimeMilliseconds::NegativeInfinity;
}
// something to ask for? (or check forced?)
if (iEAskCnt || fForceCheck)
@ -2629,11 +2626,7 @@ void C4NetIOUDP::Peer::OnRecv(const C4NetIOPacket &rPacket) // (mt-safe)
IPackets.Clear();
IMCPackets.Clear();
if(tNextReCheck)
{
delete tNextReCheck;
tNextReCheck = NULL;
}
tNextReCheck = C4TimeMilliseconds::PositiveInfinity;
iLastPacketAsked = iLastMCPacketAsked = 0;
// Activate Multicast?
@ -2792,10 +2785,8 @@ void C4NetIOUDP::Peer::Close(const char *szReason) // (mt-safe)
void C4NetIOUDP::Peer::CheckTimeout()
{
// timeout set?
if (!tTimeout) return;
// check
if (C4TimeMilliseconds::Now() > *tTimeout)
if (C4TimeMilliseconds::Now() > tTimeout)
OnTimeout();
}
@ -2945,13 +2936,11 @@ void C4NetIOUDP::Peer::SetTimeout(int iLength, int iRetryCnt) // (mt-safe)
{
if (iLength != TO_INF)
{
if(!tTimeout) tTimeout = new C4TimeMilliseconds();
*tTimeout = C4TimeMilliseconds::Now() + iLength;
tTimeout = C4TimeMilliseconds::Now() + iLength;
}
else
{
delete tTimeout;
tTimeout = NULL;
tTimeout = C4TimeMilliseconds::PositiveInfinity;
}
iRetries = iRetryCnt;
}
@ -3170,8 +3159,7 @@ void C4NetIOUDP::DoCheck() // (mt-safe)
pPeer->Check();
// set time for next check
if(!tNextCheck) tNextCheck = new C4TimeMilliseconds();
*tNextCheck = C4TimeMilliseconds::Now() + iCheckInterval;
tNextCheck = C4TimeMilliseconds::Now() + iCheckInterval;
}
// debug

View File

@ -620,12 +620,12 @@ protected:
// output critical section
CStdCSec OutCSec;
// connection check time limit. NULL if no recheck time yet
C4TimeMilliseconds *tNextReCheck;
// connection check time limit.
C4TimeMilliseconds tNextReCheck;
unsigned int iLastPacketAsked, iLastMCPacketAsked;
// timeout time. NULL if no timeout time set yet
C4TimeMilliseconds *tTimeout;
// timeout time.
C4TimeMilliseconds tTimeout;
unsigned int iRetries;
// statistics
@ -661,7 +661,7 @@ protected:
unsigned int GetMCAckPacketCounter() const { return iMCAckPacketCounter; }
// timeout checking
C4TimeMilliseconds *GetTimeout() { return tTimeout; }
C4TimeMilliseconds GetTimeout() { return tTimeout; }
void CheckTimeout();
// selected for broadcast?
@ -726,8 +726,8 @@ protected:
addr_t MCLoopbackAddr;
bool fDelayedLoopbackTest;
// check timing. NULL if no next check yet
C4TimeMilliseconds *tNextCheck;
// check timing.
C4TimeMilliseconds tNextCheck;
// outgoing packet list (for multicast)
PacketList OPackets;

View File

@ -146,7 +146,7 @@ C4Network2::C4Network2()
pLobby(NULL), fLobbyRunning(false), pLobbyCountdown(NULL),
iNextClientID(0),
iLastChaseTargetUpdate(0),
tLastActivateRequest(NULL),
tLastActivateRequest(C4TimeMilliseconds::NegativeInfinity),
iLastReferenceUpdate(0),
iLastLeagueUpdate(0),
pLeagueClient(NULL),
@ -624,7 +624,7 @@ void C4Network2::Execute()
else
{
// request activate, if neccessary
if (tLastActivateRequest) RequestActivate();
if (!tLastActivateRequest.IsInfinite()) RequestActivate();
}
}
@ -664,11 +664,7 @@ void C4Network2::Clear()
// stuff
fAllowJoin = false;
iDynamicTick = -1; fDynamicNeeded = false;
if(tLastActivateRequest)
{
delete tLastActivateRequest;
tLastActivateRequest = NULL;
}
tLastActivateRequest = C4TimeMilliseconds::NegativeInfinity;
iLastChaseTargetUpdate = iLastReferenceUpdate = iLastLeagueUpdate = 0;
fDelayedActivateReq = false;
delete pVoteDialog; pVoteDialog = NULL;
@ -1769,11 +1765,7 @@ void C4Network2::RequestActivate()
// neither observer nor activated?
if (Game.Clients.getLocal()->isObserver() || Game.Clients.getLocal()->isActivated())
{
if(tLastActivateRequest)
{
delete tLastActivateRequest;
tLastActivateRequest = NULL;
}
tLastActivateRequest = C4TimeMilliseconds::NegativeInfinity;
return;
}
// host? just do it
@ -1786,9 +1778,8 @@ void C4Network2::RequestActivate()
return;
}
// ensure interval
if (tLastActivateRequest)
if(C4TimeMilliseconds::Now() < *tLastActivateRequest + C4NetActivationReqInterval)
return;
if(C4TimeMilliseconds::Now() < tLastActivateRequest + C4NetActivationReqInterval)
return;
// status not reached yet? May be chasing, let's delay this.
if (!fStatusReached)
{
@ -1798,10 +1789,7 @@ void C4Network2::RequestActivate()
// request
Clients.SendMsgToHost(MkC4NetIOPacket(PID_ClientActReq, C4PacketActivateReq(Game.FrameCounter)));
// store time
if(!tLastActivateRequest)
tLastActivateRequest = new C4TimeMilliseconds(C4TimeMilliseconds::Now());
else
*tLastActivateRequest = C4TimeMilliseconds::Now();
tLastActivateRequest = C4TimeMilliseconds::Now();
}
void C4Network2::DeactivateInactiveClients()

View File

@ -166,8 +166,8 @@ protected:
// chase
uint32_t iLastChaseTargetUpdate;
// time of last activation request. NULL if no last request
C4TimeMilliseconds *tLastActivateRequest;
// time of last activation request.
C4TimeMilliseconds tLastActivateRequest;
// reference
uint32_t iLastReferenceUpdate;

View File

@ -1303,7 +1303,8 @@ C4Network2IOConnection::C4Network2IOConnection()
fBroadcastTarget(false),
iTimestamp(0),
iPingTime(-1),
tLastPing(NULL), tLastPong(NULL),
tLastPing(C4TimeMilliseconds::NegativeInfinity),
tLastPong(C4TimeMilliseconds::NegativeInfinity),
fConnSent(false),
fPostMortemSent(false),
iOutPacketCounter(0), iInPacketCounter(0),
@ -1320,9 +1321,6 @@ C4Network2IOConnection::~C4Network2IOConnection()
if (pNetClass && !isClosed()) Close();
// clear the packet log
ClearPacketLog();
delete tLastPing;
delete tLastPong;
}
int C4Network2IOConnection::getLag() const
@ -1330,9 +1328,9 @@ int C4Network2IOConnection::getLag() const
if (iPingTime != -1)
{
// Last ping not answered yet?
if(tLastPing && (!tLastPong || *tLastPing > *tLastPong))
if(tLastPing > tLastPong)
{
int iPingLag = C4TimeMilliseconds::Now() - *tLastPing;
int iPingLag = C4TimeMilliseconds::Now() - tLastPing;
// Use it for lag measurement once it's larger then the last ping time
// (the ping time won't be better than this anyway once the pong's here)
return Max(iPingLag, iPingTime);
@ -1369,13 +1367,11 @@ void C4Network2IOConnection::SetPeerAddr(const C4NetIO::addr_t &nPeerAddr)
void C4Network2IOConnection::OnPing()
{
// Still no pong for the last ping?
if(tLastPing && tLastPong)
if (*tLastPong < *tLastPing)
return;
if (tLastPong < tLastPing)
return;
// Save time
if(!tLastPing) tLastPing = new C4TimeMilliseconds();
*tLastPing = C4TimeMilliseconds::Now();
tLastPing = C4TimeMilliseconds::Now();
}
void C4Network2IOConnection::SetPingTime(int inPingTime)
@ -1383,8 +1379,7 @@ void C4Network2IOConnection::SetPingTime(int inPingTime)
// save it
iPingTime = inPingTime;
// pong received - save timestamp
if(!tLastPong) tLastPong = new C4TimeMilliseconds();
*tLastPong = C4TimeMilliseconds::Now();
tLastPong = C4TimeMilliseconds::Now();
}
void C4Network2IOConnection::SetStatus(C4Network2IOConnStatus nStatus)

View File

@ -231,8 +231,8 @@ protected:
bool fBroadcastTarget; // broadcast target?
time_t iTimestamp; // timestamp of last status change
int iPingTime; // ping
C4TimeMilliseconds *tLastPing; // if > iLastPong, it's the first ping that hasn't been answered yet, NULL if no ping received yet
C4TimeMilliseconds *tLastPong; // last pong received, NULL if no pong received yet
C4TimeMilliseconds tLastPing; // if > iLastPong, it's the first ping that hasn't been answered yet, NULL if no ping received yet
C4TimeMilliseconds tLastPong; // last pong received, NULL if no pong received yet
C4ClientCore CCore; // client core (>= CS_HalfAccepted)
CStdCSec CCoreCSec;
int iIRate, iORate; // input/output rates (by C4NetIO, in b/s)

View File

@ -62,6 +62,9 @@ public:
/* Returns the stored time. Do not use this for comparisons because this method always
returns the stored time, independent of whether this variable is actually infinite. */
uint32_t AsInt() const { return time; }
/* Returns whether this variable is set to some infinity. This does normally mean that
the variable is not initialized yet. */
bool IsInfinite() const { return inf != NoInfinity; }
/* Returns a string representation useful for debugging and logging purposes. */
const char* AsString() const;

View File

@ -86,44 +86,33 @@ public:
class CStdTimerProc : public StdSchedulerProc
{
public:
CStdTimerProc(uint32_t iDelay) : tLastTimer(NULL), iDelay(iDelay) { }
CStdTimerProc(uint32_t iDelay) : tLastTimer(C4TimeMilliseconds::NegativeInfinity), iDelay(iDelay) { }
~CStdTimerProc() { Set(); }
private:
C4TimeMilliseconds *tLastTimer;
C4TimeMilliseconds tLastTimer;
uint32_t iDelay;
public:
void Set()
{
delete tLastTimer;
tLastTimer = NULL;
tLastTimer = C4TimeMilliseconds::NegativeInfinity;
}
void SetDelay(uint32_t inDelay) { iDelay = inDelay; }
bool CheckAndReset()
{
C4TimeMilliseconds tTime = C4TimeMilliseconds::Now();
// first execution
if(!tLastTimer)
{
tLastTimer = new C4TimeMilliseconds(tTime - iDelay / 2);
return true;
}
// too early to execute
if (tTime < *tLastTimer + iDelay) return false;
if (tTime < tLastTimer + iDelay) return false;
// Compensate light drifting
uint32_t iDrift = tTime - (*tLastTimer + iDelay); // a positive time difference because of above check
*tLastTimer = tTime - Min(iDrift, iDelay / 2);
int32_t iDrift = tTime - (tLastTimer + iDelay); // a positive time difference because of above check
tLastTimer = tTime - Min(iDrift, (int32_t) iDelay / 2);
return true;
}
// StdSchedulerProc override
virtual C4TimeMilliseconds GetNextTick(C4TimeMilliseconds tNow)
{
// never executed yet? Execute now
if(!tLastTimer) return tNow;
// otherwise, last time executed plus specified delay
return *tLastTimer + iDelay;
return tLastTimer + iDelay;
}
virtual bool IsScheduledExecution() { return true; }