Skip to content

Commit 98fbfbf

Browse files
feat: Implement GetCurrentRtt in the adapter (#1755)
* Make ExtractRtt more suitable for other uses * Implement GetCurrentRtt * Add CHANGELOG entry * Make the standards checks happy
1 parent dca06fd commit 98fbfbf

File tree

3 files changed

+58
-16
lines changed

3 files changed

+58
-16
lines changed

com.unity.netcode.adapter.utp/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ All notable changes to this package will be documented in this file. The format
77
### Added
88

99
- New parameters are available to simulate network conditions (delay, jitter, packet loss) in the editor and in development builds. The parameters are available under the 'Debug Simulator' section of the 'Unity Transport' component, or can be set with the `SetDebugSimulatorParameters` call. (#1745)
10+
- `GetCurrentRtt` is now properly implemented. (#1755)
1011

1112
### Changed
1213

com.unity.netcode.adapter.utp/Runtime/UnityTransport.cs

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ private void ExtractNetworkMetricsForClient(ulong transportClientId)
744744
ExtractNetworkMetricsFromPipeline(m_UnreliableSequencedFragmentedPipeline, networkConnection);
745745
ExtractNetworkMetricsFromPipeline(m_ReliableSequencedPipeline, networkConnection);
746746

747-
var rttValue = ExtractRtt(networkConnection);
747+
var rttValue = NetworkManager.IsServer ? 0 : ExtractRtt(networkConnection);
748748
NetworkMetrics.TrackRttToServer(rttValue);
749749
}
750750

@@ -773,25 +773,23 @@ private void ExtractNetworkMetricsFromPipeline(NetworkPipeline pipeline, Network
773773

774774
private int ExtractRtt(NetworkConnection networkConnection)
775775
{
776-
if (NetworkManager.IsServer)
776+
if (m_Driver.GetConnectionState(networkConnection) != NetworkConnection.State.Connected)
777777
{
778778
return 0;
779779
}
780-
else
780+
781+
m_Driver.GetPipelineBuffers(m_ReliableSequencedPipeline,
782+
NetworkPipelineStageCollection.GetStageId(typeof(ReliableSequencedPipelineStage)),
783+
networkConnection,
784+
out _,
785+
out _,
786+
out var sharedBuffer);
787+
788+
unsafe
781789
{
782-
m_Driver.GetPipelineBuffers(m_ReliableSequencedPipeline,
783-
NetworkPipelineStageCollection.GetStageId(typeof(ReliableSequencedPipelineStage)),
784-
networkConnection,
785-
out _,
786-
out _,
787-
out var sharedBuffer);
788-
789-
unsafe
790-
{
791-
var sharedContext = (ReliableUtility.SharedContext*)sharedBuffer.GetUnsafePtr();
790+
var sharedContext = (ReliableUtility.SharedContext*)sharedBuffer.GetUnsafePtr();
792791

793-
return sharedContext->RttInfo.LastRtt;
794-
}
792+
return sharedContext->RttInfo.LastRtt;
795793
}
796794
}
797795

@@ -866,7 +864,22 @@ public override void DisconnectRemoteClient(ulong clientId)
866864

867865
public override ulong GetCurrentRtt(ulong clientId)
868866
{
869-
return 0;
867+
// We don't know if this is getting called from inside NGO (which presumably knows to
868+
// use the transport client ID) or from a user (which will be using the NGO client ID).
869+
// So we just try both cases (ExtractRtt returns 0 for invalid connections).
870+
871+
if (NetworkManager != null)
872+
{
873+
var transportId = NetworkManager.ClientIdToTransportId(clientId);
874+
875+
var rtt = ExtractRtt(ParseClientId(transportId));
876+
if (rtt > 0)
877+
{
878+
return (ulong)rtt;
879+
}
880+
}
881+
882+
return (ulong)ExtractRtt(ParseClientId(clientId));
870883
}
871884

872885
public override void Initialize(NetworkManager networkManager = null)

com.unity.netcode.adapter.utp/Tests/Runtime/TransportTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,34 @@ public IEnumerator SimulatorParametersAreEffective()
361361

362362
yield return null;
363363
}
364+
365+
// Check that RTT is reported correctly.
366+
[UnityTest]
367+
[UnityPlatform(include = new[] { RuntimePlatform.OSXEditor, RuntimePlatform.WindowsEditor, RuntimePlatform.LinuxEditor })]
368+
public IEnumerator CurrentRttReportedCorrectly()
369+
{
370+
const int simulatedRtt = 25;
371+
372+
InitializeTransport(out m_Server, out m_ServerEvents);
373+
InitializeTransport(out m_Client1, out m_Client1Events);
374+
375+
m_Server.SetDebugSimulatorParameters(simulatedRtt, 0, 0);
376+
377+
m_Server.StartServer();
378+
m_Client1.StartClient();
379+
380+
yield return WaitForNetworkEvent(NetworkEvent.Connect, m_Client1Events);
381+
382+
var data = new ArraySegment<byte>(new byte[] { 42 });
383+
m_Client1.Send(m_Client1.ServerClientId, data, NetworkDelivery.Reliable);
384+
385+
yield return WaitForNetworkEvent(NetworkEvent.Data, m_ServerEvents,
386+
timeout: MaxNetworkEventWaitTime + (2 * simulatedRtt));
387+
388+
Assert.GreaterOrEqual(m_Client1.GetCurrentRtt(m_Client1.ServerClientId), simulatedRtt);
389+
390+
yield return null;
391+
}
364392
}
365393
}
366394
#endif

0 commit comments

Comments
 (0)