Skip to content

feat: Adding support for netsim to be able to work with Tools adapter #2184

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

Merged
merged 17 commits into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions com.unity.netcode.gameobjects/Runtime/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
[assembly: InternalsVisibleTo("Unity.Netcode.RuntimeTests")]
[assembly: InternalsVisibleTo("Unity.Netcode.TestHelpers.Runtime")]
[assembly: InternalsVisibleTo("Unity.Netcode.Adapter.UTP")]
[assembly: InternalsVisibleTo("Unity.Multiplayer.Tools.Adapters.Ngo1WithUtp2")]
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
// NetSim Implementation compilation boilerplate
// All references to UNITY_MP_TOOLS_NETSIM_IMPLEMENTATION_ENABLED should be defined in the same way,
// as any discrepancies are likely to result in build failures
#if UNITY_EDITOR || (DEVELOPMENT_BUILD && !UNITY_MP_TOOLS_NETSIM_DISABLED_IN_DEVELOP) || (!DEVELOPMENT_BUILD && UNITY_MP_TOOLS_NETSIM_ENABLED_IN_RELEASE)
#define UNITY_MP_TOOLS_NETSIM_IMPLEMENTATION_ENABLED
#endif

using System;
using System.Collections.Generic;
using UnityEngine;
Expand Down Expand Up @@ -343,6 +350,9 @@ public struct SimulatorParameters
/// - packet jitter (variances in latency, see: https://en.wikipedia.org/wiki/Jitter)
/// - packet drop rate (packet loss)
/// </summary>
#if UTP_TRANSPORT_2_0_ABOVE
[Obsolete("DebugSimulator is no longer supported and has no effect. Use Network Simulator from the Multiplayer Tools package.", false)]
#endif
public SimulatorParameters DebugSimulator = new SimulatorParameters
{
PacketDelayMS = 0,
Expand All @@ -359,6 +369,10 @@ private struct PacketLossCache
public float PacketLoss;
};

internal static event Action<int, NetworkDriver> TransportInitialized;
internal static event Action<int> TransportDisposed;
Comment on lines +372 to +373
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason we can't make these public and then remove the internalsvisibleto? I'd love to get rid of "hidden" public apis.

internal NetworkDriver NetworkDriver => m_Driver;

private PacketLossCache m_PacketLossCache = new PacketLossCache();

private State m_State = State.Disconnected;
Expand Down Expand Up @@ -402,6 +416,8 @@ private void InitDriver()
out m_UnreliableFragmentedPipeline,
out m_UnreliableSequencedFragmentedPipeline,
out m_ReliableSequencedPipeline);

TransportInitialized?.Invoke(GetInstanceID(), NetworkDriver);
}

private void DisposeInternals()
Expand All @@ -419,6 +435,8 @@ private void DisposeInternals()
}

m_SendQueue.Clear();

TransportDisposed?.Invoke(GetInstanceID());
}

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

private void ConfigureSimulator()
#if UTP_TRANSPORT_2_0_ABOVE
private void ConfigureSimulatorForUtp2()
{
// As DebugSimulator is deprecated, the 'packetDelayMs', 'packetJitterMs' and 'packetDropPercentage'
// parameters are set to the default and are supposed to be changed using Network Simulator tool instead.
m_NetworkSettings.WithSimulatorStageParameters(
maxPacketCount: 300, // TODO Is there any way to compute a better value?
maxPacketSize: NetworkParameterConstants.MTU,
packetDelayMs: 0,
packetJitterMs: 0,
packetDropPercentage: 0,
randomSeed: DebugSimulatorRandomSeed ?? (uint)System.Diagnostics.Stopwatch.GetTimestamp()
, mode: ApplyMode.AllPackets
);

m_NetworkSettings.WithNetworkSimulatorParameters();
}
#else
private void ConfigureSimulatorForUtp1()
{
m_NetworkSettings.WithSimulatorStageParameters(
maxPacketCount: 300, // TODO Is there any way to compute a better value?
Expand All @@ -1338,11 +1377,9 @@ private void ConfigureSimulator()
packetJitterMs: DebugSimulator.PacketJitterMS,
packetDropPercentage: DebugSimulator.PacketDropRate,
randomSeed: DebugSimulatorRandomSeed ?? (uint)System.Diagnostics.Stopwatch.GetTimestamp()
#if UTP_TRANSPORT_2_0_ABOVE
, mode: ApplyMode.AllPackets
#endif
);
}
#endif

