Added XML summaries everywhere and improved NetworkConfig to be less error prone
This commit is contained in:
parent
a0157be9dd
commit
07178c6bcf
@ -6,10 +6,23 @@ using UnityEngine.Networking;
|
||||
|
||||
namespace MLAPI.Data
|
||||
{
|
||||
/// <summary>
|
||||
/// A data object that represents a NetworkTransport channel
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public class Channel
|
||||
{
|
||||
/// <summary>
|
||||
/// The name of the channel
|
||||
/// </summary>
|
||||
public string Name;
|
||||
/// <summary>
|
||||
/// The Transport QOS type
|
||||
/// </summary>
|
||||
public QosType Type;
|
||||
/// <summary>
|
||||
/// Wheter or not the channel should be encrypted
|
||||
/// </summary>
|
||||
public bool Encrypted;
|
||||
}
|
||||
}
|
||||
|
19
MLAPI/Data/MessageType.cs
Normal file
19
MLAPI/Data/MessageType.cs
Normal 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;
|
||||
}
|
||||
}
|
@ -3,22 +3,44 @@ using System;
|
||||
|
||||
namespace MLAPI.Data
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a ClientId structure
|
||||
/// </summary>
|
||||
public struct NetId
|
||||
{
|
||||
/// <summary>
|
||||
/// The hostId this client is on
|
||||
/// </summary>
|
||||
public byte HostId;
|
||||
/// <summary>
|
||||
/// The connectionId this client is assigned
|
||||
/// </summary>
|
||||
public ushort ConnectionId;
|
||||
/// <summary>
|
||||
/// Meta data about hte client
|
||||
/// </summary>
|
||||
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()
|
||||
{
|
||||
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()
|
||||
{
|
||||
return Meta == 2;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Static ServerNetId for comparison
|
||||
/// </summary>
|
||||
/// <value>The server net identifier.</value>
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
HostId = hostId;
|
||||
@ -42,6 +70,10 @@ namespace MLAPI.Data
|
||||
|
||||
private static byte[] tempUIntBytes = new byte[4];
|
||||
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)
|
||||
{
|
||||
tempUIntBytes = BitConverter.GetBytes(clientId);
|
||||
@ -49,7 +81,10 @@ namespace MLAPI.Data
|
||||
ConnectionId = BitConverter.ToUInt16(tempUIntBytes, 1);
|
||||
Meta = tempUIntBytes[3];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the clientId.
|
||||
/// </summary>
|
||||
/// <returns>The client identifier.</returns>
|
||||
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
|
||||
/// <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)
|
||||
{
|
||||
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
|
||||
/// <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()
|
||||
{
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
return !(client1 == client2);
|
||||
|
@ -39,17 +39,10 @@ namespace MLAPI.Data
|
||||
/// <summary>
|
||||
/// Registered MessageTypes
|
||||
/// </summary>
|
||||
public List<string> MessageTypes = new List<string>();
|
||||
/// <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>();
|
||||
public List<MessageType> MessageTypes = new List<MessageType>();
|
||||
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 List<string> EncryptedChannels = new List<string>();
|
||||
/// <summary>
|
||||
/// A list of SceneNames that can be used during networked games.
|
||||
/// </summary>
|
||||
@ -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);
|
||||
|
@ -2,11 +2,23 @@
|
||||
|
||||
namespace MLAPI.Data
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a Transport host
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public class TransportHost
|
||||
{
|
||||
/// <summary>
|
||||
/// The name of the host
|
||||
/// </summary>
|
||||
public string Name = Guid.NewGuid().ToString().Replace("-", "");
|
||||
/// <summary>
|
||||
/// The port the host should listen to
|
||||
/// </summary>
|
||||
public int Port = 7777;
|
||||
/// <summary>
|
||||
/// If true, the socket will listen on TCP-Websockets, otherwise UDP
|
||||
/// </summary>
|
||||
public bool Websockets = false;
|
||||
}
|
||||
}
|
||||
|
@ -95,6 +95,7 @@
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Data\NetId.cs" />
|
||||
<Compile Include="NetworkingManagerComponents\Binary\MessageChunker.cs" />
|
||||
<Compile Include="Data\MessageType.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="packages.config" />
|
||||
|
@ -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;
|
||||
|
@ -145,6 +145,9 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
private Dictionary<uint, byte[]> diffieHellmanPublicKeys;
|
||||
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;
|
||||
|
||||
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>();
|
||||
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<string> messageTypes = new List<string>(NetworkConfig.MessageTypes)
|
||||
List<MessageType> messageTypes = new List<MessageType>(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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Starts a server with a given NetworkingConfiguration
|
||||
/// Starts a server
|
||||
/// </summary>
|
||||
/// <param name="netConfig">The NetworkingConfiguration to use</param>
|
||||
public void StartServer()
|
||||
{
|
||||
if (isServer || isClient)
|
||||
@ -405,9 +442,8 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Starts a client with a given NetworkingConfiguration
|
||||
/// Starts a client
|
||||
/// </summary>
|
||||
/// <param name="netConfig">The NetworkingConfiguration to use</param>
|
||||
public void StartClient()
|
||||
{
|
||||
if (isServer || isClient)
|
||||
@ -429,9 +465,8 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Starts a client with a given NetworkingConfiguration
|
||||
/// Starts a client using Websockets
|
||||
/// </summary>
|
||||
/// <param name="netConfig">The NetworkingConfiguration to use</param>
|
||||
public void StartClientWebsocket()
|
||||
{
|
||||
if (isServer || isClient)
|
||||
@ -512,9 +547,8 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Starts a Host with a given NetworkingConfiguration
|
||||
/// Starts a Host
|
||||
/// </summary>
|
||||
/// <param name="netConfig">The NetworkingConfiguration to use</param>
|
||||
public void StartHost()
|
||||
{
|
||||
if (isServer || isClient)
|
||||
|
Loading…
x
Reference in New Issue
Block a user