Network/ENet: Add peer-side lag simulation

This commit is contained in:
Lynix
2017-02-20 23:37:31 +01:00
parent cc57fed42e
commit b0d0a63fca
6 changed files with 104 additions and 14 deletions

View File

@@ -100,13 +100,20 @@ namespace Nz
static bool Initialize();
static void Uninitialize();
struct PendingPacket
struct PendingIncomingPacket
{
IpAddress from;
NetPacket data;
UInt32 deliveryTime;
};
struct PendingOutgoingPacket
{
IpAddress to;
NetPacket data;
UInt32 deliveryTime;
};
std::array<ENetProtocol, ENetConstants::ENetProtocol_MaximumPacketCommands> m_commands;
std::array<NetBuffer, ENetConstants::ENetProtocol_MaximumPacketCommands * 2 + 1> m_buffers;
std::array<UInt8, ENetConstants::ENetProtocol_MaximumMTU> m_packetData[2];
@@ -123,7 +130,8 @@ namespace Nz
std::size_t m_receivedDataLength;
std::uniform_int_distribution<UInt16> m_packetDelayDistribution;
std::vector<ENetPeer> m_peers;
std::vector<PendingPacket> m_pendingPackets;
std::vector<PendingIncomingPacket> m_pendingIncomingPackets;
std::vector<PendingOutgoingPacket> m_pendingOutgoingPackets;
UInt8* m_receivedData;
Bitset<UInt64> m_dispatchQueue;
MemoryPool m_packetPool;

View File

@@ -27,6 +27,7 @@
#include <Nazara/Network/UdpSocket.hpp>
#include <array>
#include <list>
#include <random>
#include <vector>
namespace Nz
@@ -59,6 +60,7 @@ namespace Nz
inline bool HasPendingCommands();
inline bool IsConnected() const;
inline bool IsSimulationEnabled() const;
void Ping();
@@ -68,6 +70,8 @@ namespace Nz
bool Send(UInt8 channelId, ENetPacketRef packetRef);
bool Send(UInt8 channelId, ENetPacketFlags flags, NetPacket&& packet);
void SimulateNetwork(double packetLossProbability, UInt16 minDelay, UInt16 maxDelay);
void ThrottleConfigure(UInt32 interval, UInt32 acceleration, UInt32 deceleration);
ENetPeer& operator=(const ENetPeer&) = delete;
@@ -177,12 +181,14 @@ namespace Nz
ENetHost* m_host;
IpAddress m_address; /**< Internet address of the peer */
std::array<UInt32, unsequencedWindow> m_unsequencedWindow;
std::bernoulli_distribution m_packetLossProbability;
std::list<IncomingCommmand> m_dispatchedCommands;
std::list<OutgoingCommand> m_outgoingReliableCommands;
std::list<OutgoingCommand> m_outgoingUnreliableCommands;
std::list<OutgoingCommand> m_sentReliableCommands;
std::list<OutgoingCommand> m_sentUnreliableCommands;
std::size_t m_totalWaitingData;
std::uniform_int_distribution<UInt16> m_packetDelayDistribution;
std::vector<Acknowledgement> m_acknowledgements;
std::vector<Channel> m_channels;
ENetPeerState m_state;
@@ -232,6 +238,7 @@ namespace Nz
UInt32 m_windowSize;
UInt64 m_totalPacketLost;
UInt64 m_totalPacketSent;
bool m_isSimulationEnabled;
};
}

View File

@@ -12,7 +12,8 @@ namespace Nz
m_host(host),
m_incomingSessionID(0xFF),
m_outgoingSessionID(0xFF),
m_incomingPeerID(peerId)
m_incomingPeerID(peerId),
m_isSimulationEnabled(false)
{
Reset();
}
@@ -62,6 +63,11 @@ namespace Nz
return m_state == ENetPeerState::Connected || m_state == ENetPeerState::DisconnectLater;
}
inline bool ENetPeer::IsSimulationEnabled() const
{
return m_isSimulationEnabled;
}
inline void ENetPeer::ChangeState(ENetPeerState state)
{
if (state == ENetPeerState::Connected || state == ENetPeerState::DisconnectLater)

View File

@@ -28,7 +28,7 @@ namespace Nz
{
ENetHost_BandwidthThrottleInterval = 1000,
ENetHost_DefaultMaximumPacketSize = 32 * 1024 * 1024,
ENetHost_DefaultMaximumWaitingData = 32 * 1024 * 1024,
ENetHost_DefaultMaximumWaitingData = 32 * 1024 * 1024,
ENetHost_DefaultMTU = 1400,
ENetHost_ReceiveBufferSize = 256 * 1024,
ENetHost_SendBufferSize = 256 * 1024,