diff --git a/MLAPI/Data/Channel.cs b/MLAPI/Data/Channel.cs index b0b0826..18997fb 100644 --- a/MLAPI/Data/Channel.cs +++ b/MLAPI/Data/Channel.cs @@ -6,10 +6,23 @@ using UnityEngine.Networking; namespace MLAPI.Data { + /// + /// A data object that represents a NetworkTransport channel + /// [Serializable] public class Channel { + /// + /// The name of the channel + /// public string Name; + /// + /// The Transport QOS type + /// public QosType Type; + /// + /// Wheter or not the channel should be encrypted + /// + public bool Encrypted; } } diff --git a/MLAPI/Data/MessageType.cs b/MLAPI/Data/MessageType.cs new file mode 100644 index 0000000..ff325ff --- /dev/null +++ b/MLAPI/Data/MessageType.cs @@ -0,0 +1,19 @@ +using System; +namespace MLAPI.Data +{ + /// + /// Represents a MLAPI message type + /// + [Serializable] + public class MessageType + { + /// + /// The name of the messageType + /// + public string Name; + /// + /// Wheter or not the channel should have passthrough support. + /// + public bool Passthrough; + } +} diff --git a/MLAPI/Data/NetId.cs b/MLAPI/Data/NetId.cs index f2e5502..cae1a56 100644 --- a/MLAPI/Data/NetId.cs +++ b/MLAPI/Data/NetId.cs @@ -3,22 +3,44 @@ using System; namespace MLAPI.Data { + /// + /// Represents a ClientId structure + /// public struct NetId { + /// + /// The hostId this client is on + /// public byte HostId; + /// + /// The connectionId this client is assigned + /// public ushort ConnectionId; + /// + /// Meta data about hte client + /// public byte Meta; + /// + /// Returns wheter or not the clientId represents a -1 + /// + /// true, if host, false otherwise. public bool IsHost() { return Meta == 1; } - + /// + /// Returns if this is a invalid clientId, (-2) + /// + /// true, if invalid, false otherwise. public bool IsInvalid() { return Meta == 2; } - + /// + /// Static ServerNetId for comparison + /// + /// The server net identifier. public static NetId ServerNetId { get @@ -26,7 +48,13 @@ namespace MLAPI.Data return new NetId((byte)NetworkingManager.singleton.serverHostId, (ushort)NetworkingManager.singleton.serverConnectionId, false, false); } } - + /// + /// Initializes a new instance of the netId struct from transport values + /// + /// Host identifier. + /// Connection identifier. + /// If set to true is host. + /// If set to true is invalid. public NetId(byte hostId, ushort connectionId, bool isHost, bool isInvalid) { HostId = hostId; @@ -42,6 +70,10 @@ namespace MLAPI.Data private static byte[] tempUIntBytes = new byte[4]; private static byte[] tempUShortBytes = new byte[2]; + /// + /// Initializes a new instance of the netId struct from a clientId + /// + /// Client identifier. public NetId(uint clientId) { tempUIntBytes = BitConverter.GetBytes(clientId); @@ -49,7 +81,10 @@ namespace MLAPI.Data ConnectionId = BitConverter.ToUInt16(tempUIntBytes, 1); Meta = tempUIntBytes[3]; } - + /// + /// Gets the clientId. + /// + /// The client identifier. public uint GetClientId() { tempUShortBytes = BitConverter.GetBytes(ConnectionId); @@ -59,7 +94,13 @@ namespace MLAPI.Data tempUIntBytes[3] = Meta; return BitConverter.ToUInt32(tempUIntBytes, 0); } - + // Rider generated vvv + /// + /// Determines whether the specified is equal to the current . + /// + /// The to compare with the current . + /// true if the specified is equal to the current ; + /// otherwise, false. public override bool Equals (object obj) { if (obj == null || GetType() != obj.GetType()) @@ -68,17 +109,34 @@ namespace MLAPI.Data NetId key = (NetId)obj; return (HostId == key.HostId) && (ConnectionId == key.ConnectionId); } - + // Rider generated vvv + /// + /// Serves as a hash function for a object. + /// + /// A hash code for this instance that is suitable for use in hashing algorithms and data structures such as a + /// hash table. public override int GetHashCode() { return (int)GetClientId(); } - + // Rider generated vvv + /// + /// Determines whether a specified instance of is equal to another specified . + /// + /// The first to compare. + /// The second to compare. + /// true if client1 and client2 are equal; otherwise, false. public static bool operator ==(NetId client1, NetId client2) { return (client1.HostId == client2.HostId && client1.ConnectionId == client2.ConnectionId) || (client1.IsHost() == client2.IsHost()); } - + // Rider generated vvv + /// + /// Determines whether a specified instance of is not equal to another specified . + /// + /// The first to compare. + /// The second to compare. + /// true if client1 and client2 are not equal; otherwise, false. public static bool operator !=(NetId client1, NetId client2) { return !(client1 == client2); diff --git a/MLAPI/Data/NetworkConfig.cs b/MLAPI/Data/NetworkConfig.cs index 27c93b9..518db21 100644 --- a/MLAPI/Data/NetworkConfig.cs +++ b/MLAPI/Data/NetworkConfig.cs @@ -39,17 +39,10 @@ namespace MLAPI.Data /// /// Registered MessageTypes /// - public List MessageTypes = new List(); - /// - /// List of MessageTypes that can be passed through by Server. MessageTypes in this list should thus not be trusted to as great of an extent as normal messages. - /// - public List PassthroughMessageTypes = new List(); + public List MessageTypes = new List(); internal HashSet PassthroughMessageHashSet = new HashSet(); - /// - /// Set of channels that will have all message contents encrypted when used - /// - public List EncryptedChannels = new List(); internal HashSet EncryptedChannelsHashSet = new HashSet(); + internal List EncryptedChannels = new List(); /// /// A list of SceneNames that can be used during networked games. /// @@ -161,17 +154,14 @@ namespace MLAPI.Data { writer.Write(Channels[i].Name); writer.Write((byte)Channels[i].Type); + if (EnableEncryption) + writer.Write(Channels[i].Encrypted); } for (int i = 0; i < MessageTypes.Count; i++) { - writer.Write(MessageTypes[i]); - } - if (AllowPassthroughMessages) - { - for (int i = 0; i < PassthroughMessageTypes.Count; i++) - { - writer.Write(PassthroughMessageTypes[i]); - } + writer.Write(MessageTypes[i].Name); + if (AllowPassthroughMessages) + writer.Write(MessageTypes[i].Passthrough); } if (EnableSceneSwitching) { @@ -180,13 +170,6 @@ namespace MLAPI.Data writer.Write(RegisteredScenes[i]); } } - if(EnableEncryption) - { - for (int i = 0; i < EncryptedChannels.Count; i++) - { - writer.Write(EncryptedChannels[i]); - } - } if(HandleObjectSpawning) { writer.Write(SpawnablePrefabs.Count); diff --git a/MLAPI/Data/TransportHost.cs b/MLAPI/Data/TransportHost.cs index 8230919..ebae508 100644 --- a/MLAPI/Data/TransportHost.cs +++ b/MLAPI/Data/TransportHost.cs @@ -2,11 +2,23 @@ namespace MLAPI.Data { + /// + /// Represents a Transport host + /// [Serializable] public class TransportHost { + /// + /// The name of the host + /// public string Name = Guid.NewGuid().ToString().Replace("-", ""); + /// + /// The port the host should listen to + /// public int Port = 7777; + /// + /// If true, the socket will listen on TCP-Websockets, otherwise UDP + /// public bool Websockets = false; } } diff --git a/MLAPI/MLAPI.csproj b/MLAPI/MLAPI.csproj index 0ae7f97..c9f409e 100644 --- a/MLAPI/MLAPI.csproj +++ b/MLAPI/MLAPI.csproj @@ -95,6 +95,7 @@ + diff --git a/MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs b/MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs index 4f06c87..523b2a3 100644 --- a/MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs +++ b/MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs @@ -756,7 +756,7 @@ namespace MLAPI.MonoBehaviours.Core Debug.LogWarning("MLAPI: Sending messages on the internal MLAPI channels is not allowed!"); return; } - if (!isServer && (!NetworkingManager.singleton.NetworkConfig.AllowPassthroughMessages || !NetworkingManager.singleton.NetworkConfig.PassthroughMessageTypes.Contains(messageType))) + if (!isServer && (!NetworkingManager.singleton.NetworkConfig.AllowPassthroughMessages || !NetworkingManager.singleton.NetworkConfig.PassthroughMessageHashSet.Contains(MessageManager.messageTypes[messageType]))) { Debug.LogWarning("MLAPI: Invalid Passthrough send. Ensure AllowPassthroughMessages are turned on and that the MessageType " + messageType + " is registered as a passthroughMessageType"); return; @@ -789,7 +789,7 @@ namespace MLAPI.MonoBehaviours.Core Debug.LogWarning("MLAPI: Sending messages on the internal MLAPI channels is not allowed!"); return; } - if (!isServer && (!NetworkingManager.singleton.NetworkConfig.AllowPassthroughMessages || !NetworkingManager.singleton.NetworkConfig.PassthroughMessageTypes.Contains(messageType))) + if (!isServer && (!NetworkingManager.singleton.NetworkConfig.AllowPassthroughMessages || !NetworkingManager.singleton.NetworkConfig.PassthroughMessageHashSet.Contains(MessageManager.messageTypes[messageType]))) { Debug.LogWarning("MLAPI: Invalid Passthrough send. Ensure AllowPassthroughMessages are turned on and that the MessageType " + messageType + " is registered as a passthroughMessageType"); return; @@ -889,7 +889,7 @@ namespace MLAPI.MonoBehaviours.Core Debug.LogWarning("MLAPI: Sending messages on the internal MLAPI channels is not allowed!"); return; } - if (!isServer && (!NetworkingManager.singleton.NetworkConfig.AllowPassthroughMessages || !NetworkingManager.singleton.NetworkConfig.PassthroughMessageTypes.Contains(messageType))) + if (!isServer && (!NetworkingManager.singleton.NetworkConfig.AllowPassthroughMessages || !NetworkingManager.singleton.NetworkConfig.PassthroughMessageHashSet.Contains(MessageManager.messageTypes[messageType]))) { Debug.LogWarning("MLAPI: Invalid Passthrough send. Ensure AllowPassthroughMessages are turned on and that the MessageType " + messageType + " is registered as a passthroughMessageType"); return; @@ -924,7 +924,7 @@ namespace MLAPI.MonoBehaviours.Core Debug.LogWarning("MLAPI: Sending messages on the internal MLAPI channels is not allowed!"); return; } - if (!isServer && (!NetworkingManager.singleton.NetworkConfig.AllowPassthroughMessages || !NetworkingManager.singleton.NetworkConfig.PassthroughMessageTypes.Contains(messageType))) + if (!isServer && (!NetworkingManager.singleton.NetworkConfig.AllowPassthroughMessages || !NetworkingManager.singleton.NetworkConfig.PassthroughMessageHashSet.Contains(MessageManager.messageTypes[messageType]))) { Debug.LogWarning("MLAPI: Invalid Passthrough send. Ensure AllowPassthroughMessages are turned on and that the MessageType " + messageType + " is registered as a passthroughMessageType"); return; diff --git a/MLAPI/MonoBehaviours/Core/NetworkingManager.cs b/MLAPI/MonoBehaviours/Core/NetworkingManager.cs index 40ed8cd..b35257e 100644 --- a/MLAPI/MonoBehaviours/Core/NetworkingManager.cs +++ b/MLAPI/MonoBehaviours/Core/NetworkingManager.cs @@ -145,6 +145,9 @@ namespace MLAPI.MonoBehaviours.Core private Dictionary diffieHellmanPublicKeys; private byte[] clientAesKey; + /// + /// An inspector bool that acts as a Trigger for regenerating RSA keys. Should not be used outside Unity editor. + /// public bool RegenerateRSAKeys = false; private void OnValidate() @@ -264,6 +267,17 @@ namespace MLAPI.MonoBehaviours.Core } }; + if (NetworkConfig.EnableEncryption) + { + for (int i = 0; i < NetworkConfig.Channels.Count; i++) + { + if (NetworkConfig.Channels[i].Encrypted) + { + NetworkConfig.EncryptedChannels.Add(NetworkConfig.Channels[i].Name); + } + } + } + HashSet channelNames = new HashSet(); for (int i = 0; i < internalChannels.Count; i++) { @@ -289,15 +303,43 @@ namespace MLAPI.MonoBehaviours.Core MessageManager.messageTypes.Add("MLAPI_CHANGE_OWNER", 8); MessageManager.messageTypes.Add("MLAPI_SYNC_VAR_UPDATE", 9); - List messageTypes = new List(NetworkConfig.MessageTypes) + List messageTypes = new List(NetworkConfig.MessageTypes) { - "MLAPI_OnRecieveTransformFromClient", - "MLAPI_OnRecieveTransformFromServer", - "MLAPI_HandleAnimationMessage", - "MLAPI_HandleAnimationParameterMessage", - "MLAPI_HandleAnimationTriggerMessage", - "MLAPI_OnNavMeshStateUpdate", - "MLAPI_OnNavMeshCorrectionUpdate" + new MessageType() + { + Name = "MLAPI_OnRecieveTransformFromClient", + Passthrough = false + }, + new MessageType() + { + Name = "MLAPI_OnRecieveTransformFromServer", + Passthrough = false + }, + new MessageType() + { + Name = "MLAPI_HandleAnimationMessage", + Passthrough = false + }, + new MessageType() + { + Name = "MLAPI_HandleAnimationParameterMessage", + Passthrough = false + }, + new MessageType() + { + Name = "MLAPI_HandleAnimationTriggerMessage", + Passthrough = false + }, + new MessageType() + { + Name = "MLAPI_OnNavMeshStateUpdate", + Passthrough = false + }, + new MessageType() + { + Name = "MLAPI_OnNavMeshCorrectionUpdate", + Passthrough = false + } }; if (NetworkConfig.EnableSceneSwitching) @@ -332,14 +374,10 @@ namespace MLAPI.MonoBehaviours.Core if (NetworkConfig.AllowPassthroughMessages) { - for (int i = 0; i < NetworkConfig.PassthroughMessageTypes.Count; i++) + for (int i = 0; i < NetworkConfig.MessageTypes.Count; i++) { - if(!NetworkConfig.MessageTypes.Contains(NetworkConfig.PassthroughMessageTypes[i])) - { - Debug.LogWarning("MLAPI: The messageType " + NetworkConfig.PassthroughMessageTypes[i] + " doesn't exist"); - continue; - } - NetworkConfig.PassthroughMessageHashSet.Add(MessageManager.messageTypes[NetworkConfig.PassthroughMessageTypes[i]]); + if (NetworkConfig.MessageTypes[i].Passthrough) + NetworkConfig.PassthroughMessageHashSet.Add(MessageManager.messageTypes[NetworkConfig.MessageTypes[i].Name]); } } @@ -360,17 +398,16 @@ namespace MLAPI.MonoBehaviours.Core ushort messageId = 32; for (ushort i = 0; i < messageTypes.Count; i++) { - MessageManager.messageTypes.Add(messageTypes[i], messageId); - MessageManager.reverseMessageTypes.Add(messageId, messageTypes[i]); + MessageManager.messageTypes.Add(messageTypes[i].Name, messageId); + MessageManager.reverseMessageTypes.Add(messageId, messageTypes[i].Name); messageId++; } return cConfig; } /// - /// Starts a server with a given NetworkingConfiguration + /// Starts a server /// - /// The NetworkingConfiguration to use public void StartServer() { if (isServer || isClient) @@ -405,9 +442,8 @@ namespace MLAPI.MonoBehaviours.Core } /// - /// Starts a client with a given NetworkingConfiguration + /// Starts a client /// - /// The NetworkingConfiguration to use public void StartClient() { if (isServer || isClient) @@ -429,9 +465,8 @@ namespace MLAPI.MonoBehaviours.Core } /// - /// Starts a client with a given NetworkingConfiguration + /// Starts a client using Websockets /// - /// The NetworkingConfiguration to use public void StartClientWebsocket() { if (isServer || isClient) @@ -512,9 +547,8 @@ namespace MLAPI.MonoBehaviours.Core } /// - /// Starts a Host with a given NetworkingConfiguration + /// Starts a Host /// - /// The NetworkingConfiguration to use public void StartHost() { if (isServer || isClient)