From f9c69ba1a81fb650be12273858f05deae786d347 Mon Sep 17 00:00:00 2001 From: James Frowen Date: Fri, 30 Oct 2020 02:17:38 +0000 Subject: [PATCH 01/14] breaking: Using Events instead of Message for connect and disconnect * Using events is simpler to understand than InvokeHandler * Allows multiple objects to listen for connect events to add message handlers to NetworkClient this change does cause client OnClientConnect in host mode to be called the same BREAKING CHANGE: use events in NetworkServer/Client instead of handlers for Connect/DisconnectMessage --- Assets/Mirror/Runtime/Messages.cs | 2 + Assets/Mirror/Runtime/NetworkClient.cs | 13 +++++-- Assets/Mirror/Runtime/NetworkManager.cs | 16 ++++---- Assets/Mirror/Runtime/NetworkServer.cs | 14 ++++++- Assets/Mirror/Runtime/UNetwork.cs | 4 ++ Assets/Mirror/Tests/Editor/BuiltInMessages.cs | 24 ------------ .../Mirror/Tests/Editor/MessagePackerTest.cs | 7 +++- .../Tests/Editor/NetworkBehaviourTests.cs | 6 --- .../Mirror/Tests/Editor/NetworkClientTests.cs | 7 +--- .../Tests/Editor/NetworkIdentityTests.cs | 1 - ...rkManagerStopHostOnServerDisconnectTest.cs | 10 ++++- .../Mirror/Tests/Editor/NetworkServerTest.cs | 39 +------------------ Assets/Mirror/Tests/Editor/RemoteTestBase.cs | 5 +++ 13 files changed, 57 insertions(+), 91 deletions(-) diff --git a/Assets/Mirror/Runtime/Messages.cs b/Assets/Mirror/Runtime/Messages.cs index 6242351a2b..9b8f489a44 100644 --- a/Assets/Mirror/Runtime/Messages.cs +++ b/Assets/Mirror/Runtime/Messages.cs @@ -30,8 +30,10 @@ public struct NotReadyMessage : NetworkMessage { } public struct AddPlayerMessage : NetworkMessage { } + [System.Obsolete("this message is never sent over network, replace with an event", true)] public struct DisconnectMessage : NetworkMessage { } + [System.Obsolete("this message is never sent over network, replace with an event", true)] public struct ConnectMessage : NetworkMessage { } public struct SceneMessage : NetworkMessage diff --git a/Assets/Mirror/Runtime/NetworkClient.cs b/Assets/Mirror/Runtime/NetworkClient.cs index ed9d93ec18..e05038ac8c 100644 --- a/Assets/Mirror/Runtime/NetworkClient.cs +++ b/Assets/Mirror/Runtime/NetworkClient.cs @@ -33,6 +33,8 @@ public static class NetworkClient public static NetworkConnection connection { get; internal set; } internal static ConnectState connectState = ConnectState.None; + internal static event ConnectionEvent OnConnectedEvent; + internal static event ConnectionEvent OnDisconnectEvent; /// /// The IP address of the server that this client is connected to. @@ -125,7 +127,9 @@ public static void ConnectHost() public static void ConnectLocalServer() { NetworkServer.OnConnected(NetworkServer.localConnection); - NetworkServer.localConnection.Send(new ConnectMessage()); + + Debug.Assert(connection is ULocalConnectionToServer, "Connection should be local connection"); + OnConnectedEvent?.Invoke(connection); } /// @@ -164,7 +168,7 @@ static void OnDisconnected() ClientScene.HandleClientDisconnect(connection); - connection?.InvokeHandler(new DisconnectMessage(), -1); + OnDisconnectEvent?.Invoke(connection); } internal static void OnDataReceived(ArraySegment data, int channelId) @@ -187,7 +191,7 @@ static void OnConnected() // thus we should set the connected state before calling the handler connectState = ConnectState.Connected; NetworkTime.UpdateClient(); - connection.InvokeHandler(new ConnectMessage(), -1); + OnConnectedEvent?.Invoke(connection); } else logger.LogError("Skipped Connect message handling because connection is null."); } @@ -206,7 +210,8 @@ public static void Disconnect() { if (isConnected) { - NetworkServer.localConnection.Send(new DisconnectMessage()); + Debug.Assert(connection is ULocalConnectionToServer, "Connection should be local connection"); + OnDisconnectEvent?.Invoke(connection); } NetworkServer.RemoveLocalConnection(); } diff --git a/Assets/Mirror/Runtime/NetworkManager.cs b/Assets/Mirror/Runtime/NetworkManager.cs index 36c60ff7a5..f1f1749f1e 100644 --- a/Assets/Mirror/Runtime/NetworkManager.cs +++ b/Assets/Mirror/Runtime/NetworkManager.cs @@ -750,8 +750,8 @@ bool InitializeSingleton() void RegisterServerMessages() { - NetworkServer.RegisterHandler(OnServerConnectInternal, false); - NetworkServer.RegisterHandler(OnServerDisconnectInternal, false); + NetworkServer.OnConnectedEvent += OnServerConnectInternal; + NetworkServer.OnDisconnectEvent += OnServerDisconnectInternal; NetworkServer.RegisterHandler(OnServerAddPlayerInternal); NetworkServer.RegisterHandler(OnServerErrorInternal, false); @@ -761,8 +761,8 @@ void RegisterServerMessages() void RegisterClientMessages() { - NetworkClient.RegisterHandler(OnClientConnectInternal, false); - NetworkClient.RegisterHandler(OnClientDisconnectInternal, false); + NetworkClient.OnConnectedEvent += OnClientConnectInternal; + NetworkClient.OnDisconnectEvent += OnClientDisconnectInternal; NetworkClient.RegisterHandler(OnClientNotReadyMessageInternal); NetworkClient.RegisterHandler(OnClientErrorInternal, false); NetworkClient.RegisterHandler(OnClientSceneInternal, false); @@ -1149,7 +1149,7 @@ public Transform GetStartPosition() #region Server Internal Message Handlers - void OnServerConnectInternal(NetworkConnection conn, ConnectMessage connectMsg) + void OnServerConnectInternal(NetworkConnection conn) { logger.Log("NetworkManager.OnServerConnectInternal"); @@ -1183,7 +1183,7 @@ void OnServerAuthenticated(NetworkConnection conn) OnServerConnect(conn); } - void OnServerDisconnectInternal(NetworkConnection conn, DisconnectMessage msg) + void OnServerDisconnectInternal(NetworkConnection conn) { logger.Log("NetworkManager.OnServerDisconnectInternal"); OnServerDisconnect(conn); @@ -1230,7 +1230,7 @@ void OnServerErrorInternal(NetworkConnection conn, ErrorMessage msg) #region Client Internal Message Handlers - void OnClientConnectInternal(NetworkConnection conn, ConnectMessage message) + void OnClientConnectInternal(NetworkConnection conn) { logger.Log("NetworkManager.OnClientConnectInternal"); @@ -1268,7 +1268,7 @@ void OnClientAuthenticated(NetworkConnection conn) } } - void OnClientDisconnectInternal(NetworkConnection conn, DisconnectMessage msg) + void OnClientDisconnectInternal(NetworkConnection conn) { logger.Log("NetworkManager.OnClientDisconnectInternal"); OnClientDisconnect(conn); diff --git a/Assets/Mirror/Runtime/NetworkServer.cs b/Assets/Mirror/Runtime/NetworkServer.cs index d4229efa92..4f93596107 100644 --- a/Assets/Mirror/Runtime/NetworkServer.cs +++ b/Assets/Mirror/Runtime/NetworkServer.cs @@ -72,6 +72,15 @@ public static class NetworkServer /// public static float disconnectInactiveTimeout = 60f; + /// + /// Called after new connection is set up on server + /// + public static event ConnectionEvent OnConnectedEvent; + /// + /// Called after connection disconnects from server + /// + public static event ConnectionEvent OnDisconnectEvent; + /// /// This shuts down the server and disconnects all clients. /// @@ -540,7 +549,7 @@ internal static void OnConnected(NetworkConnectionToClient conn) // add connection and invoke connected event AddConnection(conn); - conn.InvokeHandler(new ConnectMessage(), -1); + OnConnectedEvent?.Invoke(conn); } internal static void OnDisconnected(int connectionId) @@ -559,7 +568,8 @@ internal static void OnDisconnected(int connectionId) static void OnDisconnected(NetworkConnection conn) { - conn.InvokeHandler(new DisconnectMessage(), -1); + // todo change to event instead of message + OnDisconnectEvent?.Invoke(conn); if (logger.LogEnabled()) logger.Log("Server lost client:" + conn); } diff --git a/Assets/Mirror/Runtime/UNetwork.cs b/Assets/Mirror/Runtime/UNetwork.cs index deb3bdf1e8..6366bca7f8 100644 --- a/Assets/Mirror/Runtime/UNetwork.cs +++ b/Assets/Mirror/Runtime/UNetwork.cs @@ -15,6 +15,10 @@ namespace Mirror // Handles requests to unspawn objects on the client public delegate void UnSpawnDelegate(GameObject spawned); + + public delegate void ConnectionEvent(NetworkConnection conn); + + // invoke type for Cmd/Rpc public enum MirrorInvokeType { diff --git a/Assets/Mirror/Tests/Editor/BuiltInMessages.cs b/Assets/Mirror/Tests/Editor/BuiltInMessages.cs index 429fa4fd44..964732fcf5 100644 --- a/Assets/Mirror/Tests/Editor/BuiltInMessages.cs +++ b/Assets/Mirror/Tests/Editor/BuiltInMessages.cs @@ -30,30 +30,6 @@ public void CommandMessage() Is.EqualTo(message.payload.Array[message.payload.Offset + i])); } - [Test] - public void ConnectMessage() - { - // try setting value with constructor - ConnectMessage message = new ConnectMessage(); - byte[] arr = MessagePackerTest.PackToByteArray(message); - Assert.DoesNotThrow(() => - { - MessagePackerTest.UnpackFromByteArray(arr); - }); - } - - [Test] - public void DisconnectMessage() - { - // try setting value with constructor - DisconnectMessage message = new DisconnectMessage(); - byte[] arr = MessagePackerTest.PackToByteArray(message); - Assert.DoesNotThrow(() => - { - MessagePackerTest.UnpackFromByteArray(arr); - }); - } - [Test] public void ErrorMessage() { diff --git a/Assets/Mirror/Tests/Editor/MessagePackerTest.cs b/Assets/Mirror/Tests/Editor/MessagePackerTest.cs index ee5a44cbb1..d52f260d61 100644 --- a/Assets/Mirror/Tests/Editor/MessagePackerTest.cs +++ b/Assets/Mirror/Tests/Editor/MessagePackerTest.cs @@ -51,16 +51,19 @@ public void TestPacking() Assert.That(unpacked.sceneOperation, Is.EqualTo(SceneOperation.LoadAdditive)); } + struct Message1 : NetworkMessage { } + struct Message2 : NetworkMessage { } + [Test] public void UnpackWrongMessage() { - ConnectMessage message = new ConnectMessage(); + Message1 message = new Message1(); byte[] data = PackToByteArray(message); Assert.Throws(() => { - DisconnectMessage unpacked = UnpackFromByteArray(data); + Message2 unpacked = MessagePacker.Unpack(data); }); } diff --git a/Assets/Mirror/Tests/Editor/NetworkBehaviourTests.cs b/Assets/Mirror/Tests/Editor/NetworkBehaviourTests.cs index c48d4fa6eb..87e10a4bc2 100644 --- a/Assets/Mirror/Tests/Editor/NetworkBehaviourTests.cs +++ b/Assets/Mirror/Tests/Editor/NetworkBehaviourTests.cs @@ -300,8 +300,6 @@ public void SendCommandInternal() // we need to start a server and connect a client in order to be // able to send commands // message handlers - NetworkServer.RegisterHandler((conn, msg) => { }, false); - NetworkServer.RegisterHandler((conn, msg) => { }, false); NetworkServer.RegisterHandler((conn, msg) => { }, false); NetworkServer.RegisterHandler((conn, msg) => { }, false); NetworkServer.Listen(1); @@ -413,8 +411,6 @@ public void SendRPCInternal() // we need to start a server and connect a client in order to be // able to send commands // message handlers - NetworkServer.RegisterHandler((conn, msg) => { }, false); - NetworkServer.RegisterHandler((conn, msg) => { }, false); NetworkServer.RegisterHandler((conn, msg) => { }, false); NetworkServer.RegisterHandler((conn, msg) => { }, false); NetworkServer.Listen(1); @@ -499,8 +495,6 @@ public void SendTargetRPCInternal() // we need to start a server and connect a client in order to be // able to send commands // message handlers - NetworkServer.RegisterHandler((conn, msg) => { }, false); - NetworkServer.RegisterHandler((conn, msg) => { }, false); NetworkServer.RegisterHandler((conn, msg) => { }, false); NetworkServer.RegisterHandler((conn, msg) => { }, false); NetworkServer.Listen(1); diff --git a/Assets/Mirror/Tests/Editor/NetworkClientTests.cs b/Assets/Mirror/Tests/Editor/NetworkClientTests.cs index f042cd9411..eb45a5e926 100644 --- a/Assets/Mirror/Tests/Editor/NetworkClientTests.cs +++ b/Assets/Mirror/Tests/Editor/NetworkClientTests.cs @@ -1,4 +1,4 @@ -using System; +using System; using NUnit.Framework; using UnityEngine; @@ -17,13 +17,8 @@ public void SetUp() Transport.activeTransport = transportGO.AddComponent(); // we need a server to connect to - NetworkServer.RegisterHandler((conn, msg) => { }, false); - NetworkServer.RegisterHandler((conn, msg) => { }, false); NetworkServer.RegisterHandler((conn, msg) => { }, false); NetworkServer.Listen(10); - - // setup client handlers too - NetworkClient.RegisterHandler(msg => { }, false); } [TearDown] diff --git a/Assets/Mirror/Tests/Editor/NetworkIdentityTests.cs b/Assets/Mirror/Tests/Editor/NetworkIdentityTests.cs index 23fc1576ae..e4da593c8e 100644 --- a/Assets/Mirror/Tests/Editor/NetworkIdentityTests.cs +++ b/Assets/Mirror/Tests/Editor/NetworkIdentityTests.cs @@ -845,7 +845,6 @@ public void OnStartServerInHostModeSetsIsClientTrue() // call client connect so that internals are set up // (it won't actually successfully connect) // -> also set up connectmessage handler to avoid unhandled msg error - NetworkClient.RegisterHandler(msg => { }, false); NetworkClient.Connect("localhost"); // manually invoke transport.OnConnected so that NetworkClient.active is set to true diff --git a/Assets/Mirror/Tests/Editor/NetworkManagerStopHostOnServerDisconnectTest.cs b/Assets/Mirror/Tests/Editor/NetworkManagerStopHostOnServerDisconnectTest.cs index 17b86b7b3a..ff7ce6b370 100644 --- a/Assets/Mirror/Tests/Editor/NetworkManagerStopHostOnServerDisconnectTest.cs +++ b/Assets/Mirror/Tests/Editor/NetworkManagerStopHostOnServerDisconnectTest.cs @@ -1,4 +1,5 @@ -using NUnit.Framework; +using System; +using NUnit.Framework; using UnityEngine; namespace Mirror.Tests @@ -13,6 +14,7 @@ class NetworkManagerOnServerDisconnect : NetworkManager public class NetworkManagerStopHostOnServerDisconnectTest { GameObject gameObject; + GameObject playerPrefab; NetworkManagerOnServerDisconnect manager; MemoryTransport transport; @@ -22,12 +24,18 @@ public void SetUp() gameObject = new GameObject(); transport = gameObject.AddComponent(); manager = gameObject.AddComponent(); + playerPrefab = new GameObject("player"); + NetworkIdentity id = playerPrefab.AddComponent(); + id.assetId = Guid.NewGuid(); + id.sceneId = 0; + manager.playerPrefab = playerPrefab; } [TearDown] public void TearDown() { GameObject.DestroyImmediate(gameObject); + GameObject.DestroyImmediate(playerPrefab); } // test to prevent https://github.com/vis2k/Mirror/issues/1515 diff --git a/Assets/Mirror/Tests/Editor/NetworkServerTest.cs b/Assets/Mirror/Tests/Editor/NetworkServerTest.cs index 7c75351426..e86e7f9c67 100644 --- a/Assets/Mirror/Tests/Editor/NetworkServerTest.cs +++ b/Assets/Mirror/Tests/Editor/NetworkServerTest.cs @@ -119,8 +119,6 @@ public void IsActiveTest() public void MaxConnectionsTest() { // message handlers - NetworkServer.RegisterHandler((conn, msg) => { }, false); - NetworkServer.RegisterHandler((conn, msg) => { }, false); NetworkServer.RegisterHandler((conn, msg) => { }, false); // listen with maxconnections=1 @@ -141,8 +139,7 @@ public void ConnectMessageHandlerTest() { // message handlers bool connectCalled = false; - NetworkServer.RegisterHandler((conn, msg) => { connectCalled = true; }, false); - NetworkServer.RegisterHandler((conn, msg) => { }, false); + NetworkServer.OnConnectedEvent += (conn) => { connectCalled = true; }; NetworkServer.RegisterHandler((conn, msg) => { }, false); // listen @@ -159,8 +156,7 @@ public void DisconnectMessageHandlerTest() { // message handlers bool disconnectCalled = false; - NetworkServer.RegisterHandler((conn, msg) => { }, false); - NetworkServer.RegisterHandler((conn, msg) => { disconnectCalled = true; }, false); + NetworkServer.OnDisconnectEvent += (conn) => { disconnectCalled = true; }; NetworkServer.RegisterHandler((conn, msg) => { }, false); // listen @@ -180,8 +176,6 @@ public void DisconnectMessageHandlerTest() public void ConnectionsDictTest() { // message handlers - NetworkServer.RegisterHandler((conn, msg) => { }, false); - NetworkServer.RegisterHandler((conn, msg) => { }, false); NetworkServer.RegisterHandler((conn, msg) => { }, false); // listen @@ -216,8 +210,6 @@ public void OnConnectedOnlyAllowsGreaterZeroConnectionIdsTest() // <0 is never used // message handlers - NetworkServer.RegisterHandler((conn, msg) => { }, false); - NetworkServer.RegisterHandler((conn, msg) => { }, false); NetworkServer.RegisterHandler((conn, msg) => { }, false); // listen @@ -240,8 +232,6 @@ public void OnConnectedOnlyAllowsGreaterZeroConnectionIdsTest() public void ConnectDuplicateConnectionIdsTest() { // message handlers - NetworkServer.RegisterHandler((conn, msg) => { }, false); - NetworkServer.RegisterHandler((conn, msg) => { }, false); NetworkServer.RegisterHandler((conn, msg) => { }, false); // listen @@ -315,8 +305,6 @@ public void LocalClientActiveTest() public void AddConnectionTest() { // message handlers - NetworkServer.RegisterHandler((conn, msg) => { }, false); - NetworkServer.RegisterHandler((conn, msg) => { }, false); NetworkServer.RegisterHandler((conn, msg) => { }, false); // listen @@ -356,8 +344,6 @@ public void AddConnectionTest() public void RemoveConnectionTest() { // message handlers - NetworkServer.RegisterHandler((conn, msg) => { }, false); - NetworkServer.RegisterHandler((conn, msg) => { }, false); NetworkServer.RegisterHandler((conn, msg) => { }, false); // listen @@ -382,8 +368,6 @@ public void RemoveConnectionTest() public void DisconnectAllConnectionsTest() { // message handlers - NetworkServer.RegisterHandler((conn, msg) => { }, false); - NetworkServer.RegisterHandler((conn, msg) => { }, false); NetworkServer.RegisterHandler((conn, msg) => { }, false); // listen @@ -404,8 +388,6 @@ public void DisconnectAllConnectionsTest() public void DisconnectAllTest() { // message handlers - NetworkServer.RegisterHandler((conn, msg) => { }, false); - NetworkServer.RegisterHandler((conn, msg) => { }, false); NetworkServer.RegisterHandler((conn, msg) => { }, false); // listen @@ -432,8 +414,6 @@ public void DisconnectAllTest() public void OnDataReceivedTest() { // message handlers - NetworkServer.RegisterHandler((conn, msg) => { }, false); - NetworkServer.RegisterHandler((conn, msg) => { }, false); NetworkServer.RegisterHandler((conn, msg) => { }, false); // add one custom message handler @@ -478,8 +458,6 @@ public void OnDataReceivedTest() public void OnDataReceivedInvalidConnectionIdTest() { // message handlers - NetworkServer.RegisterHandler((conn, msg) => { }, false); - NetworkServer.RegisterHandler((conn, msg) => { }, false); NetworkServer.RegisterHandler((conn, msg) => { }, false); // add one custom message handler @@ -722,8 +700,6 @@ public void ActivateHostSceneCallsOnStartClient() public void SendToAllTest() { // message handlers - NetworkServer.RegisterHandler((conn, msg) => { }, false); - NetworkServer.RegisterHandler((conn, msg) => { }, false); NetworkServer.RegisterHandler((conn, msg) => { }, false); // listen @@ -758,8 +734,6 @@ public void SendToAllTest() public void RegisterUnregisterClearHandlerTest() { // message handlers that are needed for the test - NetworkServer.RegisterHandler((conn, msg) => { }, false); - NetworkServer.RegisterHandler((conn, msg) => { }, false); NetworkServer.RegisterHandler((conn, msg) => { }, false); @@ -806,7 +780,6 @@ public void RegisterUnregisterClearHandlerTest() // unregister second handler via ClearHandlers to test that one too. send, should fail NetworkServer.ClearHandlers(); // (only add this one to avoid disconnect error) - NetworkServer.RegisterHandler((conn, msg) => { }, false); writer = new NetworkWriter(); MessagePacker.Pack(new TestMessage1(), writer); // log error messages are expected @@ -821,8 +794,6 @@ public void RegisterUnregisterClearHandlerTest() public void SendToClientOfPlayer() { // message handlers - NetworkServer.RegisterHandler((conn, msg) => { }, false); - NetworkServer.RegisterHandler((conn, msg) => { }, false); NetworkServer.RegisterHandler((conn, msg) => { }, false); // listen @@ -899,8 +870,6 @@ public void GetNetworkIdentityErrorIfNotFound() public void ShowForConnection() { // message handlers - NetworkServer.RegisterHandler((conn, msg) => { }, false); - NetworkServer.RegisterHandler((conn, msg) => { }, false); NetworkServer.RegisterHandler((conn, msg) => { }, false); // listen @@ -951,8 +920,6 @@ public void ShowForConnection() public void HideForConnection() { // message handlers - NetworkServer.RegisterHandler((conn, msg) => { }, false); - NetworkServer.RegisterHandler((conn, msg) => { }, false); NetworkServer.RegisterHandler((conn, msg) => { }, false); // listen @@ -1089,8 +1056,6 @@ public void UnSpawn() public void ShutdownCleanupTest() { // message handlers - NetworkServer.RegisterHandler((conn, msg) => { }, false); - NetworkServer.RegisterHandler((conn, msg) => { }, false); NetworkServer.RegisterHandler((conn, msg) => { }, false); // listen diff --git a/Assets/Mirror/Tests/Editor/RemoteTestBase.cs b/Assets/Mirror/Tests/Editor/RemoteTestBase.cs index 9ad3a2d1e6..292c31ecfa 100644 --- a/Assets/Mirror/Tests/Editor/RemoteTestBase.cs +++ b/Assets/Mirror/Tests/Editor/RemoteTestBase.cs @@ -13,6 +13,11 @@ public void Setup() { Transport.activeTransport = new GameObject().AddComponent(); + // shutdown has to be called after activeTransport is set + // make sure shutdown correctly before running test + NetworkClient.Shutdown(); + NetworkServer.Shutdown(); + // start server/client NetworkServer.Listen(1); NetworkClient.ConnectHost(); From a8e02adb41fa6f28956d4f8c916962d0f285cd57 Mon Sep 17 00:00:00 2001 From: James Frowen Date: Sat, 7 Nov 2020 14:14:59 +0000 Subject: [PATCH 02/14] Obsoleting InvokeHandler - removing body (removes references to calls) - adding ObsoleteAttribute --- Assets/Mirror/Runtime/NetworkConnection.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Assets/Mirror/Runtime/NetworkConnection.cs b/Assets/Mirror/Runtime/NetworkConnection.cs index ac746bfff6..44a79ecb91 100644 --- a/Assets/Mirror/Runtime/NetworkConnection.cs +++ b/Assets/Mirror/Runtime/NetworkConnection.cs @@ -203,6 +203,8 @@ internal bool InvokeHandler(int msgType, NetworkReader reader, int channelId) /// The message type to unregister. /// The message object to process. /// Returns true if the handler was successfully invoked + // todo remove this in 3 months, Obsoleted 2020-11-07 + [Obsolete("InvokeHandler is only used to invoke messages locally, instead call the methods directly. see https://github.com/vis2k/Mirror/pull/2397")] public bool InvokeHandler(T msg, int channelId) where T : struct, NetworkMessage { From 3b419e9ae29e3b093c4aca814578a3ceebcf10a4 Mon Sep 17 00:00:00 2001 From: James Frowen Date: Sat, 7 Nov 2020 14:15:15 +0000 Subject: [PATCH 03/14] adding note and updating message for messages --- Assets/Mirror/Runtime/Messages.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Assets/Mirror/Runtime/Messages.cs b/Assets/Mirror/Runtime/Messages.cs index 9b8f489a44..972452fa3f 100644 --- a/Assets/Mirror/Runtime/Messages.cs +++ b/Assets/Mirror/Runtime/Messages.cs @@ -30,10 +30,12 @@ public struct NotReadyMessage : NetworkMessage { } public struct AddPlayerMessage : NetworkMessage { } - [System.Obsolete("this message is never sent over network, replace with an event", true)] + // todo remove this in 3 months, Obsoleted 2020-11-07 + [System.Obsolete("This message is never sent over network, Replaced with NetworkServer.OnDisconnected and NetworkClient.OnDisconnected events", true)] public struct DisconnectMessage : NetworkMessage { } - [System.Obsolete("this message is never sent over network, replace with an event", true)] + // todo remove this in 3 months, Obsoleted 2020-11-07 + [System.Obsolete("This message is never sent over network, Replaced with NetworkServer.OnConnected and NetworkClient.OnConnected events", true)] public struct ConnectMessage : NetworkMessage { } public struct SceneMessage : NetworkMessage From 09c72d02a825ac58ec94e1c79aaaa222ab75244f Mon Sep 17 00:00:00 2001 From: James Frowen Date: Sat, 7 Nov 2020 14:15:52 +0000 Subject: [PATCH 04/14] renaming events and functions - function not have Internal sufix - events no longer have even sufix --- Assets/Mirror/Runtime/NetworkClient.cs | 34 +++++++++++++------ Assets/Mirror/Runtime/NetworkManager.cs | 8 ++--- Assets/Mirror/Runtime/NetworkServer.cs | 19 ++++++----- .../Mirror/Tests/Editor/NetworkServerTest.cs | 4 +-- .../NetworkServerWithHostRuntimeTest.cs | 4 +-- 5 files changed, 42 insertions(+), 27 deletions(-) diff --git a/Assets/Mirror/Runtime/NetworkClient.cs b/Assets/Mirror/Runtime/NetworkClient.cs index e05038ac8c..024abc4971 100644 --- a/Assets/Mirror/Runtime/NetworkClient.cs +++ b/Assets/Mirror/Runtime/NetworkClient.cs @@ -33,8 +33,6 @@ public static class NetworkClient public static NetworkConnection connection { get; internal set; } internal static ConnectState connectState = ConnectState.None; - internal static event ConnectionEvent OnConnectedEvent; - internal static event ConnectionEvent OnDisconnectEvent; /// /// The IP address of the server that this client is connected to. @@ -58,6 +56,16 @@ public static class NetworkClient /// public static bool isLocalClient => connection is ULocalConnectionToServer; + /// + /// Called after successfully connected to server + /// + public static event ConnectionEvent OnConnected; + + /// + /// Called after disconnected from server + /// + public static event ConnectionEvent OnDisconnected; + /// /// Connect client to a NetworkServer instance. /// @@ -126,10 +134,10 @@ public static void ConnectHost() /// public static void ConnectLocalServer() { - NetworkServer.OnConnected(NetworkServer.localConnection); + NetworkServer.OnConnectedInternal(NetworkServer.localConnection); Debug.Assert(connection is ULocalConnectionToServer, "Connection should be local connection"); - OnConnectedEvent?.Invoke(connection); + OnConnected?.Invoke(connection); } /// @@ -145,7 +153,7 @@ public static void DisconnectLocalServer() // 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); + NetworkServer.OnDisconnectedInternal(NetworkServer.localConnection.connectionId); } } @@ -162,13 +170,16 @@ static void OnError(Exception exception) logger.LogException(exception); } - static void OnDisconnected() + /// + /// Handles OnClientDisconnected from transport + /// + static void OnDisconnectedInternal() { connectState = ConnectState.Disconnected; ClientScene.HandleClientDisconnect(connection); - OnDisconnectEvent?.Invoke(connection); + OnDisconnected?.Invoke(connection); } internal static void OnDataReceived(ArraySegment data, int channelId) @@ -180,7 +191,10 @@ internal static void OnDataReceived(ArraySegment data, int channelId) else logger.LogError("Skipped Data message handling because connection is null."); } - static void OnConnected() + /// + /// Handles OnClientConnected from transport + /// + static void OnConnectedInternal() { if (connection != null) { @@ -191,7 +205,7 @@ static void OnConnected() // thus we should set the connected state before calling the handler connectState = ConnectState.Connected; NetworkTime.UpdateClient(); - OnConnectedEvent?.Invoke(connection); + OnConnected?.Invoke(connection); } else logger.LogError("Skipped Connect message handling because connection is null."); } @@ -211,7 +225,7 @@ public static void Disconnect() if (isConnected) { Debug.Assert(connection is ULocalConnectionToServer, "Connection should be local connection"); - OnDisconnectEvent?.Invoke(connection); + OnDisconnected?.Invoke(connection); } NetworkServer.RemoveLocalConnection(); } diff --git a/Assets/Mirror/Runtime/NetworkManager.cs b/Assets/Mirror/Runtime/NetworkManager.cs index f1f1749f1e..946fb73c28 100644 --- a/Assets/Mirror/Runtime/NetworkManager.cs +++ b/Assets/Mirror/Runtime/NetworkManager.cs @@ -750,8 +750,8 @@ bool InitializeSingleton() void RegisterServerMessages() { - NetworkServer.OnConnectedEvent += OnServerConnectInternal; - NetworkServer.OnDisconnectEvent += OnServerDisconnectInternal; + NetworkServer.OnConnected += OnServerConnectInternal; + NetworkServer.OnDisconnected += OnServerDisconnectInternal; NetworkServer.RegisterHandler(OnServerAddPlayerInternal); NetworkServer.RegisterHandler(OnServerErrorInternal, false); @@ -761,8 +761,8 @@ void RegisterServerMessages() void RegisterClientMessages() { - NetworkClient.OnConnectedEvent += OnClientConnectInternal; - NetworkClient.OnDisconnectEvent += OnClientDisconnectInternal; + NetworkClient.OnConnected += OnClientConnectInternal; + NetworkClient.OnDisconnected += OnClientDisconnectInternal; NetworkClient.RegisterHandler(OnClientNotReadyMessageInternal); NetworkClient.RegisterHandler(OnClientErrorInternal, false); NetworkClient.RegisterHandler(OnClientSceneInternal, false); diff --git a/Assets/Mirror/Runtime/NetworkServer.cs b/Assets/Mirror/Runtime/NetworkServer.cs index 4f93596107..83eaa0486b 100644 --- a/Assets/Mirror/Runtime/NetworkServer.cs +++ b/Assets/Mirror/Runtime/NetworkServer.cs @@ -75,11 +75,12 @@ public static class NetworkServer /// /// Called after new connection is set up on server /// - public static event ConnectionEvent OnConnectedEvent; + public static event ConnectionEvent OnConnected; + /// /// Called after connection disconnects from server /// - public static event ConnectionEvent OnDisconnectEvent; + public static event ConnectionEvent OnDisconnected; /// /// This shuts down the server and disconnects all clients. @@ -504,7 +505,7 @@ static void AddTransportHandlers() Transport.activeTransport.OnServerError = OnError; } - static void OnConnected(int connectionId) + static void OnConnectedInternal(int connectionId) { if (logger.LogEnabled()) logger.Log("Server accepted client:" + connectionId); @@ -543,16 +544,16 @@ static void OnConnected(int connectionId) } } - internal static void OnConnected(NetworkConnectionToClient conn) + internal static void OnConnectedInternal(NetworkConnectionToClient conn) { if (logger.LogEnabled()) logger.Log("Server accepted client:" + conn); // add connection and invoke connected event AddConnection(conn); - OnConnectedEvent?.Invoke(conn); + OnConnected?.Invoke(conn); } - internal static void OnDisconnected(int connectionId) + internal static void OnDisconnectedInternal(int connectionId) { if (logger.LogEnabled()) logger.Log("Server disconnect client:" + connectionId); @@ -562,14 +563,14 @@ internal static void OnDisconnected(int connectionId) RemoveConnection(connectionId); if (logger.LogEnabled()) logger.Log("Server lost client:" + connectionId); - OnDisconnected(conn); + OnDisconnectedInternal(conn); } } - static void OnDisconnected(NetworkConnection conn) + static void OnDisconnectedInternal(NetworkConnection conn) { // todo change to event instead of message - OnDisconnectEvent?.Invoke(conn); + OnDisconnected?.Invoke(conn); if (logger.LogEnabled()) logger.Log("Server lost client:" + conn); } diff --git a/Assets/Mirror/Tests/Editor/NetworkServerTest.cs b/Assets/Mirror/Tests/Editor/NetworkServerTest.cs index e86e7f9c67..db01d6d2b5 100644 --- a/Assets/Mirror/Tests/Editor/NetworkServerTest.cs +++ b/Assets/Mirror/Tests/Editor/NetworkServerTest.cs @@ -139,7 +139,7 @@ public void ConnectMessageHandlerTest() { // message handlers bool connectCalled = false; - NetworkServer.OnConnectedEvent += (conn) => { connectCalled = true; }; + NetworkServer.OnConnected += (conn) => { connectCalled = true; }; NetworkServer.RegisterHandler((conn, msg) => { }, false); // listen @@ -156,7 +156,7 @@ public void DisconnectMessageHandlerTest() { // message handlers bool disconnectCalled = false; - NetworkServer.OnDisconnectEvent += (conn) => { disconnectCalled = true; }; + NetworkServer.OnDisconnected += (conn) => { disconnectCalled = true; }; NetworkServer.RegisterHandler((conn, msg) => { }, false); // listen diff --git a/Assets/Mirror/Tests/Runtime/NetworkServerWithHostRuntimeTest.cs b/Assets/Mirror/Tests/Runtime/NetworkServerWithHostRuntimeTest.cs index e967062723..701fd76fb1 100644 --- a/Assets/Mirror/Tests/Runtime/NetworkServerWithHostRuntimeTest.cs +++ b/Assets/Mirror/Tests/Runtime/NetworkServerWithHostRuntimeTest.cs @@ -1,4 +1,4 @@ -using System.Collections; +using System.Collections; using NUnit.Framework; using UnityEngine; using UnityEngine.TestTools; @@ -56,7 +56,7 @@ public IEnumerator DisconnectTimeoutTest() const int remoteConnectionId = 1; const int localConnectionId = 0; NetworkConnectionToClient remoteConnection = new NetworkConnectionToClient(remoteConnectionId); - NetworkServer.OnConnected(remoteConnection); + NetworkServer.OnConnectedInternal(remoteConnection); NetworkServer.AddPlayerForConnection(remoteConnection, remotePlayer); // There's a host player from HostSetup + remotePlayer From 901fcf05a223e8eee2b6ee8fd38cebe6dab5e2ff Mon Sep 17 00:00:00 2001 From: James Frowen Date: Sun, 8 Nov 2020 01:33:36 +0000 Subject: [PATCH 05/14] moving test to runtime folder --- .../NetworkManagerStopHostOnServerDisconnectTest.cs | 2 ++ .../NetworkManagerStopHostOnServerDisconnectTest.cs.meta | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) rename Assets/Mirror/Tests/{Editor => Runtime}/NetworkManagerStopHostOnServerDisconnectTest.cs (95%) rename Assets/Mirror/Tests/{Editor => Runtime}/NetworkManagerStopHostOnServerDisconnectTest.cs.meta (83%) diff --git a/Assets/Mirror/Tests/Editor/NetworkManagerStopHostOnServerDisconnectTest.cs b/Assets/Mirror/Tests/Runtime/NetworkManagerStopHostOnServerDisconnectTest.cs similarity index 95% rename from Assets/Mirror/Tests/Editor/NetworkManagerStopHostOnServerDisconnectTest.cs rename to Assets/Mirror/Tests/Runtime/NetworkManagerStopHostOnServerDisconnectTest.cs index ff7ce6b370..437b12814e 100644 --- a/Assets/Mirror/Tests/Editor/NetworkManagerStopHostOnServerDisconnectTest.cs +++ b/Assets/Mirror/Tests/Runtime/NetworkManagerStopHostOnServerDisconnectTest.cs @@ -36,6 +36,8 @@ public void TearDown() { GameObject.DestroyImmediate(gameObject); GameObject.DestroyImmediate(playerPrefab); + NetworkServer.Shutdown(); + NetworkClient.Shutdown(); } // test to prevent https://github.com/vis2k/Mirror/issues/1515 diff --git a/Assets/Mirror/Tests/Editor/NetworkManagerStopHostOnServerDisconnectTest.cs.meta b/Assets/Mirror/Tests/Runtime/NetworkManagerStopHostOnServerDisconnectTest.cs.meta similarity index 83% rename from Assets/Mirror/Tests/Editor/NetworkManagerStopHostOnServerDisconnectTest.cs.meta rename to Assets/Mirror/Tests/Runtime/NetworkManagerStopHostOnServerDisconnectTest.cs.meta index b21084477b..d5358b644c 100644 --- a/Assets/Mirror/Tests/Editor/NetworkManagerStopHostOnServerDisconnectTest.cs.meta +++ b/Assets/Mirror/Tests/Runtime/NetworkManagerStopHostOnServerDisconnectTest.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 2f583081473a64b92b971678e571382a +guid: 63a3472b4e4641342b092337b5065b1b MonoImporter: externalObjects: {} serializedVersion: 2 From 49ae04b9118090acd335cd89a5ad6862b9bb05bc Mon Sep 17 00:00:00 2001 From: James Frowen Date: Sun, 8 Nov 2020 01:34:01 +0000 Subject: [PATCH 06/14] setting Transport.activeTransport and shutdown after test --- Assets/Mirror/Tests/Editor/NetworkManagerTest.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Assets/Mirror/Tests/Editor/NetworkManagerTest.cs b/Assets/Mirror/Tests/Editor/NetworkManagerTest.cs index 11f73a8522..d4c877b42f 100644 --- a/Assets/Mirror/Tests/Editor/NetworkManagerTest.cs +++ b/Assets/Mirror/Tests/Editor/NetworkManagerTest.cs @@ -14,13 +14,15 @@ public class NetworkManagerTest public void SetupNetworkManager() { gameObject = new GameObject(); - gameObject.AddComponent(); + Transport.activeTransport = gameObject.AddComponent(); manager = gameObject.AddComponent(); } [TearDown] public void TearDownNetworkManager() { + NetworkServer.Shutdown(); + NetworkClient.Shutdown(); GameObject.DestroyImmediate(gameObject); } From b0085043756f457ff47bdf9e25f563954ada9ab9 Mon Sep 17 00:00:00 2001 From: James Frowen Date: Sun, 8 Nov 2020 01:34:58 +0000 Subject: [PATCH 07/14] removing handlers on stop --- Assets/Mirror/Runtime/NetworkManager.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Assets/Mirror/Runtime/NetworkManager.cs b/Assets/Mirror/Runtime/NetworkManager.cs index 946fb73c28..7160697859 100644 --- a/Assets/Mirror/Runtime/NetworkManager.cs +++ b/Assets/Mirror/Runtime/NetworkManager.cs @@ -1,10 +1,10 @@ using System; using System.Collections.Generic; using System.Linq; +using kcp2k; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.Serialization; -using kcp2k; namespace Mirror { @@ -623,6 +623,8 @@ public void StopServer() logger.Log("NetworkManager StopServer"); isNetworkActive = false; + NetworkServer.OnConnected -= OnServerConnectInternal; + NetworkServer.OnDisconnected -= OnServerDisconnectInternal; NetworkServer.Shutdown(); // set offline mode BEFORE changing scene so that FinishStartScene @@ -656,6 +658,8 @@ public void StopClient() isNetworkActive = false; // shutdown client + NetworkClient.OnConnected -= OnClientConnectInternal; + NetworkClient.OnDisconnected -= OnClientDisconnectInternal; NetworkClient.Disconnect(); NetworkClient.Shutdown(); From e55671da6e7507fcd9b97f829799bb07472ee8c3 Mon Sep 17 00:00:00 2001 From: James Frowen Date: Wed, 11 Nov 2020 00:17:56 +0000 Subject: [PATCH 08/14] temp --- Assets/Mirror/Runtime/NetworkClient.cs | 8 +++- Assets/Mirror/Runtime/NetworkServer.cs | 7 +++- Assets/Mirror/Tests/Common/MemoryTransport.cs | 8 ---- .../Mirror/Tests/Editor/NetworkClientTests.cs | 37 +++++++++++++++++++ 4 files changed, 48 insertions(+), 12 deletions(-) diff --git a/Assets/Mirror/Runtime/NetworkClient.cs b/Assets/Mirror/Runtime/NetworkClient.cs index 024abc4971..c520e631ef 100644 --- a/Assets/Mirror/Runtime/NetworkClient.cs +++ b/Assets/Mirror/Runtime/NetworkClient.cs @@ -194,7 +194,7 @@ internal static void OnDataReceived(ArraySegment data, int channelId) /// /// Handles OnClientConnected from transport /// - static void OnConnectedInternal() + internal static void OnConnectedInternal() { if (connection != null) { @@ -393,7 +393,11 @@ public static void Shutdown() // we do NOT call Transport.Shutdown, because someone only called // NetworkClient.Shutdown. we can't assume that the server is // supposed to be shut down too! - Transport.activeTransport.ClientDisconnect(); + Transport.activeTransport?.ClientDisconnect(); + + // clear event listeners + OnConnected = null; + OnDisconnected = null; } } } diff --git a/Assets/Mirror/Runtime/NetworkServer.cs b/Assets/Mirror/Runtime/NetworkServer.cs index 83eaa0486b..57c03a9872 100644 --- a/Assets/Mirror/Runtime/NetworkServer.cs +++ b/Assets/Mirror/Runtime/NetworkServer.cs @@ -108,6 +108,9 @@ public static void Shutdown() CleanupNetworkIdentities(); NetworkIdentity.ResetNextNetworkId(); + + OnConnected = null; + OnDisconnected = null; } static void CleanupNetworkIdentities() @@ -437,7 +440,7 @@ public static void DisconnectAllConnections() conn.Disconnect(); // call OnDisconnected unless local player in host mode if (conn.connectionId != NetworkConnection.LocalConnectionId) - OnDisconnected(conn); + OnDisconnectedInternal(conn); } connections.Clear(); } @@ -534,7 +537,7 @@ static void OnConnectedInternal(int connectionId) { // add connection NetworkConnectionToClient conn = new NetworkConnectionToClient(connectionId); - OnConnected(conn); + OnConnectedInternal(conn); } else { diff --git a/Assets/Mirror/Tests/Common/MemoryTransport.cs b/Assets/Mirror/Tests/Common/MemoryTransport.cs index ebd5c21d1d..78d3c381f0 100644 --- a/Assets/Mirror/Tests/Common/MemoryTransport.cs +++ b/Assets/Mirror/Tests/Common/MemoryTransport.cs @@ -124,20 +124,12 @@ public override void ServerSend(int connectionId, int channelId, ArraySegment { called++; }; + + NetworkClient.Connect("localhost"); + ((MemoryTransport)Transport.activeTransport).LateUpdate(); + + Assert.That(called, Is.EqualTo(1)); + + // remove events + NetworkClient.Disconnect(); + NetworkClient.Shutdown(); + + NetworkClient.Connect("localhost"); + ((MemoryTransport)Transport.activeTransport).LateUpdate(); + + Assert.That(called, Is.EqualTo(1), "Call count should still be 1"); + + // reset + NetworkClient.Disconnect(); + NetworkClient.Shutdown(); + + + // add event again + NetworkClient.OnConnected += (c) => + { + called++; + }; + + NetworkClient.Connect("localhost"); + ((MemoryTransport)Transport.activeTransport).LateUpdate(); + + Assert.That(called, Is.EqualTo(2), "Call count should now be 2"); + } } } From ce6ce4db11d15e5144fe01d88787f6806e12a20c Mon Sep 17 00:00:00 2001 From: James Frowen Date: Wed, 25 Nov 2020 23:51:37 +0000 Subject: [PATCH 09/14] fixing function name --- Assets/Mirror/Runtime/NetworkClient.cs | 10 +++++----- Assets/Mirror/Runtime/NetworkServer.cs | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Assets/Mirror/Runtime/NetworkClient.cs b/Assets/Mirror/Runtime/NetworkClient.cs index c520e631ef..b066431fa5 100644 --- a/Assets/Mirror/Runtime/NetworkClient.cs +++ b/Assets/Mirror/Runtime/NetworkClient.cs @@ -153,15 +153,15 @@ public static void DisconnectLocalServer() // 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.OnDisconnectedInternal(NetworkServer.localConnection.connectionId); + NetworkServer.OnDisconnectedTransport(NetworkServer.localConnection.connectionId); } } static void AddTransportHandlers() { - Transport.activeTransport.OnClientConnected = OnConnected; + Transport.activeTransport.OnClientConnected = OnConnectedTransport; Transport.activeTransport.OnClientDataReceived = OnDataReceived; - Transport.activeTransport.OnClientDisconnected = OnDisconnected; + Transport.activeTransport.OnClientDisconnected = OnDisconnectedTransport; Transport.activeTransport.OnClientError = OnError; } @@ -173,7 +173,7 @@ static void OnError(Exception exception) /// /// Handles OnClientDisconnected from transport /// - static void OnDisconnectedInternal() + static void OnDisconnectedTransport() { connectState = ConnectState.Disconnected; @@ -194,7 +194,7 @@ internal static void OnDataReceived(ArraySegment data, int channelId) /// /// Handles OnClientConnected from transport /// - internal static void OnConnectedInternal() + internal static void OnConnectedTransport() { if (connection != null) { diff --git a/Assets/Mirror/Runtime/NetworkServer.cs b/Assets/Mirror/Runtime/NetworkServer.cs index 57c03a9872..6763b6b486 100644 --- a/Assets/Mirror/Runtime/NetworkServer.cs +++ b/Assets/Mirror/Runtime/NetworkServer.cs @@ -502,13 +502,13 @@ static void CheckForInactiveConnections() static void AddTransportHandlers() { - Transport.activeTransport.OnServerConnected = OnConnected; + Transport.activeTransport.OnServerConnected = OnConnectedTransport; Transport.activeTransport.OnServerDataReceived = OnDataReceived; - Transport.activeTransport.OnServerDisconnected = OnDisconnected; + Transport.activeTransport.OnServerDisconnected = OnDisconnectedTransport; Transport.activeTransport.OnServerError = OnError; } - static void OnConnectedInternal(int connectionId) + static void OnConnectedTransport(int connectionId) { if (logger.LogEnabled()) logger.Log("Server accepted client:" + connectionId); @@ -556,7 +556,7 @@ internal static void OnConnectedInternal(NetworkConnectionToClient conn) OnConnected?.Invoke(conn); } - internal static void OnDisconnectedInternal(int connectionId) + internal static void OnDisconnectedTransport(int connectionId) { if (logger.LogEnabled()) logger.Log("Server disconnect client:" + connectionId); From c5a18493de12a39b13f6841ce4cdfd738a526f8d Mon Sep 17 00:00:00 2001 From: James Frowen Date: Wed, 25 Nov 2020 23:58:12 +0000 Subject: [PATCH 10/14] adding comment --- Assets/Mirror/Runtime/NetworkClient.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Assets/Mirror/Runtime/NetworkClient.cs b/Assets/Mirror/Runtime/NetworkClient.cs index b066431fa5..3c42b4aef0 100644 --- a/Assets/Mirror/Runtime/NetworkClient.cs +++ b/Assets/Mirror/Runtime/NetworkClient.cs @@ -137,6 +137,7 @@ public static void ConnectLocalServer() NetworkServer.OnConnectedInternal(NetworkServer.localConnection); Debug.Assert(connection is ULocalConnectionToServer, "Connection should be local connection"); + // OnConnected should be called with connToServer OnConnected?.Invoke(connection); } From ce1018539246b9546708a3539f8e2f9698d7d896 Mon Sep 17 00:00:00 2001 From: James Frowen Date: Thu, 26 Nov 2020 00:09:28 +0000 Subject: [PATCH 11/14] updating message --- Assets/Mirror/Runtime/NetworkConnection.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/Mirror/Runtime/NetworkConnection.cs b/Assets/Mirror/Runtime/NetworkConnection.cs index 44a79ecb91..d0f9883a55 100644 --- a/Assets/Mirror/Runtime/NetworkConnection.cs +++ b/Assets/Mirror/Runtime/NetworkConnection.cs @@ -204,7 +204,7 @@ internal bool InvokeHandler(int msgType, NetworkReader reader, int channelId) /// The message object to process. /// Returns true if the handler was successfully invoked // todo remove this in 3 months, Obsoleted 2020-11-07 - [Obsolete("InvokeHandler is only used to invoke messages locally, instead call the methods directly. see https://github.com/vis2k/Mirror/pull/2397")] + [Obsolete("InvokeHandler is only used to invoke messages locally and will be removed soon, instead call the methods directly. see https://github.com/vis2k/Mirror/pull/2397")] public bool InvokeHandler(T msg, int channelId) where T : struct, NetworkMessage { From 53ea0d3d89d83dfa68498c53a503d64673d296dd Mon Sep 17 00:00:00 2001 From: James Frowen Date: Thu, 26 Nov 2020 00:09:34 +0000 Subject: [PATCH 12/14] server comments --- Assets/Mirror/Runtime/NetworkServer.cs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Assets/Mirror/Runtime/NetworkServer.cs b/Assets/Mirror/Runtime/NetworkServer.cs index 6763b6b486..6e596b7338 100644 --- a/Assets/Mirror/Runtime/NetworkServer.cs +++ b/Assets/Mirror/Runtime/NetworkServer.cs @@ -109,6 +109,7 @@ public static void Shutdown() CleanupNetworkIdentities(); NetworkIdentity.ResetNextNetworkId(); + // clear event listeners OnConnected = null; OnDisconnected = null; } @@ -508,6 +509,9 @@ static void AddTransportHandlers() Transport.activeTransport.OnServerError = OnError; } + /// + /// Handles OnServerConnected from transport + /// static void OnConnectedTransport(int connectionId) { if (logger.LogEnabled()) logger.Log("Server accepted client:" + connectionId); @@ -547,6 +551,9 @@ static void OnConnectedTransport(int connectionId) } } + /// + /// Handles OnConnectedTransport and host connection + /// internal static void OnConnectedInternal(NetworkConnectionToClient conn) { if (logger.LogEnabled()) logger.Log("Server accepted client:" + conn); @@ -556,6 +563,9 @@ internal static void OnConnectedInternal(NetworkConnectionToClient conn) OnConnected?.Invoke(conn); } + /// + /// Handles OnServerDisconnected from transport + /// internal static void OnDisconnectedTransport(int connectionId) { if (logger.LogEnabled()) logger.Log("Server disconnect client:" + connectionId); @@ -570,9 +580,11 @@ internal static void OnDisconnectedTransport(int connectionId) } } + /// + /// Handles OnDisconnectedTransport and host connection + /// static void OnDisconnectedInternal(NetworkConnection conn) { - // todo change to event instead of message OnDisconnected?.Invoke(conn); if (logger.LogEnabled()) logger.Log("Server lost client:" + conn); } From 1c7156fca23aa23b3baca627bd76b0fd32ceb86f Mon Sep 17 00:00:00 2001 From: James Frowen Date: Thu, 26 Nov 2020 00:09:40 +0000 Subject: [PATCH 13/14] fixing compile errors --- Assets/Mirror/Tests/Editor/MessagePackerTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/Mirror/Tests/Editor/MessagePackerTest.cs b/Assets/Mirror/Tests/Editor/MessagePackerTest.cs index d52f260d61..b3f29d268e 100644 --- a/Assets/Mirror/Tests/Editor/MessagePackerTest.cs +++ b/Assets/Mirror/Tests/Editor/MessagePackerTest.cs @@ -63,7 +63,7 @@ public void UnpackWrongMessage() Assert.Throws(() => { - Message2 unpacked = MessagePacker.Unpack(data); + Message2 unpacked = UnpackFromByteArray(data); }); } From 248609e2c3a0f47ce5a83806a6b8c3faca555f78 Mon Sep 17 00:00:00 2001 From: James Frowen Date: Thu, 26 Nov 2020 00:37:17 +0000 Subject: [PATCH 14/14] fixing test --- ...etworkManagerStopHostOnServerDisconnectTest.cs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Assets/Mirror/Tests/Runtime/NetworkManagerStopHostOnServerDisconnectTest.cs b/Assets/Mirror/Tests/Runtime/NetworkManagerStopHostOnServerDisconnectTest.cs index 437b12814e..88ec364695 100644 --- a/Assets/Mirror/Tests/Runtime/NetworkManagerStopHostOnServerDisconnectTest.cs +++ b/Assets/Mirror/Tests/Runtime/NetworkManagerStopHostOnServerDisconnectTest.cs @@ -1,8 +1,10 @@ using System; +using System.Collections; using NUnit.Framework; using UnityEngine; +using UnityEngine.TestTools; -namespace Mirror.Tests +namespace Mirror.Tests.Runtime { class NetworkManagerOnServerDisconnect : NetworkManager { @@ -22,7 +24,7 @@ public class NetworkManagerStopHostOnServerDisconnectTest public void SetUp() { gameObject = new GameObject(); - transport = gameObject.AddComponent(); + transport= gameObject.AddComponent(); manager = gameObject.AddComponent(); playerPrefab = new GameObject("player"); NetworkIdentity id = playerPrefab.AddComponent(); @@ -38,12 +40,17 @@ public void TearDown() GameObject.DestroyImmediate(playerPrefab); NetworkServer.Shutdown(); NetworkClient.Shutdown(); + Transport.activeTransport = null; } // test to prevent https://github.com/vis2k/Mirror/issues/1515 - [Test] - public void StopHostCallsOnServerDisconnectForHostClient() + [UnityTest] + public IEnumerator StopHostCallsOnServerDisconnectForHostClient() { + // wait for awake to be called before setting activeTransport + yield return null; + Transport.activeTransport = transport; + // 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));