Skip to content

Commit e3b422b

Browse files
author
mischa
committed
ParseFastack explained & tests
1 parent 4824c26 commit e3b422b

2 files changed

Lines changed: 112 additions & 1 deletion

File tree

kcp2k/Assets/Tests/Editor/KcpTests.cs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ void Output(byte[] data, int len) {}
159159
Assert.That(kcp.snd_buf[2], Is.EqualTo(three));
160160
}
161161

162+
// test with empty buffer
162163
[Test]
163164
public void ParseUna_Empty()
164165
{
@@ -172,6 +173,7 @@ void Output(byte[] data, int len) {}
172173
Assert.That(kcp.snd_buf.Count, Is.EqualTo(0));
173174
}
174175

176+
// test where no elements should be removed
175177
[Test]
176178
public void ParseUna_None()
177179
{
@@ -196,6 +198,7 @@ void Output(byte[] data, int len) {}
196198
Assert.That(kcp.snd_buf[2], Is.EqualTo(three));
197199
}
198200

201+
// test where some elements should be removed
199202
[Test]
200203
public void ParseUna_Some()
201204
{
@@ -219,6 +222,7 @@ void Output(byte[] data, int len) {}
219222
Assert.That(kcp.snd_buf[1], Is.EqualTo(three));
220223
}
221224

225+
// test where all elements should be removed
222226
[Test]
223227
public void ParseUna_All()
224228
{
@@ -240,6 +244,111 @@ void Output(byte[] data, int len) {}
240244
Assert.That(kcp.snd_buf.Count, Is.EqualTo(0));
241245
}
242246

247+
// test with no elements in send buffer
248+
[Test]
249+
public void ParseFastack_Empty()
250+
{
251+
void Output(byte[] data, int len) {}
252+
253+
// setup KCP
254+
Kcp kcp = new Kcp(0, Output);
255+
256+
kcp.snd_una = 1; // == sn to ensure <= is checked
257+
kcp.snd_nxt = 2; // sn + 1 to ensure < is checked
258+
259+
kcp.ParseFastack(1, 0);
260+
Assert.That(kcp.snd_buf.Count, Is.EqualTo(0));
261+
}
262+
263+
// test where no elements should be counted
264+
[Test]
265+
public void ParseFastAck_None()
266+
{
267+
void Output(byte[] data, int len) {}
268+
269+
// setup KCP
270+
Kcp kcp = new Kcp(0, Output);
271+
272+
// insert three segments into send buffer
273+
Segment one = new Segment{sn=2};
274+
kcp.snd_buf.Add(one);
275+
Segment two = new Segment{sn=3};
276+
kcp.snd_buf.Add(two);
277+
Segment three = new Segment{sn=4};
278+
kcp.snd_buf.Add(three);
279+
280+
// sn needs to be between snd_una and snd_nxt
281+
kcp.snd_una = 1; // == sn to ensure <= is checked
282+
kcp.snd_nxt = 2; // sn + 1 to ensure < is checked
283+
284+
// only segments with seg.sn < sn should have their fastack incremented
285+
// in this case, none
286+
kcp.ParseFastack(1, 0);
287+
Assert.That(kcp.snd_buf.Count, Is.EqualTo(3));
288+
Assert.That(kcp.snd_buf[0].fastack, Is.EqualTo(0));
289+
Assert.That(kcp.snd_buf[1].fastack, Is.EqualTo(0));
290+
Assert.That(kcp.snd_buf[2].fastack, Is.EqualTo(0));
291+
}
292+
293+
// test where some elements should be counted
294+
[Test]
295+
public void ParseFastAck_Some()
296+
{
297+
void Output(byte[] data, int len) {}
298+
299+
// setup KCP
300+
Kcp kcp = new Kcp(0, Output);
301+
302+
// insert three segments into send buffer
303+
Segment one = new Segment{sn=2};
304+
kcp.snd_buf.Add(one);
305+
Segment two = new Segment{sn=3};
306+
kcp.snd_buf.Add(two);
307+
Segment three = new Segment{sn=4};
308+
kcp.snd_buf.Add(three);
309+
310+
// sn needs to be between snd_una and snd_nxt
311+
kcp.snd_una = 3; // == sn to ensure <= is checked
312+
kcp.snd_nxt = 4; // sn + 1 to ensure < is checked
313+
314+
// only segments with seg.sn < sn should have their fastack incremented
315+
kcp.ParseFastack(3, 0);
316+
Assert.That(kcp.snd_buf.Count, Is.EqualTo(3));
317+
Assert.That(kcp.snd_buf[0].fastack, Is.EqualTo(1));
318+
Assert.That(kcp.snd_buf[1].fastack, Is.EqualTo(0));
319+
Assert.That(kcp.snd_buf[2].fastack, Is.EqualTo(0));
320+
}
321+
322+
// test where all elements should be counted
323+
[Test]
324+
public void ParseFastAck_All()
325+
{
326+
void Output(byte[] data, int len) {}
327+
328+
// setup KCP
329+
Kcp kcp = new Kcp(0, Output);
330+
331+
// insert three segments into send buffer
332+
Segment one = new Segment{sn=2};
333+
kcp.snd_buf.Add(one);
334+
Segment two = new Segment{sn=3};
335+
kcp.snd_buf.Add(two);
336+
Segment three = new Segment{sn=4};
337+
kcp.snd_buf.Add(three);
338+
339+
// sn needs to be between snd_una and snd_nxt
340+
kcp.snd_una = 5; // == sn to ensure <= is checked
341+
kcp.snd_nxt = 6; // sn + 1 to ensure < is checked
342+
343+
// only segments with seg.sn < sn should have their fastack incremented
344+
// in this case, all
345+
kcp.ParseFastack(5, 0);
346+
Assert.That(kcp.snd_buf.Count, Is.EqualTo(3));
347+
Assert.That(kcp.snd_buf[0].fastack, Is.EqualTo(1));
348+
Assert.That(kcp.snd_buf[1].fastack, Is.EqualTo(1));
349+
Assert.That(kcp.snd_buf[2].fastack, Is.EqualTo(1));
350+
}
351+
243352
// peek without any messages in the receive queue
244353
[Test]
245354
public void PeekSize_Empty()

kcp2k/Assets/kcp2k/kcp/Kcp.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,10 @@ internal void ParseUna(uint una)
410410
}
411411

412412
// ikcp_parse_fastack
413-
void ParseFastack(uint sn, uint ts) // serial number, timestamp
413+
internal void ParseFastack(uint sn, uint ts) // serial number, timestamp
414414
{
415+
// sn needs to be between snd_una and snd_nxt
416+
// if !(snd_una <= sn && sn < snd_nxt) return;
415417
if (Utils.TimeDiff(sn, snd_una) < 0)
416418
return;
417419

0 commit comments

Comments
 (0)