Skip to content

Commit a49ad61

Browse files
fix: report each hybrid disconnect once while shutting down, and guard a negative clock step
While its NetworkManager is shutting down, UnifiedUpdateConnections now disconnects only the connections that still have ConnectionState, which it removes, so OnNetCodeDisconnect is not invoked again every frame for the same connections. It also stops collecting new connections while shutting down and drops an unused local. NetworkTimeSystem.Advance no longer throws when the delta time or AdjustmentRatio is negative.
1 parent 3c4cb32 commit a49ad61

2 files changed

Lines changed: 13 additions & 9 deletions

File tree

‎com.unity.netcode.gameobjects/Runtime/Components/Helpers/UnifiedUpdateConnections.cs‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ protected override void OnCreate()
5151

5252
protected override void OnUpdate()
5353
{
54-
var isServer = World.IsServer();
5554
var commandBuffer = new EntityCommandBuffer(Allocator.Temp);
5655
var networkManager = m_HybridNetworkManagerQuery.GetSingleton<HybridNetworkManager>().NetworkManager.Value;
5756
// The world outlives its NetworkManager if ShutdownInternal throws before disposing it.
@@ -75,14 +74,17 @@ protected override void OnUpdate()
7574
m_TempConnections.Clear();
7675

7776
// TODO: We should figure out how to associate the N4E NetworkId with the NGO ClientId
78-
foreach (var (networkId, entity) in SystemAPI.Query<NetworkId>().WithAll<NetworkStreamConnection>()
79-
.WithNone<NetworkStreamInGame>().WithEntityAccess())
77+
if (!isShuttingDown)
8078
{
81-
if (!m_NewConnections.ContainsKey(networkId.Value))
79+
foreach (var (networkId, entity) in SystemAPI.Query<NetworkId>().WithAll<NetworkStreamConnection>()
80+
.WithNone<NetworkStreamInGame>().WithEntityAccess())
8281
{
83-
var newConnection = new NetcodeConnection
84-
{ World = World, Entity = entity, NetworkId = networkId.Value };
85-
m_NewConnections.Add(networkId.Value, newConnection);
82+
if (!m_NewConnections.ContainsKey(networkId.Value))
83+
{
84+
var newConnection = new NetcodeConnection
85+
{ World = World, Entity = entity, NetworkId = networkId.Value };
86+
m_NewConnections.Add(networkId.Value, newConnection);
87+
}
8688
}
8789
}
8890

@@ -116,7 +118,8 @@ protected override void OnUpdate()
116118
// make sure we have disconnected all known connections.
117119
if (isShuttingDown)
118120
{
119-
foreach (var (networkId, entity) in SystemAPI.Query<NetworkId>().WithEntityAccess())
121+
// Only connections that went in-game have ConnectionState, which is removed here, so each is reported once.
122+
foreach (var (networkId, entity) in SystemAPI.Query<NetworkId>().WithAll<ConnectionState>().WithEntityAccess())
120123
{
121124
commandBuffer.RemoveComponent<ConnectionState>(entity);
122125
NetworkManager.OnNetCodeDisconnect?.Invoke(new NetcodeConnection

‎com.unity.netcode.gameobjects/Runtime/Timing/NetworkTimeSystem.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,8 @@ public bool Advance(double deltaTimeSec)
263263
// Move each offset toward its target by at most AdjustmentRatio of this frame's delta, stopping once it gets there.
264264
// Stepping the full amount every frame overshot the target, so a synced clock alternated between running fast and
265265
// slow, and anything driven by it juddered.
266-
var maximumStep = deltaTimeSec * AdjustmentRatio;
266+
// A negative step would make Math.Clamp throw, so a negative delta or AdjustmentRatio adjusts nothing.
267+
var maximumStep = Math.Max(0.0, deltaTimeSec * AdjustmentRatio);
267268
m_CurrentLocalTimeOffset += Math.Clamp(m_DesiredLocalTimeOffset - m_CurrentLocalTimeOffset, -maximumStep, maximumStep);
268269
m_CurrentServerTimeOffset += Math.Clamp(m_DesiredServerTimeOffset - m_CurrentServerTimeOffset, -maximumStep, maximumStep);
269270

0 commit comments

Comments
 (0)