Network/ENet: Clean up time functions

This commit is contained in:
Lynix 2017-01-31 23:04:53 +01:00
parent 2f057191aa
commit 6b8d9deb43
4 changed files with 58 additions and 23 deletions

View File

@ -13,6 +13,14 @@
namespace Nz
{
constexpr UInt32 ENetTimeOverflow = 24 * 60 * 60 * 1000;
inline UInt32 ENetTimeDifference(UInt32 a, UInt32 b);
inline bool ENetTimeLess(UInt32 a, UInt32 b);
inline bool ENetTimeLessEqual(UInt32 a, UInt32 b);
inline bool ENetTimeGreater(UInt32 a, UInt32 b);
inline bool ENetTimeGreaterEqual(UInt32 a, UInt32 b);
class ENetPeer;
// Constants for the ENet implementation and protocol
@ -294,4 +302,6 @@ namespace Nz
#endif
}
#include <Nazara/Network/ENetProtocol.inl>
#endif // NAZARA_ENETPROTOCOL_HPP

View File

@ -0,0 +1,34 @@
// Copyright (C) 2017 Jérôme Leclercq
// This file is part of the "Nazara Engine - Network module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Network/ENetProtocol.hpp>
#include <Nazara/Network/Debug.hpp>
namespace Nz
{
UInt32 ENetTimeDifference(UInt32 a, UInt32 b)
{
return (ENetTimeLess(a, b)) ? b - a : a - b;
}
bool ENetTimeLess(UInt32 a, UInt32 b)
{
return (a - b >= ENetTimeOverflow);
}
bool ENetTimeLessEqual(UInt32 a, UInt32 b)
{
return !ENetTimeGreater(a, b);
}
bool ENetTimeGreater(UInt32 a, UInt32 b)
{
return ENetTimeLess(b, a);
}
bool ENetTimeGreaterEqual(UInt32 a, UInt32 b)
{
return !ENetTimeLess(a, b);
}
}

View File

