Skip to content

Commit 6555a1e

Browse files
refactor: Remove SIPTransport and replace it with UnityTransport (#1870)
* delete `SIPTransport` & `SIPTransportTests` * fix compile errors * fix This fixes the timing issues with the MessageOrdering.SpawnRpcDespawn integration test. This also includes a minor update to NetcodeIntegrationTestHelpers (destroy as opposed to destroy immediate). * fix This fixes the issue with MultiClientConnectionApproval.ClientConnectedCallbackTest failing when using UTP as opposed to SIP. * fix Fixed issue with ConnectionApprovalHandler's prefab getting destroyed when a client was disconnected. Applied same fix to the NetcodeIntegrationTest when creating a prefab. Also fixing issue with NetworkManager spewing spam messages if the NetworkConfig.ConnectionApproval is set but there is no ConnectionApprovalCallback assigned. Adjusted it to make sure it is either the Server or Host that this applies to, but not a client. * fix This fixes the ConnectionApprovalMismatchTest issue that caused it to fail with UTP. Switching over to client notifications (i.e. the server detects the mismatch and disconnects the clients, we want to make sure the clients received the disconnection notification). * fix Global fix for teardown and NetworkObject destroying....make sure the server is the NetworkManagerOwner before destroying it. * fix Some NetcodeIntegrationTest adjustments to handle the switch over to UTP. * fix Fixes issues with NetworkObjectParentingTests (increased wait time between changes to 1 full Network Tick). Also the recent NetcodeIntegrationTest updates helped fix some of the issues with NetworkObjectParentingTests. * fix Fixes the issue with NetworkSpawnManagerTests.TestConnectAndDisconnect. * Fix - NetworkSceneManager Switching to UnityTransport exposed a potential bug where the server disconnects a client while the client is still processing a SceneEventType.Synchronize message which by the time the client processes this it has already sent the SceneEventType.SynchronizeComplete message back to the server that *could* attempt to send a resynchronization message where the MessageSystem will crash because the m_SendQueues entry for the now disconnected client no longer exists. * Fix - NetcodeIntegrationTest Added some additional UnityTransport settings for payload size, send queue size, and reducing the connection attempts and time out to connect per attempt. Added the ability to specify whether you want the server or clients to be instantiated first (see comments under NetcodeIntegrationTest.m_CreateServerFirst for further explanation). * Fix - ClientOnlyConnectionTests Removing UNet and just using UTP. Reducing the timeout period so it doesn't take over 60 seconds to timeout. * Fix - DisconnectTests Fixes timing issues with this test that is causing it to not passing when using UTP. * fix - NetworkObjectOwnershipTests Fixed timing issues exposed when using UTP. * fix - NetworkSceneManagerEventDataPoolTest This fixes an issue when I originally switched it to be the server as the default NetworkManager to be instantiated when using NetcodeIntegrationTestHelper.Create. This sets the, recently updated NetcodeIntegrationTest m_CreateServerFirst, clients to instantiate before the server (needed for in-scene objects) * fix - NetworkSceneManagerSeneVerification This test requires the clients to be instantiated before the server. * style * style * update MTT-3016 MTT-3016 Changelog entries. * Update CHANGELOG.md * Update CHANGELOG.md * fix - tools This resolves the issues with tools integration tests and switching over to UnityTransport. * Update com.unity.netcode.gameobjects/CHANGELOG.md Co-authored-by: Fatih Mar <[email protected]> * Update com.unity.netcode.gameobjects/CHANGELOG.md Co-authored-by: Fatih Mar <[email protected]> * fix The following fixes some edge-case timing related issues that are being exposed in Yamato where it might take longer than 1 network tick depending upon the virtual machine's current load. Primarily, the majority of the fixes are handled by just waiting for the condition or timing out. * fix - console - ps4 the time out was never yielding and causing issues on PS4/ * revert Removing some code that didn't need to make it into this PR. Co-authored-by: Fatih Mar <[email protected]>
1 parent 5e26650 commit 6555a1e

35 files changed

+260
-498
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ Additional documentation and release notes are available at [Multiplayer Documen
1818

1919
### Changed
2020

21+
- Changed `NetcodeIntegrationTestHelpers` to use `UnityTransport` (#1870)
2122
- Updated `UnityTransport` dependency on `com.unity.transport` to 1.0.0 (#1849)
2223

2324
### Removed
2425

26+
- Removed `SIPTransport` (#1870)
2527
- Removed `SnapshotSystem` (#1852)
2628
- Removed `com.unity.modules.animation`, `com.unity.modules.physics` and `com.unity.modules.physics2d` dependencies from the package (#1812)
2729
- Removed `com.unity.collections` dependency from the package (#1849)

com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,9 @@ private bool CanStart(StartType type)
930930
return false;
931931
}
932932

933-
if (NetworkConfig.ConnectionApproval)
933+
// Only if it is starting as a server or host do we need to check this
934+
// Clients don't invoke the ConnectionApprovalCallback
935+
if (NetworkConfig.ConnectionApproval && type != StartType.Client)
934936
{
935937
if (ConnectionApprovalCallback == null)
936938
{

com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1746,7 +1746,9 @@ private void HandleServerSceneEvent(uint sceneEventId, ulong clientId)
17461746
// NetworkObjects
17471747
m_NetworkManager.InvokeOnClientConnectedCallback(clientId);
17481748

1749-
if (sceneEventData.ClientNeedsReSynchronization() && !DisableReSynchronization)
1749+
// Check to see if the client needs to resynchronize and before sending the message make sure the client is still connected to avoid
1750+
// a potential crash within the MessageSystem (i.e. sending to a client that no longer exists)
1751+
if (sceneEventData.ClientNeedsReSynchronization() && !DisableReSynchronization && m_NetworkManager.ConnectedClients.ContainsKey(clientId))
17501752
{
17511753
sceneEventData.SceneEventType = SceneEventType.ReSynchronize;
17521754
SendSceneEventData(sceneEventId, new ulong[] { clientId });

com.unity.netcode.gameobjects/TestHelpers/Runtime/NetcodeIntegrationTest.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ public static void DeregisterNetworkObject(ulong localClientId, ulong networkObj
7979
protected const uint k_DefaultTickRate = 30;
8080
protected abstract int NumberOfClients { get; }
8181

82+
/// <summary>
83+
/// Set this to false to create the clients first.
84+
/// Note: If you are using scene placed NetworkObjects or doing any form of scene testing and
85+
/// get prefab hash id "soft synchronization" errors, then set this to false and run your test
86+
/// again. This is a work-around until we can resolve some issues with NetworkManagerOwner and
87+
/// NetworkManager.Singleton.
88+
/// </summary>
89+
protected bool m_CreateServerFirst = true;
90+
8291
public enum NetworkManagerInstatiationMode
8392
{
8493
PerTest, // This will create and destroy new NetworkManagers for each test within a child derived class
@@ -108,8 +117,6 @@ public enum HostOrServer
108117
protected bool m_UseHost = true;
109118
protected int m_TargetFrameRate = 60;
110119

111-
protected NetcodeIntegrationTestHelpers.InstanceTransport m_NetworkTransport = NetcodeIntegrationTestHelpers.InstanceTransport.SIP;
112-
113120
private NetworkManagerInstatiationMode m_NetworkManagerInstatiationMode;
114121

115122
private bool m_EnableVerboseDebug;
@@ -252,7 +259,7 @@ protected void CreateServerAndClients(int numberOfClients)
252259
CreatePlayerPrefab();
253260

254261
// Create multiple NetworkManager instances
255-
if (!NetcodeIntegrationTestHelpers.Create(numberOfClients, out NetworkManager server, out NetworkManager[] clients, m_TargetFrameRate, m_NetworkTransport))
262+
if (!NetcodeIntegrationTestHelpers.Create(numberOfClients, out NetworkManager server, out NetworkManager[] clients, m_TargetFrameRate, m_CreateServerFirst))
256263
{
257264
Debug.LogError("Failed to create instances");
258265
Assert.Fail("Failed to create instances");
@@ -558,6 +565,7 @@ protected void DestroySceneNetworkObjects()
558565
}
559566
if (CanDestroyNetworkObject(networkObject))
560567
{
568+
networkObject.NetworkManagerOwner = m_ServerNetworkManager;
561569
// Destroy the GameObject that holds the NetworkObject component
562570
Object.DestroyImmediate(networkObject.gameObject);
563571
}
@@ -668,6 +676,7 @@ protected GameObject CreateNetworkObjectPrefab(string baseName)
668676
var gameObject = new GameObject();
669677
gameObject.name = baseName;
670678
var networkObject = gameObject.AddComponent<NetworkObject>();
679+
networkObject.NetworkManagerOwner = m_ServerNetworkManager;
671680
NetcodeIntegrationTestHelpers.MakeNetworkObjectTestPrefab(networkObject);
672681
var networkPrefab = new NetworkPrefab() { Prefab = gameObject };
673682
m_ServerNetworkManager.NetworkConfig.NetworkPrefabs.Add(networkPrefab);

com.unity.netcode.gameobjects/TestHelpers/Runtime/NetcodeIntegrationTestHelpers.cs

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,6 @@ public void OnAfterHandleMessage<T>(ref T message, ref NetworkContext context) w
107107

108108
public static List<NetworkManager> NetworkManagerInstances => s_NetworkManagerInstances;
109109

110-
public enum InstanceTransport
111-
{
112-
SIP,
113-
UTP
114-
}
115-
116110
internal static IntegrationTestSceneHandler ClientSceneHandler = null;
117111

118112
/// <summary>
@@ -162,20 +156,32 @@ public static void RegisterHandlers(NetworkManager networkManager, bool serverSi
162156
}
163157
}
164158

165-
/// <summary>
166-
/// Create the correct NetworkTransport, attach it to the game object and return it.
167-
/// Default value is SIPTransport.
168-
/// </summary>
169-
internal static NetworkTransport CreateInstanceTransport(InstanceTransport instanceTransport, GameObject go)
159+
public static NetworkManager CreateServer()
170160
{
171-
switch (instanceTransport)
161+
// Create gameObject
162+
var go = new GameObject("NetworkManager - Server");
163+
164+
// Create networkManager component
165+
var server = go.AddComponent<NetworkManager>();
166+
NetworkManagerInstances.Insert(0, server);
167+
168+
// Create transport
169+
var unityTransport = go.AddComponent<UnityTransport>();
170+
// We need to increase this buffer size for tests that spawn a bunch of things
171+
unityTransport.MaxPayloadSize = 256000;
172+
unityTransport.MaxSendQueueSize = 1024 * 1024;
173+
174+
// Allow 4 connection attempts that each will time out after 500ms
175+
unityTransport.MaxConnectAttempts = 4;
176+
unityTransport.ConnectTimeoutMS = 500;
177+
178+
// Set the NetworkConfig
179+
server.NetworkConfig = new NetworkConfig()
172180
{
173-
case InstanceTransport.SIP:
174-
return go.AddComponent<SIPTransport>();
175-
default:
176-
case InstanceTransport.UTP:
177-
return go.AddComponent<UnityTransport>();
178-
}
181+
// Set transport
182+
NetworkTransport = unityTransport
183+
};
184+
return server;
179185
}
180186

181187
/// <summary>
@@ -185,24 +191,22 @@ internal static NetworkTransport CreateInstanceTransport(InstanceTransport insta
185191
/// <param name="server">The server NetworkManager</param>
186192
/// <param name="clients">The clients NetworkManagers</param>
187193
/// <param name="targetFrameRate">The targetFrameRate of the Unity engine to use while the multi instance helper is running. Will be reset on shutdown.</param>
188-
public static bool Create(int clientCount, out NetworkManager server, out NetworkManager[] clients, int targetFrameRate = 60, InstanceTransport instanceTransport = InstanceTransport.SIP)
194+
/// <param name="serverFirst">This determines if the server or clients will be instantiated first (defaults to server first)</param>
195+
public static bool Create(int clientCount, out NetworkManager server, out NetworkManager[] clients, int targetFrameRate = 60, bool serverFirst = true)
189196
{
190197
s_NetworkManagerInstances = new List<NetworkManager>();
191-
CreateNewClients(clientCount, out clients, instanceTransport);
192-
193-
// Create gameObject
194-
var go = new GameObject("NetworkManager - Server");
198+
server = null;
199+
if (serverFirst)
200+
{
201+
server = CreateServer();
202+
}
195203

196-
// Create networkManager component
197-
server = go.AddComponent<NetworkManager>();
198-
NetworkManagerInstances.Insert(0, server);
204+
CreateNewClients(clientCount, out clients);
199205

200-
// Set the NetworkConfig
201-
server.NetworkConfig = new NetworkConfig()
206+
if (!serverFirst)
202207
{
203-
// Set transport
204-
NetworkTransport = CreateInstanceTransport(instanceTransport, go)
205-
};
208+
server = CreateServer();
209+
}
206210

207211
s_OriginalTargetFrameRate = Application.targetFrameRate;
208212
Application.targetFrameRate = targetFrameRate;
@@ -215,7 +219,7 @@ public static bool Create(int clientCount, out NetworkManager server, out Networ
215219
/// </summary>
216220
/// <param name="clientCount">The amount of clients</param>
217221
/// <param name="clients"></param>
218-
public static bool CreateNewClients(int clientCount, out NetworkManager[] clients, InstanceTransport instanceTransport = InstanceTransport.SIP)
222+
public static bool CreateNewClients(int clientCount, out NetworkManager[] clients)
219223
{
220224
clients = new NetworkManager[clientCount];
221225
var activeSceneName = SceneManager.GetActiveScene().name;
@@ -226,11 +230,14 @@ public static bool CreateNewClients(int clientCount, out NetworkManager[] client
226230
// Create networkManager component
227231
clients[i] = go.AddComponent<NetworkManager>();
228232

233+
// Create transport
234+
var unityTransport = go.AddComponent<UnityTransport>();
235+
229236
// Set the NetworkConfig
230237
clients[i].NetworkConfig = new NetworkConfig()
231238
{
232239
// Set transport
233-
NetworkTransport = CreateInstanceTransport(instanceTransport, go)
240+
NetworkTransport = unityTransport
234241
};
235242
}
236243

@@ -273,7 +280,10 @@ public static void Destroy()
273280
// Destroy the network manager instances
274281
foreach (var networkManager in NetworkManagerInstances)
275282
{
276-
Object.DestroyImmediate(networkManager.gameObject);
283+
if (networkManager.gameObject != null)
284+
{
285+
Object.Destroy(networkManager.gameObject);
286+
}
277287
}
278288

279289
NetworkManagerInstances.Clear();

com.unity.netcode.gameobjects/TestHelpers/Runtime/NetworkManagerHelper.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using UnityEngine;
44
using NUnit.Framework;
5+
using Unity.Netcode.Transports.UTP;
56

67
namespace Unity.Netcode.TestHelpers.Runtime
78
{
@@ -67,11 +68,7 @@ public static bool StartNetworkManager(out NetworkManager networkManager, Networ
6768

6869
Debug.Log($"{nameof(NetworkManager)} Instantiated.");
6970

70-
// NOTE: For now we only use SIPTransport for tests until UnityTransport
71-
// has been verified working in nightly builds
72-
// TODO-MTT-2486: Provide support for other transports once tested and verified
73-
// working on consoles.
74-
var sipTransport = NetworkManagerGameObject.AddComponent<SIPTransport>();
71+
var unityTransport = NetworkManagerGameObject.AddComponent<UnityTransport>();
7572
if (networkConfig == null)
7673
{
7774
networkConfig = new NetworkConfig
@@ -81,7 +78,7 @@ public static bool StartNetworkManager(out NetworkManager networkManager, Networ
8178
}
8279

8380
NetworkManagerObject.NetworkConfig = networkConfig;
84-
NetworkManagerObject.NetworkConfig.NetworkTransport = sipTransport;
81+
NetworkManagerObject.NetworkConfig.NetworkTransport = unityTransport;
8582

8683
// Starts the network manager in the mode specified
8784
StartNetworkManagerMode(managerMode);

0 commit comments

Comments
 (0)