Skip to content

Commit 4008889

Browse files
Jesse Olmer0xFA11
andauthored
fix: UNetTransport.StartServer should return false when failed (MTT-3365, #854) (#1887)
* fix: UNetTransport.StartServer should return false when failed [#854] UNetTransport.StartServer now checks the result of the underlying UnityEngine.Networking.NetworkTransport.AddHost against the (undocumented) -1 failure code Co-authored-by: Fatih Mar <[email protected]>
1 parent 83752c8 commit 4008889

File tree

5 files changed

+68
-3
lines changed

5 files changed

+68
-3
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
1818
- Fixed `NetworkTransform` generating false positive rotation delta checks when rolling over between 0 and 360 degrees. (#1890)
1919
- Fixed client throwing an exception if it has messages in the outbound queue when processing the `NetworkEvent.Disconnect` event and is using UTP. (#1884)
2020
- Fixed issue during client synchronization if 'ValidateSceneBeforeLoading' returned false it would halt the client synchronization process resulting in a client that was approved but not synchronized or fully connected with the server. (#1883)
21-
21+
- Fixed an issue where UNetTransport.StartServer would return success even if the underlying transport failed to start (#854)
2222

2323
## [1.0.0-pre.7] - 2022-04-06
2424

com.unity.netcode.gameobjects/Runtime/Transports/UNET/UNetTransport.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,8 @@ public override bool StartClient()
205205
public override bool StartServer()
206206
{
207207
var topology = new HostTopology(GetConfig(), MaxConnections);
208-
UnityEngine.Networking.NetworkTransport.AddHost(topology, ServerListenPort, null);
209-
return true;
208+
// Undocumented, but AddHost returns -1 in case of any type of failure. See UNET::NetLibraryManager::AddHost
209+
return -1 != UnityEngine.Networking.NetworkTransport.AddHost(topology, ServerListenPort, null);
210210
}
211211

212212
public override void DisconnectRemoteClient(ulong clientId)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#if UNITY_UNET_PRESENT
2+
#pragma warning disable 618 // disable is obsolete
3+
using NUnit.Framework;
4+
using Unity.Netcode.Transports.UNET;
5+
using UnityEngine;
6+
using UnityEngine.TestTools;
7+
8+
namespace Unity.Netcode.EditorTests
9+
{
10+
public class UNetTransportTests
11+
{
12+
[Test]
13+
public void StartServerReturnsFalseOnFailure()
14+
{
15+
UNetTransport unet1 = null;
16+
UNetTransport unet2 = null;
17+
18+
try
19+
{
20+
// Arrange
21+
22+
// We're expecting an error from UNET, but don't care to validate the specific message
23+
LogAssert.ignoreFailingMessages = true;
24+
25+
var go = new GameObject();
26+
unet1 = go.AddComponent<UNetTransport>();
27+
unet1.ServerListenPort = 1;
28+
unet1.Initialize();
29+
unet1.StartServer();
30+
unet2 = go.AddComponent<UNetTransport>();
31+
unet2.ServerListenPort = 1;
32+
unet2.Initialize();
33+
34+
// Act
35+
var result = unet2.StartServer();
36+
37+
// Assert
38+
Assert.IsFalse(result, "UNET fails to initialize against port already in use");
39+
}
40+
finally
41+
{
42+
unet1?.Shutdown();
43+
unet2?.Shutdown();
44+
}
45+
}
46+
}
47+
}
48+
#pragma warning restore 618
49+
#endif

com.unity.netcode.gameobjects/Tests/Editor/Transports/UNetTransportTests.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

com.unity.netcode.gameobjects/Tests/Editor/com.unity.netcode.editortests.asmdef

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
"name": "com.unity.multiplayer.tools",
2525
"expression": "",
2626
"define": "MULTIPLAYER_TOOLS"
27+
},
28+
{
29+
"name": "Unity",
30+
"expression": "(0,2022.2.0a5)",
31+
"define": "UNITY_UNET_PRESENT"
2732
}
2833
]
2934
}

0 commit comments

Comments
 (0)