Skip to content

Commit 5344c18

Browse files
feat: Allow specifying a different listen address in the adapter (1.0.0) (#1607)
1 parent 0937356 commit 5344c18

File tree

3 files changed

+69
-23
lines changed

3 files changed

+69
-23
lines changed

com.unity.netcode.adapter.utp/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ All notable changes to this package will be documented in this file. The format
66

77
### Added
88

9+
- 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)
910
- 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)
1011

1112
### Changed
1213

1314
- Rename the 'Send Queue Batch Size' property to 'Max Payload Size' to better reflect its usage. (#1584)
15+
- 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)
1416
- Updated Unity Transport package to 1.0.0-pre.12. (#1615)
1517

1618
### Fixed

com.unity.netcode.adapter.utp/Runtime/UnityTransport.cs

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ private enum State
9090
public const int InitialMaxSendQueueSize = 16 * InitialMaxPayloadSize;
9191

9292
private static ConnectionAddressData s_DefaultConnectionAddressData = new ConnectionAddressData()
93-
{ Address = "127.0.0.1", Port = 7777 };
93+
{ Address = "127.0.0.1", Port = 7777, ServerListenAddress = null };
9494

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

137-
public static implicit operator NetworkEndPoint(ConnectionAddressData d)
137+
[Tooltip("UDP port of the server.")]
138+
[SerializeField] public ushort Port;
139+
140+
[Tooltip("IP address the server will listen on. If not provided, will use 'Address'.")]
141+
[SerializeField] public string ServerListenAddress;
142+
143+
private static NetworkEndPoint ParseNetworkEndpoint(string ip, ushort port)
138144
{
139-
if (!NetworkEndPoint.TryParse(d.Address, (ushort)d.Port, out var networkEndPoint))
145+
if (!NetworkEndPoint.TryParse(ip, port, out var endpoint))
140146
{
141-
Debug.LogError($"Invalid address {d.Address}:{d.Port}");
147+
Debug.LogError($"Invalid network endpoint: {ip}:{port}.");
142148
return default;
143149
}
144150

145-
return networkEndPoint;
151+
return endpoint;
146152
}
147153

154+
public NetworkEndPoint ServerEndPoint => ParseNetworkEndpoint(Address, Port);
155+
156+
public NetworkEndPoint ListenEndPoint => ParseNetworkEndpoint(ServerListenAddress ?? Address, Port);
157+
158+
[Obsolete("Use ServerEndPoint or ListenEndPoint properties instead.")]
159+
public static implicit operator NetworkEndPoint(ConnectionAddressData d) =>
160+
ParseNetworkEndpoint(d.Address, d.Port);
161+
162+
[Obsolete("Construct manually from NetworkEndPoint.Address and NetworkEndPoint.Port instead.")]
148163
public static implicit operator ConnectionAddressData(NetworkEndPoint d) =>
149-
new ConnectionAddressData() { Address = d.Address.Split(':')[0], Port = d.Port };
164+
new ConnectionAddressData() { Address = d.Address.Split(':')[0], Port = d.Port, ServerListenAddress = null };
150165
}
151166

152167
public ConnectionAddressData ConnectionData = s_DefaultConnectionAddressData;
@@ -272,7 +287,7 @@ private bool ClientBindAndConnect()
272287
}
273288
else
274289
{
275-
serverEndpoint = ConnectionData;
290+
serverEndpoint = ConnectionData.ServerEndPoint;
276291
}
277292

278293
InitDriver();
@@ -421,26 +436,36 @@ public void SetClientRelayData(string ipAddress, ushort port, byte[] allocationI
421436
/// <summary>
422437
/// Sets IP and Port information. This will be ignored if using the Unity Relay and you should call <see cref="SetRelayServerData"/>
423438
/// </summary>
424-
public void SetConnectionData(string ipv4Address, ushort port)
439+
public void SetConnectionData(string ipv4Address, ushort port, string listenAddress = null)
425440
{
426-
if (!NetworkEndPoint.TryParse(ipv4Address, port, out var endPoint))
441+
ConnectionData = new ConnectionAddressData
427442
{
428-
Debug.LogError($"Invalid address {ipv4Address}:{port}");
429-
ConnectionData = default;
430-
431-
return;
432-
}
443+
Address = ipv4Address,
444+
Port = port,
445+
ServerListenAddress = listenAddress
446+
};
433447

434-
SetConnectionData(endPoint);
448+
SetProtocol(ProtocolType.UnityTransport);
435449
}
436450

