Skip to content

Commit f4a6034

Browse files
fix: Don't accept invalid connections in UnityTransport.Send [1.X]
1 parent 9728a78 commit f4a6034

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-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.
1920
- Fixed initial `NetworkTransform` spawn, ensure it uses world space. (#3361)
2021
- Fixed issue where `AnticipatedNetworkVariable` previous value returned by `AnticipatedNetworkVariable.OnAuthoritativeValueChanged` is updated correctly on the non-authoritative side. (#3322)
2122

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(m_Client1.ServerClientId, data, NetworkDelivery.Reliable);
571+
572+
yield return EnsureNoNetworkEvent(m_Client1Events);
573+
}
557574
}
558575
}

0 commit comments

Comments
 (0)