/// <summary>
/// Creates the internal NetworkDriver
Expand All @@ -1357,14 +1394,14 @@ public void CreateDriver(UnityTransport transport, out NetworkDriver driver,
out NetworkPipeline unreliableSequencedFragmentedPipeline,
out NetworkPipeline reliableSequencedPipeline)
{
#if MULTIPLAYER_TOOLS_1_0_0_PRE_7
#if !UTP_TRANSPORT_2_0_ABOVE
#if MULTIPLAYER_TOOLS_1_0_0_PRE_7 && !UTP_TRANSPORT_2_0_ABOVE
NetworkPipelineStageCollection.RegisterPipelineStage(new NetworkMetricsPipelineStage());
#endif
#endif

#if UNITY_EDITOR || DEVELOPMENT_BUILD
ConfigureSimulator();
#if UTP_TRANSPORT_2_0_ABOVE && UNITY_MP_TOOLS_NETSIM_IMPLEMENTATION_ENABLED
ConfigureSimulatorForUtp2();
#elif !UTP_TRANSPORT_2_0_ABOVE && (UNITY_EDITOR || DEVELOPMENT_BUILD)
ConfigureSimulatorForUtp1();
#endif

m_NetworkSettings.WithNetworkConfigParameters(
Expand Down Expand Up @@ -1395,42 +1432,53 @@ public void CreateDriver(UnityTransport transport, out NetworkDriver driver,
driver = NetworkDriver.Create(m_NetworkSettings);
#endif

#if MULTIPLAYER_TOOLS_1_0_0_PRE_7
#if UTP_TRANSPORT_2_0_ABOVE
driver.RegisterPipelineStage<NetworkMetricsPipelineStage>(new NetworkMetricsPipelineStage());
#if MULTIPLAYER_TOOLS_1_0_0_PRE_7 && UTP_TRANSPORT_2_0_ABOVE
driver.RegisterPipelineStage(new NetworkMetricsPipelineStage());
#endif

#if !UTP_TRANSPORT_2_0_ABOVE
SetupPipelinesForUtp1(driver,
out unreliableFragmentedPipeline,
out unreliableSequencedFragmentedPipeline,
out reliableSequencedPipeline);
#else
SetupPipelinesForUtp2(driver,
out unreliableFragmentedPipeline,
out unreliableSequencedFragmentedPipeline,
out reliableSequencedPipeline);
#endif
}

#if !UTP_TRANSPORT_2_0_ABOVE
private void SetupPipelinesForUtp1(NetworkDriver driver,
out NetworkPipeline unreliableFragmentedPipeline,
out NetworkPipeline unreliableSequencedFragmentedPipeline,
out NetworkPipeline reliableSequencedPipeline)
{
#if UNITY_EDITOR || DEVELOPMENT_BUILD
if (DebugSimulator.PacketDelayMS > 0 || DebugSimulator.PacketDropRate > 0)
{
unreliableFragmentedPipeline = driver.CreatePipeline(
typeof(FragmentationPipelineStage),
typeof(SimulatorPipelineStage)
#if !UTP_TRANSPORT_2_0_ABOVE
, typeof(SimulatorPipelineStageInSend)
#endif
typeof(SimulatorPipelineStage),
typeof(SimulatorPipelineStageInSend)
#if MULTIPLAYER_TOOLS_1_0_0_PRE_7
, typeof(NetworkMetricsPipelineStage)
#endif
);
unreliableSequencedFragmentedPipeline = driver.CreatePipeline(
typeof(FragmentationPipelineStage),
typeof(UnreliableSequencedPipelineStage),
typeof(SimulatorPipelineStage)
#if !UTP_TRANSPORT_2_0_ABOVE
, typeof(SimulatorPipelineStageInSend)
#endif
typeof(SimulatorPipelineStage),
typeof(SimulatorPipelineStageInSend)
#if MULTIPLAYER_TOOLS_1_0_0_PRE_7
, typeof(NetworkMetricsPipelineStage)
#endif
);
reliableSequencedPipeline = driver.CreatePipeline(
typeof(ReliableSequencedPipelineStage),
typeof(SimulatorPipelineStage)
#if !UTP_TRANSPORT_2_0_ABOVE
, typeof(SimulatorPipelineStageInSend)
#endif
typeof(SimulatorPipelineStage),
typeof(SimulatorPipelineStageInSend)
#if MULTIPLAYER_TOOLS_1_0_0_PRE_7
, typeof(NetworkMetricsPipelineStage)
#endif
Expand Down Expand Up @@ -1460,7 +1508,45 @@ public void CreateDriver(UnityTransport transport, out NetworkDriver driver,
);
}
}
#else
private void SetupPipelinesForUtp2(NetworkDriver driver,
out NetworkPipeline unreliableFragmentedPipeline,
out NetworkPipeline unreliableSequencedFragmentedPipeline,
out NetworkPipeline reliableSequencedPipeline)
{

unreliableFragmentedPipeline = driver.CreatePipeline(
typeof(FragmentationPipelineStage)
#if UNITY_MP_TOOLS_NETSIM_IMPLEMENTATION_ENABLED
, typeof(SimulatorPipelineStage)
#endif
#if MULTIPLAYER_TOOLS_1_0_0_PRE_7
, typeof(NetworkMetricsPipelineStage)
#endif
);

unreliableSequencedFragmentedPipeline = driver.CreatePipeline(
typeof(FragmentationPipelineStage),
typeof(UnreliableSequencedPipelineStage)
#if UNITY_MP_TOOLS_NETSIM_IMPLEMENTATION_ENABLED
, typeof(SimulatorPipelineStage)
#endif
#if MULTIPLAYER_TOOLS_1_0_0_PRE_7
, typeof(NetworkMetricsPipelineStage)
#endif
);

reliableSequencedPipeline = driver.CreatePipeline(
typeof(ReliableSequencedPipelineStage)
#if UNITY_MP_TOOLS_NETSIM_IMPLEMENTATION_ENABLED
, typeof(SimulatorPipelineStage)
#endif
#if MULTIPLAYER_TOOLS_1_0_0_PRE_7
, typeof(NetworkMetricsPipelineStage)
#endif
);
}
#endif
// -------------- Utility Types -------------------------------------------------------------------------------


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
using Unity.Netcode.TestHelpers.Runtime;
using Unity.Netcode.TestHelpers.Runtime.Metrics;
using Unity.Netcode.Transports.UTP;
#if UTP_TRANSPORT_2_0_ABOVE
using Unity.Networking.Transport.Utilities;
#endif
using UnityEngine.TestTools;

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

// Determined through trial and error. With both UTP 1.2 and 2.0, this random seed
// results in an effective packet loss percentage between 22% and 28%. Future UTP
Expand Down Expand Up @@ -63,6 +68,14 @@ public IEnumerator TrackPacketLossAsClient()
double packetLossRateMinRange = (m_PacketLossRate - m_PacketLossRangeDelta) / 100d;
double packetLossRateMaxrange = (m_PacketLossRate + m_PacketLossRangeDelta) / 100d;
var clientNetworkManager = m_ClientNetworkManagers[0];

#if UTP_TRANSPORT_2_0_ABOVE
var clientTransport = (UnityTransport)clientNetworkManager.NetworkConfig.NetworkTransport;
clientTransport.NetworkDriver.CurrentSettings.TryGet<SimulatorUtility.Parameters>(out var parameters);
parameters.PacketDropPercentage = m_PacketLossRate;
clientTransport.NetworkDriver.ModifySimulatorStageParameters(parameters);
#endif

var waitForPacketLossMetric = new WaitForGaugeMetricValues((clientNetworkManager.NetworkMetrics as NetworkMetrics).Dispatcher,
NetworkMetricTypes.PacketLoss,
metric => packetLossRateMinRange <= metric && metric <= packetLossRateMaxrange);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ public IEnumerator SendCompletesOnUnreliableSendQueueOverflow()
yield return null;
}

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

yield return null;
}
#endif

[UnityTest]
public IEnumerator SendQueuesFlushedOnShutdown([ValueSource("k_DeliveryParameters")] NetworkDelivery delivery)
Expand Down