From 20508588cbf9b2af5d666fa10c2cfb71f3eff2cc Mon Sep 17 00:00:00 2001 From: RevenantX Date: Sun, 14 Jan 2018 14:12:29 +0200 Subject: [PATCH] fix reliable channel (fix #129) --- LibSample/EchoMessagesTest.cs | 4 ++++ LibSample/Program.cs | 32 +++++++++++++++++--------------- LiteNetLib/ReliableChannel.cs | 13 +++++-------- 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/LibSample/EchoMessagesTest.cs b/LibSample/EchoMessagesTest.cs index d86daea7..06612e1d 100644 --- a/LibSample/EchoMessagesTest.cs +++ b/LibSample/EchoMessagesTest.cs @@ -138,6 +138,8 @@ public void Run() _serverListener = new ServerListener(); NetManager server = new NetManager(_serverListener, 2, "myapp1"); + server.SimulatePacketLoss = true; + server.SimulationPacketLossChance = 70; //server.ReuseAddress = true; if (!server.Start(9050)) { @@ -152,6 +154,8 @@ public void Run() NetManager client1 = new NetManager(_clientListener, "myapp1"); //client1.SimulateLatency = true; + client1.SimulatePacketLoss = true; + client1.SimulationPacketLossChance = 70; client1.SimulationMaxLatency = 1500; client1.MergeEnabled = true; if (!client1.Start()) diff --git a/LibSample/Program.cs b/LibSample/Program.cs index 45c035c4..884d9f7f 100644 --- a/LibSample/Program.cs +++ b/LibSample/Program.cs @@ -1,26 +1,28 @@ using System; -using LibSample; using LiteNetLib; -class Program +namespace LibSample { - static void Main(string[] args) + class Program { - //Test ntp - NetUtils.RequestTimeFromNTP("pool.ntp.org", 123, dateTime => + static void Main(string[] args) { - if (dateTime.HasValue) + //Test ntp + NetUtils.RequestTimeFromNTP("pool.ntp.org", 123, dateTime => { - Console.WriteLine("[MAIN] Synced time test: " + dateTime.Value); - } - }); + if (dateTime.HasValue) + { + Console.WriteLine("[MAIN] Synced time test: " + dateTime.Value); + } + }); - //new EchoMessagesTest().Run(); - //new HolePunchServerTest().Run(); - //new BroadcastTest().Run(); + new EchoMessagesTest().Run(); + //new HolePunchServerTest().Run(); + //new BroadcastTest().Run(); - //new BenchmarkTest.TestHost().Run(); - new SpeedBecnh().Run(); - //new SerializerTest().Run(); + //new BenchmarkTest.TestHost().Run(); + //new SpeedBecnh().Run(); + //new SerializerTest().Run(); + } } } diff --git a/LiteNetLib/ReliableChannel.cs b/LiteNetLib/ReliableChannel.cs index 0545f7b1..e9041ccd 100644 --- a/LiteNetLib/ReliableChannel.cs +++ b/LiteNetLib/ReliableChannel.cs @@ -13,7 +13,7 @@ private sealed class PendingPacket public PendingPacket Next; public PendingPacket Prev; - public NetPacket GetAndClear() + public void Clear() { if (Prev != null) { @@ -25,11 +25,8 @@ public NetPacket GetAndClear() } Prev = null; Next = null; - - var packet = Packet; Packet = null; TimeStamp = null; - return packet; } } @@ -143,14 +140,14 @@ public void ProcessAck(NetPacket packet) } PendingPacket pendingPacket = _pendingPackets[ackSequence % _windowSize]; - NetPacket removed = pendingPacket.GetAndClear(); - if (removed != null) + if (pendingPacket.Packet != null) { if (pendingPacket == _headPendingPacket) { - _headPendingPacket = null; + _headPendingPacket = _headPendingPacket.Prev; } - _peer.Recycle(removed); + _peer.Recycle(pendingPacket.Packet); + pendingPacket.Clear(); NetUtils.DebugWrite("[PA]Removing reliableInOrder ack: {0} - true", ackSequence); } else