Skip to content

Commit 86b7d99

Browse files
authored
chore: UTP Transport 2.0 support [MTT-4473] (#2162)
* chore: adding preprocessor define in Netcode for GameObjects, when UTP Transport is 2.0 or above * chore: adding preprocessor define in Netcode for GameObjects, when UTP Transport is 2.0 or above * chore: adding preprocessor define in Netcode for GameObjects tests, when UTP Transport is 2.0 or above * chore: conditional use of UTP 2.0 * style: coding standards * style: coding standards * Adding some fixed from PR review comment * fix: fixing tools package compilation, when used in netcode for gameobjects, with UTP 2.0 * fix: fixing tools package compilation, when used in netcode for gameobjects, with UTP 2.0 * fix: build break for UTP 2.0 builds without tools installed * fix: fixing build errors with Collection 2.1.0-exp3
1 parent a9de999 commit 86b7d99

8 files changed

Lines changed: 107 additions & 23 deletions

File tree

com.unity.netcode.gameobjects/Runtime/NetworkVariable/Collections/NetworkList.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public override void ResetDirty()
5353
if (m_DirtyEvents.Length > 0)
5454
{
5555
m_DirtyEvents.Clear();
56-
m_ListAtLastReset.CopyFrom(m_List);
56+
m_ListAtLastReset.CopyFrom(m_List.AsArray());
5757
}
5858
}
5959

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
using System;
22
using Unity.Networking.Transport;
3+
#if UTP_TRANSPORT_2_0_ABOVE
4+
using Unity.Collections;
5+
using Unity.Collections.LowLevel.Unsafe;
6+
#endif
37

48
namespace Unity.Netcode.Transports.UTP
59
{
@@ -25,7 +29,11 @@ public BatchedReceiveQueue(DataStreamReader reader)
2529
{
2630
fixed (byte* dataPtr = m_Data)
2731
{
32+
#if UTP_TRANSPORT_2_0_ABOVE
33+
reader.ReadBytesUnsafe(dataPtr, reader.Length);
34+
#else
2835
reader.ReadBytes(dataPtr, reader.Length);
36+
#endif
2937
}
3038
}
3139

@@ -62,7 +70,11 @@ public void PushReader(DataStreamReader reader)
6270
{
6371
fixed (byte* dataPtr = m_Data)
6472
{
73+
#if UTP_TRANSPORT_2_0_ABOVE
74+
reader.ReadBytesUnsafe(dataPtr + m_Offset + m_Length, reader.Length);
75+
#else
6576
reader.ReadBytes(dataPtr + m_Offset + m_Length, reader.Length);
77+
#endif
6678
}
6779
}
6880

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@ private void AppendDataAtTail(ArraySegment<byte> data)
7979

8080
fixed (byte* dataPtr = data.Array)
8181
{
82+
#if UTP_TRANSPORT_2_0_ABOVE
83+
writer.WriteBytesUnsafe(dataPtr + data.Offset, data.Count);
84+
#else
8285
writer.WriteBytes(dataPtr + data.Offset, data.Count);
86+
#endif
8387
}
8488
}
8589

@@ -149,7 +153,12 @@ public int FillWriterWithMessages(ref DataStreamWriter writer)
149153

