Skip to content

Commit 374cb49

Browse files
refactor: Simplify construction of RelayServerData (#2237)
1 parent e9d9454 commit 374cb49

File tree

1 file changed

+5
-63
lines changed

1 file changed

+5
-63
lines changed

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

Lines changed: 5 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -544,46 +544,13 @@ private bool ServerBindAndListen(NetworkEndpoint endPoint)
544544
return true;
545545
}
546546

547-
private static RelayAllocationId ConvertFromAllocationIdBytes(byte[] allocationIdBytes)
548-
{
549-
unsafe
550-
{
551-
fixed (byte* ptr = allocationIdBytes)
552-
{
553-
return RelayAllocationId.FromBytePointer(ptr, allocationIdBytes.Length);
554-
}
555-
}
556-
}
557-
558-
private static RelayHMACKey ConvertFromHMAC(byte[] hmac)
559-
{
560-
unsafe
561-
{
562-
fixed (byte* ptr = hmac)
563-
{
564-
return RelayHMACKey.FromBytePointer(ptr, RelayHMACKey.k_Length);
565-
}
566-
}
567-
}
568-
569-
private static RelayConnectionData ConvertConnectionData(byte[] connectionData)
570-
{
571-
unsafe
572-
{
573-
fixed (byte* ptr = connectionData)
574-
{
575-
return RelayConnectionData.FromBytePointer(ptr, RelayConnectionData.k_Length);
576-
}
577-
}
578-
}
579-
580547
private void SetProtocol(ProtocolType inProtocol)
581548
{
582549
m_ProtocolType = inProtocol;
583550
}
584551

585552
/// <summary>Set the relay server data for the server.</summary>
586-
/// <param name="ipv4Address">IP address of the relay server.</param>
553+
/// <param name="ipv4Address">IP address or hostname of the relay server.</param>
587554
/// <param name="port">UDP port of the relay server.</param>
588555
/// <param name="allocationIdBytes">Allocation ID as a byte array.</param>
589556
/// <param name="keyBytes">Allocation key as a byte array.</param>
@@ -592,33 +559,8 @@ private void SetProtocol(ProtocolType inProtocol)
592559
/// <param name="isSecure">Whether the connection is secure (uses DTLS).</param>
593560
public void SetRelayServerData(string ipv4Address, ushort port, byte[] allocationIdBytes, byte[] keyBytes, byte[] connectionDataBytes, byte[] hostConnectionDataBytes = null, bool isSecure = false)
594561
{
595-
RelayConnectionData hostConnectionData;
596-
597-
if (!NetworkEndpoint.TryParse(ipv4Address, port, out var serverEndpoint))
598-
{
599-
Debug.LogError($"Invalid address {ipv4Address}:{port}");
600-
601-
// We set this to default to cause other checks to fail to state you need to call this
602-
// function again.
603-
m_RelayServerData = default;
604-
return;
605-
}
606-
607-
var allocationId = ConvertFromAllocationIdBytes(allocationIdBytes);
608-
var key = ConvertFromHMAC(keyBytes);
609-
var connectionData = ConvertConnectionData(connectionDataBytes);
610-
611-
if (hostConnectionDataBytes != null)
612-
{
613-
hostConnectionData = ConvertConnectionData(hostConnectionDataBytes);
614-
}
615-
else
616-
{
617-
hostConnectionData = connectionData;
618-
}
619-
620-
m_RelayServerData = new RelayServerData(ref serverEndpoint, 0, ref allocationId, ref connectionData, ref hostConnectionData, ref key, isSecure);
621-
562+
var hostConnectionData = hostConnectionDataBytes ?? connectionDataBytes;
563+
m_RelayServerData = new RelayServerData(ipv4Address, port, allocationIdBytes, connectionDataBytes, hostConnectionData, keyBytes, isSecure);
622564
SetProtocol(ProtocolType.RelayUnityTransport);
623565
}
624566

@@ -631,7 +573,7 @@ public void SetRelayServerData(RelayServerData serverData)
631573
}
632574

633575
/// <summary>Set the relay server data for the host.</summary>
634-
/// <param name="ipAddress">IP address of the relay server.</param>
576+
/// <param name="ipAddress">IP address or hostname of the relay server.</param>
635577
/// <param name="port">UDP port of the relay server.</param>
636578
/// <param name="allocationId">Allocation ID as a byte array.</param>
637579
/// <param name="key">Allocation key as a byte array.</param>
@@ -643,7 +585,7 @@ public void SetHostRelayData(string ipAddress, ushort port, byte[] allocationId,
643585
}
644586

645587
/// <summary>Set the relay server data for the host.</summary>
646-
/// <param name="ipAddress">IP address of the relay server.</param>
588+
/// <param name="ipAddress">IP address or hostname of the relay server.</param>
647589
/// <param name="port">UDP port of the relay server.</param>
648590
/// <param name="allocationId">Allocation ID as a byte array.</param>
649591
/// <param name="key">Allocation key as a byte array.</param>

0 commit comments

Comments
 (0)