From e3b422b0839797bf9828b0895d3df63687b9821d Mon Sep 17 00:00:00 2001 From: mischa Date: Thu, 11 May 2023 15:31:00 +0900 Subject: [PATCH] ParseFastack explained & tests --- kcp2k/Assets/Tests/Editor/KcpTests.cs | 109 ++++++++++++++++++++++++++ kcp2k/Assets/kcp2k/kcp/Kcp.cs | 4 +- 2 files changed, 112 insertions(+), 1 deletion(-) diff --git a/kcp2k/Assets/Tests/Editor/KcpTests.cs b/kcp2k/Assets/Tests/Editor/KcpTests.cs index 07f0217d..99c0eed3 100644 --- a/kcp2k/Assets/Tests/Editor/KcpTests.cs +++ b/kcp2k/Assets/Tests/Editor/KcpTests.cs @@ -159,6 +159,7 @@ void Output(byte[] data, int len) {} Assert.That(kcp.snd_buf[2], Is.EqualTo(three)); } + // test with empty buffer [Test] public void ParseUna_Empty() { @@ -172,6 +173,7 @@ void Output(byte[] data, int len) {} Assert.That(kcp.snd_buf.Count, Is.EqualTo(0)); } + // test where no elements should be removed [Test] public void ParseUna_None() { @@ -196,6 +198,7 @@ void Output(byte[] data, int len) {} Assert.That(kcp.snd_buf[2], Is.EqualTo(three)); } + // test where some elements should be removed [Test] public void ParseUna_Some() { @@ -219,6 +222,7 @@ void Output(byte[] data, int len) {} Assert.That(kcp.snd_buf[1], Is.EqualTo(three)); } + // test where all elements should be removed [Test] public void ParseUna_All() { @@ -240,6 +244,111 @@ void Output(byte[] data, int len) {} Assert.That(kcp.snd_buf.Count, Is.EqualTo(0)); } + // test with no elements in send buffer + [Test] + public void ParseFastack_Empty() + { + void Output(byte[] data, int len) {} + + // setup KCP + Kcp kcp = new Kcp(0, Output); + + kcp.snd_una = 1; // == sn to ensure <= is checked + kcp.snd_nxt = 2; // sn + 1 to ensure < is checked + + kcp.ParseFastack(1, 0); + Assert.That(kcp.snd_buf.Count, Is.EqualTo(0)); + } + + // test where no elements should be counted + [Test] + public void ParseFastAck_None() + { + void Output(byte[] data, int len) {} + + // setup KCP + Kcp kcp = new Kcp(0, Output); + + // insert three segments into send buffer + Segment one = new Segment{sn=2}; + kcp.snd_buf.Add(one); + Segment two = new Segment{sn=3}; + kcp.snd_buf.Add(two); + Segment three = new Segment{sn=4}; + kcp.snd_buf.Add(three); + + // sn needs to be between snd_una and snd_nxt + kcp.snd_una = 1; // == sn to ensure <= is checked + kcp.snd_nxt = 2; // sn + 1 to ensure < is checked + + // only segments with seg.sn < sn should have their fastack incremented + // in this case, none + kcp.ParseFastack(1, 0); + Assert.That(kcp.snd_buf.Count, Is.EqualTo(3)); + Assert.That(kcp.snd_buf[0].fastack, Is.EqualTo(0)); + Assert.That(kcp.snd_buf[1].fastack, Is.EqualTo(0)); + Assert.That(kcp.snd_buf[2].fastack, Is.EqualTo(0)); + } + + // test where some elements should be counted + [Test] + public void ParseFastAck_Some() + { + void Output(byte[] data, int len) {} + + // setup KCP + Kcp kcp = new Kcp(0, Output); + + // insert three segments into send buffer + Segment one = new Segment{sn=2}; + kcp.snd_buf.Add(one); + Segment two = new Segment{sn=3}; + kcp.snd_buf.Add(two); + Segment three = new Segment{sn=4}; + kcp.snd_buf.Add(three); + + // sn needs to be between snd_una and snd_nxt + kcp.snd_una = 3; // == sn to ensure <= is checked + kcp.snd_nxt = 4; // sn + 1 to ensure < is checked + + // only segments with seg.sn < sn should have their fastack incremented + kcp.ParseFastack(3, 0); + Assert.That(kcp.snd_buf.Count, Is.EqualTo(3)); + Assert.That(kcp.snd_buf[0].fastack, Is.EqualTo(1)); + Assert.That(kcp.snd_buf[1].fastack, Is.EqualTo(0)); + Assert.That(kcp.snd_buf[2].fastack, Is.EqualTo(0)); + } + + // test where all elements should be counted + [Test] + public void ParseFastAck_All() + { + void Output(byte[] data, int len) {} + + // setup KCP + Kcp kcp = new Kcp(0, Output); + + // insert three segments into send buffer + Segment one = new Segment{sn=2}; + kcp.snd_buf.Add(one); + Segment two = new Segment{sn=3}; + kcp.snd_buf.Add(two); + Segment three = new Segment{sn=4}; + kcp.snd_buf.Add(three); + + // sn needs to be between snd_una and snd_nxt + kcp.snd_una = 5; // == sn to ensure <= is checked + kcp.snd_nxt = 6; // sn + 1 to ensure < is checked + + // only segments with seg.sn < sn should have their fastack incremented + // in this case, all + kcp.ParseFastack(5, 0); + Assert.That(kcp.snd_buf.Count, Is.EqualTo(3)); + Assert.That(kcp.snd_buf[0].fastack, Is.EqualTo(1)); + Assert.That(kcp.snd_buf[1].fastack, Is.EqualTo(1)); + Assert.That(kcp.snd_buf[2].fastack, Is.EqualTo(1)); + } + // peek without any messages in the receive queue [Test] public void PeekSize_Empty() diff --git a/kcp2k/Assets/kcp2k/kcp/Kcp.cs b/kcp2k/Assets/kcp2k/kcp/Kcp.cs index 5d8de357..37a2a310 100755 --- a/kcp2k/Assets/kcp2k/kcp/Kcp.cs +++ b/kcp2k/Assets/kcp2k/kcp/Kcp.cs @@ -410,8 +410,10 @@ internal void ParseUna(uint una) } // ikcp_parse_fastack - void ParseFastack(uint sn, uint ts) // serial number, timestamp + internal void ParseFastack(uint sn, uint ts) // serial number, timestamp { + // sn needs to be between snd_una and snd_nxt + // if !(snd_una <= sn && sn < snd_nxt) return; if (Utils.TimeDiff(sn, snd_una) < 0) return;