Skip to content

Commit 9738a6e

Browse files
fix: Don't accept invalid connections in UnityTransport.Send [1.X] (Unity-Technologies#3383)
Backport of PR Unity-Technologies#3382. ## Changelog - Fixed: Fixed an issue in `UnityTransport` where the transport would accept sends on invalid connections, leading to a useless memory allocation and confusing error message. ## Testing and Documentation - Includes integration test. - No documentation changes or additions were necessary. --------- Co-authored-by: michalChrobot <[email protected]> Co-authored-by: Michał Chrobot <[email protected]>
1 parent 56a2f20 commit 9738a6e

File tree

5 files changed

+43
-1
lines changed

5 files changed

+43
-1
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
1616

1717
### Fixed
1818

19+
- Fixed an issue in `UnityTransport` where the transport would accept sends on invalid connections, leading to a useless memory allocation and confusing error message. (#3383)
1920
- Fixed issue where `NetworkAnimator` would log an error if there was no destination transition information. (#3384)
2021
- Fixed initial `NetworkTransform` spawn, ensure it uses world space. (#3361)
2122
- Fixed issue where `AnticipatedNetworkVariable` previous value returned by `AnticipatedNetworkVariable.OnAuthoritativeValueChanged` is updated correctly on the non-authoritative side. (#3322)

com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1342,8 +1342,13 @@ public override NetcodeNetworkEvent PollEvent(out ulong clientId, out ArraySegme
13421342
/// <param name="networkDelivery">The delivery type (QoS) to send data with</param>
13431343
public override void Send(ulong clientId, ArraySegment<byte> payload, NetworkDelivery networkDelivery)
13441344
{
1345-
var pipeline = SelectSendPipeline(networkDelivery);
1345+
var connection = ParseClientId(clientId);
1346+
if (!m_Driver.IsCreated || m_Driver.GetConnectionState(connection) != NetworkConnection.State.Connected)
1347+
{
1348+
return;
1349+
}
13461350

1351+
var pipeline = SelectSendPipeline(networkDelivery);
13471352
if (pipeline != m_ReliableSequencedPipeline && payload.Count > m_MaxPayloadSize)
13481353
{
13491354
Debug.LogError($"Unreliable payload of size {payload.Count} larger than configured 'Max Payload Size' ({m_MaxPayloadSize}).");

com.unity.netcode.gameobjects/Tests/Runtime/Transports/UnityTransportTestHelpers.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,23 @@ public static IEnumerator WaitForNetworkEvent(NetworkEvent type, List<TransportE
3636
Assert.Fail("Timed out while waiting for network event.");
3737
}
3838

39+
// Wait to ensure no event is sent.
40+
public static IEnumerator EnsureNoNetworkEvent(List<TransportEvent> events, float timeout = MaxNetworkEventWaitTime)
41+
{
42+
int initialCount = events.Count;
43+
float startTime = Time.realtimeSinceStartup;
44+
45+
while (Time.realtimeSinceStartup - startTime < timeout)
46+
{
47+
if (events.Count > initialCount)
48+
{
49+
Assert.Fail("Received unexpected network event.");
50+
}
51+
52+
yield return new WaitForSeconds(0.01f);
53+
}
54+
}
55+
3956
// Common code to initialize a UnityTransport that logs its events.
4057
public static void InitializeTransport(out UnityTransport transport, out List<TransportEvent> events,
4158
int maxPayloadSize = UnityTransport.InitialMaxPayloadSize, int maxSendQueueSize = 0, NetworkFamily family = NetworkFamily.Ipv4)

com.unity.netcode.gameobjects/Tests/Runtime/Transports/UnityTransportTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,5 +554,22 @@ public IEnumerator DoesNotActAfterShutdown([Values] AfterShutdownAction afterShu
554554
m_Server.DisconnectLocalClient();
555555
}
556556
}
557+
558+
[UnityTest]
559+
public IEnumerator DoesNotAttemptToSendOnInvalidConnections()
560+
{
561+
InitializeTransport(out m_Server, out m_ServerEvents);
562+
InitializeTransport(out m_Client1, out m_Client1Events);
563+
564+
m_Server.StartServer();
565+
m_Client1.StartClient();
566+
567+
yield return WaitForNetworkEvent(NetworkEvent.Connect, m_Client1Events);
568+
569+
var data = new ArraySegment<byte>(new byte[42]);
570+
m_Server.Send(0, data, NetworkDelivery.Reliable);
571+
572+
yield return EnsureNoNetworkEvent(m_Client1Events);
573+
}
557574
}
558575
}

pvpExceptions.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2591,6 +2591,7 @@
25912591
"Unity.Netcode.RuntimeTests.UnityTransportTestHelpers: undocumented",
25922592
"Unity.Netcode.RuntimeTests.UnityTransportTestHelpers: MaxNetworkEventWaitTime: undocumented",
25932593
"Unity.Netcode.RuntimeTests.UnityTransportTestHelpers: IEnumerator WaitForNetworkEvent(NetworkEvent, List<TransportEvent>, float): undocumented",
2594+
"Unity.Netcode.RuntimeTests.UnityTransportTestHelpers: IEnumerator EnsureNoNetworkEvent(List<TransportEvent>, float): undocumented",
25942595
"Unity.Netcode.RuntimeTests.UnityTransportTestHelpers: void InitializeTransport(out UnityTransport, out List<TransportEvent>, int, int, NetworkFamily): undocumented",
25952596
"Unity.Netcode.RuntimeTests.UnityTransportTestHelpers.TransportEvent: undocumented",
25962597
"Unity.Netcode.RuntimeTests.UnityTransportTestHelpers.TransportEvent: Type: undocumented",
@@ -2617,6 +2618,7 @@
26172618
"Unity.Netcode.RuntimeTests.UnityTransportTests: IEnumerator SendQueuesFlushedOnRemoteClientDisconnect(NetworkDelivery): undocumented",
26182619
"Unity.Netcode.RuntimeTests.UnityTransportTests: IEnumerator ReliablePayloadsCanBeLargerThanMaximum(): undocumented",
26192620
"Unity.Netcode.RuntimeTests.UnityTransportTests: IEnumerator DoesNotActAfterShutdown(AfterShutdownAction): undocumented",
2621+
"Unity.Netcode.RuntimeTests.UnityTransportTests: IEnumerator DoesNotAttemptToSendOnInvalidConnections(): undocumented",
26202622
"Unity.Netcode.RuntimeTests.UnityTransportTests.AfterShutdownAction: undocumented",
26212623
"Unity.Netcode.RuntimeTests.UnityTransportTests.AfterShutdownAction: Send: undocumented",
26222624
"Unity.Netcode.RuntimeTests.UnityTransportTests.AfterShutdownAction: DisconnectRemoteClient: undocumented",

0 commit comments

Comments
 (0)