Skip to content

Commit 993c0f8

Browse files
Merge branch 'develop' into fix/findobjectsoftype-to-findobjectsbytype
2 parents 1ab0302 + d48c373 commit 993c0f8

3 files changed

Lines changed: 47 additions & 11 deletions

File tree

com.unity.netcode.gameobjects/CHANGELOG.md

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

1313
### Changed
14+
- The default listen address of `UnityTransport` is now 0.0.0.0. (#2307)
1415

1516
### Fixed
1617

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

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,9 @@ public struct ConnectionAddressData
303303
public ushort Port;
304304

305305
/// <summary>
306-
/// IP address the server will listen on. If not provided, will use 'Address'.
306+
/// IP address the server will listen on. If not provided, will use 0.0.0.0.
307307
/// </summary>
308-
[Tooltip("IP address the server will listen on. If not provided, will use 'Address'.")]
308+
[Tooltip("IP address the server will listen on. If not provided, will use 0.0.0.0.")]
309309
[SerializeField]
310310
public string ServerListenAddress;
311311

@@ -330,7 +330,29 @@ private static NetworkEndpoint ParseNetworkEndpoint(string ip, ushort port)
330330
/// <summary>
331331
/// Endpoint (IP address and port) server will listen/bind on.
332332
/// </summary>
333-
public NetworkEndpoint ListenEndPoint => ParseNetworkEndpoint((ServerListenAddress?.Length == 0) ? Address : ServerListenAddress, Port);
333+
public NetworkEndpoint ListenEndPoint
334+
{
335+
get
336+
{
337+
if (string.IsNullOrEmpty(ServerListenAddress))
338+
{
339+
var ep = NetworkEndpoint.AnyIpv4;
340+
341+
// If an address was entered and it's IPv6, switch to using :: as the
342+
// default listen address. (Otherwise we always assume IPv4.)
343+
if (!string.IsNullOrEmpty(Address) && ServerEndPoint.Family == NetworkFamily.Ipv6)
344+
{
345+
ep = NetworkEndpoint.AnyIpv6;
346+
}
347+
348+
return ep.WithPort(Port);
349+
}
350+
else
351+
{
352+
return ParseNetworkEndpoint(ServerListenAddress, Port);
353+
}
354+
}
355+
}
334356
}
335357

336358
/// <summary>
@@ -529,14 +551,14 @@ private bool ServerBindAndListen(NetworkEndpoint endPoint)
529551
int result = m_Driver.Bind(endPoint);
530552
if (result != 0)
531553
{
532-
Debug.LogError("Server failed to bind");
554+
Debug.LogError("Server failed to bind. This is usually caused by another process being bound to the same port.");
533555
return false;
534556
}
535557

536558
result = m_Driver.Listen();
537559
if (result != 0)
538560
{
539-
Debug.LogError("Server failed to listen");
561+
Debug.LogError("Server failed to listen.");
540562
return false;
541563
}
542564

@@ -1473,7 +1495,7 @@ public void CreateDriver(UnityTransport transport, out NetworkDriver driver,
14731495
{
14741496
if (NetworkManager.IsServer)
14751497
{
1476-
if (String.IsNullOrEmpty(m_ServerCertificate) || String.IsNullOrEmpty(m_ServerPrivateKey))
1498+
if (string.IsNullOrEmpty(m_ServerCertificate) || string.IsNullOrEmpty(m_ServerPrivateKey))
14771499
{
14781500
throw new Exception("In order to use encrypted communications, when hosting, you must set the server certificate and key.");
14791501
}
@@ -1482,11 +1504,11 @@ public void CreateDriver(UnityTransport transport, out NetworkDriver driver,
14821504
}
14831505
else
14841506
{
1485-
if (String.IsNullOrEmpty(m_ServerCommonName))
1507+
if (string.IsNullOrEmpty(m_ServerCommonName))
14861508
{
14871509
throw new Exception("In order to use encrypted communications, clients must set the server common name.");
14881510
}
1489-
else if (String.IsNullOrEmpty(m_ClientCaCertificate))
1511+
else if (string.IsNullOrEmpty(m_ClientCaCertificate))
14901512
{
14911513
m_NetworkSettings.WithSecureClientParameters(m_ServerCommonName);
14921514
}

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,26 @@ public void UnityTransport_RestartSucceedsAfterFailure()
115115
UnityTransport transport = new GameObject().AddComponent<UnityTransport>();
116116
transport.Initialize();
117117

118-
transport.SetConnectionData("127.0.0.", 4242);
118+
transport.SetConnectionData("127.0.0.", 4242, "127.0.0.");
119119
Assert.False(transport.StartServer());
120120

121121
LogAssert.Expect(LogType.Error, "Invalid network endpoint: 127.0.0.:4242.");
122-
LogAssert.Expect(LogType.Error, "Server failed to bind");
122+
LogAssert.Expect(LogType.Error, "Server failed to bind. This is usually caused by another process being bound to the same port.");
123123

124-
transport.SetConnectionData("127.0.0.1", 4242);
124+
transport.SetConnectionData("127.0.0.1", 4242, "127.0.0.1");
125+
Assert.True(transport.StartServer());
126+
127+
transport.Shutdown();
128+
}
129+
130+
// Check that leaving all addresses empty is valid.
131+
[Test]
132+
public void UnityTransport_StartServerWithoutAddresses()
133+
{
134+
UnityTransport transport = new GameObject().AddComponent<UnityTransport>();
135+
transport.Initialize();
136+
137+
transport.SetConnectionData(string.Empty, 4242);
125138
Assert.True(transport.StartServer());
126139

127140
transport.Shutdown();

0 commit comments

Comments
 (0)