From fa85e53f0508050c421248a9350680fe7454f0c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albin=20Cor=C3=A9n?= <2108U9@gmail.com> Date: Mon, 8 Jan 2018 08:09:58 +0100 Subject: [PATCH] Added shutdown functions and changed protection level of API --- .../MonoBehaviours/Core/NetworkedBehaviour.cs | 22 ++++----- .../MonoBehaviours/Core/NetworkingManager.cs | 46 ++++++++++++++++++- 2 files changed, 56 insertions(+), 12 deletions(-) diff --git a/MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs b/MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs index e8910c9..9741f0b 100644 --- a/MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs +++ b/MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs @@ -47,7 +47,7 @@ namespace MLAPI } } private NetworkedObject _networkedObject = null; - public uint objectNetworkId + public uint networkId { get { @@ -66,14 +66,14 @@ namespace MLAPI //Change data type private Dictionary registeredMessageHandlers = new Dictionary(); - public int RegisterMessageHandler(string name, Action action) + protected int RegisterMessageHandler(string name, Action action) { int counter = MessageManager.AddIncomingMessageHandler(name, action); registeredMessageHandlers.Add(name, counter); return counter; } - public void DeregisterMessageHandler(string name, int counter) + protected void DeregisterMessageHandler(string name, int counter) { MessageManager.RemoveIncomingMessageHandler(name, counter); } @@ -86,7 +86,7 @@ namespace MLAPI } } - public void SendToServer(string messageType, string channelName, byte[] data) + protected void SendToServer(string messageType, string channelName, byte[] data) { if (isServer) { @@ -98,7 +98,7 @@ namespace MLAPI } } - public void SendToLocalClient(string messageType, string channelName, byte[] data) + protected void SendToLocalClient(string messageType, string channelName, byte[] data) { if (!isServer) { @@ -108,7 +108,7 @@ namespace MLAPI NetworkingManager.singleton.Send(ownerClientId, messageType, channelName, data); } - public void SendToNonLocalClients(string messageType, string channelName, byte[] data) + protected void SendToNonLocalClients(string messageType, string channelName, byte[] data) { if (!isServer) { @@ -118,7 +118,7 @@ namespace MLAPI NetworkingManager.singleton.Send(messageType, channelName, data, ownerClientId); } - public void SendToClient(int clientId, string messageType, string channelName, byte[] data) + protected void SendToClient(int clientId, string messageType, string channelName, byte[] data) { if (!isServer) { @@ -128,7 +128,7 @@ namespace MLAPI NetworkingManager.singleton.Send(clientId, messageType, channelName, data); } - public void SendToClients(int[] clientIds, string messageType, string channelName, byte[] data) + protected void SendToClients(int[] clientIds, string messageType, string channelName, byte[] data) { if (!isServer) { @@ -138,7 +138,7 @@ namespace MLAPI NetworkingManager.singleton.Send(clientIds, messageType, channelName, data); } - public void SendToClients(List clientIds, string messageType, string channelName, byte[] data) + protected void SendToClients(List clientIds, string messageType, string channelName, byte[] data) { if (!isServer) { @@ -148,7 +148,7 @@ namespace MLAPI NetworkingManager.singleton.Send(clientIds, messageType, channelName, data); } - public void SendToClients(string messageType, string channelName, byte[] data) + protected void SendToClients(string messageType, string channelName, byte[] data) { if (!isServer) { @@ -158,7 +158,7 @@ namespace MLAPI NetworkingManager.singleton.Send(messageType, channelName, data); } - public NetworkedObject GetNetworkedObject(uint networkId) + protected NetworkedObject GetNetworkedObject(uint networkId) { return SpawnManager.spawnedObjects[networkId]; } diff --git a/MLAPI/MonoBehaviours/Core/NetworkingManager.cs b/MLAPI/MonoBehaviours/Core/NetworkingManager.cs index aa5c226..179e2ca 100644 --- a/MLAPI/MonoBehaviours/Core/NetworkingManager.cs +++ b/MLAPI/MonoBehaviours/Core/NetworkingManager.cs @@ -147,6 +147,41 @@ namespace MLAPI serverClientId = NetworkTransport.Connect(hostId, NetworkConfig.Address, NetworkConfig.Port, 0, out error); } + public void StopServer() + { + HashSet sentIds = new HashSet(); + //Don't know if I have to disconnect the clients. I'm assuming the NetworkTransport does all the cleaning on shtudown. But this way the clients get a disconnect message from server (so long it does't get lost) + foreach (KeyValuePair pair in connectedClients) + { + if(!sentIds.Contains(pair.Key)) + { + sentIds.Add(pair.Key); + NetworkTransport.Disconnect(hostId, pair.Key, out error); + } + } + foreach (int clientId in pendingClients) + { + if (!sentIds.Contains(clientId)) + { + sentIds.Add(clientId); + NetworkTransport.Disconnect(hostId, clientId, out error); + } + } + Shutdown(); + } + + public void StopHost() + { + StopServer(); + //We don't stop client since we dont actually have a transport connection to our own host. We just handle host messages directly in the MLAPI + } + + public void StopClient() + { + NetworkTransport.Disconnect(hostId, serverClientId, out error); + Shutdown(); + } + public void StartHost(NetworkingConfiguration netConfig) { ConnectionConfig cConfig = Init(netConfig); @@ -188,6 +223,15 @@ namespace MLAPI private void OnDestroy() { singleton = null; + Shutdown(); + } + + private void Shutdown() + { + isListening = false; + isClient = false; + isServer = false; + NetworkTransport.Shutdown(); } //Receive stuff @@ -602,7 +646,7 @@ namespace MLAPI } } - internal void DisconnectClient(int clientId) + private void DisconnectClient(int clientId) { if (!isServer) return;