@ -6,15 +6,6 @@
#include <Nazara/Network/NetPacket.hpp>
#include <Nazara/Network/Debug.hpp>
#define ENET_TIME_OVERFLOW 86400000
#define ENET_TIME_LESS(a, b) ((a) - (b) >= ENET_TIME_OVERFLOW)
#define ENET_TIME_GREATER(a, b) ((b) - (a) >= ENET_TIME_OVERFLOW)
#define ENET_TIME_LESS_EQUAL(a, b) (! ENET_TIME_GREATER (a, b))
#define ENET_TIME_GREATER_EQUAL(a, b) (! ENET_TIME_LESS (a, b))
#define ENET_TIME_DIFFERENCE(a, b) ((a) - (b) >= ENET_TIME_OVERFLOW ? (b) - (a) : (a) - (b))
namespace Nz
{
/// Temporary
@ -235,7 +226,7 @@ namespace Nz
do
{
if (ENET_TIME_DIFFERENCE(m_serviceTime, m_bandwidthThrottleEpoch) >= ENetConstants::ENetHost_BandwidthThrottleInterval)
if (ENetTimeDifference(m_serviceTime, m_bandwidthThrottleEpoch) >= ENetConstants::ENetHost_BandwidthThrottleInterval)
ThrottleBandwidth();
switch (SendOutgoingCommands(event, true))
@ -291,17 +282,17 @@ namespace Nz
return 1;
}
if (ENET_TIME_GREATER_EQUAL(m_serviceTime, timeout))
if (ENetTimeGreaterEqual(m_serviceTime, timeout))
return 0;
for (;;)
{
m_serviceTime = GetElapsedMilliseconds();
if (ENET_TIME_GREATER_EQUAL(m_serviceTime, timeout))
if (ENetTimeGreaterEqual(m_serviceTime, timeout))
return 0;
if (m_poller.Wait(ENET_TIME_DIFFERENCE(timeout, m_serviceTime)))
if (m_poller.Wait(ENetTimeDifference(timeout, m_serviceTime)))
break;
}
@ -962,7 +953,7 @@ namespace Nz
if (!currentPeer->m_acknowledgements.empty())
SendAcknowledgements(currentPeer);
if (checkForTimeouts && !currentPeer->m_sentReliableCommands.empty() && ENET_TIME_GREATER_EQUAL(m_serviceTime, currentPeer->m_nextTimeout) && currentPeer->CheckTimeouts(event))
if (checkForTimeouts && !currentPeer->m_sentReliableCommands.empty() && ENetTimeGreaterEqual(m_serviceTime, currentPeer->m_nextTimeout) && currentPeer->CheckTimeouts(event))
{
if (event && event->type != ENetEventType::None)
return 1;
@ -971,7 +962,7 @@ namespace Nz
}
if ((currentPeer->m_outgoingReliableCommands.empty() || SendReliableOutgoingCommands(currentPeer)) && currentPeer->m_sentReliableCommands.empty() &&
ENET_TIME_DIFFERENCE(m_serviceTime, currentPeer->m_lastReceiveTime) >= currentPeer->m_pingInterval && currentPeer->m_mtu - m_packetSize >= sizeof(ENetProtocolPing))
ENetTimeDifference(m_serviceTime, currentPeer->m_lastReceiveTime) >= currentPeer->m_pingInterval && currentPeer->m_mtu - m_packetSize >= sizeof(ENetProtocolPing))
{
currentPeer->Ping();
SendReliableOutgoingCommands(currentPeer);
@ -985,7 +976,7 @@ namespace Nz
if (currentPeer->m_packetLossEpoch == 0)
currentPeer->m_packetLossEpoch = m_serviceTime;
else if (ENET_TIME_DIFFERENCE(m_serviceTime, currentPeer->m_packetLossEpoch) >= ENetPeer_PacketLossInterval && currentPeer->m_packetsSent > 0)
else if (ENetTimeDifference(m_serviceTime, currentPeer->m_packetLossEpoch) >= ENetPeer_PacketLossInterval && currentPeer->m_packetsSent > 0)
{
UInt32 packetLoss = currentPeer->m_packetsLost * ENetPeer_PacketLossScale / currentPeer->m_packetsSent;

View File

@ -319,14 +319,14 @@ namespace Nz
++currentCommand;
if (ENET_TIME_DIFFERENCE(m_host->m_serviceTime, outgoingCommand->sentTime) < outgoingCommand->roundTripTimeout)
if (ENetTimeDifference(serviceTime, outgoingCommand->sentTime) < outgoingCommand->roundTripTimeout)
continue;
if (m_earliestTimeout == 0 || ENET_TIME_LESS(outgoingCommand->sentTime, m_earliestTimeout))
if (m_earliestTimeout == 0 || ENetTimeLess(outgoingCommand->sentTime, m_earliestTimeout))
m_earliestTimeout = outgoingCommand->sentTime;
if (m_earliestTimeout != 0 && (ENET_TIME_DIFFERENCE(m_host->m_serviceTime, m_earliestTimeout) >= m_timeoutMaximum ||
(outgoingCommand->roundTripTimeout >= outgoingCommand->roundTripTimeoutLimit && ENET_TIME_DIFFERENCE(m_host->m_serviceTime, m_earliestTimeout) >= m_timeoutMinimum)))
if (m_earliestTimeout != 0 && (ENetTimeDifference(serviceTime, m_earliestTimeout) >= m_timeoutMaximum ||
(outgoingCommand->roundTripTimeout >= outgoingCommand->roundTripTimeoutLimit && ENetTimeDifference(serviceTime, m_earliestTimeout) >= m_timeoutMinimum)))
{
m_host->NotifyDisconnect(this, event);
return true;
@ -481,13 +481,13 @@ namespace Nz
if ((receivedSentTime & 0x8000) > (serviceTime & 0x8000))
receivedSentTime -= 0x10000;
if (ENET_TIME_LESS(serviceTime, receivedSentTime))
if (ENetTimeLess(serviceTime, receivedSentTime))
return true;
m_lastReceiveTime = serviceTime;
m_earliestTimeout = 0;
UInt32 roundTripTime = ENET_TIME_DIFFERENCE(serviceTime, receivedSentTime);
UInt32 roundTripTime = ENetTimeDifference(serviceTime, receivedSentTime);
Throttle(roundTripTime);
@ -507,7 +507,7 @@ namespace Nz
m_lowestRoundTripTime = std::min(m_lowestRoundTripTime, m_roundTripTime);
m_highestRoundTripTimeVariance = std::max(m_highestRoundTripTimeVariance, m_roundTripTimeVariance);
if (m_packetThrottleEpoch == 0 || ENET_TIME_DIFFERENCE(serviceTime, m_packetThrottleEpoch) >= m_packetThrottleInterval)
if (m_packetThrottleEpoch == 0 || ENetTimeDifference(serviceTime, m_packetThrottleEpoch) >= m_packetThrottleInterval)
{
m_lastRoundTripTime = m_lowestRoundTripTime;
m_lastRoundTripTimeVariance = m_highestRoundTripTimeVariance;