-
Notifications
You must be signed in to change notification settings - Fork 450
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
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
7c1ebbc
Adding support for netsim to be able to work with Tools adapter
92de75a
Adding the simulator only if active in build inside unity transport.
49a0ceb
Setting up Simulator Pipeline stage if NetSim is available
95ba089
Merge branch 'develop' into feature/tools-netsim-support
d748605
Apply suggestions from code review
23a0f5b
Removal of Debug Simulator in UTP1, updated callback to avoid moving to
1461103
Merge branch 'develop' into feature/tools-netsim-support
55a131c
Fixed compiling issues
87144f0
Fix compiler error typo: "river" should be "driver"
DenninDalke 3093b22
Deprecate DebugSimulator when using UTP2
DenninDalke 838485b
Deprecate SetDebugSimulatorParameters on UTP1
DenninDalke 3abb7b0
Merge branch 'develop' into feature/tools-netsim-support
DenninDalke 9263fd1
Add tool suggestion to DebugSimulator deprecation
DenninDalke afbcdcc
Add tool suggestion for DebugSimulator deprecation and comment
DenninDalke 5063a87
Move comments out the parameter list to avoid CI failure
DenninDalke 61e04e5
Fix formatting
DenninDalke af4f757
Merge branch 'develop' into feature/tools-netsim-support
DenninDalke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
@@ -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, | ||
|
@@ -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
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. Any reason we can't make these public and then remove the |
||
internal NetworkDriver NetworkDriver => m_Driver; | ||
|
||
private PacketLossCache m_PacketLossCache = new PacketLossCache(); | ||
|
||
private State m_State = State.Disconnected; | ||
|
@@ -402,6 +416,8 @@ private void InitDriver() | |
out m_UnreliableFragmentedPipeline, | ||
out m_UnreliableSequencedFragmentedPipeline, | ||
out m_ReliableSequencedPipeline); | ||
|
||
TransportInitialized?.Invoke(GetInstanceID(), NetworkDriver); | ||
} | ||
|
||
private void DisposeInternals() | ||
|
@@ -419,6 +435,8 @@ private void DisposeInternals() | |
} | ||
|
||
m_SendQueue.Clear(); | ||
|
||
TransportDisposed?.Invoke(GetInstanceID()); | ||
} | ||
|
||
private NetworkPipeline SelectSendPipeline(NetworkDelivery delivery) | ||
|
@@ -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) | ||
|
@@ -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, | ||
DenninDalke marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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? | ||
|
@@ -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 | ||
|
@@ -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( | ||
|
@@ -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 | ||
|
@@ -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 ------------------------------------------------------------------------------- | ||
|
||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.