Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@
// creates its own bootstrap, so the worlds created for other NetworkManagers are registered again.
private static readonly List<NetcodeWorld> k_CreatedWorlds = new List<NetcodeWorld>();

#if UNITY_EDITOR
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void ResetStaticsOnLoad()
{
Instance = null;
OnInitialized = null;
Port = 7979;
CurrentNetworkManagerForInitialization = null;
LastCreatedWorld = null;
s_WorldCounter = 0;
k_CreatedWorlds.Clear();
}

Check warning on line 45 in com.unity.netcode.gameobjects/Runtime/Components/Helpers/UnifiedBootstrap.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

com.unity.netcode.gameobjects/Runtime/Components/Helpers/UnifiedBootstrap.cs#L37-L45

Added lines #L37 - L45 were not covered by tests
#endif

private static void RegisterCreatedWorlds()
{
for (int i = k_CreatedWorlds.Count - 1; i >= 0; i--)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1266,7 +1266,12 @@
NetworkConfig.InitializePrefabs();
if (NetworkConfig.Prefabs.HasGhostPrefabs)
{
NetworkConfig.NetworkTransport = gameObject.AddComponent<UnifiedNetcodeTransport>();
// Reused so that restarting a session does not add another transport.
if (!TryGetComponent(out UnifiedNetcodeTransport unifiedTransport))
{
unifiedTransport = gameObject.AddComponent<UnifiedNetcodeTransport>();
}
NetworkConfig.NetworkTransport = unifiedTransport;

Check warning on line 1274 in com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs#L1270-L1274

Added lines #L1270 - L1274 were not covered by tests

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2

After a client-to-server/host restart, a remote connection matching the previous server’s transport ID is rejected as already connected. UnifiedNetcodeTransport.Initialize replaces m_Connections but preserves m_ServerClientId from OnClientConnectedToServer. NetworkConnectionManager.TransportIdToClientId treats that retained ID as the server, so ConnectEventHandler rejects the connection. Previously, creating another transport restored the -1 sentinel. Reset m_ServerClientId to -1 in UnifiedNetcodeTransport.Initialize before reusing the component.

🤖 Helpful? 👍/👎

}
#endif
#if UNIFIED_NETCODE
Expand Down Expand Up @@ -1400,6 +1405,7 @@
TypeManager.Initialize();
UnifiedBootstrap.CurrentNetworkManagerForInitialization = this;
new UnifiedBootstrap().Initialize("Default World");
UnifiedBootstrap.CurrentNetworkManagerForInitialization = null;

Check warning on line 1408 in com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

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

Added line #L1408 was not covered by tests

if (NetcodeWorld != null && NetcodeWorld.IsCreated)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,16 @@

private Dictionary<int, ConnectionInfo> m_Connections;

private bool RemoveConnection(int connectionId)
{
if (!m_Connections.Remove(connectionId, out ConnectionInfo connectionInfo))
{
return false;

Check warning on line 222 in com.unity.netcode.gameobjects/Runtime/Transports/Unified/UnifiedNetcodeTransport.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

com.unity.netcode.gameobjects/Runtime/Transports/Unified/UnifiedNetcodeTransport.cs#L219-L222

Added lines #L219 - L222 were not covered by tests
}
connectionInfo.SendQueue.Dispose();
return true;
}

Check warning on line 226 in com.unity.netcode.gameobjects/Runtime/Transports/Unified/UnifiedNetcodeTransport.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

com.unity.netcode.gameobjects/Runtime/Transports/Unified/UnifiedNetcodeTransport.cs#L224-L226

Added lines #L224 - L226 were not covered by tests

internal void DispatchMessage(int connectionId, in FixedBytes1280 buffer)
{
// The connection is removed on disconnect, which can be ahead of its last messages.
Expand All @@ -223,7 +233,7 @@
return;
}

using var arr = FixedBytes1280.ToNativeArray(buffer);
var arr = FixedBytes1280.ToNativeArray(buffer);

Check warning on line 236 in com.unity.netcode.gameobjects/Runtime/Transports/Unified/UnifiedNetcodeTransport.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

com.unity.netcode.gameobjects/Runtime/Transports/Unified/UnifiedNetcodeTransport.cs#L236

Added line #L236 was not covered by tests
var reader = new DataStreamReader(arr);
if (connectionInfo.ReceiveQueue == null)
{
Expand All @@ -233,6 +243,11 @@
{
connectionInfo.ReceiveQueue.PushReader(reader);
}
#if ENABLE_UNITY_COLLECTIONS_CHECKS
// Disposing an Allocator.None array does not release its safety handle. The queue has copied the data.
var safety = NativeArrayUnsafeUtility.GetAtomicSafetyHandle(arr);
CollectionHelper.DisposeSafetyHandle(ref safety);

Check warning on line 249 in com.unity.netcode.gameobjects/Runtime/Transports/Unified/UnifiedNetcodeTransport.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

com.unity.netcode.gameobjects/Runtime/Transports/Unified/UnifiedNetcodeTransport.cs#L248-L249

Added lines #L248 - L249 were not covered by tests
#endif

var message = connectionInfo.ReceiveQueue.PopMessage();
while (message.Count != 0)
Expand Down Expand Up @@ -278,6 +293,7 @@

private void OnClientConnectedToServer(Connection connection, NetcodeConnectionEvent connectionEvent)
{
RemoveConnection(connectionEvent.Id.Value);

Check warning on line 296 in com.unity.netcode.gameobjects/Runtime/Transports/Unified/UnifiedNetcodeTransport.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

com.unity.netcode.gameobjects/Runtime/Transports/Unified/UnifiedNetcodeTransport.cs#L296

Added line #L296 was not covered by tests
m_Connections[connectionEvent.Id.Value] = new ConnectionInfo
{
ReceiveQueue = null,
Expand All @@ -292,6 +308,7 @@

private void OnServerNewClientConnection(Connection connection, NetcodeConnectionEvent connectionEvent)
{
RemoveConnection(connectionEvent.Id.Value);

Check warning on line 311 in com.unity.netcode.gameobjects/Runtime/Transports/Unified/UnifiedNetcodeTransport.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

com.unity.netcode.gameobjects/Runtime/Transports/Unified/UnifiedNetcodeTransport.cs#L311

Added line #L311 was not covered by tests
m_Connections[connectionEvent.Id.Value] = new ConnectionInfo
{
ReceiveQueue = null,
Expand Down Expand Up @@ -375,13 +392,14 @@
GetDisconnectMessageFromNetworkStreamDisconnectReason(connectionEvent.DisconnectReason)
);
// Removed before notifying, so the shutdown this triggers does not notify again from DisconnectLocalClient.
m_Connections.Remove(connectionEvent.Id.Value);
RemoveConnection(connectionEvent.Id.Value);

Check warning on line 395 in com.unity.netcode.gameobjects/Runtime/Transports/Unified/UnifiedNetcodeTransport.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

com.unity.netcode.gameobjects/Runtime/Transports/Unified/UnifiedNetcodeTransport.cs#L395

Added line #L395 was not covered by tests
InvokeOnTransportEvent(NetworkEvent.Disconnect, (ulong)connectionEvent.Id.Value, default, m_RealTimeProvider.RealTimeSinceStartup);
}

private void OnServerClientDisconnected(Connection connection, NetcodeConnectionEvent connectionEvent)
{
InvokeOnTransportEvent(NetworkEvent.Disconnect, (ulong)connectionEvent.Id.Value, default, m_RealTimeProvider.RealTimeSinceStartup);
RemoveConnection(connectionEvent.Id.Value);

Check warning on line 402 in com.unity.netcode.gameobjects/Runtime/Transports/Unified/UnifiedNetcodeTransport.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

com.unity.netcode.gameobjects/Runtime/Transports/Unified/UnifiedNetcodeTransport.cs#L402

Added line #L402 was not covered by tests
}

private void OnClientConnectionEvent(Connection connection, NetcodeConnectionEvent connectionEvent)
Expand Down Expand Up @@ -435,14 +453,18 @@

public override void DisconnectRemoteClient(ulong clientId)
{
m_NetworkManager.NetcodeWorld.DisconnectAClient(m_Connections[(int)clientId].Connection);
m_Connections.Remove((int)clientId);
// The connection is already removed when the client disconnected first.
if (m_Connections.TryGetValue((int)clientId, out ConnectionInfo connectionInfo))
{
m_NetworkManager.NetcodeWorld.DisconnectAClient(connectionInfo.Connection);
RemoveConnection((int)clientId);
}

Check warning on line 461 in com.unity.netcode.gameobjects/Runtime/Transports/Unified/UnifiedNetcodeTransport.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

com.unity.netcode.gameobjects/Runtime/Transports/Unified/UnifiedNetcodeTransport.cs#L457-L461

Added lines #L457 - L461 were not covered by tests
}

public override void DisconnectLocalClient()
{
// Remove the connection 1st (the world might not be available)
var wasConnected = m_Connections.Remove((int)ServerClientId);
var wasConnected = RemoveConnection((int)ServerClientId);

Check warning on line 467 in com.unity.netcode.gameobjects/Runtime/Transports/Unified/UnifiedNetcodeTransport.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

com.unity.netcode.gameobjects/Runtime/Transports/Unified/UnifiedNetcodeTransport.cs#L467

Added line #L467 was not covered by tests

// TODO-FIX-REVIEW-ME:
// This was causing errors to occur upon shutdown during an integration test.
Expand Down Expand Up @@ -495,6 +517,15 @@
netcodeWorld.OnConnectionEvent -= OnClientConnectionEvent;
netcodeWorld.OnConnectionEvent -= OnServerConnectionEvent;
}

if (m_Connections != null)
{
foreach (var connectionInfo in m_Connections.Values)
{
connectionInfo.SendQueue.Dispose();
}
m_Connections.Clear();
}

Check warning on line 528 in com.unity.netcode.gameobjects/Runtime/Transports/Unified/UnifiedNetcodeTransport.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

com.unity.netcode.gameobjects/Runtime/Transports/Unified/UnifiedNetcodeTransport.cs#L521-L528

Added lines #L521 - L528 were not covered by tests
}
}
}
Expand Down
Loading