From bde2f0f3af6e95e6a5cfdf67b0036dbb0c21fd77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albin=20Cor=C3=A9n?= <2108U9@gmail.com> Date: Sun, 7 Jan 2018 06:28:13 +0100 Subject: [PATCH] Added host support --- .../MonoBehaviours/Core/NetworkedBehaviour.cs | 10 ++- .../MonoBehaviours/Core/NetworkingManager.cs | 75 ++++++++++++++++++- README.md | 1 + 3 files changed, 78 insertions(+), 8 deletions(-) diff --git a/MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs b/MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs index c04c102..d9710bb 100644 --- a/MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs +++ b/MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs @@ -82,12 +82,14 @@ namespace MLAPI public void SendToServer(string messageType, string channelName, byte[] data) { - if(isServer) + if (isServer) { - Debug.LogWarning("MLAPI: Sending messages from server to server is not yet supported"); - return; + NetworkingManager.singleton.InvokeMessageHandlers(messageType, data, -1); + } + else + { + NetworkingManager.singleton.Send(NetworkingManager.singleton.serverClientId, messageType, channelName, data); } - NetworkingManager.singleton.Send(NetworkingManager.singleton.serverClientId, messageType, channelName, data); } public void SendToLocalClient(string messageType, string channelName, byte[] data) diff --git a/MLAPI/MonoBehaviours/Core/NetworkingManager.cs b/MLAPI/MonoBehaviours/Core/NetworkingManager.cs index a093eca..ea0fa8c 100644 --- a/MLAPI/MonoBehaviours/Core/NetworkingManager.cs +++ b/MLAPI/MonoBehaviours/Core/NetworkingManager.cs @@ -139,6 +139,17 @@ namespace MLAPI singleton.releasedNetworkObjectIds.Push(networkId); } + internal void InvokeMessageHandlers(string messageType, byte[] data, int clientId) + { + if(!messageTypes.ContainsKey(messageType) || !messageCallbacks.ContainsKey(messageTypes[messageType])) + return; + + foreach (KeyValuePair> pair in messageCallbacks[messageTypes[messageType]]) + { + pair.Value(clientId, data); + } + } + internal int AddIncomingMessageHandler(string name, Action action) { if(messageTypes.ContainsKey(name)) @@ -566,6 +577,17 @@ namespace MLAPI internal void Send(int clientId, string messageType, string channelName, byte[] data) { + if(isHost && clientId == -1) + { + //Host trying to send data to it's own client + InvokeMessageHandlers(messageType, data, clientId); + return; + } + else if(clientId == -1) + { + //Client trying to send data to host + clientId = serverClientId; + } using (MemoryStream stream = new MemoryStream()) { using (BinaryWriter writer = new BinaryWriter(stream)) @@ -597,7 +619,18 @@ namespace MLAPI int channel = channels[channelName]; for (int i = 0; i < clientIds.Length; i++) { - NetworkTransport.Send(hostId, clientIds[i], channel, dataToSend, size, out error); + int clientId = clientIds[i]; + if (isHost && clientId == -1) + { + InvokeMessageHandlers(messageType, data, clientId); + continue; + } + else if (clientId == -1) + { + //Client trying to send data to host + clientId = serverClientId; + } + NetworkTransport.Send(hostId, clientId, channel, dataToSend, size, out error); } } } @@ -618,7 +651,18 @@ namespace MLAPI int channel = channels[channelName]; for (int i = 0; i < clientIds.Count; i++) { - NetworkTransport.Send(hostId, clientIds[i], channel, dataToSend, size, out error); + int clientId = clientIds[i]; + if (isHost && clientId == -1) + { + InvokeMessageHandlers(messageType, data, clientId); + continue; + } + else if (clientId == -1) + { + //Client trying to send data to host + clientId = serverClientId; + } + NetworkTransport.Send(hostId, clientId, channel, dataToSend, size, out error); } } } @@ -639,7 +683,19 @@ namespace MLAPI int channel = channels[channelName]; foreach (KeyValuePair pair in connectedClients) { - NetworkTransport.Send(hostId, pair.Key, channel, dataToSend, size, out error); + int clientId = pair.Key; + if(isHost && pair.Key == -1) + { + InvokeMessageHandlers(messageType, data, pair.Key); + continue; + } + else if (clientId == -1) + { + //Client trying to send data to host + clientId = serverClientId; + } + NetworkTransport.Send(hostId, clientId, channel, dataToSend, size, out error); + } } } @@ -662,7 +718,18 @@ namespace MLAPI { if (pair.Key == clientIdToIgnore) continue; - NetworkTransport.Send(hostId, pair.Key, channel, dataToSend, size, out error); + int clientId = pair.Key; + if (isHost && pair.Key == -1) + { + InvokeMessageHandlers(messageType, data, clientId); + continue; + } + else if (clientId == -1) + { + //Client trying to send data to host + clientId = serverClientId; + } + NetworkTransport.Send(hostId, clientId, channel, dataToSend, size, out error); } } } diff --git a/README.md b/README.md index bebfd81..dd26002 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ It's licenced under the MIT licence :D * Serializer (both for the library to speed up and to allow structs to be sent easily) * SyncVars (allow variables to automatically be synced to new clients and current clients when it's changed) * Message compression +* Host support (Client hosts the server) (done) That's all I can think of right now. But there is more to come, especially if people show intrest in the project.