Fix: ArgumentExceptionError when adding snapshots to buffer (#2862)

* Fix - From !isLocalPlayer to !hasAuthority

Changed !isLocalPlayer to !hasAuthority because object might not be player object.

* Update Assets/Mirror/Components/NetworkTransform2k/NetworkTransformBase.cs

* Fix ArgumentExceptionError when adding snapshot to buffer - SnapshotInterpolation.cs

Occassionally duplicate messages may be received with the same remoteTimestamp. Adding an already existing key to buffer (Sorted List) will throw an ArgumentExceptionError and disconnect the client from the server.
Fixed by adding an if check to add snapshot only if remoteTimestamp key does not exist in the buffer.

* Test for ArgExceptionError - Adding snapshot to buffer with same key

* Update SnapshotInterpolation.cs

* Update SnapshotInterpolationTests.cs

Co-authored-by: vis2k <info@noobtuts.com>
This commit is contained in:
ninjakickja 2021-08-01 19:53:10 +08:00 committed by GitHub
parent 03cd2e286f
commit 053ab364a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 1 deletions

View File

@ -40,7 +40,10 @@ public static void InsertIfNewEnough<T>(T snapshot, SortedList<double, T> buffer
return;
// otherwise sort it into the list
buffer.Add(timestamp, snapshot);
// an UDP messages might arrive twice sometimes.
// SortedList throws if key already exists, so check.
if (!buffer.ContainsKey(timestamp))
buffer.Add(timestamp, snapshot);
}
// helper function to check if we have >= n old enough snapshots.

View File

@ -161,6 +161,29 @@ public void HasAmountOlderThan_EnoughAndOldEnough()
Assert.That(SnapshotInterpolation.HasAmountOlderThan(buffer, 2.1, 3), Is.True);
}
// UDP messages might arrive twice sometimes.
// make sure InsertIfNewEnough can handle it.
[Test]
public void InsertIfNewEnough_ReceivedSameSnapshotTwice()
{
SimpleSnapshot a = new SimpleSnapshot(0, 0, 0);
SimpleSnapshot b = new SimpleSnapshot(1, 1, 0);
SimpleSnapshot c = new SimpleSnapshot(2, 2, 0);
// add two valid snapshots first.
// we can't add 'duplicates' before 3rd and 4th anyway.
SnapshotInterpolation.InsertIfNewEnough(a, buffer);
SnapshotInterpolation.InsertIfNewEnough(b, buffer);
// insert C which is newer than B.
// then insert it again because it arrive twice.
SnapshotInterpolation.InsertIfNewEnough(c, buffer);
SnapshotInterpolation.InsertIfNewEnough(c, buffer);
// count should still be 3.
Assert.That(buffer.Count, Is.EqualTo(3));
}
[Test]
public void CalculateCatchup_Empty()
{