Added XML summaries everywhere and improved NetworkConfig to be less error prone

This commit is contained in:
Albin Corén 2018-04-04 08:10:08 +02:00
parent a0157be9dd
commit 07178c6bcf
8 changed files with 181 additions and 61 deletions

View File

@ -6,10 +6,23 @@ using UnityEngine.Networking;
namespace MLAPI.Data namespace MLAPI.Data
{ {
/// <summary>
/// A data object that represents a NetworkTransport channel
/// </summary>
[Serializable] [Serializable]
public class Channel public class Channel
{ {
/// <summary>
/// The name of the channel
/// </summary>
public string Name; public string Name;
/// <summary>
/// The Transport QOS type
/// </summary>
public QosType Type; public QosType Type;
/// <summary>
/// Wheter or not the channel should be encrypted
/// </summary>
public bool Encrypted;
} }
} }

19
MLAPI/Data/MessageType.cs Normal file
View File

@ -0,0 +1,19 @@
using System;
namespace MLAPI.Data
{
/// <summary>
/// Represents a MLAPI message type
/// </summary>
[Serializable]
public class MessageType
{
/// <summary>
/// The name of the messageType
/// </summary>
public string Name;
/// <summary>
/// Wheter or not the channel should have passthrough support.
/// </summary>
public bool Passthrough;
}
}

View File

