Skip to content

Commit 5989836

Browse files
JS FauteuxJesse OlmerDenninDalke
authored
feat: Adding support for netsim to be able to work with Tools adapter (#2184)
* Adding support for netsim to be able to work with Tools adapter * Setting up Simulator Pipeline stage if NetSim is available * Deprecate DebugSimulator when using UTP2 Co-authored-by: Jesse Olmer <jesseo@unity3d.com> Co-authored-by: DenninDalke <dennindalke@gmail.com>
1 parent f06b562 commit 5989836

File tree

4 files changed

+126
-24
lines changed

4 files changed

+126
-24
lines changed

com.unity.netcode.gameobjects/Runtime/AssemblyInfo.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212
[assembly: InternalsVisibleTo("Unity.Netcode.RuntimeTests")]
1313
[assembly: InternalsVisibleTo("Unity.Netcode.TestHelpers.Runtime")]
1414
[assembly: InternalsVisibleTo("Unity.Netcode.Adapter.UTP")]
15+
[assembly: InternalsVisibleTo("Unity.Multiplayer.Tools.Adapters.Ngo1WithUtp2")]

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

Lines changed: 110 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
// NetSim Implementation compilation boilerplate
2+
// All references to UNITY_MP_TOOLS_NETSIM_IMPLEMENTATION_ENABLED should be defined in the same way,
3+
// as any discrepancies are likely to result in build failures
4+
#if UNITY_EDITOR || (DEVELOPMENT_BUILD && !UNITY_MP_TOOLS_NETSIM_DISABLED_IN_DEVELOP) || (!DEVELOPMENT_BUILD && UNITY_MP_TOOLS_NETSIM_ENABLED_IN_RELEASE)
5+
#define UNITY_MP_TOOLS_NETSIM_IMPLEMENTATION_ENABLED
6+
#endif
7+
18
using System;
29
using System.Collections.Generic;
310
using UnityEngine;
@@ -343,6 +350,9 @@ public struct SimulatorParameters
343350
/// - packet jitter (variances in latency, see: https://en.wikipedia.org/wiki/Jitter)
344351
/// - packet drop rate (packet loss)
345352
/// </summary>
353+
#if UTP_TRANSPORT_2_0_ABOVE
354+
[Obsolete("DebugSimulator is no longer supported and has no effect. Use Network Simulator from the Multiplayer Tools package.", false)]
355+
#endif
346356
public SimulatorParameters DebugSimulator = new SimulatorParameters
347357
{
348358
PacketDelayMS = 0,
@@ -359,6 +369,10 @@ private struct PacketLossCache
359369
public float PacketLoss;
360370
};
361371

372+
internal static event Action<int, NetworkDriver> TransportInitialized;
373+
internal static event Action<int> TransportDisposed;
374+
internal NetworkDriver NetworkDriver => m_Driver;
375+
362376
private PacketLossCache m_PacketLossCache = new PacketLossCache();
363377

364378
private State m_State = State.Disconnected;
@@ -402,6 +416,8 @@ private void InitDriver()
402416
out m_UnreliableFragmentedPipeline,
403417
out m_UnreliableSequencedFragmentedPipeline,
404418
out m_ReliableSequencedPipeline);
419+
420+
TransportInitialized?.Invoke(GetInstanceID(), NetworkDriver);
405421
}
406422

407423
private void DisposeInternals()
@@ -419,6 +435,8 @@ private void DisposeInternals()
419435
}
420436

421437
m_SendQueue.Clear();
438+
439+
TransportDisposed?.Invoke(GetInstanceID());
422440
}
423441

