-
Notifications
You must be signed in to change notification settings - Fork 461
fix: Packet loss now returns the value of the current frame instead of connection lifetime #2004
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
5f27a53
eeb2a02
48c22fc
bd9755c
d4c2bcd
79d9266
9bdd609
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -243,6 +243,15 @@ public struct SimulatorParameters | |
| PacketDropRate = 0 | ||
| }; | ||
|
|
||
| private struct PacketLossCache | ||
| { | ||
| public int PacketsReceived; | ||
| public int PacketsDropped; | ||
| public float PacketLoss; | ||
| }; | ||
|
|
||
| private PacketLossCache m_PacketLossCache = new PacketLossCache(); | ||
|
|
||
| private State m_State = State.Disconnected; | ||
| private NetworkDriver m_Driver; | ||
| private NetworkSettings m_NetworkSettings; | ||
|
|
@@ -839,11 +848,22 @@ private float ExtractPacketLoss(NetworkConnection networkConnection) | |
| { | ||
| var sharedContext = (ReliableUtility.SharedContext*)sharedBuffer.GetUnsafePtr(); | ||
|
|
||
| var packetReceived = (float)sharedContext->stats.PacketsReceived; | ||
| var packetDropped = (float)sharedContext->stats.PacketsDropped; | ||
| var packetLoss = packetReceived > 0 ? packetDropped / packetReceived : 0; | ||
| var packetReceivedDelta = (float)(sharedContext->stats.PacketsReceived - m_PacketLossCache.PacketsReceived); | ||
| var packetDroppedDelta = (float)(sharedContext->stats.PacketsDropped - m_PacketLossCache.PacketsDropped); | ||
|
|
||
| // There can be multiple update happening in a single frame where no packets have transitioned | ||
| // In those situation we want to return the last packet loss value instead of 0 to avoid invalid swings | ||
| if(packetDroppedDelta == 0 && packetReceivedDelta == 0) | ||
| { | ||
| return m_PacketLossCache.PacketLoss; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it make sense to return 0 in this case? It would mean that you don't need to store the previous value 🤔
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a question, maybe what you have is already the best approach |
||
| } | ||
|
|
||
| m_PacketLossCache.PacketsReceived = sharedContext->stats.PacketsReceived; | ||
| m_PacketLossCache.PacketsDropped = sharedContext->stats.PacketsDropped; | ||
|
|
||
| m_PacketLossCache.PacketLoss = packetReceivedDelta > 0 ? packetDroppedDelta / packetReceivedDelta : 0; | ||
|
|
||
| return packetLoss; | ||
| return m_PacketLossCache.PacketLoss; | ||
| } | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.