Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions com.unity.netcode.gameobjects/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
### Added

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

### Fixed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,9 @@ public struct ConnectionAddressData
public ushort Port;

/// <summary>
/// IP address the server will listen on. If not provided, will use 'Address'.
/// IP address the server will listen on. If not provided, will use 0.0.0.0.
/// </summary>
[Tooltip("IP address the server will listen on. If not provided, will use 'Address'.")]
[Tooltip("IP address the server will listen on. If not provided, will use 0.0.0.0.")]
[SerializeField]
public string ServerListenAddress;

Expand All @@ -330,7 +330,29 @@ private static NetworkEndpoint ParseNetworkEndpoint(string ip, ushort port)
/// <summary>
/// Endpoint (IP address and port) server will listen/bind on.
/// </summary>
public NetworkEndpoint ListenEndPoint => ParseNetworkEndpoint((ServerListenAddress?.Length == 0) ? Address : ServerListenAddress, Port);
public NetworkEndpoint ListenEndPoint
{
get
{
if (string.IsNullOrEmpty(ServerListenAddress))
{
var ep = NetworkEndpoint.AnyIpv4;

// If an address was entered and it's IPv6, switch to using :: as the
// default listen address. (Otherwise we always assume IPv4.)
if (!string.IsNullOrEmpty(Address) && ServerEndPoint.Family == NetworkFamily.Ipv6)
{
ep = NetworkEndpoint.AnyIpv6;
}

return ep.WithPort(Port);
}
else
{
return ParseNetworkEndpoint(ServerListenAddress, Port);
}
}
}
}

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

result = m_Driver.Listen();
if (result != 0)
{
Debug.LogError("Server failed to listen");
Debug.LogError("Server failed to listen.");
return false;
}

Expand Down Expand Up @@ -1473,7 +1495,7 @@ public void CreateDriver(UnityTransport transport, out NetworkDriver driver,
{
if (NetworkManager.IsServer)
{
if (String.IsNullOrEmpty(m_ServerCertificate) || String.IsNullOrEmpty(m_ServerPrivateKey))
if (string.IsNullOrEmpty(m_ServerCertificate) || string.IsNullOrEmpty(m_ServerPrivateKey))
{
throw new Exception("In order to use encrypted communications, when hosting, you must set the server certificate and key.");
}
Expand All @@ -1482,11 +1504,11 @@ public void CreateDriver(UnityTransport transport, out NetworkDriver driver,
}
else
{
if (String.IsNullOrEmpty(m_ServerCommonName))
if (string.IsNullOrEmpty(m_ServerCommonName))
{
throw new Exception("In order to use encrypted communications, clients must set the server common name.");
}
else if (String.IsNullOrEmpty(m_ClientCaCertificate))
else if (string.IsNullOrEmpty(m_ClientCaCertificate))
{
m_NetworkSettings.WithSecureClientParameters(m_ServerCommonName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,26 @@ public void UnityTransport_RestartSucceedsAfterFailure()
UnityTransport transport = new GameObject().AddComponent<UnityTransport>();
transport.Initialize();

transport.SetConnectionData("127.0.0.", 4242);
transport.SetConnectionData("127.0.0.", 4242, "127.0.0.");
Assert.False(transport.StartServer());

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

transport.SetConnectionData("127.0.0.1", 4242);
transport.SetConnectionData("127.0.0.1", 4242, "127.0.0.1");
Assert.True(transport.StartServer());

transport.Shutdown();
}

// Check that leaving all addresses empty is valid.
[Test]
public void UnityTransport_StartServerWithoutAddresses()
{
UnityTransport transport = new GameObject().AddComponent<UnityTransport>();
transport.Initialize();

transport.SetConnectionData(string.Empty, 4242);
Assert.True(transport.StartServer());

transport.Shutdown();
Expand Down