From 75418c42a831c17487e1e152e7d94124f3ec0072 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albin=20Cor=C3=A9n?= Date: Tue, 13 Mar 2018 11:00:06 +0100 Subject: [PATCH] Added multihost support --- MLAPI/Data/NetworkingConfiguration.cs | 5 +- .../MonoBehaviours/Core/NetworkingManager.cs | 50 +++++++++++-------- .../LagCompensationManager.cs | 3 +- 3 files changed, 35 insertions(+), 23 deletions(-) diff --git a/MLAPI/Data/NetworkingConfiguration.cs b/MLAPI/Data/NetworkingConfiguration.cs index fe969cb..1e4ad0f 100644 --- a/MLAPI/Data/NetworkingConfiguration.cs +++ b/MLAPI/Data/NetworkingConfiguration.cs @@ -20,10 +20,13 @@ namespace MLAPI public int SendTickrate = 64; public int EventTickrate = 64; public int MaxConnections = 100; - public int Port = 7777; + public int UDPPort = 7777; + public int WebsocketPort = 7778; public string Address = "127.0.0.1"; public int ClientConnectionBufferTimeout = 10; public bool ConnectionApproval = false; + public bool UseUDPListener = true; + public bool UseWebsocketListener = false; public Action> ConnectionApprovalCallback = null; public byte[] ConnectionData = new byte[0]; public float SecondsHistory = 5; diff --git a/MLAPI/MonoBehaviours/Core/NetworkingManager.cs b/MLAPI/MonoBehaviours/Core/NetworkingManager.cs index 7bae11b..e290c1d 100644 --- a/MLAPI/MonoBehaviours/Core/NetworkingManager.cs +++ b/MLAPI/MonoBehaviours/Core/NetworkingManager.cs @@ -192,43 +192,38 @@ namespace MLAPI } } HostTopology hostTopology = new HostTopology(cConfig, NetworkConfig.MaxConnections); - hostId = NetworkTransport.AddHost(hostTopology, NetworkConfig.Port); + if (netConfig.UseUDPListener) + hostId = NetworkTransport.AddHost(hostTopology, NetworkConfig.UDPPort); + if (netConfig.UseWebsocketListener) + hostId = NetworkTransport.AddWebsocketHost(hostTopology, NetworkConfig.WebsocketPort); isServer = true; isClient = false; isListening = true; } - public void StartClient(NetworkingConfiguration netConfig) + public void StartClient(NetworkingConfiguration netConfig, bool websocket = false) { ConnectionConfig cConfig = Init(netConfig); HostTopology hostTopology = new HostTopology(cConfig, NetworkConfig.MaxConnections); - hostId = NetworkTransport.AddHost(hostTopology, 0, null); + if (!websocket) + hostId = NetworkTransport.AddHost(hostTopology, 0, null); + else + hostId = NetworkTransport.AddWebsocketHost(hostTopology, 0, null); isServer = false; isClient = true; isListening = true; - serverClientId = NetworkTransport.Connect(hostId, NetworkConfig.Address, NetworkConfig.Port, 0, out error); + int port = websocket ? NetworkConfig.WebsocketPort : NetworkConfig.UDPPort; + serverClientId = NetworkTransport.Connect(hostId, NetworkConfig.Address, 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); - } + NetworkTransport.Disconnect(ClientIdManager.GetClientIdKey(pair.Key).hostId, + ClientIdManager.GetClientIdKey(pair.Key).connectionId, out error); } Shutdown(); } @@ -256,7 +251,18 @@ namespace MLAPI } } HostTopology hostTopology = new HostTopology(cConfig, NetworkConfig.MaxConnections); - hostId = NetworkTransport.AddHost(hostTopology, NetworkConfig.Port, null); + if (netConfig.UseWebsocketListener) + { + hostId = NetworkTransport.AddWebsocketHost(hostTopology, NetworkConfig.WebsocketPort); + ClientIdManager.clientIdToKey.Add(-1, new ClientIdKey(hostId, -1)); + ClientIdManager.keyToClientId.Add(new ClientIdKey(hostId, -1), -1); + } + if (netConfig.UseUDPListener) + { + hostId = NetworkTransport.AddHost(hostTopology, NetworkConfig.UDPPort); + ClientIdManager.clientIdToKey.Add(-1, new ClientIdKey(hostId, -1)); + ClientIdManager.keyToClientId.Add(new ClientIdKey(hostId, -1), -1); + } isServer = true; isClient = true; isListening = true; @@ -297,7 +303,7 @@ namespace MLAPI } //Receive stuff - internal int hostId; + private int hostId; private int connectionId; private int channelId; private int receivedSize; @@ -313,7 +319,8 @@ namespace MLAPI { foreach (KeyValuePair pair in connectedClients) { - NetworkTransport.SendQueuedMessages(hostId, pair.Key, out error); + NetworkTransport.SendQueuedMessages(ClientIdManager.GetClientIdKey(pair.Key).hostId, + ClientIdManager.GetClientIdKey(pair.Key).connectionId, out error); } lastSendTickTime = Time.time; } @@ -324,6 +331,7 @@ namespace MLAPI do { processedEvents++; + int hostId; eventType = NetworkTransport.Receive(out hostId, out connectionId, out channelId, messageBuffer, messageBuffer.Length, out receivedSize, out error); NetworkError networkError = (NetworkError)error; if (networkError == NetworkError.Timeout) diff --git a/MLAPI/NetworkingManagerComponents/LagCompensationManager.cs b/MLAPI/NetworkingManagerComponents/LagCompensationManager.cs index de73c0d..ef1a289 100644 --- a/MLAPI/NetworkingManagerComponents/LagCompensationManager.cs +++ b/MLAPI/NetworkingManagerComponents/LagCompensationManager.cs @@ -38,7 +38,8 @@ namespace MLAPI.NetworkingManagerComponents Debug.LogWarning("MLAPI: Lag compensation simulations are only to be ran on the server."); return; } - float milisecondsDelay = NetworkTransport.GetCurrentRTT(NetworkingManager.singleton.hostId, clientId, out error) / 2f; + float milisecondsDelay = NetworkTransport.GetCurrentRTT(ClientIdManager.GetClientIdKey(clientId).hostId, + ClientIdManager.GetClientIdKey(clientId).connectionId, out error) / 2f; Simulate(milisecondsDelay * 1000f, action); }