fix: #1515 - StopHost now invokes OnServerDisconnected for the host client too (#1601)

* fix: #1515 - StopHost now invokes OnServerDisconnected for the host client too

* avoid NullReferenceException when calling StopHost without StartHost

* test WIP

* test
This commit is contained in:
vis2k 2020-03-26 20:54:53 +01:00 committed by GitHub
parent c8a1a5e56f
commit 678ac68b58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 83 additions and 3 deletions

View File

@ -124,6 +124,23 @@ internal static void ConnectLocalServer()
NetworkServer.localConnection.Send(new ConnectMessage());
}
/// <summary>
/// disconnect host mode. this is needed to call DisconnectMessage for
/// the host client too.
/// </summary>
internal static void DisconnectLocalServer()
{
// only if host connection is running
if (NetworkServer.localConnection != null)
{
// TODO ConnectLocalServer manually sends a ConnectMessage to the
// local connection. should we send a DisconnectMessage here too?
// (if we do then we get an Unknown Message ID log)
//NetworkServer.localConnection.Send(new DisconnectMessage());
NetworkServer.OnDisconnected(NetworkServer.localConnection.connectionId);
}
}
static void InitializeTransportHandlers()
{
Transport.activeTransport.OnClientConnected.AddListener(OnConnected);

View File

@ -547,6 +547,15 @@ void StartHostClient()
public void StopHost()
{
OnStopHost();
// TODO try to move DisconnectLocalServer into StopClient(), and
// then call StopClient() before StopServer(). needs testing!.
// DisconnectLocalServer needs to be called so that the host client
// receives a DisconnectMessage too.
// fixes: https://github.com/vis2k/Mirror/issues/1515
NetworkClient.DisconnectLocalServer();
StopServer();
StopClient();
}

View File

@ -473,7 +473,7 @@ internal static void OnConnected(NetworkConnectionToClient conn)
conn.InvokeHandler(new ConnectMessage(), -1);
}
static void OnDisconnected(int connectionId)
internal static void OnDisconnected(int connectionId)
{
if (LogFilter.Debug) Debug.Log("Server disconnect client:" + connectionId);

View File

@ -0,0 +1,43 @@
using NUnit.Framework;
using UnityEngine;
namespace Mirror.Tests
{
class NetworkManagerOnServerDisconnect : NetworkManager
{
public int called;
public override void OnServerDisconnect(NetworkConnection conn) { ++called; }
}
[TestFixture]
public class NetworkManagerStopHostOnServerDisconnectedTest
{
GameObject gameObject;
NetworkManagerOnServerDisconnect manager;
[SetUp]
public void SetUp()
{
gameObject = new GameObject();
manager = gameObject.AddComponent<NetworkManagerOnServerDisconnect>();
}
[TearDown]
public void TearDown()
{
GameObject.DestroyImmediate(gameObject);
}
// test to prevent https://github.com/vis2k/Mirror/issues/1515
[Test]
public void StopHostCallsOnServerDisconnectForHostClient()
{
// OnServerDisconnect is always called when a client disconnects.
// it should also be called for the host client when we stop the host
Assert.That(manager.called, Is.EqualTo(0));
manager.StartHost();
manager.StopHost();
Assert.That(manager.called, Is.EqualTo(1));
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2f583081473a64b92b971678e571382a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -114,7 +114,7 @@ public void RegisterStartPositionTest()
public void UnRegisterStartPositionTest()
{
Assert.That(NetworkManager.startPositions.Count , Is.Zero);
NetworkManager.RegisterStartPosition(gameObject.transform);
Assert.That(NetworkManager.startPositions.Count , Is.EqualTo(1));
Assert.That(NetworkManager.startPositions, Has.Member(gameObject.transform));
@ -127,7 +127,7 @@ public void UnRegisterStartPositionTest()
public void GetStartPositionTest()
{
Assert.That(NetworkManager.startPositions.Count , Is.Zero);
NetworkManager.RegisterStartPosition(gameObject.transform);
Assert.That(NetworkManager.startPositions.Count , Is.EqualTo(1));
Assert.That(NetworkManager.startPositions, Has.Member(gameObject.transform));