Network/RUdpConnection: Add packet loss simulator

Former-commit-id: 8778d54b5b5a9038ec6b9d888cf6c49ad6c5721c
This commit is contained in:
Lynix
2016-04-17 17:56:02 +02:00
parent 1a5dd41407
commit 76bc70b210
3 changed files with 26 additions and 2 deletions

View File

@@ -56,6 +56,8 @@ namespace Nz
inline void SetProtocolId(UInt32 protocolId);
inline void SetTimeBeforeAck(UInt32 ms);
inline void SimulateNetwork(double packetLoss);
void Update();
RUdpConnection& operator=(const RUdpConnection&) = delete;
@@ -137,9 +139,10 @@ namespace Nz
UInt64 stateData1;
};
std::unordered_map<IpAddress, std::size_t> m_peerByIP;
std::bernoulli_distribution m_packetLossProbability;
std::queue<RUdpMessage> m_receivedMessages;
std::size_t m_peerIterator;
std::unordered_map<IpAddress, std::size_t> m_peerByIP;
std::vector<PeerData> m_peers;
Bitset<UInt64> m_activeClients;
Clock m_clock;
@@ -151,6 +154,7 @@ namespace Nz
UInt32 m_timeBeforePing;
UInt32 m_timeBeforeTimeOut;
UInt64 m_currentTime;
bool m_isSimulationEnabled;
bool m_shouldAcceptConnections;
static std::mt19937_64 s_randomGenerator;

View File

@@ -121,6 +121,19 @@ namespace Nz
NazaraError("PacketReliability not handled (0x" + String::Number(reliability, 16) + ')');
return false;
}
inline void RUdpConnection::SimulateNetwork(double packetLoss)
{
NazaraAssert(packetLoss >= 0.0 && packetLoss <= 1.0, "Packet loss must be in range [0..1]");
if (packetLoss > 0.0)
{
m_isSimulationEnabled = true;
m_packetLossProbability = std::bernoulli_distribution(packetLoss);
}
else
m_isSimulationEnabled = false;
}
}
#include <Nazara/Network/DebugOff.hpp>