diff --git a/MLAPI/Data/NetworkingConfiguration.cs b/MLAPI/Data/NetworkingConfiguration.cs index 9588670..4fbe192 100644 --- a/MLAPI/Data/NetworkingConfiguration.cs +++ b/MLAPI/Data/NetworkingConfiguration.cs @@ -1,7 +1,9 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Security.Cryptography; +using UnityEngine; using UnityEngine.Networking; namespace MLAPI @@ -11,11 +13,11 @@ namespace MLAPI public ushort ProtocolVersion = 0; public Dictionary Channels = new Dictionary(); public List MessageTypes = new List(); - public int MessageBufferSize = 65536; + public int MessageBufferSize = 65535; public int MaxMessagesPerFrame = 150; public int MaxConnections = 100; public int Port = 7777; - public string Address; + public string Address = "127.0.0.1"; public int ClientConnectionBufferTimeout = 10; public bool ConnectionApproval = false; public Action> ConnectionApprovalCallback; @@ -27,7 +29,6 @@ namespace MLAPI //TODO public bool EncryptMessages = false; - //Cached config hash private byte[] ConfigHash = null; public byte[] GetConfig(bool cache = true) @@ -68,7 +69,7 @@ namespace MLAPI public bool CompareConfig(byte[] hash) { - return hash == GetConfig(); + return hash.SequenceEqual(GetConfig()); } } } diff --git a/MLAPI/MLAPI.csproj b/MLAPI/MLAPI.csproj index ba5e30f..66102ae 100644 --- a/MLAPI/MLAPI.csproj +++ b/MLAPI/MLAPI.csproj @@ -21,6 +21,7 @@ DEBUG;TRACE prompt 4 + Auto pdbonly @@ -37,7 +38,8 @@ - + + False ..\..\..\..\..\..\Program Files\Unity\Editor\Data\Managed\UnityEngine.dll diff --git a/MLAPI/MonoBehaviours/Core/NetworkingManager.cs b/MLAPI/MonoBehaviours/Core/NetworkingManager.cs index b1f1c1d..4db8b94 100644 --- a/MLAPI/MonoBehaviours/Core/NetworkingManager.cs +++ b/MLAPI/MonoBehaviours/Core/NetworkingManager.cs @@ -34,8 +34,8 @@ namespace MLAPI private Dictionary messageHandlerCounter; private Dictionary> releasedMessageHandlerCounters; private int localConnectionId; - public Scene PlayScene; - public Scene MenuScene; + public int PlaySceneIndex; + public int MenuSceneIndex; private Dictionary spawnedObjects; private List spawnedObjectIds; private Stack releasedNetworkObjectIds; @@ -212,7 +212,7 @@ namespace MLAPI { if(NetworkConfig.ConnectionApprovalCallback == null) { - Debug.LogWarning("MLAPI: No ConnectionApproval callback defined. Connection aproval will timeout"); + Debug.LogWarning("MLAPI: No ConnectionApproval callback defined. Connection approval will timeout"); } } @@ -229,17 +229,14 @@ namespace MLAPI HashSet channelNames = new HashSet(); foreach (KeyValuePair pair in NetworkConfig.Channels) { - if(pair.Key.StartsWith("MLAPI_")) - { - Debug.LogWarning("MLAPI: Channel names are not allowed to start with MLAPI_. This is to prevent name conflicts"); - continue; - } - else if(channelNames.Contains(pair.Key)) + if(channelNames.Contains(pair.Key)) { Debug.LogWarning("MLAPI: Duplicate channel name: " + pair.Key); continue; } - channels.Add(pair.Key, cConfig.AddChannel(pair.Value)); + int channelId = cConfig.AddChannel(pair.Value); + channels.Add(pair.Key, channelId); + channelNames.Add(pair.Key); } //0-32 are reserved for MLAPI messages for (ushort i = 32; i < NetworkConfig.MessageTypes.Count; i++) @@ -252,29 +249,31 @@ namespace MLAPI public void StartServer(NetworkingConfiguration netConfig) { + SceneManager.LoadScene(PlaySceneIndex); ConnectionConfig cConfig = Init(netConfig); HostTopology hostTopology = new HostTopology(cConfig, NetworkConfig.MaxConnections); - hostId = NetworkTransport.AddHost(hostTopology, NetworkConfig.Port, null); + hostId = NetworkTransport.AddHost(hostTopology, NetworkConfig.Port); isServer = true; isClient = false; isListening = true; - SceneManager.LoadScene(PlayScene.buildIndex); } public void StartClient(NetworkingConfiguration netConfig) { ConnectionConfig cConfig = Init(netConfig); HostTopology hostTopology = new HostTopology(cConfig, NetworkConfig.MaxConnections); - hostId = NetworkTransport.AddHost(hostTopology, 0); + hostId = NetworkTransport.AddHost(hostTopology, 0, null); + //NetworkTransport.AddSceneId(PlaySceneIndex); - localConnectionId = NetworkTransport.Connect(hostId, NetworkConfig.Address, NetworkConfig.Port, 0, out error); isServer = false; isClient = true; isListening = true; + localConnectionId = NetworkTransport.Connect(hostId, NetworkConfig.Address, NetworkConfig.Port, 0, out error); } public void StartHost(NetworkingConfiguration netConfig) { + SceneManager.LoadScene(PlaySceneIndex); ConnectionConfig cConfig = Init(netConfig); HostTopology hostTopology = new HostTopology(cConfig, NetworkConfig.MaxConnections); hostId = NetworkTransport.AddHost(hostTopology, NetworkConfig.Port, null); @@ -282,7 +281,6 @@ namespace MLAPI isClient = true; isListening = true; connectedClients.Add(-1, new NetworkedClient() { ClientId = -1 }); - SceneManager.LoadScene(PlayScene.buildIndex); } private void OnEnable() @@ -316,12 +314,12 @@ namespace MLAPI do { messagesProcessed++; - eventType = NetworkTransport.Receive(out hostId, out connectionId, out channelId, messageBuffer, NetworkConfig.MessageBufferSize, out receivedSize, out error); + eventType = NetworkTransport.Receive(out hostId, out connectionId, out channelId, messageBuffer, messageBuffer.Length, out receivedSize, out error); NetworkError networkError = (NetworkError)error; if (networkError != NetworkError.Ok) { Debug.LogWarning("MLAPI: NetworkTransport receive error: " + networkError.ToString()); - return; + continue; } switch (eventType) { @@ -342,12 +340,13 @@ namespace MLAPI { writer.Write(NetworkConfig.ConnectionData); } - Send(connectionId, "MLAPI_CONNECTION_REQUEST", "MLAPI_RELIABLE_FRAGMENTED", writeStream.ToArray()); } + Send(connectionId, "MLAPI_CONNECTION_REQUEST", "MLAPI_RELIABLE_FRAGMENTED", writeStream.ToArray()); } } break; case NetworkEventType.DataEvent: + Debug.Log("RECIEVE"); HandleIncomingData(connectionId, ref messageBuffer); break; } @@ -405,7 +404,7 @@ namespace MLAPI using (BinaryReader messageReader = new BinaryReader(messageReadStream)) { byte[] configHash = messageReader.ReadBytes(32); - if (NetworkConfig.CompareConfig(configHash) == false) + if (!NetworkConfig.CompareConfig(configHash)) { Debug.LogWarning("MLAPI: NetworkConfiguration missmatch. The configuration between the server and client does not match."); DisconnectClient(connectionId); @@ -427,7 +426,7 @@ namespace MLAPI case 1: //Server informs client it has been approved: if (isClient) { - SceneManager.LoadScene(PlayScene.buildIndex); + SceneManager.LoadScene(PlaySceneIndex); using (MemoryStream messageReadStream = new MemoryStream(reader.ReadBytes(int.MaxValue))) { using (BinaryReader messageReader = new BinaryReader(messageReadStream)) @@ -477,7 +476,9 @@ namespace MLAPI { writer.Write(messageTypes[messageType]); writer.Write(data); - NetworkTransport.Send(hostId, connectionId, channels[channelName], data, data.Length, out error); + //2 bytes for message type + int size = data.Length + 2; + NetworkTransport.Send(hostId, connectionId, channels[channelName], stream.ToArray(), size, out error); } } } @@ -549,9 +550,8 @@ namespace MLAPI writer.Write(spawnedObjects[spawnedObjectIds[i]].SpawnablePrefabId); } } - - Send(connectionId, "MLAPI_CLIENT_LIST", "MLAPI_RELIABLE_FRAGMENTED", writeStream.ToArray()); } + Send(connectionId, "MLAPI_CLIENT_LIST", "MLAPI_RELIABLE_FRAGMENTED", writeStream.ToArray()); } } else