150154
unsafe
151155
{
156+
#if UTP_TRANSPORT_2_0_ABOVE
157+
var slice = m_Data.GetSubArray(HeadIndex, Length);
158+
var reader = new DataStreamReader(slice);
159+
#else
152160
var reader = new DataStreamReader((byte*)m_Data.GetUnsafePtr() + HeadIndex, Length);
161+
#endif
153162

154163
var writerAvailable = writer.Capacity;
155164
var readerOffset = 0;
@@ -168,7 +177,11 @@ public int FillWriterWithMessages(ref DataStreamWriter writer)
168177
writer.WriteInt(messageLength);
169178

170179
var messageOffset = HeadIndex + reader.GetBytesRead();
180+
#if UTP_TRANSPORT_2_0_ABOVE
181+
writer.WriteBytesUnsafe((byte*)m_Data.GetUnsafePtr() + messageOffset, messageLength);
182+
#else
171183
writer.WriteBytes((byte*)m_Data.GetUnsafePtr() + messageOffset, messageLength);
184+
#endif
172185

173186
writerAvailable -= sizeof(int) + messageLength;
174187
readerOffset += sizeof(int) + messageLength;
@@ -205,7 +218,11 @@ public int FillWriterWithBytes(ref DataStreamWriter writer)
205218

206219
unsafe
207220
{
221+
#if UTP_TRANSPORT_2_0_ABOVE
222+
writer.WriteBytesUnsafe((byte*)m_Data.GetUnsafePtr() + HeadIndex, copyLength);
223+
#else
208224
writer.WriteBytes((byte*)m_Data.GetUnsafePtr() + HeadIndex, copyLength);
225+
#endif
209226
}
210227

211228
return copyLength;

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

Lines changed: 59 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
using Unity.Networking.Transport.Relay;
1010
using Unity.Networking.Transport.Utilities;
1111

12+
#if !UTP_TRANSPORT_2_0_ABOVE
13+
using NetworkEndpoint = Unity.Networking.Transport.NetworkEndPoint;
14+
#endif
15+
1216
namespace Unity.Netcode.Transports.UTP
1317
{
1418
/// <summary>
@@ -263,9 +267,9 @@ public struct ConnectionAddressData
263267
[SerializeField]
264268
public string ServerListenAddress;
265269

266-
private static NetworkEndPoint ParseNetworkEndpoint(string ip, ushort port)
270+
private static NetworkEndpoint ParseNetworkEndpoint(string ip, ushort port)
267271
{
268-
if (!NetworkEndPoint.TryParse(ip, port, out var endpoint))
272+
if (!NetworkEndpoint.TryParse(ip, port, out var endpoint))
269273
{
270274
Debug.LogError($"Invalid network endpoint: {ip}:{port}.");
271275
return default;
@@ -277,12 +281,12 @@ private static NetworkEndPoint ParseNetworkEndpoint(string ip, ushort port)
277281
/// <summary>
278282
/// Endpoint (IP address and port) clients will connect to.
279283
/// </summary>
280-
public NetworkEndPoint ServerEndPoint => ParseNetworkEndpoint(Address, Port);
284+
public NetworkEndpoint ServerEndPoint => ParseNetworkEndpoint(Address, Port);
281285

282286
/// <summary>
283287
/// Endpoint (IP address and port) server will listen/bind on.
284288
/// </summary>
285-
public NetworkEndPoint ListenEndPoint => ParseNetworkEndpoint((ServerListenAddress == string.Empty) ? Address : ServerListenAddress, Port);
289+
public NetworkEndpoint ListenEndPoint => ParseNetworkEndpoint((ServerListenAddress == string.Empty) ? Address : ServerListenAddress, Port);
286290
}
287291

288292
/// <summary>
@@ -425,7 +429,7 @@ private NetworkPipeline SelectSendPipeline(NetworkDelivery delivery)
425429

426430
private bool ClientBindAndConnect()
427431
{
428-
var serverEndpoint = default(NetworkEndPoint);
432+
var serverEndpoint = default(NetworkEndpoint);
429433

430434
if (m_ProtocolType == ProtocolType.RelayUnityTransport)
431435
{
@@ -446,7 +450,7 @@ private bool ClientBindAndConnect()
446450

447451
InitDriver();
448452

449-
int result = m_Driver.Bind(NetworkEndPoint.AnyIpv4);
453+
int result = m_Driver.Bind(NetworkEndpoint.AnyIpv4);
450454
if (result != 0)
451455
{
452456
Debug.LogError("Client failed to bind");
@@ -459,7 +463,7 @@ private bool ClientBindAndConnect()
459463
return true;
460464
}
461465

462-
private bool ServerBindAndListen(NetworkEndPoint endPoint)
466+
private bool ServerBindAndListen(NetworkEndpoint endPoint)
463467
{
464468
InitDriver();
465469

@@ -536,7 +540,7 @@ public void SetRelayServerData(string ipv4Address, ushort port, byte[] allocatio
536540
{
537541
RelayConnectionData hostConnectionData;
538542

539-
if (!NetworkEndPoint.TryParse(ipv4Address, port, out var serverEndpoint))
543+
if (!NetworkEndpoint.TryParse(ipv4Address, port, out var serverEndpoint))
540544
{
541545
Debug.LogError($"Invalid address {ipv4Address}:{port}");
542546

@@ -613,7 +617,7 @@ public void SetConnectionData(string ipv4Address, ushort port, string listenAddr
613617
/// </summary>
614618
/// <param name="endPoint">The remote end point</param>
615619
/// <param name="listenEndPoint">The local listen endpoint</param>
616-
public void SetConnectionData(NetworkEndPoint endPoint, NetworkEndPoint listenEndPoint = default)
620+
public void SetConnectionData(NetworkEndpoint endPoint, NetworkEndpoint listenEndPoint = default)
617621
{
618622
string serverAddress = endPoint.Address.Split(':')[0];
619623

@@ -662,7 +666,7 @@ private bool StartRelayServer()
662666
else
663667
{
664668
m_NetworkSettings.WithRelayParameters(ref m_RelayServerData, m_HeartbeatTimeoutMS);
665-
return ServerBindAndListen(NetworkEndPoint.AnyIpv4);
669+
return ServerBindAndListen(NetworkEndpoint.AnyIpv4);
666670
}
667671
}
668672

@@ -907,7 +911,11 @@ private void ExtractNetworkMetricsFromPipeline(NetworkPipeline pipeline, Network
907911
{
908912
//Don't need to dispose of the buffers, they are filled with data pointers.
909913
m_Driver.GetPipelineBuffers(pipeline,
914+
#if UTP_TRANSPORT_2_0_ABOVE
915+
NetworkPipelineStageId.Get<NetworkMetricsPipelineStage>(),
916+
#else
910917
NetworkPipelineStageCollection.GetStageId(typeof(NetworkMetricsPipelineStage)),
918+
#endif
911919
networkConnection,
912920
out _,
913921
out _,
@@ -934,7 +942,11 @@ private int ExtractRtt(NetworkConnection networkConnection)
934942
}
935943

936944
m_Driver.GetPipelineBuffers(m_ReliableSequencedPipeline,
945+
#if UTP_TRANSPORT_2_0_ABOVE
946+
NetworkPipelineStageId.Get<ReliableSequencedPipelineStage>(),
947+
#else
937948
NetworkPipelineStageCollection.GetStageId(typeof(ReliableSequencedPipelineStage)),
949+
#endif
938950
networkConnection,
939951
out _,
940952
out _,
@@ -956,7 +968,11 @@ private float ExtractPacketLoss(NetworkConnection networkConnection)
956968
}
957969

958970
m_Driver.GetPipelineBuffers(m_ReliableSequencedPipeline,
971+
#if UTP_TRANSPORT_2_0_ABOVE
972+
NetworkPipelineStageId.Get<ReliableSequencedPipelineStage>(),
973+
#else
959974
NetworkPipelineStageCollection.GetStageId(typeof(ReliableSequencedPipelineStage)),
975+
#endif
960976
networkConnection,
961977
out _,
962978
out _,
@@ -1117,11 +1133,12 @@ public override void Initialize(NetworkManager networkManager = null)
11171133
// account for the overhead of its length when we store it in the send queue.
11181134
var fragmentationCapacity = m_MaxPayloadSize + BatchedSendQueue.PerMessageOverhead;
11191135

1120-
m_NetworkSettings
1121-
.WithFragmentationStageParameters(payloadCapacity: fragmentationCapacity)
1122-
.WithBaselibNetworkInterfaceParameters(
1123-
receiveQueueCapacity: m_MaxPacketQueueSize,
1124-
sendQueueCapacity: m_MaxPacketQueueSize);
1136+
m_NetworkSettings.WithFragmentationStageParameters(payloadCapacity: fragmentationCapacity);
1137+
#if !UTP_TRANSPORT_2_0_ABOVE
1138+
m_NetworkSettings.WithBaselibNetworkInterfaceParameters(
1139+
receiveQueueCapacity: m_MaxPacketQueueSize,
1140+
sendQueueCapacity: m_MaxPacketQueueSize);
1141+
#endif
11251142
#endif
11261143
}
11271144

@@ -1306,6 +1323,9 @@ private void ConfigureSimulator()
13061323
packetDelayMs: DebugSimulator.PacketDelayMS,
13071324
packetJitterMs: DebugSimulator.PacketJitterMS,
13081325
packetDropPercentage: DebugSimulator.PacketDropRate
1326+
#if UTP_TRANSPORT_2_0_ABOVE
1327+
, mode: ApplyMode.AllPackets
1328+
#endif
13091329
);
13101330
}
13111331

@@ -1323,8 +1343,10 @@ public void CreateDriver(UnityTransport transport, out NetworkDriver driver,
13231343
out NetworkPipeline reliableSequencedPipeline)
13241344
{
13251345
#if MULTIPLAYER_TOOLS_1_0_0_PRE_7
1346+
#if !UTP_TRANSPORT_2_0_ABOVE
13261347
NetworkPipelineStageCollection.RegisterPipelineStage(new NetworkMetricsPipelineStage());
13271348
#endif
1349+
#endif
13281350

13291351
#if UNITY_EDITOR || DEVELOPMENT_BUILD
13301352
ConfigureSimulator();
@@ -1334,34 +1356,50 @@ public void CreateDriver(UnityTransport transport, out NetworkDriver driver,
13341356
maxConnectAttempts: transport.m_MaxConnectAttempts,
13351357
connectTimeoutMS: transport.m_ConnectTimeoutMS,
13361358
disconnectTimeoutMS: transport.m_DisconnectTimeoutMS,
1359+
#if UTP_TRANSPORT_2_0_ABOVE
1360+
sendQueueCapacity: m_MaxPacketQueueSize,
1361+
receiveQueueCapacity: m_MaxPacketQueueSize,
1362+
#endif
13371363
heartbeatTimeoutMS: transport.m_HeartbeatTimeoutMS);
13381364

13391365
driver = NetworkDriver.Create(m_NetworkSettings);
13401366

1367+
#if MULTIPLAYER_TOOLS_1_0_0_PRE_7
1368+
#if UTP_TRANSPORT_2_0_ABOVE
1369+
driver.RegisterPipelineStage<NetworkMetricsPipelineStage>(new NetworkMetricsPipelineStage());
1370+
#endif
1371+
#endif
1372+
13411373
#if UNITY_EDITOR || DEVELOPMENT_BUILD
13421374
if (DebugSimulator.PacketDelayMS > 0 || DebugSimulator.PacketDropRate > 0)
13431375
{
13441376
unreliableFragmentedPipeline = driver.CreatePipeline(
13451377
typeof(FragmentationPipelineStage),
1346-
typeof(SimulatorPipelineStage),
1347-
typeof(SimulatorPipelineStageInSend)
1378+
typeof(SimulatorPipelineStage)
1379+
#if !UTP_TRANSPORT_2_0_ABOVE
1380+
, typeof(SimulatorPipelineStageInSend)
1381+
#endif
13481382
#if MULTIPLAYER_TOOLS_1_0_0_PRE_7
13491383
, typeof(NetworkMetricsPipelineStage)
13501384
#endif
13511385
);
13521386
unreliableSequencedFragmentedPipeline = driver.CreatePipeline(
13531387
typeof(FragmentationPipelineStage),
13541388
typeof(UnreliableSequencedPipelineStage),
1355-
typeof(SimulatorPipelineStage),
1356-
typeof(SimulatorPipelineStageInSend)
1389+
typeof(SimulatorPipelineStage)
1390+
#if !UTP_TRANSPORT_2_0_ABOVE
1391+
, typeof(SimulatorPipelineStageInSend)
1392+
#endif
13571393
#if MULTIPLAYER_TOOLS_1_0_0_PRE_7
13581394
,typeof(NetworkMetricsPipelineStage)
13591395
#endif
13601396
);
13611397
reliableSequencedPipeline = driver.CreatePipeline(
13621398
typeof(ReliableSequencedPipelineStage),
1363-
typeof(SimulatorPipelineStage),
1364-
typeof(SimulatorPipelineStageInSend)
1399+
typeof(SimulatorPipelineStage)
1400+
#if !UTP_TRANSPORT_2_0_ABOVE
1401+
, typeof(SimulatorPipelineStageInSend)
1402+
#endif
13651403
#if MULTIPLAYER_TOOLS_1_0_0_PRE_7
13661404
,typeof(NetworkMetricsPipelineStage)
13671405
#endif

com.unity.netcode.gameobjects/Runtime/com.unity.netcode.runtime.asmdef

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
"name": "com.unity.multiplayer.tools",
3131
"expression": "1.0.0-pre.7",
3232
"define": "MULTIPLAYER_TOOLS_1_0_0_PRE_7"
33+
},
34+
{
35+
"name": "com.unity.transport",
36+
"expression": "2.0.0-exp",
37+
"define": "UTP_TRANSPORT_2_0_ABOVE"
3338
}
3439
]
3540
}

com.unity.netcode.gameobjects/Tests/Runtime/Metrics/PacketLossMetricsTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ public IEnumerator TrackPacketLossAsServer()
5555
}
5656

5757
[UnityTest]
58+
#if UTP_TRANSPORT_2_0_ABOVE
59+
[Ignore("Pending adjustment for UTP 2.0")]
60+
#endif
5861
public IEnumerator TrackPacketLossAsClient()
5962
{
6063
double packetLossRateMinRange = (m_PacketLossRate-m_PacketLossRangeDelta) / 100d;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ private void OnDestroy()
6565

6666
public void Connect()
6767
{
68+
#if UTP_TRANSPORT_2_0_ABOVE
69+
var endpoint = NetworkEndpoint.LoopbackIpv4;
70+
#else
6871
var endpoint = NetworkEndPoint.LoopbackIpv4;
72+
#endif
6973
endpoint.Port = 7777;
7074

7175
m_Connection = m_Driver.Connect(endpoint);

com.unity.netcode.gameobjects/Tests/Runtime/com.unity.netcode.runtimetests.asmdef

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
"name": "com.unity.modules.physics",
4040
"expression": "",
4141
"define": "COM_UNITY_MODULES_PHYSICS"
42+
},
43+
{
44+
"name": "com.unity.transport",
45+
"expression": "2.0.0-exp",
46+
"define": "UTP_TRANSPORT_2_0_ABOVE"
4247
}
4348
]
44-
}
49+
}

0 commit comments

Comments
 (0)