@ -3,22 +3,44 @@ using System;
namespace MLAPI.Data namespace MLAPI.Data
{ {
/// <summary>
/// Represents a ClientId structure
/// </summary>
public struct NetId public struct NetId
{ {
/// <summary>
/// The hostId this client is on
/// </summary>
public byte HostId; public byte HostId;
/// <summary>
/// The connectionId this client is assigned
/// </summary>
public ushort ConnectionId; public ushort ConnectionId;
/// <summary>
/// Meta data about hte client
/// </summary>
public byte Meta; public byte Meta;
/// <summary>
/// Returns wheter or not the clientId represents a -1
/// </summary>
/// <returns><c>true</c>, if host, <c>false</c> otherwise.</returns>
public bool IsHost() public bool IsHost()
{ {
return Meta == 1; return Meta == 1;
} }
/// <summary>
/// Returns if this is a invalid clientId, (-2)
/// </summary>
/// <returns><c>true</c>, if invalid, <c>false</c> otherwise.</returns>
public bool IsInvalid() public bool IsInvalid()
{ {
return Meta == 2; return Meta == 2;
} }
/// <summary>
/// Static ServerNetId for comparison
/// </summary>
/// <value>The server net identifier.</value>
public static NetId ServerNetId public static NetId ServerNetId
{ {
get get
@ -26,7 +48,13 @@ namespace MLAPI.Data
return new NetId((byte)NetworkingManager.singleton.serverHostId, (ushort)NetworkingManager.singleton.serverConnectionId, false, false); return new NetId((byte)NetworkingManager.singleton.serverHostId, (ushort)NetworkingManager.singleton.serverConnectionId, false, false);
} }
} }
/// <summary>
/// Initializes a new instance of the netId struct from transport values
/// </summary>
/// <param name="hostId">Host identifier.</param>
/// <param name="connectionId">Connection identifier.</param>
/// <param name="isHost">If set to <c>true</c> is host.</param>
/// <param name="isInvalid">If set to <c>true</c> is invalid.</param>
public NetId(byte hostId, ushort connectionId, bool isHost, bool isInvalid) public NetId(byte hostId, ushort connectionId, bool isHost, bool isInvalid)
{ {
HostId = hostId; HostId = hostId;
@ -42,6 +70,10 @@ namespace MLAPI.Data
private static byte[] tempUIntBytes = new byte[4]; private static byte[] tempUIntBytes = new byte[4];
private static byte[] tempUShortBytes = new byte[2]; private static byte[] tempUShortBytes = new byte[2];
/// <summary>
/// Initializes a new instance of the netId struct from a clientId
/// </summary>
/// <param name="clientId">Client identifier.</param>
public NetId(uint clientId) public NetId(uint clientId)
{ {
tempUIntBytes = BitConverter.GetBytes(clientId); tempUIntBytes = BitConverter.GetBytes(clientId);
@ -49,7 +81,10 @@ namespace MLAPI.Data
ConnectionId = BitConverter.ToUInt16(tempUIntBytes, 1); ConnectionId = BitConverter.ToUInt16(tempUIntBytes, 1);
Meta = tempUIntBytes[3]; Meta = tempUIntBytes[3];
} }
/// <summary>
/// Gets the clientId.
/// </summary>
/// <returns>The client identifier.</returns>
public uint GetClientId() public uint GetClientId()
{ {
tempUShortBytes = BitConverter.GetBytes(ConnectionId); tempUShortBytes = BitConverter.GetBytes(ConnectionId);
@ -59,7 +94,13 @@ namespace MLAPI.Data
tempUIntBytes[3] = Meta; tempUIntBytes[3] = Meta;
return BitConverter.ToUInt32(tempUIntBytes, 0); return BitConverter.ToUInt32(tempUIntBytes, 0);
} }
// Rider generated vvv
/// <summary>
/// Determines whether the specified <see cref="object"/> is equal to the current <see cref="T:MLAPI.Data.NetId"/>.
/// </summary>
/// <param name="obj">The <see cref="object"/> to compare with the current <see cref="T:MLAPI.Data.NetId"/>.</param>
/// <returns><c>true</c> if the specified <see cref="object"/> is equal to the current <see cref="T:MLAPI.Data.NetId"/>;
/// otherwise, <c>false</c>.</returns>
public override bool Equals (object obj) public override bool Equals (object obj)
{ {
if (obj == null || GetType() != obj.GetType()) if (obj == null || GetType() != obj.GetType())
@ -68,17 +109,34 @@ namespace MLAPI.Data
NetId key = (NetId)obj; NetId key = (NetId)obj;
return (HostId == key.HostId) && (ConnectionId == key.ConnectionId); return (HostId == key.HostId) && (ConnectionId == key.ConnectionId);
} }
// Rider generated vvv
/// <summary>
/// Serves as a hash function for a <see cref="T:MLAPI.Data.NetId"/> object.
/// </summary>
/// <returns>A hash code for this instance that is suitable for use in hashing algorithms and data structures such as a
/// hash table.</returns>
public override int GetHashCode() public override int GetHashCode()
{ {
return (int)GetClientId(); return (int)GetClientId();
} }
// Rider generated vvv
/// <summary>
/// Determines whether a specified instance of <see cref="MLAPI.Data.NetId"/> is equal to another specified <see cref="MLAPI.Data.NetId"/>.
/// </summary>
/// <param name="client1">The first <see cref="MLAPI.Data.NetId"/> to compare.</param>
/// <param name="client2">The second <see cref="MLAPI.Data.NetId"/> to compare.</param>
/// <returns><c>true</c> if <c>client1</c> and <c>client2</c> are equal; otherwise, <c>false</c>.</returns>
public static bool operator ==(NetId client1, NetId client2) public static bool operator ==(NetId client1, NetId client2)
{ {
return (client1.HostId == client2.HostId && client1.ConnectionId == client2.ConnectionId) || (client1.IsHost() == client2.IsHost()); return (client1.HostId == client2.HostId && client1.ConnectionId == client2.ConnectionId) || (client1.IsHost() == client2.IsHost());
} }
// Rider generated vvv
/// <summary>
/// Determines whether a specified instance of <see cref="MLAPI.Data.NetId"/> is not equal to another specified <see cref="MLAPI.Data.NetId"/>.
/// </summary>
/// <param name="client1">The first <see cref="MLAPI.Data.NetId"/> to compare.</param>
/// <param name="client2">The second <see cref="MLAPI.Data.NetId"/> to compare.</param>
/// <returns><c>true</c> if <c>client1</c> and <c>client2</c> are not equal; otherwise, <c>false</c>.</returns>
public static bool operator !=(NetId client1, NetId client2) public static bool operator !=(NetId client1, NetId client2)
{ {
return !(client1 == client2); return !(client1 == client2);

View File

@ -39,17 +39,10 @@ namespace MLAPI.Data
/// <summary> /// <summary>
/// Registered MessageTypes /// Registered MessageTypes
/// </summary> /// </summary>
public List<string> MessageTypes = new List<string>(); public List<MessageType> MessageTypes = new List<MessageType>();
/// <summary>
/// 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.
/// </summary>
public List<string> PassthroughMessageTypes = new List<string>();
internal HashSet<ushort> PassthroughMessageHashSet = new HashSet<ushort>(); internal HashSet<ushort> PassthroughMessageHashSet = new HashSet<ushort>();
/// <summary>
/// Set of channels that will have all message contents encrypted when used
/// </summary>
public List<string> EncryptedChannels = new List<string>();
internal HashSet<string> EncryptedChannelsHashSet = new HashSet<string>(); internal HashSet<string> EncryptedChannelsHashSet = new HashSet<string>();
internal List<string> EncryptedChannels = new List<string>();
/// <summary> /// <summary>
/// A list of SceneNames that can be used during networked games. /// A list of SceneNames that can be used during networked games.
/// </summary> /// </summary>
@ -161,17 +154,14 @@ namespace MLAPI.Data
{ {
writer.Write(Channels[i].Name); writer.Write(Channels[i].Name);
writer.Write((byte)Channels[i].Type); writer.Write((byte)Channels[i].Type);
if (EnableEncryption)
writer.Write(Channels[i].Encrypted);
} }
for (int i = 0; i < MessageTypes.Count; i++) for (int i = 0; i < MessageTypes.Count; i++)
{ {
writer.Write(MessageTypes[i]); writer.Write(MessageTypes[i].Name);
} if (AllowPassthroughMessages)
if (AllowPassthroughMessages) writer.Write(MessageTypes[i].Passthrough);
{
for (int i = 0; i < PassthroughMessageTypes.Count; i++)
{
writer.Write(PassthroughMessageTypes[i]);
}
} }
if (EnableSceneSwitching) if (EnableSceneSwitching)
{ {
@ -180,13 +170,6 @@ namespace MLAPI.Data
writer.Write(RegisteredScenes[i]); writer.Write(RegisteredScenes[i]);
} }
} }
if(EnableEncryption)
{
for (int i = 0; i < EncryptedChannels.Count; i++)
{
writer.Write(EncryptedChannels[i]);
}
}
if(HandleObjectSpawning) if(HandleObjectSpawning)
{ {
writer.Write(SpawnablePrefabs.Count); writer.Write(SpawnablePrefabs.Count);

View File

@ -2,11 +2,23 @@
namespace MLAPI.Data namespace MLAPI.Data
{ {
/// <summary>
/// Represents a Transport host
/// </summary>
[Serializable] [Serializable]
public class TransportHost public class TransportHost
{ {
/// <summary>
/// The name of the host
/// </summary>
public string Name = Guid.NewGuid().ToString().Replace("-", ""); public string Name = Guid.NewGuid().ToString().Replace("-", "");
/// <summary>
/// The port the host should listen to
/// </summary>
public int Port = 7777; public int Port = 7777;
/// <summary>
/// If true, the socket will listen on TCP-Websockets, otherwise UDP
/// </summary>
public bool Websockets = false; public bool Websockets = false;
} }
} }

View File

@ -95,6 +95,7 @@
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Data\NetId.cs" /> <Compile Include="Data\NetId.cs" />
<Compile Include="NetworkingManagerComponents\Binary\MessageChunker.cs" /> <Compile Include="NetworkingManagerComponents\Binary\MessageChunker.cs" />
<Compile Include="Data\MessageType.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="packages.config" /> <None Include="packages.config" />

View File

@ -756,7 +756,7 @@ namespace MLAPI.MonoBehaviours.Core
Debug.LogWarning("MLAPI: Sending messages on the internal MLAPI channels is not allowed!"); Debug.LogWarning("MLAPI: Sending messages on the internal MLAPI channels is not allowed!");
return; 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"); Debug.LogWarning("MLAPI: Invalid Passthrough send. Ensure AllowPassthroughMessages are turned on and that the MessageType " + messageType + " is registered as a passthroughMessageType");
return; return;
@ -789,7 +789,7 @@ namespace MLAPI.MonoBehaviours.Core
Debug.LogWarning("MLAPI: Sending messages on the internal MLAPI channels is not allowed!"); Debug.LogWarning("MLAPI: Sending messages on the internal MLAPI channels is not allowed!");
return; 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"); Debug.LogWarning("MLAPI: Invalid Passthrough send. Ensure AllowPassthroughMessages are turned on and that the MessageType " + messageType + " is registered as a passthroughMessageType");
return; return;
@ -889,7 +889,7 @@ namespace MLAPI.MonoBehaviours.Core
Debug.LogWarning("MLAPI: Sending messages on the internal MLAPI channels is not allowed!"); Debug.LogWarning("MLAPI: Sending messages on the internal MLAPI channels is not allowed!");
return; 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"); Debug.LogWarning("MLAPI: Invalid Passthrough send. Ensure AllowPassthroughMessages are turned on and that the MessageType " + messageType + " is registered as a passthroughMessageType");
return; return;
@ -924,7 +924,7 @@ namespace MLAPI.MonoBehaviours.Core
Debug.LogWarning("MLAPI: Sending messages on the internal MLAPI channels is not allowed!"); Debug.LogWarning("MLAPI: Sending messages on the internal MLAPI channels is not allowed!");
return; 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"); Debug.LogWarning("MLAPI: Invalid Passthrough send. Ensure AllowPassthroughMessages are turned on and that the MessageType " + messageType + " is registered as a passthroughMessageType");
return; return;

View File

@ -145,6 +145,9 @@ namespace MLAPI.MonoBehaviours.Core
private Dictionary<uint, byte[]> diffieHellmanPublicKeys; private Dictionary<uint, byte[]> diffieHellmanPublicKeys;
private byte[] clientAesKey; private byte[] clientAesKey;
/// <summary>
/// An inspector bool that acts as a Trigger for regenerating RSA keys. Should not be used outside Unity editor.
/// </summary>
public bool RegenerateRSAKeys = false; public bool RegenerateRSAKeys = false;
private void OnValidate() 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<string> channelNames = new HashSet<string>(); HashSet<string> channelNames = new HashSet<string>();
for (int i = 0; i < internalChannels.Count; i++) 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_CHANGE_OWNER", 8);
MessageManager.messageTypes.Add("MLAPI_SYNC_VAR_UPDATE", 9); MessageManager.messageTypes.Add("MLAPI_SYNC_VAR_UPDATE", 9);
List<string> messageTypes = new List<string>(NetworkConfig.MessageTypes) List<MessageType> messageTypes = new List<MessageType>(NetworkConfig.MessageTypes)
{ {
"MLAPI_OnRecieveTransformFromClient", new MessageType()
"MLAPI_OnRecieveTransformFromServer", {
"MLAPI_HandleAnimationMessage", Name = "MLAPI_OnRecieveTransformFromClient",
"MLAPI_HandleAnimationParameterMessage", Passthrough = false
"MLAPI_HandleAnimationTriggerMessage", },
"MLAPI_OnNavMeshStateUpdate", new MessageType()
"MLAPI_OnNavMeshCorrectionUpdate" {
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) if (NetworkConfig.EnableSceneSwitching)
@ -332,14 +374,10 @@ namespace MLAPI.MonoBehaviours.Core
if (NetworkConfig.AllowPassthroughMessages) 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])) if (NetworkConfig.MessageTypes[i].Passthrough)
{ NetworkConfig.PassthroughMessageHashSet.Add(MessageManager.messageTypes[NetworkConfig.MessageTypes[i].Name]);
Debug.LogWarning("MLAPI: The messageType " + NetworkConfig.PassthroughMessageTypes[i] + " doesn't exist");
continue;
}
NetworkConfig.PassthroughMessageHashSet.Add(MessageManager.messageTypes[NetworkConfig.PassthroughMessageTypes[i]]);
} }
} }
@ -360,17 +398,16 @@ namespace MLAPI.MonoBehaviours.Core
ushort messageId = 32; ushort messageId = 32;
for (ushort i = 0; i < messageTypes.Count; i++) for (ushort i = 0; i < messageTypes.Count; i++)
{ {
MessageManager.messageTypes.Add(messageTypes[i], messageId); MessageManager.messageTypes.Add(messageTypes[i].Name, messageId);
MessageManager.reverseMessageTypes.Add(messageId, messageTypes[i]); MessageManager.reverseMessageTypes.Add(messageId, messageTypes[i].Name);
messageId++; messageId++;
} }
return cConfig; return cConfig;
} }
/// <summary> /// <summary>
/// Starts a server with a given NetworkingConfiguration /// Starts a server
/// </summary> /// </summary>
/// <param name="netConfig">The NetworkingConfiguration to use</param>
public void StartServer() public void StartServer()
{ {
if (isServer || isClient) if (isServer || isClient)
@ -405,9 +442,8 @@ namespace MLAPI.MonoBehaviours.Core
} }
/// <summary> /// <summary>
/// Starts a client with a given NetworkingConfiguration /// Starts a client
/// </summary> /// </summary>
/// <param name="netConfig">The NetworkingConfiguration to use</param>
public void StartClient() public void StartClient()
{ {
if (isServer || isClient) if (isServer || isClient)
@ -429,9 +465,8 @@ namespace MLAPI.MonoBehaviours.Core
} }
/// <summary> /// <summary>
/// Starts a client with a given NetworkingConfiguration /// Starts a client using Websockets
/// </summary> /// </summary>
/// <param name="netConfig">The NetworkingConfiguration to use</param>
public void StartClientWebsocket() public void StartClientWebsocket()
{ {
if (isServer || isClient) if (isServer || isClient)
@ -512,9 +547,8 @@ namespace MLAPI.MonoBehaviours.Core
} }
/// <summary> /// <summary>
/// Starts a Host with a given NetworkingConfiguration /// Starts a Host
/// </summary> /// </summary>
/// <param name="netConfig">The NetworkingConfiguration to use</param>
public void StartHost() public void StartHost()
{ {
if (isServer || isClient) if (isServer || isClient)