fix: NetworkServer.OnConnected allows for connectionIds < 0 now. some transports like kcp use hashing where connectionIds can easily be < 0, which previously was not allowed.

This commit is contained in:
vis2k 2021-01-21 11:02:02 +08:00
parent 11e2216894
commit 12ee96f5d9
2 changed files with 8 additions and 6 deletions

View File

@ -499,10 +499,12 @@ static void OnConnected(int connectionId)
{ {
if (logger.LogEnabled()) logger.Log("Server accepted client:" + connectionId); if (logger.LogEnabled()) logger.Log("Server accepted client:" + connectionId);
// connectionId needs to be > 0 because 0 is reserved for local player // connectionId needs to be != 0 because 0 is reserved for local player
if (connectionId <= 0) // note that some transports like kcp generate connectionId by
// hashing which can be < 0 as well, so we need to allow < 0!
if (connectionId == 0)
{ {
logger.LogError("Server.HandleConnect: invalid connectionId: " + connectionId + " . Needs to be >0, because 0 is reserved for local player."); logger.LogError("Server.HandleConnect: invalid connectionId: " + connectionId + " . Needs to be != 0, because 0 is reserved for local player.");
Transport.activeTransport.ServerDisconnect(connectionId); Transport.activeTransport.ServerDisconnect(connectionId);
return; return;
} }

View File

@ -209,7 +209,7 @@ public void ConnectionsDictTest()
} }
[Test] [Test]
public void OnConnectedOnlyAllowsGreaterZeroConnectionIdsTest() public void OnConnectedOnlyAllowsNonZeroConnectionIdsTest()
{ {
// OnConnected should only allow connectionIds >= 0 // OnConnected should only allow connectionIds >= 0
// 0 is for local player // 0 is for local player
@ -230,8 +230,8 @@ public void OnConnectedOnlyAllowsGreaterZeroConnectionIdsTest()
Transport.activeTransport.OnServerConnected.Invoke(0); Transport.activeTransport.OnServerConnected.Invoke(0);
Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); Assert.That(NetworkServer.connections.Count, Is.EqualTo(0));
// connect <0 // connect == 0 should fail
Transport.activeTransport.OnServerConnected.Invoke(-1); Transport.activeTransport.OnServerConnected.Invoke(0);
Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); Assert.That(NetworkServer.connections.Count, Is.EqualTo(0));
LogAssert.ignoreFailingMessages = false; LogAssert.ignoreFailingMessages = false;
} }