Network/ENet: Clean up time functions
This commit is contained in:
parent
2f057191aa
commit
6b8d9deb43
|
|
@ -13,6 +13,14 @@
|
||||||
|
|
||||||
namespace Nz
|
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;
|
class ENetPeer;
|
||||||
|
|
||||||
// Constants for the ENet implementation and protocol
|
// Constants for the ENet implementation and protocol
|
||||||
|
|
@ -294,4 +302,6 @@ namespace Nz
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#include <Nazara/Network/ENetProtocol.inl>
|
||||||
|
|
||||||
#endif // NAZARA_ENETPROTOCOL_HPP
|
#endif // NAZARA_ENETPROTOCOL_HPP
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -6,15 +6,6 @@
|
||||||
#include <Nazara/Network/NetPacket.hpp>
|
#include <Nazara/Network/NetPacket.hpp>
|
||||||
#include <Nazara/Network/Debug.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
|
namespace Nz
|
||||||
{
|
{
|
||||||
/// Temporary
|
/// Temporary
|
||||||
|
|
@ -235,7 +226,7 @@ namespace Nz
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
if (ENET_TIME_DIFFERENCE(m_serviceTime, m_bandwidthThrottleEpoch) >= ENetConstants::ENetHost_BandwidthThrottleInterval)
|
if (ENetTimeDifference(m_serviceTime, m_bandwidthThrottleEpoch) >= ENetConstants::ENetHost_BandwidthThrottleInterval)
|
||||||
ThrottleBandwidth();
|
ThrottleBandwidth();
|
||||||
|
|
||||||
switch (SendOutgoingCommands(event, true))
|
switch (SendOutgoingCommands(event, true))
|
||||||
|
|
@ -291,17 +282,17 @@ namespace Nz
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ENET_TIME_GREATER_EQUAL(m_serviceTime, timeout))
|
if (ENetTimeGreaterEqual(m_serviceTime, timeout))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
m_serviceTime = GetElapsedMilliseconds();
|
m_serviceTime = GetElapsedMilliseconds();
|
||||||
|
|
||||||
if (ENET_TIME_GREATER_EQUAL(m_serviceTime, timeout))
|
if (ENetTimeGreaterEqual(m_serviceTime, timeout))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if (m_poller.Wait(ENET_TIME_DIFFERENCE(timeout, m_serviceTime)))
|
if (m_poller.Wait(ENetTimeDifference(timeout, m_serviceTime)))
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -962,7 +953,7 @@ namespace Nz
|
||||||
if (!currentPeer->m_acknowledgements.empty())
|
if (!currentPeer->m_acknowledgements.empty())
|
||||||
SendAcknowledgements(currentPeer);
|
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)
|
if (event && event->type != ENetEventType::None)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
@ -971,7 +962,7 @@ namespace Nz
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((currentPeer->m_outgoingReliableCommands.empty() || SendReliableOutgoingCommands(currentPeer)) && currentPeer->m_sentReliableCommands.empty() &&
|
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();
|
currentPeer->Ping();
|
||||||
SendReliableOutgoingCommands(currentPeer);
|
SendReliableOutgoingCommands(currentPeer);
|
||||||
|
|
@ -985,7 +976,7 @@ namespace Nz
|
||||||
|
|
||||||
if (currentPeer->m_packetLossEpoch == 0)
|
if (currentPeer->m_packetLossEpoch == 0)
|
||||||
currentPeer->m_packetLossEpoch = m_serviceTime;
|
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;
|
UInt32 packetLoss = currentPeer->m_packetsLost * ENetPeer_PacketLossScale / currentPeer->m_packetsSent;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -319,14 +319,14 @@ namespace Nz
|
||||||
|
|
||||||
++currentCommand;
|
++currentCommand;
|
||||||
|
|
||||||
if (ENET_TIME_DIFFERENCE(m_host->m_serviceTime, outgoingCommand->sentTime) < outgoingCommand->roundTripTimeout)
|
if (ENetTimeDifference(serviceTime, outgoingCommand->sentTime) < outgoingCommand->roundTripTimeout)
|
||||||
continue;
|
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;
|
m_earliestTimeout = outgoingCommand->sentTime;
|
||||||
|
|
||||||
if (m_earliestTimeout != 0 && (ENET_TIME_DIFFERENCE(m_host->m_serviceTime, m_earliestTimeout) >= m_timeoutMaximum ||
|
if (m_earliestTimeout != 0 && (ENetTimeDifference(serviceTime, m_earliestTimeout) >= m_timeoutMaximum ||
|
||||||
(outgoingCommand->roundTripTimeout >= outgoingCommand->roundTripTimeoutLimit && ENET_TIME_DIFFERENCE(m_host->m_serviceTime, m_earliestTimeout) >= m_timeoutMinimum)))
|
(outgoingCommand->roundTripTimeout >= outgoingCommand->roundTripTimeoutLimit && ENetTimeDifference(serviceTime, m_earliestTimeout) >= m_timeoutMinimum)))
|
||||||
{
|
{
|
||||||
m_host->NotifyDisconnect(this, event);
|
m_host->NotifyDisconnect(this, event);
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -481,13 +481,13 @@ namespace Nz
|
||||||
if ((receivedSentTime & 0x8000) > (serviceTime & 0x8000))
|
if ((receivedSentTime & 0x8000) > (serviceTime & 0x8000))
|
||||||
receivedSentTime -= 0x10000;
|
receivedSentTime -= 0x10000;
|
||||||
|
|
||||||
if (ENET_TIME_LESS(serviceTime, receivedSentTime))
|
if (ENetTimeLess(serviceTime, receivedSentTime))
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
m_lastReceiveTime = serviceTime;
|
m_lastReceiveTime = serviceTime;
|
||||||
m_earliestTimeout = 0;
|
m_earliestTimeout = 0;
|
||||||
|
|
||||||
UInt32 roundTripTime = ENET_TIME_DIFFERENCE(serviceTime, receivedSentTime);
|
UInt32 roundTripTime = ENetTimeDifference(serviceTime, receivedSentTime);
|
||||||
|
|
||||||
Throttle(roundTripTime);
|
Throttle(roundTripTime);
|
||||||
|
|
||||||
|
|
@ -507,7 +507,7 @@ namespace Nz
|
||||||
m_lowestRoundTripTime = std::min(m_lowestRoundTripTime, m_roundTripTime);
|
m_lowestRoundTripTime = std::min(m_lowestRoundTripTime, m_roundTripTime);
|
||||||
m_highestRoundTripTimeVariance = std::max(m_highestRoundTripTimeVariance, m_roundTripTimeVariance);
|
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_lastRoundTripTime = m_lowestRoundTripTime;
|
||||||
m_lastRoundTripTimeVariance = m_highestRoundTripTimeVariance;
|
m_lastRoundTripTimeVariance = m_highestRoundTripTimeVariance;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue