From 153c44aa5b98ae3986ff7668ad4cbc0a5b1a27e6 Mon Sep 17 00:00:00 2001 From: Jeffrey Rainy Date: Thu, 15 Jul 2021 17:10:48 -0400 Subject: [PATCH 1/5] feat: snapshot. Adding the RTT computation API. Not actually computed for now --- .../Runtime/Core/SnapshotRTT.cs | 94 +++++++++++++++++++ .../Runtime/Core/SnapshotRTT.cs.meta | 11 +++ .../Runtime/Core/SnapshotSystem.cs | 13 ++- 3 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 com.unity.multiplayer.mlapi/Runtime/Core/SnapshotRTT.cs create mode 100644 com.unity.multiplayer.mlapi/Runtime/Core/SnapshotRTT.cs.meta diff --git a/com.unity.multiplayer.mlapi/Runtime/Core/SnapshotRTT.cs b/com.unity.multiplayer.mlapi/Runtime/Core/SnapshotRTT.cs new file mode 100644 index 0000000000..39b43a6626 --- /dev/null +++ b/com.unity.multiplayer.mlapi/Runtime/Core/SnapshotRTT.cs @@ -0,0 +1,94 @@ +using System; +using UnityEngine; + +namespace MLAPI +{ + internal class ClientRtt + { + /// + /// Round-trip-time data + /// + public struct Rtt + { + public double Best; + public double Average; + public double Worst; + public int SampleCount; + } + public ClientRtt() + { + m_RttSendTimes = new double[k_RingSize]; + m_SendKey = new int[k_RingSize]; + m_MeasuredLatencies = new double[k_RingSize]; + } + + /// + /// Returns the Round-trip-time computation for this client + /// + public Rtt GetRtt() + { + var ret = new Rtt(); // is this a memory alloc ? How do I get a stack alloc ? + var index = m_LatenciesBegin; + double total = 0.0; + ret.Best = m_MeasuredLatencies[m_LatenciesBegin]; + ret.Worst = m_MeasuredLatencies[m_LatenciesBegin]; + + while (index != m_LatenciesEnd) + { + total += m_MeasuredLatencies[index]; + ret.SampleCount++; + ret.Best = Math.Min(ret.Best, m_MeasuredLatencies[index]); + ret.Worst = Math.Max(ret.Worst, m_MeasuredLatencies[index]); + index = (index + 1) % k_RttSize; + } + + if (ret.SampleCount != 0) + { + ret.Average = total / ret.SampleCount; + } + else + { + ret.Average = 0; + ret.Best = 0; + ret.Worst = 0; + ret.SampleCount = 0; + } + + return ret; + } + + internal void NotifySend(int key, double when) + { + m_RttSendTimes[key % k_RingSize] = when; + m_SendKey[key % k_RingSize] = key; + } + + internal void NotifyAck(int key, double when) + { + // if the same slot was not used by a later send + if (m_SendKey[key % k_RingSize] == key) + { + double latency = when - m_RttSendTimes[key % k_RingSize]; + Debug.Log(string.Format("Measured latency of {0}", latency)); + m_MeasuredLatencies[m_LatenciesEnd] = latency; + m_LatenciesEnd = (m_LatenciesEnd + 1) % k_RttSize; + + if (m_LatenciesEnd == m_LatenciesBegin) + { + m_LatenciesBegin = (m_LatenciesBegin + 1) % k_RttSize; + } + } + } + + private const int k_RttSize = 5; // number of RTT to keep an average of (plus one) + + private const int + k_RingSize = 64; // number of slots to use for RTT computations (max number of in-flight packets) + + private double[] m_RttSendTimes; // times at which packet were sent for RTT computations + private int[] m_SendKey; // tick (or other key) at which packets were sent (to allow matching) + private double[] m_MeasuredLatencies; // measured latencies (ring buffer) + private int m_LatenciesBegin = 0; // ring buffer begin + private int m_LatenciesEnd = 0; // ring buffer end + } +} diff --git a/com.unity.multiplayer.mlapi/Runtime/Core/SnapshotRTT.cs.meta b/com.unity.multiplayer.mlapi/Runtime/Core/SnapshotRTT.cs.meta new file mode 100644 index 0000000000..92cc411c7d --- /dev/null +++ b/com.unity.multiplayer.mlapi/Runtime/Core/SnapshotRTT.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 69c3c1c5a885d4aed99ee2e1fa40f763 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/com.unity.multiplayer.mlapi/Runtime/Core/SnapshotSystem.cs b/com.unity.multiplayer.mlapi/Runtime/Core/SnapshotSystem.cs index 216ab945d0..f3a2f21c2f 100644 --- a/com.unity.multiplayer.mlapi/Runtime/Core/SnapshotSystem.cs +++ b/com.unity.multiplayer.mlapi/Runtime/Core/SnapshotSystem.cs @@ -2,13 +2,11 @@ using System.Collections.Generic; using System.IO; using MLAPI.Configuration; -using MLAPI.Messaging; using MLAPI.NetworkVariable; using MLAPI.Serialization; using MLAPI.Serialization.Pooled; using MLAPI.Transports; using UnityEngine; -using UnityEngine.UIElements; namespace MLAPI { @@ -275,9 +273,20 @@ public class SnapshotSystem : INetworkUpdateSystem, IDisposable private NetworkManager m_NetworkManager = NetworkManager.Singleton; private Snapshot m_Snapshot = new Snapshot(NetworkManager.Singleton, false); private Dictionary m_ClientReceivedSnapshot = new Dictionary(); + private Dictionary m_ClientRtts = new Dictionary(); private ushort m_CurrentTick = 0; + internal ClientRtt Rtt(ulong clientId) + { + if (!m_ClientRtts.ContainsKey(clientId)) + { + m_ClientRtts.Add(clientId, new ClientRtt()); + } + + return m_ClientRtts[clientId]; + } + /// /// Constructor /// From ee0ce09e7171534769eb5f7d7bf26557ca7d36fa Mon Sep 17 00:00:00 2001 From: Jeffrey Rainy Date: Thu, 15 Jul 2021 17:14:35 -0400 Subject: [PATCH 2/5] test: snapshot. RTT computation test. --- .../Tests/Editor/SnapshotRttTests.cs | 33 +++++++++++++++++++ .../Tests/Editor/SnapshotRttTests.cs.meta | 11 +++++++ 2 files changed, 44 insertions(+) create mode 100644 com.unity.multiplayer.mlapi/Tests/Editor/SnapshotRttTests.cs create mode 100644 com.unity.multiplayer.mlapi/Tests/Editor/SnapshotRttTests.cs.meta diff --git a/com.unity.multiplayer.mlapi/Tests/Editor/SnapshotRttTests.cs b/com.unity.multiplayer.mlapi/Tests/Editor/SnapshotRttTests.cs new file mode 100644 index 0000000000..1b14c5a3b2 --- /dev/null +++ b/com.unity.multiplayer.mlapi/Tests/Editor/SnapshotRttTests.cs @@ -0,0 +1,33 @@ +using MLAPI; +using NUnit.Framework; + +namespace MLAPI.EditorTests +{ + public class SnapshotRttTests + { + [Test] + public void TestRtt() + { + var snapshot = new SnapshotSystem(); + var client1 = snapshot.Rtt(0); + + client1.NotifySend(0, 0.0); + client1.NotifySend(1, 10.0); + client1.NotifySend(2, 20.0); + client1.NotifySend(3, 30.0); + + client1.NotifyAck(1, 15.0); + client1.NotifyAck(3, 40.0); + + double epsilon = 0.0001; + + ClientRtt.Rtt ret = client1.GetRtt(); + Assert.True(ret.Average < 7.5 + epsilon); + Assert.True(ret.Average > 7.5 - epsilon); + Assert.True(ret.Worst < 10.0 + epsilon); + Assert.True(ret.Worst > 10.0 - epsilon); + Assert.True(ret.Best < 5.0 + epsilon); + Assert.True(ret.Best > 5.0 - epsilon); + } + } +} diff --git a/com.unity.multiplayer.mlapi/Tests/Editor/SnapshotRttTests.cs.meta b/com.unity.multiplayer.mlapi/Tests/Editor/SnapshotRttTests.cs.meta new file mode 100644 index 0000000000..8cb731049a --- /dev/null +++ b/com.unity.multiplayer.mlapi/Tests/Editor/SnapshotRttTests.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a05afab7f08d44c07b2c5e144ba0b45a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: From 035f0843c9ba120adba10b739d44f50ad8566828 Mon Sep 17 00:00:00 2001 From: Jeffrey Rainy Date: Mon, 19 Jul 2021 10:27:04 -0400 Subject: [PATCH 3/5] feat: snapshot. RTT computation PR comments --- .../Runtime/Core/SnapshotRTT.cs | 68 ++++++++++--------- .../Runtime/Core/SnapshotSystem.cs | 6 +- .../Tests/Editor/SnapshotRttTests.cs | 62 ++++++++++++++--- 3 files changed, 91 insertions(+), 45 deletions(-) diff --git a/com.unity.multiplayer.mlapi/Runtime/Core/SnapshotRTT.cs b/com.unity.multiplayer.mlapi/Runtime/Core/SnapshotRTT.cs index 39b43a6626..08e152d67b 100644 --- a/com.unity.multiplayer.mlapi/Runtime/Core/SnapshotRTT.cs +++ b/com.unity.multiplayer.mlapi/Runtime/Core/SnapshotRTT.cs @@ -3,22 +3,32 @@ namespace MLAPI { - internal class ClientRtt + internal class ConnectionRtt { + private const int k_RttSize = 5; // number of RTT to keep an average of (plus one) + private const int k_RingSize = 64; // number of slots to use for RTT computations (max number of in-flight packets) + + private double[] m_RttSendTimes; // times at which packet were sent for RTT computations + private int[] m_SendSequence; // tick, or other key, at which packets were sent (to allow matching) + private double[] m_MeasuredLatencies; // measured latencies (ring buffer) + private int m_LatenciesBegin = 0; // ring buffer begin + private int m_LatenciesEnd = 0; // ring buffer end + /// /// Round-trip-time data /// public struct Rtt { - public double Best; - public double Average; - public double Worst; - public int SampleCount; + public double BestSec; // best RTT + public double AverageSec; // average RTT + public double WorstSec; // worst RTT + public double LastSec; // latest ack'ed RTT + public int SampleCount; // number of contributing samples } - public ClientRtt() + public ConnectionRtt() { m_RttSendTimes = new double[k_RingSize]; - m_SendKey = new int[k_RingSize]; + m_SendSequence = new int[k_RingSize]; m_MeasuredLatencies = new double[k_RingSize]; } @@ -30,46 +40,49 @@ public Rtt GetRtt() var ret = new Rtt(); // is this a memory alloc ? How do I get a stack alloc ? var index = m_LatenciesBegin; double total = 0.0; - ret.Best = m_MeasuredLatencies[m_LatenciesBegin]; - ret.Worst = m_MeasuredLatencies[m_LatenciesBegin]; + ret.BestSec = m_MeasuredLatencies[m_LatenciesBegin]; + ret.WorstSec = m_MeasuredLatencies[m_LatenciesBegin]; while (index != m_LatenciesEnd) { total += m_MeasuredLatencies[index]; ret.SampleCount++; - ret.Best = Math.Min(ret.Best, m_MeasuredLatencies[index]); - ret.Worst = Math.Max(ret.Worst, m_MeasuredLatencies[index]); + ret.BestSec = Math.Min(ret.BestSec, m_MeasuredLatencies[index]); + ret.WorstSec = Math.Max(ret.WorstSec, m_MeasuredLatencies[index]); index = (index + 1) % k_RttSize; } if (ret.SampleCount != 0) { - ret.Average = total / ret.SampleCount; + ret.AverageSec = total / ret.SampleCount; + // the latest RTT is one before m_LatenciesEnd + ret.LastSec = m_MeasuredLatencies[(m_LatenciesEnd + (k_RingSize - 1)) % k_RingSize]; } else { - ret.Average = 0; - ret.Best = 0; - ret.Worst = 0; + ret.AverageSec = 0; + ret.BestSec = 0; + ret.WorstSec = 0; ret.SampleCount = 0; + ret.LastSec = 0; } return ret; } - internal void NotifySend(int key, double when) + internal void NotifySend(int sequence, double timeSec) { - m_RttSendTimes[key % k_RingSize] = when; - m_SendKey[key % k_RingSize] = key; + m_RttSendTimes[sequence % k_RingSize] = timeSec; + m_SendSequence[sequence % k_RingSize] = sequence; } - internal void NotifyAck(int key, double when) + internal void NotifyAck(int sequence, double timeSec) { // if the same slot was not used by a later send - if (m_SendKey[key % k_RingSize] == key) + if (m_SendSequence[sequence % k_RingSize] == sequence) { - double latency = when - m_RttSendTimes[key % k_RingSize]; - Debug.Log(string.Format("Measured latency of {0}", latency)); + double latency = timeSec - m_RttSendTimes[sequence % k_RingSize]; + m_MeasuredLatencies[m_LatenciesEnd] = latency; m_LatenciesEnd = (m_LatenciesEnd + 1) % k_RttSize; @@ -79,16 +92,5 @@ internal void NotifyAck(int key, double when) } } } - - private const int k_RttSize = 5; // number of RTT to keep an average of (plus one) - - private const int - k_RingSize = 64; // number of slots to use for RTT computations (max number of in-flight packets) - - private double[] m_RttSendTimes; // times at which packet were sent for RTT computations - private int[] m_SendKey; // tick (or other key) at which packets were sent (to allow matching) - private double[] m_MeasuredLatencies; // measured latencies (ring buffer) - private int m_LatenciesBegin = 0; // ring buffer begin - private int m_LatenciesEnd = 0; // ring buffer end } } diff --git a/com.unity.multiplayer.mlapi/Runtime/Core/SnapshotSystem.cs b/com.unity.multiplayer.mlapi/Runtime/Core/SnapshotSystem.cs index f3a2f21c2f..db444a2293 100644 --- a/com.unity.multiplayer.mlapi/Runtime/Core/SnapshotSystem.cs +++ b/com.unity.multiplayer.mlapi/Runtime/Core/SnapshotSystem.cs @@ -273,15 +273,15 @@ public class SnapshotSystem : INetworkUpdateSystem, IDisposable private NetworkManager m_NetworkManager = NetworkManager.Singleton; private Snapshot m_Snapshot = new Snapshot(NetworkManager.Singleton, false); private Dictionary m_ClientReceivedSnapshot = new Dictionary(); - private Dictionary m_ClientRtts = new Dictionary(); + private Dictionary m_ClientRtts = new Dictionary(); private ushort m_CurrentTick = 0; - internal ClientRtt Rtt(ulong clientId) + internal ConnectionRtt Rtt(ulong clientId) { if (!m_ClientRtts.ContainsKey(clientId)) { - m_ClientRtts.Add(clientId, new ClientRtt()); + m_ClientRtts.Add(clientId, new ConnectionRtt()); } return m_ClientRtts[clientId]; diff --git a/com.unity.multiplayer.mlapi/Tests/Editor/SnapshotRttTests.cs b/com.unity.multiplayer.mlapi/Tests/Editor/SnapshotRttTests.cs index 1b14c5a3b2..e0cd864db2 100644 --- a/com.unity.multiplayer.mlapi/Tests/Editor/SnapshotRttTests.cs +++ b/com.unity.multiplayer.mlapi/Tests/Editor/SnapshotRttTests.cs @@ -1,33 +1,77 @@ using MLAPI; using NUnit.Framework; +using UnityEngine; namespace MLAPI.EditorTests { public class SnapshotRttTests { [Test] - public void TestRtt() + public void TestBasicRtt() { var snapshot = new SnapshotSystem(); var client1 = snapshot.Rtt(0); client1.NotifySend(0, 0.0); client1.NotifySend(1, 10.0); + + client1.NotifyAck(1, 15.0); + client1.NotifySend(2, 20.0); client1.NotifySend(3, 30.0); + client1.NotifySend(4, 32.0); - client1.NotifyAck(1, 15.0); + client1.NotifyAck(4, 38.0); client1.NotifyAck(3, 40.0); double epsilon = 0.0001; - ClientRtt.Rtt ret = client1.GetRtt(); - Assert.True(ret.Average < 7.5 + epsilon); - Assert.True(ret.Average > 7.5 - epsilon); - Assert.True(ret.Worst < 10.0 + epsilon); - Assert.True(ret.Worst > 10.0 - epsilon); - Assert.True(ret.Best < 5.0 + epsilon); - Assert.True(ret.Best > 5.0 - epsilon); + ConnectionRtt.Rtt ret = client1.GetRtt(); + Assert.True(ret.AverageSec < 7.0 + epsilon); + Assert.True(ret.AverageSec > 7.0 - epsilon); + Assert.True(ret.WorstSec < 10.0 + epsilon); + Assert.True(ret.WorstSec > 10.0 - epsilon); + Assert.True(ret.BestSec < 5.0 + epsilon); + Assert.True(ret.BestSec > 5.0 - epsilon); + + // note: `last` latency is latest received Ack, not latest sent sequence. + Assert.True(ret.LastSec < 10.0 + epsilon); + Assert.True(ret.LastSec > 10.0 - epsilon); + } + + [Test] + public void TestEdgeCasesRtt() + { + var snapshot = new SnapshotSystem(); + var client1 = snapshot.Rtt(0); + var iterationCount = 200; + var extraCount = 100; + + // feed in some messages + for (var iteration = 0; iteration < iterationCount; iteration++) + { + client1.NotifySend(iteration, 25.0 * iteration); + } + // ack some random ones in there (1 out of each 9), always 7.0 later + for (var iteration = 0; iteration < iterationCount; iteration += 9) + { + client1.NotifyAck(iteration, 25.0 * iteration + 7.0); + } + // ack some unused key, to check it doesn't throw off the values + for (var iteration = iterationCount; iteration < iterationCount + extraCount; iteration++) + { + client1.NotifyAck(iteration, 42.0); + } + + double epsilon = 0.0001; + + ConnectionRtt.Rtt ret = client1.GetRtt(); + Assert.True(ret.AverageSec < 7.0 + epsilon); + Assert.True(ret.AverageSec > 7.0 - epsilon); + Assert.True(ret.WorstSec < 7.0 + epsilon); + Assert.True(ret.WorstSec > 7.0 - epsilon); + Assert.True(ret.BestSec < 7.0 + epsilon); + Assert.True(ret.BestSec > 7.0 - epsilon); } } } From b1df742195301a4ac1461fd62b0d210aa54169a6 Mon Sep 17 00:00:00 2001 From: Jeffrey Rainy Date: Mon, 19 Jul 2021 12:39:36 -0400 Subject: [PATCH 4/5] test: RTT test style improvement, moved constants from local to class constants --- .../Tests/Editor/SnapshotRttTests.cs | 34 +++++++++---------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/com.unity.multiplayer.mlapi/Tests/Editor/SnapshotRttTests.cs b/com.unity.multiplayer.mlapi/Tests/Editor/SnapshotRttTests.cs index e0cd864db2..00b033f92c 100644 --- a/com.unity.multiplayer.mlapi/Tests/Editor/SnapshotRttTests.cs +++ b/com.unity.multiplayer.mlapi/Tests/Editor/SnapshotRttTests.cs @@ -6,6 +6,8 @@ namespace MLAPI.EditorTests { public class SnapshotRttTests { + private const double k_Epsilon = 0.0001; + [Test] public void TestBasicRtt() { @@ -24,19 +26,17 @@ public void TestBasicRtt() client1.NotifyAck(4, 38.0); client1.NotifyAck(3, 40.0); - double epsilon = 0.0001; - ConnectionRtt.Rtt ret = client1.GetRtt(); - Assert.True(ret.AverageSec < 7.0 + epsilon); - Assert.True(ret.AverageSec > 7.0 - epsilon); - Assert.True(ret.WorstSec < 10.0 + epsilon); - Assert.True(ret.WorstSec > 10.0 - epsilon); - Assert.True(ret.BestSec < 5.0 + epsilon); - Assert.True(ret.BestSec > 5.0 - epsilon); + Assert.True(ret.AverageSec < 7.0 + k_Epsilon); + Assert.True(ret.AverageSec > 7.0 - k_Epsilon); + Assert.True(ret.WorstSec < 10.0 + k_Epsilon); + Assert.True(ret.WorstSec > 10.0 - k_Epsilon); + Assert.True(ret.BestSec < 5.0 + k_Epsilon); + Assert.True(ret.BestSec > 5.0 - k_Epsilon); // note: `last` latency is latest received Ack, not latest sent sequence. - Assert.True(ret.LastSec < 10.0 + epsilon); - Assert.True(ret.LastSec > 10.0 - epsilon); + Assert.True(ret.LastSec < 10.0 + k_Epsilon); + Assert.True(ret.LastSec > 10.0 - k_Epsilon); } [Test] @@ -63,15 +63,13 @@ public void TestEdgeCasesRtt() client1.NotifyAck(iteration, 42.0); } - double epsilon = 0.0001; - ConnectionRtt.Rtt ret = client1.GetRtt(); - Assert.True(ret.AverageSec < 7.0 + epsilon); - Assert.True(ret.AverageSec > 7.0 - epsilon); - Assert.True(ret.WorstSec < 7.0 + epsilon); - Assert.True(ret.WorstSec > 7.0 - epsilon); - Assert.True(ret.BestSec < 7.0 + epsilon); - Assert.True(ret.BestSec > 7.0 - epsilon); + Assert.True(ret.AverageSec < 7.0 + k_Epsilon); + Assert.True(ret.AverageSec > 7.0 - k_Epsilon); + Assert.True(ret.WorstSec < 7.0 + k_Epsilon); + Assert.True(ret.WorstSec > 7.0 - k_Epsilon); + Assert.True(ret.BestSec < 7.0 + k_Epsilon); + Assert.True(ret.BestSec > 7.0 - k_Epsilon); } } } From ee36cc3a872d7bde3b4433e773873b9a54477129 Mon Sep 17 00:00:00 2001 From: Jeffrey Rainy Date: Mon, 19 Jul 2021 16:21:07 -0400 Subject: [PATCH 5/5] feat: snapshot. RTT computation PR comments, internally accessible RingSize and RttSize --- .../Runtime/Core/SnapshotRTT.cs | 26 +++++++++---------- .../Runtime/Core/SnapshotSystem.cs | 2 +- .../Tests/Editor/SnapshotRttTests.cs | 8 +++--- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/com.unity.multiplayer.mlapi/Runtime/Core/SnapshotRTT.cs b/com.unity.multiplayer.mlapi/Runtime/Core/SnapshotRTT.cs index 08e152d67b..b06989d2f1 100644 --- a/com.unity.multiplayer.mlapi/Runtime/Core/SnapshotRTT.cs +++ b/com.unity.multiplayer.mlapi/Runtime/Core/SnapshotRTT.cs @@ -5,8 +5,8 @@ namespace MLAPI { internal class ConnectionRtt { - private const int k_RttSize = 5; // number of RTT to keep an average of (plus one) - private const int k_RingSize = 64; // number of slots to use for RTT computations (max number of in-flight packets) + internal const int RttSize = 5; // number of RTT to keep an average of (plus one) + internal const int RingSize = 64; // number of slots to use for RTT computations (max number of in-flight packets) private double[] m_RttSendTimes; // times at which packet were sent for RTT computations private int[] m_SendSequence; // tick, or other key, at which packets were sent (to allow matching) @@ -27,9 +27,9 @@ public struct Rtt } public ConnectionRtt() { - m_RttSendTimes = new double[k_RingSize]; - m_SendSequence = new int[k_RingSize]; - m_MeasuredLatencies = new double[k_RingSize]; + m_RttSendTimes = new double[RingSize]; + m_SendSequence = new int[RingSize]; + m_MeasuredLatencies = new double[RingSize]; } /// @@ -49,14 +49,14 @@ public Rtt GetRtt() ret.SampleCount++; ret.BestSec = Math.Min(ret.BestSec, m_MeasuredLatencies[index]); ret.WorstSec = Math.Max(ret.WorstSec, m_MeasuredLatencies[index]); - index = (index + 1) % k_RttSize; + index = (index + 1) % RttSize; } if (ret.SampleCount != 0) { ret.AverageSec = total / ret.SampleCount; // the latest RTT is one before m_LatenciesEnd - ret.LastSec = m_MeasuredLatencies[(m_LatenciesEnd + (k_RingSize - 1)) % k_RingSize]; + ret.LastSec = m_MeasuredLatencies[(m_LatenciesEnd + (RingSize - 1)) % RingSize]; } else { @@ -72,23 +72,23 @@ public Rtt GetRtt() internal void NotifySend(int sequence, double timeSec) { - m_RttSendTimes[sequence % k_RingSize] = timeSec; - m_SendSequence[sequence % k_RingSize] = sequence; + m_RttSendTimes[sequence % RingSize] = timeSec; + m_SendSequence[sequence % RingSize] = sequence; } internal void NotifyAck(int sequence, double timeSec) { // if the same slot was not used by a later send - if (m_SendSequence[sequence % k_RingSize] == sequence) + if (m_SendSequence[sequence % RingSize] == sequence) { - double latency = timeSec - m_RttSendTimes[sequence % k_RingSize]; + double latency = timeSec - m_RttSendTimes[sequence % RingSize]; m_MeasuredLatencies[m_LatenciesEnd] = latency; - m_LatenciesEnd = (m_LatenciesEnd + 1) % k_RttSize; + m_LatenciesEnd = (m_LatenciesEnd + 1) % RttSize; if (m_LatenciesEnd == m_LatenciesBegin) { - m_LatenciesBegin = (m_LatenciesBegin + 1) % k_RttSize; + m_LatenciesBegin = (m_LatenciesBegin + 1) % RttSize; } } } diff --git a/com.unity.multiplayer.mlapi/Runtime/Core/SnapshotSystem.cs b/com.unity.multiplayer.mlapi/Runtime/Core/SnapshotSystem.cs index db444a2293..2772bece10 100644 --- a/com.unity.multiplayer.mlapi/Runtime/Core/SnapshotSystem.cs +++ b/com.unity.multiplayer.mlapi/Runtime/Core/SnapshotSystem.cs @@ -277,7 +277,7 @@ public class SnapshotSystem : INetworkUpdateSystem, IDisposable private ushort m_CurrentTick = 0; - internal ConnectionRtt Rtt(ulong clientId) + internal ConnectionRtt GetConnectionRtt(ulong clientId) { if (!m_ClientRtts.ContainsKey(clientId)) { diff --git a/com.unity.multiplayer.mlapi/Tests/Editor/SnapshotRttTests.cs b/com.unity.multiplayer.mlapi/Tests/Editor/SnapshotRttTests.cs index 00b033f92c..c66dcb2c74 100644 --- a/com.unity.multiplayer.mlapi/Tests/Editor/SnapshotRttTests.cs +++ b/com.unity.multiplayer.mlapi/Tests/Editor/SnapshotRttTests.cs @@ -12,7 +12,7 @@ public class SnapshotRttTests public void TestBasicRtt() { var snapshot = new SnapshotSystem(); - var client1 = snapshot.Rtt(0); + var client1 = snapshot.GetConnectionRtt(0); client1.NotifySend(0, 0.0); client1.NotifySend(1, 10.0); @@ -43,9 +43,9 @@ public void TestBasicRtt() public void TestEdgeCasesRtt() { var snapshot = new SnapshotSystem(); - var client1 = snapshot.Rtt(0); - var iterationCount = 200; - var extraCount = 100; + var client1 = snapshot.GetConnectionRtt(0); + var iterationCount = ConnectionRtt.RingSize * 3; + var extraCount = ConnectionRtt.RingSize * 2; // feed in some messages for (var iteration = 0; iteration < iterationCount; iteration++)