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>

View File

@ -18,6 +18,7 @@ namespace Nz
m_timeBeforePing(500'000), //< 0.5s
m_timeBeforeTimeOut(10'000'000), //< 10s
m_currentTime(0),
m_isSimulationEnabled(false),
m_shouldAcceptConnections(true)
{
}
@ -34,7 +35,7 @@ namespace Nz
NetPacket connectionRequestPacket(NetCode_RequestConnection);
connectionRequestPacket << client.stateData1;
EnqueuePacket(client, PacketPriority_Immediate, PacketReliability_Unreliable, connectionRequestPacket);
EnqueuePacket(client, PacketPriority_Immediate, PacketReliability_Reliable, connectionRequestPacket);
return true;
}
@ -349,6 +350,12 @@ namespace Nz
if (peer.receivedQueue.find(sequenceId) != peer.receivedQueue.end())
return; //< Ignore
if (m_isSimulationEnabled && m_packetLossProbability(s_randomGenerator))
{
NazaraNotice(m_socket.GetBoundAddress().ToString() + ": Lost packet " + String::Number(sequenceId) + " from " + peerIp.ToString() + " for simulation purpose");
return;
}
///< Receiving a packet from an acknowledged client means the connection works in both ways
if (peer.state == PeerState_Aknowledged && packet.GetNetCode() != NetCode_RequestConnection)
{