424442
private NetworkPipeline SelectSendPipeline(NetworkDelivery delivery)
@@ -652,6 +670,9 @@ public void SetConnectionData(NetworkEndpoint endPoint, NetworkEndpoint listenEn
652670
/// <param name="packetDelay">Packet delay in milliseconds.</param>
653671
/// <param name="packetJitter">Packet jitter in milliseconds.</param>
654672
/// <param name="dropRate">Packet drop percentage.</param>
673+
#if UTP_TRANSPORT_2_0_ABOVE
674+
[Obsolete("SetDebugSimulatorParameters is no longer supported and has no effect. Use Network Simulator from the Multiplayer Tools package.", false)]
675+
#endif
655676
public void SetDebugSimulatorParameters(int packetDelay, int packetJitter, int dropRate)
656677
{
657678
if (m_Driver.IsCreated)
@@ -1329,7 +1350,25 @@ public override void Shutdown()
13291350
m_ServerClientId = 0;
13301351
}
13311352

1332-
private void ConfigureSimulator()
1353+
#if UTP_TRANSPORT_2_0_ABOVE
1354+
private void ConfigureSimulatorForUtp2()
1355+
{
1356+
// As DebugSimulator is deprecated, the 'packetDelayMs', 'packetJitterMs' and 'packetDropPercentage'
1357+
// parameters are set to the default and are supposed to be changed using Network Simulator tool instead.
1358+
m_NetworkSettings.WithSimulatorStageParameters(
1359+
maxPacketCount: 300, // TODO Is there any way to compute a better value?
1360+
maxPacketSize: NetworkParameterConstants.MTU,
1361+
packetDelayMs: 0,
1362+
packetJitterMs: 0,
1363+
packetDropPercentage: 0,
1364+
randomSeed: DebugSimulatorRandomSeed ?? (uint)System.Diagnostics.Stopwatch.GetTimestamp()
1365+
, mode: ApplyMode.AllPackets
1366+
);
1367+
1368+
m_NetworkSettings.WithNetworkSimulatorParameters();
1369+
}
1370+
#else
1371+
private void ConfigureSimulatorForUtp1()
13331372
{
13341373
m_NetworkSettings.WithSimulatorStageParameters(
13351374
maxPacketCount: 300, // TODO Is there any way to compute a better value?
@@ -1338,11 +1377,9 @@ private void ConfigureSimulator()
13381377
packetJitterMs: DebugSimulator.PacketJitterMS,
13391378
packetDropPercentage: DebugSimulator.PacketDropRate,
13401379
randomSeed: DebugSimulatorRandomSeed ?? (uint)System.Diagnostics.Stopwatch.GetTimestamp()
1341-
#if UTP_TRANSPORT_2_0_ABOVE
1342-
, mode: ApplyMode.AllPackets
1343-
#endif
13441380
);
13451381
}
1382+
#endif
13461383

13471384
/// <summary>
13481385
/// Creates the internal NetworkDriver
@@ -1357,14 +1394,14 @@ public void CreateDriver(UnityTransport transport, out NetworkDriver driver,
13571394
out NetworkPipeline unreliableSequencedFragmentedPipeline,
13581395
out NetworkPipeline reliableSequencedPipeline)
13591396
{
1360-
#if MULTIPLAYER_TOOLS_1_0_0_PRE_7
1361-
#if !UTP_TRANSPORT_2_0_ABOVE
1397+
#if MULTIPLAYER_TOOLS_1_0_0_PRE_7 && !UTP_TRANSPORT_2_0_ABOVE
13621398
NetworkPipelineStageCollection.RegisterPipelineStage(new NetworkMetricsPipelineStage());
13631399
#endif
1364-
#endif
13651400

1366-
#if UNITY_EDITOR || DEVELOPMENT_BUILD
1367-
ConfigureSimulator();
1401+
#if UTP_TRANSPORT_2_0_ABOVE && UNITY_MP_TOOLS_NETSIM_IMPLEMENTATION_ENABLED
1402+
ConfigureSimulatorForUtp2();
1403+
#elif !UTP_TRANSPORT_2_0_ABOVE && (UNITY_EDITOR || DEVELOPMENT_BUILD)
1404+
ConfigureSimulatorForUtp1();
13681405
#endif
13691406

13701407
m_NetworkSettings.WithNetworkConfigParameters(
@@ -1395,42 +1432,53 @@ public void CreateDriver(UnityTransport transport, out NetworkDriver driver,
13951432
driver = NetworkDriver.Create(m_NetworkSettings);
13961433
#endif
13971434

1398-
#if MULTIPLAYER_TOOLS_1_0_0_PRE_7
1399-
#if UTP_TRANSPORT_2_0_ABOVE
1400-
driver.RegisterPipelineStage<NetworkMetricsPipelineStage>(new NetworkMetricsPipelineStage());
1435+
#if MULTIPLAYER_TOOLS_1_0_0_PRE_7 && UTP_TRANSPORT_2_0_ABOVE
1436+
driver.RegisterPipelineStage(new NetworkMetricsPipelineStage());
14011437
#endif
1438+
1439+
#if !UTP_TRANSPORT_2_0_ABOVE
1440+
SetupPipelinesForUtp1(driver,
1441+
out unreliableFragmentedPipeline,
1442+
out unreliableSequencedFragmentedPipeline,
1443+
out reliableSequencedPipeline);
1444+
#else
1445+
SetupPipelinesForUtp2(driver,
1446+
out unreliableFragmentedPipeline,
1447+
out unreliableSequencedFragmentedPipeline,
1448+
out reliableSequencedPipeline);
14021449
#endif
1450+
}
14031451

1452+
#if !UTP_TRANSPORT_2_0_ABOVE
1453+
private void SetupPipelinesForUtp1(NetworkDriver driver,
1454+
out NetworkPipeline unreliableFragmentedPipeline,
1455+
out NetworkPipeline unreliableSequencedFragmentedPipeline,
1456+
out NetworkPipeline reliableSequencedPipeline)
1457+
{
14041458
#if UNITY_EDITOR || DEVELOPMENT_BUILD
14051459
if (DebugSimulator.PacketDelayMS > 0 || DebugSimulator.PacketDropRate > 0)
14061460
{
14071461
unreliableFragmentedPipeline = driver.CreatePipeline(
14081462
typeof(FragmentationPipelineStage),
1409-
typeof(SimulatorPipelineStage)
1410-
#if !UTP_TRANSPORT_2_0_ABOVE
1411-
, typeof(SimulatorPipelineStageInSend)
1412-
#endif
1463+
typeof(SimulatorPipelineStage),
1464+
typeof(SimulatorPipelineStageInSend)
14131465
#if MULTIPLAYER_TOOLS_1_0_0_PRE_7
14141466
, typeof(NetworkMetricsPipelineStage)
14151467
#endif
14161468
);
14171469
unreliableSequencedFragmentedPipeline = driver.CreatePipeline(
14181470
typeof(FragmentationPipelineStage),
14191471
typeof(UnreliableSequencedPipelineStage),
1420-
typeof(SimulatorPipelineStage)
1421-
#if !UTP_TRANSPORT_2_0_ABOVE
1422-
, typeof(SimulatorPipelineStageInSend)
1423-
#endif
1472+
typeof(SimulatorPipelineStage),
1473+
typeof(SimulatorPipelineStageInSend)
14241474
#if MULTIPLAYER_TOOLS_1_0_0_PRE_7
14251475
, typeof(NetworkMetricsPipelineStage)
14261476
#endif
14271477
);
14281478
reliableSequencedPipeline = driver.CreatePipeline(
14291479
typeof(ReliableSequencedPipelineStage),
1430-
typeof(SimulatorPipelineStage)
1431-
#if !UTP_TRANSPORT_2_0_ABOVE
1432-
, typeof(SimulatorPipelineStageInSend)
1433-
#endif
1480+
typeof(SimulatorPipelineStage),
1481+
typeof(SimulatorPipelineStageInSend)
14341482
#if MULTIPLAYER_TOOLS_1_0_0_PRE_7
14351483
, typeof(NetworkMetricsPipelineStage)
14361484
#endif
@@ -1460,7 +1508,45 @@ public void CreateDriver(UnityTransport transport, out NetworkDriver driver,
14601508
);
14611509
}
14621510
}
1511+
#else
1512+
private void SetupPipelinesForUtp2(NetworkDriver driver,
1513+
out NetworkPipeline unreliableFragmentedPipeline,
1514+
out NetworkPipeline unreliableSequencedFragmentedPipeline,
1515+
out NetworkPipeline reliableSequencedPipeline)
1516+
{
14631517

1518+
unreliableFragmentedPipeline = driver.CreatePipeline(
1519+
typeof(FragmentationPipelineStage)
1520+
#if UNITY_MP_TOOLS_NETSIM_IMPLEMENTATION_ENABLED
1521+
, typeof(SimulatorPipelineStage)
1522+
#endif
1523+
#if MULTIPLAYER_TOOLS_1_0_0_PRE_7
1524+
, typeof(NetworkMetricsPipelineStage)
1525+
#endif
1526+
);
1527+
1528+
unreliableSequencedFragmentedPipeline = driver.CreatePipeline(
1529+
typeof(FragmentationPipelineStage),
1530+
typeof(UnreliableSequencedPipelineStage)
1531+
#if UNITY_MP_TOOLS_NETSIM_IMPLEMENTATION_ENABLED
1532+
, typeof(SimulatorPipelineStage)
1533+
#endif
1534+
#if MULTIPLAYER_TOOLS_1_0_0_PRE_7
1535+
, typeof(NetworkMetricsPipelineStage)
1536+
#endif
1537+
);
1538+
1539+
reliableSequencedPipeline = driver.CreatePipeline(
1540+
typeof(ReliableSequencedPipelineStage)
1541+
#if UNITY_MP_TOOLS_NETSIM_IMPLEMENTATION_ENABLED
1542+
, typeof(SimulatorPipelineStage)
1543+
#endif
1544+
#if MULTIPLAYER_TOOLS_1_0_0_PRE_7
1545+
, typeof(NetworkMetricsPipelineStage)
1546+
#endif
1547+
);
1548+
}
1549+
#endif
14641550
// -------------- Utility Types -------------------------------------------------------------------------------
14651551

14661552

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
using Unity.Netcode.TestHelpers.Runtime;
99
using Unity.Netcode.TestHelpers.Runtime.Metrics;
1010
using Unity.Netcode.Transports.UTP;
11+
#if UTP_TRANSPORT_2_0_ABOVE
12+
using Unity.Networking.Transport.Utilities;
13+
#endif
1114
using UnityEngine.TestTools;
1215

1316
namespace Unity.Netcode.RuntimeTests.Metrics
@@ -26,7 +29,9 @@ public PacketLossMetricsTests()
2629
protected override void OnServerAndClientsCreated()
2730
{
2831
var clientTransport = (UnityTransport)m_ClientNetworkManagers[0].NetworkConfig.NetworkTransport;
32+
#if !UTP_TRANSPORT_2_0_ABOVE
2933
clientTransport.SetDebugSimulatorParameters(0, 0, m_PacketLossRate);
34+
#endif
3035

3136
// Determined through trial and error. With both UTP 1.2 and 2.0, this random seed
3237
// results in an effective packet loss percentage between 22% and 28%. Future UTP
@@ -63,6 +68,14 @@ public IEnumerator TrackPacketLossAsClient()
6368
double packetLossRateMinRange = (m_PacketLossRate - m_PacketLossRangeDelta) / 100d;
6469
double packetLossRateMaxrange = (m_PacketLossRate + m_PacketLossRangeDelta) / 100d;
6570
var clientNetworkManager = m_ClientNetworkManagers[0];
71+
72+
#if UTP_TRANSPORT_2_0_ABOVE
73+
var clientTransport = (UnityTransport)clientNetworkManager.NetworkConfig.NetworkTransport;
74+
clientTransport.NetworkDriver.CurrentSettings.TryGet<SimulatorUtility.Parameters>(out var parameters);
75+
parameters.PacketDropPercentage = m_PacketLossRate;
76+
clientTransport.NetworkDriver.ModifySimulatorStageParameters(parameters);
77+
#endif
78+
6679
var waitForPacketLossMetric = new WaitForGaugeMetricValues((clientNetworkManager.NetworkMetrics as NetworkMetrics).Dispatcher,
6780
NetworkMetricTypes.PacketLoss,
6881
metric => packetLossRateMinRange <= metric && metric <= packetLossRateMaxrange);

com.unity.netcode.gameobjects/Tests/Runtime/Transports/UnityTransportTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ public IEnumerator SendCompletesOnUnreliableSendQueueOverflow()
340340
yield return null;
341341
}
342342

343+
#if !UTP_TRANSPORT_2_0_ABOVE
343344
// Check that simulator parameters are effective. We only check with the drop rate, because
344345
// that's easy to check and we only really want to make sure the simulator parameters are
345346
// configured properly (the simulator pipeline stage is already well-tested in UTP).
@@ -394,6 +395,7 @@ public IEnumerator CurrentRttReportedCorrectly()
394395

395396
yield return null;
396397
}
398+
#endif
397399

398400
[UnityTest]
399401
public IEnumerator SendQueuesFlushedOnShutdown([ValueSource("k_DeliveryParameters")] NetworkDelivery delivery)

0 commit comments

Comments
 (0)