Network/RUdpConnection: Add packet loss simulator
Former-commit-id: 8778d54b5b5a9038ec6b9d888cf6c49ad6c5721c
This commit is contained in:
parent
1a5dd41407
commit
76bc70b210
|
|
@ -56,6 +56,8 @@ namespace Nz
|
||||||
inline void SetProtocolId(UInt32 protocolId);
|
inline void SetProtocolId(UInt32 protocolId);
|
||||||
inline void SetTimeBeforeAck(UInt32 ms);
|
inline void SetTimeBeforeAck(UInt32 ms);
|
||||||
|
|
||||||
|
inline void SimulateNetwork(double packetLoss);
|
||||||
|
|
||||||
void Update();
|
void Update();
|
||||||
|
|
||||||
RUdpConnection& operator=(const RUdpConnection&) = delete;
|
RUdpConnection& operator=(const RUdpConnection&) = delete;
|
||||||
|
|
@ -137,9 +139,10 @@ namespace Nz
|
||||||
UInt64 stateData1;
|
UInt64 stateData1;
|
||||||
};
|
};
|
||||||
|
|
||||||
std::unordered_map<IpAddress, std::size_t> m_peerByIP;
|
std::bernoulli_distribution m_packetLossProbability;
|
||||||
std::queue<RUdpMessage> m_receivedMessages;
|
std::queue<RUdpMessage> m_receivedMessages;
|
||||||
std::size_t m_peerIterator;
|
std::size_t m_peerIterator;
|
||||||
|
std::unordered_map<IpAddress, std::size_t> m_peerByIP;
|
||||||
std::vector<PeerData> m_peers;
|
std::vector<PeerData> m_peers;
|
||||||
Bitset<UInt64> m_activeClients;
|
Bitset<UInt64> m_activeClients;
|
||||||
Clock m_clock;
|
Clock m_clock;
|
||||||
|
|
@ -151,6 +154,7 @@ namespace Nz
|
||||||
UInt32 m_timeBeforePing;
|
UInt32 m_timeBeforePing;
|
||||||
UInt32 m_timeBeforeTimeOut;
|
UInt32 m_timeBeforeTimeOut;
|
||||||
UInt64 m_currentTime;
|
UInt64 m_currentTime;
|
||||||
|
bool m_isSimulationEnabled;
|
||||||
bool m_shouldAcceptConnections;
|
bool m_shouldAcceptConnections;
|
||||||
|
|
||||||
static std::mt19937_64 s_randomGenerator;
|
static std::mt19937_64 s_randomGenerator;
|
||||||
|
|
|
||||||
|
|
@ -121,6 +121,19 @@ namespace Nz
|
||||||
NazaraError("PacketReliability not handled (0x" + String::Number(reliability, 16) + ')');
|
NazaraError("PacketReliability not handled (0x" + String::Number(reliability, 16) + ')');
|
||||||
return false;
|
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>
|
#include <Nazara/Network/DebugOff.hpp>
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ namespace Nz
|
||||||
m_timeBeforePing(500'000), //< 0.5s
|
m_timeBeforePing(500'000), //< 0.5s
|
||||||
m_timeBeforeTimeOut(10'000'000), //< 10s
|
m_timeBeforeTimeOut(10'000'000), //< 10s
|
||||||
m_currentTime(0),
|
m_currentTime(0),
|
||||||
|
m_isSimulationEnabled(false),
|
||||||
m_shouldAcceptConnections(true)
|
m_shouldAcceptConnections(true)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
@ -34,7 +35,7 @@ namespace Nz
|
||||||
NetPacket connectionRequestPacket(NetCode_RequestConnection);
|
NetPacket connectionRequestPacket(NetCode_RequestConnection);
|
||||||
connectionRequestPacket << client.stateData1;
|
connectionRequestPacket << client.stateData1;
|
||||||
|
|
||||||
EnqueuePacket(client, PacketPriority_Immediate, PacketReliability_Unreliable, connectionRequestPacket);
|
EnqueuePacket(client, PacketPriority_Immediate, PacketReliability_Reliable, connectionRequestPacket);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -349,6 +350,12 @@ namespace Nz
|
||||||
if (peer.receivedQueue.find(sequenceId) != peer.receivedQueue.end())
|
if (peer.receivedQueue.find(sequenceId) != peer.receivedQueue.end())
|
||||||
return; //< Ignore
|
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
|
///< Receiving a packet from an acknowledged client means the connection works in both ways
|
||||||
if (peer.state == PeerState_Aknowledged && packet.GetNetCode() != NetCode_RequestConnection)
|
if (peer.state == PeerState_Aknowledged && packet.GetNetCode() != NetCode_RequestConnection)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue