Skip to content
Open
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/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Additional documentation and release notes are available at [Multiplayer Documen

### Fixed

- Ensured all callbacks are wrapped with exception handling to avoid silent errors. (#4161)
- Issue where a growable `FastBufferWriter` created with a size of 0 never returned from its first write or `TryBeginWrite` call that needed the buffer to grow. (#4224)
- Issue where a `FastBufferReader` created with an offset and the default length of -1 read past the end of its source. The `ArraySegment<byte>` overload that takes an offset now applies it from the start of the segment. (#4222)
- Issue where a client promoted to session owner could throw an exception when synchronizing a newly joining client. (#4214)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,17 +396,21 @@ protected override void OnNetworkPostSpawn()
}

/// <inheritdoc/>
/// <remarks>
/// If overriding this method, it is required that you invoke this base method.
/// </remarks>
// TODO: Not used anymore
public override void OnDestroy()
{
base.OnDestroy();
}


internal override void InternalOnDestroy()
{
if (m_CoroutineObject.IsRunning)
{
StopCoroutine(m_CoroutineObject.Coroutine);
m_CoroutineObject.IsRunning = false;
}
base.OnDestroy();
base.InternalOnDestroy();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,16 @@
LastCreatedWorld = CreateLocalWorld("LocalWorld");
}

OnInitialized?.Invoke();
// Always wrap events that can invoke user script in a try-catch to assure any
// proceeding script is still executed.
try
{
OnInitialized?.Invoke();
}
catch (Exception ex)
{
Debug.LogException(ex);
}

Check warning on line 127 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#L124-L127

Added lines #L124 - L127 were not covered by tests

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using Unity.Collections;
using Unity.Entities;
using UnityEngine;

namespace Unity.Netcode.Components
{
Expand Down Expand Up @@ -69,7 +70,14 @@

foreach (var con in m_TempConnections)
{
NetworkManager.OnNetCodeDisconnect?.Invoke(con);
try
{
NetworkManager.OnNetCodeDisconnect?.Invoke(con);
}
catch (System.Exception ex)
{
Debug.LogException(ex);
}

Check warning on line 80 in com.unity.netcode.gameobjects/Runtime/Components/Helpers/UnifiedUpdateConnections.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

com.unity.netcode.gameobjects/Runtime/Components/Helpers/UnifiedUpdateConnections.cs#L74-L80

Added lines #L74 - L80 were not covered by tests
}

m_TempConnections.Clear();
Expand Down Expand Up @@ -101,7 +109,14 @@
// Set the connection in-game
commandBuffer.AddComponent<NetworkStreamInGame>(entry.Value.Entity);
commandBuffer.AddComponent(entry.Value.Entity, new ConnectionState { NetworkId = entry.Value.NetworkId });
NetworkManager.OnNetCodeConnect?.Invoke(entry.Value);
try
{
NetworkManager.OnNetCodeConnect?.Invoke(entry.Value);
}
catch (System.Exception ex)
{
Debug.LogException(ex);
}

Check warning on line 119 in com.unity.netcode.gameobjects/Runtime/Components/Helpers/UnifiedUpdateConnections.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

com.unity.netcode.gameobjects/Runtime/Components/Helpers/UnifiedUpdateConnections.cs#L113-L119

Added lines #L113 - L119 were not covered by tests
m_TempConnections.Add(entry.Value);
}
}
Expand All @@ -123,8 +138,14 @@
foreach (var (networkId, entity) in SystemAPI.Query<NetworkId>().WithAll<ConnectionState>().WithEntityAccess())
{
commandBuffer.RemoveComponent<ConnectionState>(entity);
NetworkManager.OnNetCodeDisconnect?.Invoke(new NetcodeConnection
{ World = World, Entity = entity, NetworkId = networkId.Value });
try
{
NetworkManager.OnNetCodeDisconnect?.Invoke(new NetcodeConnection { World = World, Entity = entity, NetworkId = networkId.Value });
}
catch (System.Exception ex)
{
Debug.LogException(ex);
}

Check warning on line 148 in com.unity.netcode.gameobjects/Runtime/Components/Helpers/UnifiedUpdateConnections.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

com.unity.netcode.gameobjects/Runtime/Components/Helpers/UnifiedUpdateConnections.cs#L142-L148

Added lines #L142 - L148 were not covered by tests
}
}
commandBuffer.Playback(EntityManager);
Expand All @@ -139,7 +160,15 @@
foreach (var (networkId, entity) in SystemAPI.Query<NetworkId>().WithEntityAccess())
{
commandBuffer.RemoveComponent<ConnectionState>(entity);
NetworkManager.OnNetCodeDisconnect?.Invoke(new NetcodeConnection { World = World, Entity = entity, NetworkId = networkId.Value });

try
{
NetworkManager.OnNetCodeDisconnect?.Invoke(new NetcodeConnection { World = World, Entity = entity, NetworkId = networkId.Value });
}
catch (System.Exception ex)
{
Debug.LogException(ex);
}

Check warning on line 171 in com.unity.netcode.gameobjects/Runtime/Components/Helpers/UnifiedUpdateConnections.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

com.unity.netcode.gameobjects/Runtime/Components/Helpers/UnifiedUpdateConnections.cs#L165-L171

Added lines #L165 - L171 were not covered by tests
}
commandBuffer.Playback(EntityManager);
base.OnDestroy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,15 @@
var clientSeverOrHost = LocalClient.IsServer ? LocalClient.IsHost ? "Host" : "Server" : "Client";
var whenFailed = duringStart ? "start failure" : "failure";
NetworkLog.LogError($"{clientSeverOrHost} is shutting down due to network transport {whenFailed} of {NetworkManager.NetworkConfig.NetworkTransport.GetType().Name}!");
OnTransportFailure?.Invoke();

try
{
OnTransportFailure?.Invoke();
}
catch (Exception ex)
{
Debug.LogException(ex);
}

Check warning on line 722 in com.unity.netcode.gameobjects/Runtime/Connection/NetworkConnectionManager.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

com.unity.netcode.gameobjects/Runtime/Connection/NetworkConnectionManager.cs#L719-L722

Added lines #L719 - L722 were not covered by tests

// If we had a transport failure when trying to start, reset the local client roles and directly invoke the internal shutdown.
if (duringStart)
Expand Down Expand Up @@ -854,12 +862,25 @@
// Note: ToArray() also allocates. :(
var response = new NetworkManager.ConnectionApprovalResponse();
ClientsToApprove[context.SenderId] = response;
ConnectionApprovalCallback?.Invoke(
new NetworkManager.ConnectionApprovalRequest
{
Payload = connectionRequestMessage.ConnectionData,
ClientNetworkId = context.SenderId
}, response);
try
{
ConnectionApprovalCallback?.Invoke(
new NetworkManager.ConnectionApprovalRequest
{
Payload = connectionRequestMessage.ConnectionData,
ClientNetworkId = context.SenderId
}, response);
}
catch (Exception ex)
{

Check warning on line 875 in com.unity.netcode.gameobjects/Runtime/Connection/NetworkConnectionManager.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

com.unity.netcode.gameobjects/Runtime/Connection/NetworkConnectionManager.cs#L874-L875

Added lines #L874 - L875 were not covered by tests
// A throwing approval handler would otherwise leave a Pending response stranded in
// ClientsToApprove, hanging the connecting client until it times out. Deny instead.
Debug.LogException(ex);
response.Approved = false;
response.Pending = false;
response.CreatePlayerObject = false;
response.Reason = "Connection approval handler threw an exception.";
}

Check warning on line 883 in com.unity.netcode.gameobjects/Runtime/Connection/NetworkConnectionManager.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

com.unity.netcode.gameobjects/Runtime/Connection/NetworkConnectionManager.cs#L878-L883

Added lines #L878 - L883 were not covered by tests
}

/// <summary>
Expand Down Expand Up @@ -1747,13 +1768,22 @@
{
//The Transport is set during initialization, thus it is possible for the Transport to be null
var transport = NetworkManager.NetworkConfig?.NetworkTransport;
if (transport != null)
if (transport == null)
{
return;

Check warning on line 1773 in com.unity.netcode.gameobjects/Runtime/Connection/NetworkConnectionManager.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

com.unity.netcode.gameobjects/Runtime/Connection/NetworkConnectionManager.cs#L1772-L1773

Added lines #L1772 - L1773 were not covered by tests
}
// if the transport throws we need to ensure we finish the shutdown sequence.
try
{
transport.Shutdown();
if (NetworkManager.LogLevel <= LogLevel.Developer)
{
NetworkLog.LogInfo($"{nameof(NetworkConnectionManager)}.{nameof(Shutdown)}() -> {nameof(IsListening)} && {nameof(NetworkManager.NetworkConfig.NetworkTransport)} != null -> {nameof(NetworkTransport)}.{nameof(NetworkTransport.Shutdown)}()");
}
}
catch (Exception ex)
{
Debug.LogException(ex);
}

Check warning on line 1783 in com.unity.netcode.gameobjects/Runtime/Connection/NetworkConnectionManager.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

com.unity.netcode.gameobjects/Runtime/Connection/NetworkConnectionManager.cs#L1780-L1783

Added lines #L1780 - L1783 were not covered by tests
if (NetworkManager.LogLevel <= LogLevel.Developer)
{
NetworkLog.LogInfo($"{nameof(NetworkConnectionManager)}.{nameof(Shutdown)}() -> {nameof(IsListening)} && {nameof(NetworkManager.NetworkConfig.NetworkTransport)} != null -> {nameof(NetworkTransport)}.{nameof(NetworkTransport.Shutdown)}()");
}
}
}
Expand Down
48 changes: 42 additions & 6 deletions com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,14 @@
MessageSize = 0
};
serverRpcMessage.ReadBuffer = tempBuffer;
serverRpcMessage.Handle(ref context);
try
{
serverRpcMessage.Handle(ref context);
}
catch (Exception e)
{
Debug.LogException(e);
}

Check warning on line 198 in com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs#L192-L198

Added lines #L192 - L198 were not covered by tests
rpcWriteSize = tempBuffer.Length;
}
else
Expand Down Expand Up @@ -321,7 +328,14 @@
MessageSize = 0
};
clientRpcMessage.ReadBuffer = tempBuffer;
clientRpcMessage.Handle(ref context);
try
{
clientRpcMessage.Handle(ref context);
}
catch (Exception e)
{
Debug.LogException(e);
}

Check warning on line 338 in com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs#L335-L338

Added lines #L335 - L338 were not covered by tests
}

bufferWriter.Dispose();
Expand Down Expand Up @@ -697,8 +711,16 @@
/// </remarks>
internal void SetIsDestroying()
{
// We intentionally invoke this before setting the IsDestroying flag.
OnIsDestroying();
try
{
// We intentionally invoke this before setting the IsDestroying flag.
OnIsDestroying();
}
catch (Exception e)
{
Debug.LogException(e);
}

Check warning on line 722 in com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs#L719-L722

Added lines #L719 - L722 were not covered by tests
// Set outside of the try-catch: a throwing override must not leave this flag false.
IsDestroying = true;
}

Expand Down Expand Up @@ -996,7 +1018,14 @@
{
UpdateNetworkVariableOnOwnershipChanged();
}
OnGainedOwnership();
try
{
OnGainedOwnership();
}
catch (Exception e)
{
Debug.LogException(e);
}

Check warning on line 1028 in com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs#L1025-L1028

Added lines #L1025 - L1028 were not covered by tests
}

/// <summary>
Expand All @@ -1013,7 +1042,14 @@

internal void InternalOnOwnershipChanged(ulong previous, ulong current)
{
OnOwnershipChanged(previous, current);
try
{
OnOwnershipChanged(previous, current);
}
catch (Exception e)
{
Debug.LogException(e);
}

Check warning on line 1052 in com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs#L1049-L1052

Added lines #L1049 - L1052 were not covered by tests
}

/// <summary>
Expand Down
Loading
Loading