Skip to content

feat: Allow specifying a different listen address in the adapter (1.0.0) #1607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 26, 2022
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
2 changes: 2 additions & 0 deletions com.unity.netcode.adapter.utp/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ All notable changes to this package will be documented in this file. The format

### Added

- A new 'Server Listen Address' field under 'Connection Data' in the inspector has been added to specify the address a server should listen to, in case it differs from the main 'Address' field. The `SetConnectionData` method has been updated accordingly to take an optional parameter to specify that listen address. (#1605)
- Added new methods to set the relay server data: `SetHostRelayData` and `SetClientRelayData`. These are meant to be less error-prone than `SetRelayServerData` (which remains available). (#1609)

### Changed

- Rename the 'Send Queue Batch Size' property to 'Max Payload Size' to better reflect its usage. (#1584)
- Implicit conversions between `ConnectionAddressData` and `NetworkEndPoint` are now deprecated, since their semantics are no longer clear with the introduction of the new `ServerListenAddress` field (see above). (#1605)
- Updated Unity Transport package to 1.0.0-pre.12. (#1615)

### Fixed
Expand Down
65 changes: 45 additions & 20 deletions com.unity.netcode.adapter.utp/Runtime/UnityTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private enum State
public const int InitialMaxSendQueueSize = 16 * InitialMaxPayloadSize;

private static ConnectionAddressData s_DefaultConnectionAddressData = new ConnectionAddressData()
{ Address = "127.0.0.1", Port = 7777 };
{ Address = "127.0.0.1", Port = 7777, ServerListenAddress = null };

#pragma warning disable IDE1006 // Naming Styles
public static INetworkStreamDriverConstructor s_DriverConstructor;
Expand Down Expand Up @@ -131,22 +131,37 @@ private enum State
[Serializable]
public struct ConnectionAddressData
{
[Tooltip("IP address of the server (address to which clients will connect to).")]
[SerializeField] public string Address;
[SerializeField] public int Port;

public static implicit operator NetworkEndPoint(ConnectionAddressData d)
[Tooltip("UDP port of the server.")]
[SerializeField] public ushort Port;

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

private static NetworkEndPoint ParseNetworkEndpoint(string ip, ushort port)
{
if (!NetworkEndPoint.TryParse(d.Address, (ushort)d.Port, out var networkEndPoint))
if (!NetworkEndPoint.TryParse(ip, port, out var endpoint))
{
Debug.LogError($"Invalid address {d.Address}:{d.Port}");
Debug.LogError($"Invalid network endpoint: {ip}:{port}.");
return default;
}

return networkEndPoint;
return endpoint;
}

public NetworkEndPoint ServerEndPoint => ParseNetworkEndpoint(Address, Port);

public NetworkEndPoint ListenEndPoint => ParseNetworkEndpoint(ServerListenAddress ?? Address, Port);

[Obsolete("Use ServerEndPoint or ListenEndPoint properties instead.")]
public static implicit operator NetworkEndPoint(ConnectionAddressData d) =>
ParseNetworkEndpoint(d.Address, d.Port);

[Obsolete("Construct manually from NetworkEndPoint.Address and NetworkEndPoint.Port instead.")]
public static implicit operator ConnectionAddressData(NetworkEndPoint d) =>
new ConnectionAddressData() { Address = d.Address.Split(':')[0], Port = d.Port };
new ConnectionAddressData() { Address = d.Address.Split(':')[0], Port = d.Port, ServerListenAddress = null };
}

public ConnectionAddressData ConnectionData = s_DefaultConnectionAddressData;
Expand Down Expand Up @@ -272,7 +287,7 @@ private bool ClientBindAndConnect()
}
else
{
serverEndpoint = ConnectionData;
serverEndpoint = ConnectionData.ServerEndPoint;
}

InitDriver();
Expand Down Expand Up @@ -421,26 +436,36 @@ public void SetClientRelayData(string ipAddress, ushort port, byte[] allocationI
/// <summary>
/// Sets IP and Port information. This will be ignored if using the Unity Relay and you should call <see cref="SetRelayServerData"/>
/// </summary>
public void SetConnectionData(string ipv4Address, ushort port)
public void SetConnectionData(string ipv4Address, ushort port, string listenAddress = null)
{
if (!NetworkEndPoint.TryParse(ipv4Address, port, out var endPoint))
ConnectionData = new ConnectionAddressData
{
Debug.LogError($"Invalid address {ipv4Address}:{port}");
ConnectionData = default;

return;
}
Address = ipv4Address,
Port = port,
ServerListenAddress = listenAddress
};

SetConnectionData(endPoint);
SetProtocol(ProtocolType.UnityTransport);
}

/// <summary>
/// Sets IP and Port information. This will be ignored if using the Unity Relay and you should call <see cref="SetRelayServerData"/>
/// </summary>
public void SetConnectionData(NetworkEndPoint endPoint)
public void SetConnectionData(NetworkEndPoint endPoint, NetworkEndPoint listenEndPoint = default)
{
ConnectionData = endPoint;
SetProtocol(ProtocolType.UnityTransport);
string serverAddress = endPoint.Address.Split(':')[0];

string listenAddress = null;
if (listenEndPoint != default)
{
listenAddress = listenEndPoint.Address.Split(':')[0];
if (endPoint.Port != listenEndPoint.Port)
{
Debug.LogError($"Port mismatch between server and listen endpoints ({endPoint.Port} vs {listenEndPoint.Port}).");
}
}

SetConnectionData(serverAddress, endPoint.Port, listenAddress);
}

private bool StartRelayServer()
Expand Down Expand Up @@ -773,7 +798,7 @@ public override bool StartServer()
switch (m_ProtocolType)
{
case ProtocolType.UnityTransport:
return ServerBindAndListen(ConnectionData);
return ServerBindAndListen(ConnectionData.ListenEndPoint);
case ProtocolType.RelayUnityTransport:
return StartRelayServer();
default:
Expand Down
25 changes: 22 additions & 3 deletions com.unity.netcode.adapter.utp/Tests/Runtime/ConnectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public IEnumerator Cleanup()
[UnityTest]
public IEnumerator ConnectSingleClient()
{

InitializeTransport(out m_Server, out m_ServerEvents);
InitializeTransport(out m_Clients[0], out m_ClientsEvents[0]);

Expand All @@ -67,7 +66,6 @@ public IEnumerator ConnectSingleClient()
[UnityTest]
public IEnumerator ConnectMultipleClients()
{

InitializeTransport(out m_Server, out m_ServerEvents);
m_Server.StartServer();

Expand Down Expand Up @@ -232,7 +230,6 @@ public IEnumerator RepeatedServerDisconnectsNoop()
[UnityTest]
public IEnumerator RepeatedClientDisconnectsNoop()
{

InitializeTransport(out m_Server, out m_ServerEvents);
InitializeTransport(out m_Clients[0], out m_ClientsEvents[0]);

Expand All @@ -259,5 +256,27 @@ public IEnumerator RepeatedClientDisconnectsNoop()

yield return null;
}

// Check connection with different server/listen addresses.
[UnityTest]
public IEnumerator DifferentServerAndListenAddresses()
{
InitializeTransport(out m_Server, out m_ServerEvents);
InitializeTransport(out m_Clients[0], out m_ClientsEvents[0]);

m_Server.SetConnectionData("127.0.0.1", 10042, "0.0.0.0");
m_Clients[0].SetConnectionData("127.0.0.1", 10042);

m_Server.StartServer();
m_Clients[0].StartClient();

yield return WaitForNetworkEvent(NetworkEvent.Connect, m_ClientsEvents[0]);

// Check we've received Connect event on server too.
Assert.AreEqual(1, m_ServerEvents.Count);
Assert.AreEqual(NetworkEvent.Connect, m_ServerEvents[0].Type);

yield return null;
}
}
}