Skip to content

Commit 139b3b9

Browse files
committed
Add support for connecting via a hostname instead of IP when using supported versions of unity and unity transport.
1 parent edf0256 commit 139b3b9

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -317,10 +317,7 @@ private static NetworkEndpoint ParseNetworkEndpoint(string ip, ushort port, bool
317317
if (!NetworkEndpoint.TryParse(ip, port, out endpoint, NetworkFamily.Ipv4) &&
318318
!NetworkEndpoint.TryParse(ip, port, out endpoint, NetworkFamily.Ipv6))
319319
{
320-
if (!silent)
321-
{
322-
Debug.LogError($"Invalid network endpoint: {ip}:{port}.");
323-
}
320+
return default;
324321
}
325322

326323
return endpoint;
@@ -539,11 +536,21 @@ private bool ClientBindAndConnect()
539536
serverEndpoint = ConnectionData.ServerEndPoint;
540537
}
541538

539+
NetworkConnection serverConnection;
540+
542541
// Verify the endpoint is valid before proceeding
543542
if (serverEndpoint.Family == NetworkFamily.Invalid)
544543
{
544+
#if HOSTNAME_RESOLUTION_AVAILABLE && UTP_TRANSPORT_2_4_ABOVE
545+
// If it's not valid, try to treat it like a URL.
546+
InitDriver();
547+
serverConnection = m_Driver.Connect(ConnectionData.Address, ConnectionData.Port);
548+
m_ServerClientId = ParseClientId(serverConnection);
549+
return true;
550+
#else
545551
Debug.LogError($"Target server network address ({ConnectionData.Address}) is {nameof(NetworkFamily.Invalid)}!");
546552
return false;
553+
#endif
547554
}
548555

549556
InitDriver();
@@ -556,7 +563,7 @@ private bool ClientBindAndConnect()
556563
return false;
557564
}
558565

559-
var serverConnection = m_Driver.Connect(serverEndpoint);
566+
serverConnection = m_Driver.Connect(serverEndpoint);
560567
m_ServerClientId = ParseClientId(serverConnection);
561568

562569
return true;
@@ -647,7 +654,7 @@ public void SetClientRelayData(string ipAddress, ushort port, byte[] allocationI
647654
/// <summary>
648655
/// Sets IP and Port information. This will be ignored if using the Unity Relay and you should call <see cref="SetRelayServerData"/>
649656
/// </summary>
650-
/// <param name="ipv4Address">The remote IP address (despite the name, can be an IPv6 address)</param>
657+
/// <param name="ipv4Address">The remote IP address (despite the name, can be an IPv6 address or a domain name)</param>
651658
/// <param name="port">The remote port</param>
652659
/// <param name="listenAddress">The local listen address</param>
653660
public void SetConnectionData(string ipv4Address, ushort port, string listenAddress = null)

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,20 @@
4242
"expression": "2.1.0",
4343
"define": "UTP_TRANSPORT_2_1_ABOVE"
4444
},
45+
{
46+
"name": "com.unity.transport",
47+
"expression": "2.4.0",
48+
"define": "UTP_TRANSPORT_2_4_ABOVE"
49+
},
4550
{
4651
"name": "Unity",
4752
"expression": "2023",
4853
"define": "UNITY_DEDICATED_SERVER_ARGUMENTS_PRESENT"
54+
},
55+
{
56+
"name": "Unity",
57+
"expression": "6000.1.0a1",
58+
"define": "HOSTNAME_RESOLUTION_AVAILABLE"
4959
}
5060
]
5161
}

0 commit comments

Comments
 (0)