437451
/// <summary>
438452
/// Sets IP and Port information. This will be ignored if using the Unity Relay and you should call <see cref="SetRelayServerData"/>
439453
/// </summary>
440-
public void SetConnectionData(NetworkEndPoint endPoint)
454+
public void SetConnectionData(NetworkEndPoint endPoint, NetworkEndPoint listenEndPoint = default)
441455
{
442-
ConnectionData = endPoint;
443-
SetProtocol(ProtocolType.UnityTransport);
456+
string serverAddress = endPoint.Address.Split(':')[0];
457+
458+
string listenAddress = null;
459+
if (listenEndPoint != default)
460+
{
461+
listenAddress = listenEndPoint.Address.Split(':')[0];
462+
if (endPoint.Port != listenEndPoint.Port)
463+
{
464+
Debug.LogError($"Port mismatch between server and listen endpoints ({endPoint.Port} vs {listenEndPoint.Port}).");
465+
}
466+
}
467+
468+
SetConnectionData(serverAddress, endPoint.Port, listenAddress);
444469
}
445470

446471
private bool StartRelayServer()
@@ -773,7 +798,7 @@ public override bool StartServer()
773798
switch (m_ProtocolType)
774799
{
775800
case ProtocolType.UnityTransport:
776-
return ServerBindAndListen(ConnectionData);
801+
return ServerBindAndListen(ConnectionData.ListenEndPoint);
777802
case ProtocolType.RelayUnityTransport:
778803
return StartRelayServer();
779804
default:

com.unity.netcode.adapter.utp/Tests/Runtime/ConnectionTests.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ public IEnumerator Cleanup()
4747
[UnityTest]
4848
public IEnumerator ConnectSingleClient()
4949
{
50-
5150
InitializeTransport(out m_Server, out m_ServerEvents);
5251
InitializeTransport(out m_Clients[0], out m_ClientsEvents[0]);
5352

@@ -67,7 +66,6 @@ public IEnumerator ConnectSingleClient()
6766
[UnityTest]
6867
public IEnumerator ConnectMultipleClients()
6968
{
70-
7169
InitializeTransport(out m_Server, out m_ServerEvents);
7270
m_Server.StartServer();
7371

@@ -232,7 +230,6 @@ public IEnumerator RepeatedServerDisconnectsNoop()
232230
[UnityTest]
233231
public IEnumerator RepeatedClientDisconnectsNoop()
234232
{
235-
236233
InitializeTransport(out m_Server, out m_ServerEvents);
237234
InitializeTransport(out m_Clients[0], out m_ClientsEvents[0]);
238235

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

260257
yield return null;
261258
}
259+
260+
// Check connection with different server/listen addresses.
261+
[UnityTest]
262+
public IEnumerator DifferentServerAndListenAddresses()
263+
{
264+
InitializeTransport(out m_Server, out m_ServerEvents);
265+
InitializeTransport(out m_Clients[0], out m_ClientsEvents[0]);
266+
267+
m_Server.SetConnectionData("127.0.0.1", 10042, "0.0.0.0");
268+
m_Clients[0].SetConnectionData("127.0.0.1", 10042);
269+
270+
m_Server.StartServer();
271+
m_Clients[0].StartClient();
272+
273+
yield return WaitForNetworkEvent(NetworkEvent.Connect, m_ClientsEvents[0]);
274+
275+
// Check we've received Connect event on server too.
276+
Assert.AreEqual(1, m_ServerEvents.Count);
277+
Assert.AreEqual(NetworkEvent.Connect, m_ServerEvents[0].Type);
278+
279+
yield return null;
280+
}
262281
}
263282
}

0 commit comments

Comments
 (0)