Skip to content

Commit 7f5dc1c

Browse files
author
JS Fauteux
authored
fix: Packet loss now returns the value of the current frame instead of connection lifetime (#2004)
1 parent 6d7c00e commit 7f5dc1c

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
3434
- Fixed `NetworkAnimator` issue where it was not taking the animation speed or state speed multiplier into consideration. (#1946)
3535
- Fixed `NetworkAnimator` issue where it was not properly synchronizing late joining clients if they joined while `Animator` was transitioning between states. (#1946)
3636
- Fixed `NetworkAnimator` issue where the server was not relaying changes to non-owner clients when a client was the owner. (#1946)
37+
- Fixed issue where the `PacketLoss` metric for tools would return the packet loss over a connection lifetime instead of a single frame. (#2004)
3738

3839
## [1.0.0-pre.9] - 2022-05-10
3940

com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,15 @@ public struct SimulatorParameters
243243
PacketDropRate = 0
244244
};
245245

246+
private struct PacketLossCache
247+
{
248+
public int PacketsReceived;
249+
public int PacketsDropped;
250+
public float PacketLoss;
251+
};
252+
253+
private PacketLossCache m_PacketLossCache = new PacketLossCache();
254+
246255
private State m_State = State.Disconnected;
247256
private NetworkDriver m_Driver;
248257
private NetworkSettings m_NetworkSettings;
@@ -839,11 +848,22 @@ private float ExtractPacketLoss(NetworkConnection networkConnection)
839848
{
840849
var sharedContext = (ReliableUtility.SharedContext*)sharedBuffer.GetUnsafePtr();
841850

842-
var packetReceived = (float)sharedContext->stats.PacketsReceived;
843-
var packetDropped = (float)sharedContext->stats.PacketsDropped;
844-
var packetLoss = packetReceived > 0 ? packetDropped / packetReceived : 0;
851+
var packetReceivedDelta = (float)(sharedContext->stats.PacketsReceived - m_PacketLossCache.PacketsReceived);
852+
var packetDroppedDelta = (float)(sharedContext->stats.PacketsDropped - m_PacketLossCache.PacketsDropped);
853+
854+
// There can be multiple update happening in a single frame where no packets have transitioned
855+
// In those situation we want to return the last packet loss value instead of 0 to avoid invalid swings
856+
if (packetDroppedDelta == 0 && packetReceivedDelta == 0)
857+
{
858+
return m_PacketLossCache.PacketLoss;
859+
}
860+
861+
m_PacketLossCache.PacketsReceived = sharedContext->stats.PacketsReceived;
862+
m_PacketLossCache.PacketsDropped = sharedContext->stats.PacketsDropped;
863+
864+
m_PacketLossCache.PacketLoss = packetReceivedDelta > 0 ? packetDroppedDelta / packetReceivedDelta : 0;
845865

846-
return packetLoss;
866+
return m_PacketLossCache.PacketLoss;
847867
}
848868
}
849869

com.unity.netcode.gameobjects/Tests/Runtime/Metrics/PacketLossMetricsTests.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class PacketLossMetricsTests : NetcodeIntegrationTest
1818
{
1919
protected override int NumberOfClients => 1;
2020
private readonly int m_PacketLossRate = 25;
21-
private int m_DropInterval = 5;
21+
private readonly int m_PacketLossRangeDelta = 5;
2222

2323
public PacketLossMetricsTests()
2424
: base(HostOrServer.Server)
@@ -57,11 +57,12 @@ public IEnumerator TrackPacketLossAsServer()
5757
[UnityTest]
5858
public IEnumerator TrackPacketLossAsClient()
5959
{
60-
double packetLossRate = m_PacketLossRate/100d;
60+
double packetLossRateMinRange = (m_PacketLossRate-m_PacketLossRangeDelta) / 100d;
61+
double packetLossRateMaxrange = (m_PacketLossRate + m_PacketLossRangeDelta) / 100d;
6162
var clientNetworkManager = m_ClientNetworkManagers[0];
6263
var waitForPacketLossMetric = new WaitForGaugeMetricValues((clientNetworkManager.NetworkMetrics as NetworkMetrics).Dispatcher,
6364
NetworkMetricTypes.PacketLoss,
64-
metric => Math.Abs(metric - packetLossRate) < Double.Epsilon);
65+
metric => packetLossRateMinRange <= metric && metric <= packetLossRateMaxrange);
6566

6667
for (int i = 0; i < 1000; ++i)
6768
{
@@ -75,7 +76,7 @@ public IEnumerator TrackPacketLossAsClient()
7576
yield return waitForPacketLossMetric.WaitForMetricsReceived();
7677

7778
var packetLossValue = waitForPacketLossMetric.AssertMetricValueHaveBeenFound();
78-
Assert.AreEqual(packetLossRate, packetLossValue);
79+
Assert.That(packetLossValue, Is.InRange(packetLossRateMinRange, packetLossRateMaxrange));
7980
}
8081
}
8182
}

0 commit comments

Comments
 (0)