Skip to content

Commit 07178c6

Browse files
committed
Added XML summaries everywhere and improved NetworkConfig to be less error prone
1 parent a0157be commit 07178c6

File tree

8 files changed

+181
-61
lines changed

8 files changed

+181
-61
lines changed

MLAPI/Data/Channel.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,23 @@
66

77
namespace MLAPI.Data
88
{
9+
/// <summary>
10+
/// A data object that represents a NetworkTransport channel
11+
/// </summary>
912
[Serializable]
1013
public class Channel
1114
{
15+
/// <summary>
16+
/// The name of the channel
17+
/// </summary>
1218
public string Name;
19+
/// <summary>
20+
/// The Transport QOS type
21+
/// </summary>
1322
public QosType Type;
23+
/// <summary>
24+
/// Wheter or not the channel should be encrypted
25+
/// </summary>
26+
public bool Encrypted;
1427
}
1528
}

MLAPI/Data/MessageType.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
namespace MLAPI.Data
3+
{
4+
/// <summary>
5+
/// Represents a MLAPI message type
6+
/// </summary>
7+
[Serializable]
8+
public class MessageType
9+
{
10+
/// <summary>
11+
/// The name of the messageType
12+
/// </summary>
13+
public string Name;
14+
/// <summary>
15+
/// Wheter or not the channel should have passthrough support.
16+
/// </summary>
17+
public bool Passthrough;
18+
}
19+
}

MLAPI/Data/NetId.cs

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,58 @@
33

44
namespace MLAPI.Data
55
{
6+
/// <summary>
7+
/// Represents a ClientId structure
8+
/// </summary>
69
public struct NetId
710
{
11+
/// <summary>
12+
/// The hostId this client is on
13+
/// </summary>
814
public byte HostId;
15+
/// <summary>
16+
/// The connectionId this client is assigned
17+
/// </summary>
918
public ushort ConnectionId;
19+
/// <summary>
20+
/// Meta data about hte client
21+
/// </summary>
1022
public byte Meta;
1123

24+
/// <summary>
25+
/// Returns wheter or not the clientId represents a -1
26+
/// </summary>
27+
/// <returns><c>true</c>, if host, <c>false</c> otherwise.</returns>
1228
public bool IsHost()
1329
{
1430
return Meta == 1;
1531
}
16-
32+
/// <summary>
33+
/// Returns if this is a invalid clientId, (-2)
34+
/// </summary>
35+
/// <returns><c>true</c>, if invalid, <c>false</c> otherwise.</returns>
1736
public bool IsInvalid()
1837
{
1938
return Meta == 2;
2039
}
21-
40+
/// <summary>
41+
/// Static ServerNetId for comparison
42+
/// </summary>
43+
/// <value>The server net identifier.</value>
2244
public static NetId ServerNetId
2345
{
2446
get
2547
{
2648
return new NetId((byte)NetworkingManager.singleton.serverHostId, (ushort)NetworkingManager.singleton.serverConnectionId, false, false);
2749
}
2850
}
29-
51+
/// <summary>
52+
/// Initializes a new instance of the netId struct from transport values
53+
/// </summary>
54+
/// <param name="hostId">Host identifier.</param>
55+
/// <param name="connectionId">Connection identifier.</param>
56+
/// <param name="isHost">If set to <c>true</c> is host.</param>
57+
/// <param name="isInvalid">If set to <c>true</c> is invalid.</param>
3058
public NetId(byte hostId, ushort connectionId, bool isHost, bool isInvalid)
3159
{
3260
HostId = hostId;
@@ -42,14 +70,21 @@ public NetId(byte hostId, ushort connectionId, bool isHost, bool isInvalid)
4270

4371
private static byte[] tempUIntBytes = new byte[4];
4472
private static byte[] tempUShortBytes = new byte[2];
73+
/// <summary>
74+
/// Initializes a new instance of the netId struct from a clientId
75+
/// </summary>
76+
/// <param name="clientId">Client identifier.</param>
4577
public NetId(uint clientId)
4678
{
4779
tempUIntBytes = BitConverter.GetBytes(clientId);
4880
HostId = tempUIntBytes[0];
4981
ConnectionId = BitConverter.ToUInt16(tempUIntBytes, 1);
5082
Meta = tempUIntBytes[3];
5183
}
52-
84+
/// <summary>
85+
/// Gets the clientId.
86+
/// </summary>
87+
/// <returns>The client identifier.</returns>
5388
public uint GetClientId()
5489
{
5590
tempUShortBytes = BitConverter.GetBytes(ConnectionId);
@@ -59,7 +94,13 @@ public uint GetClientId()
5994
tempUIntBytes[3] = Meta;
6095
return BitConverter.ToUInt32(tempUIntBytes, 0);
6196
}
62-
97+
// Rider generated vvv
98+
/// <summary>
99+
/// Determines whether the specified <see cref="object"/> is equal to the current <see cref="T:MLAPI.Data.NetId"/>.
100+
/// </summary>
101+
/// <param name="obj">The <see cref="object"/> to compare with the current <see cref="T:MLAPI.Data.NetId"/>.</param>
102+
/// <returns><c>true</c> if the specified <see cref="object"/> is equal to the current <see cref="T:MLAPI.Data.NetId"/>;
103+
/// otherwise, <c>false</c>.</returns>
63104
public override bool Equals (object obj)
64105
{
65106
if (obj == null || GetType() != obj.GetType())
@@ -68,17 +109,34 @@ public override bool Equals (object obj)
68109
NetId key = (NetId)obj;
69110
return (HostId == key.HostId) && (ConnectionId == key.ConnectionId);
70111
}
71-
112+
// Rider generated vvv
113+
/// <summary>
114+
/// Serves as a hash function for a <see cref="T:MLAPI.Data.NetId"/> object.
115+
/// </summary>
116+
/// <returns>A hash code for this instance that is suitable for use in hashing algorithms and data structures such as a
117+
/// hash table.</returns>
72118
public override int GetHashCode()
73119
{
74120
return (int)GetClientId();
75121
}
76-
122+
// Rider generated vvv
123+
/// <summary>
124+
/// Determines whether a specified instance of <see cref="MLAPI.Data.NetId"/> is equal to another specified <see cref="MLAPI.Data.NetId"/>.
125+
/// </summary>
126+
/// <param name="client1">The first <see cref="MLAPI.Data.NetId"/> to compare.</param>
127+
/// <param name="client2">The second <see cref="MLAPI.Data.NetId"/> to compare.</param>
128+
/// <returns><c>true</c> if <c>client1</c> and <c>client2</c> are equal; otherwise, <c>false</c>.</returns>
77129
public static bool operator ==(NetId client1, NetId client2)
78130
{
79131
return (client1.HostId == client2.HostId && client1.ConnectionId == client2.ConnectionId) || (client1.IsHost() == client2.IsHost());
80132
}
81-
133+
// Rider generated vvv
134+
/// <summary>
135+
/// Determines whether a specified instance of <see cref="MLAPI.Data.NetId"/> is not equal to another specified <see cref="MLAPI.Data.NetId"/>.
136+
/// </summary>
137+
/// <param name="client1">The first <see cref="MLAPI.Data.NetId"/> to compare.</param>
138+
/// <param name="client2">The second <see cref="MLAPI.Data.NetId"/> to compare.</param>
139+
/// <returns><c>true</c> if <c>client1</c> and <c>client2</c> are not equal; otherwise, <c>false</c>.</returns>
82140
public static bool operator !=(NetId client1, NetId client2)
83141
{
84142
return !(client1 == client2);

MLAPI/Data/NetworkConfig.cs

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,10 @@ public class NetworkConfig
3939
/// <summary>
4040
/// Registered MessageTypes
4141
/// </summary>
42-
public List<string> MessageTypes = new List<string>();
43-
/// <summary>
44-
/// 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.
45-
/// </summary>
46-
public List<string> PassthroughMessageTypes = new List<string>();
42+
public List<MessageType> MessageTypes = new List<MessageType>();
4743
internal HashSet<ushort> PassthroughMessageHashSet = new HashSet<ushort>();
48-
/// <summary>
49-
/// Set of channels that will have all message contents encrypted when used
50-
/// </summary>
51-
public List<string> EncryptedChannels = new List<string>();
5244
internal HashSet<string> EncryptedChannelsHashSet = new HashSet<string>();
45+
internal List<string> EncryptedChannels = new List<string>();
5346
/// <summary>
5447
/// A list of SceneNames that can be used during networked games.
5548
/// </summary>
@@ -161,17 +154,14 @@ public byte[] GetConfig(bool cache = true)
161154
{
162155
writer.Write(Channels[i].Name);
163156
writer.Write((byte)Channels[i].Type);
157+
if (EnableEncryption)
158+
writer.Write(Channels[i].Encrypted);
164159
}
165160
for (int i = 0; i < MessageTypes.Count; i++)
166161
{
167-
writer.Write(MessageTypes[i]);
168-
}
169-
if (AllowPassthroughMessages)
170-
{
171-
for (int i = 0; i < PassthroughMessageTypes.Count; i++)
172-
{
173-
writer.Write(PassthroughMessageTypes[i]);
174-
}
162+
writer.Write(MessageTypes[i].Name);
163+
if (AllowPassthroughMessages)
164+
writer.Write(MessageTypes[i].Passthrough);
175165
}
176166
if (EnableSceneSwitching)
177167
{
@@ -180,13 +170,6 @@ public byte[] GetConfig(bool cache = true)
180170
writer.Write(RegisteredScenes[i]);
181171
}
182172
}
183-
if(EnableEncryption)
184-
{
185-
for (int i = 0; i < EncryptedChannels.Count; i++)
186-
{
187-
writer.Write(EncryptedChannels[i]);
188-
}
189-
}
190173
if(HandleObjectSpawning)
191174
{
192175
writer.Write(SpawnablePrefabs.Count);

MLAPI/Data/TransportHost.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,23 @@
22

33
namespace MLAPI.Data
44
{
5+
/// <summary>
6+
/// Represents a Transport host
7+
/// </summary>
58
[Serializable]
69
public class TransportHost
710
{
11+
/// <summary>
12+
/// The name of the host
13+
/// </summary>
814
public string Name = Guid.NewGuid().ToString().Replace("-", "");
15+
/// <summary>
16+
/// The port the host should listen to
17+
/// </summary>
918
public int Port = 7777;
19+
/// <summary>
20+
/// If true, the socket will listen on TCP-Websockets, otherwise UDP
21+
/// </summary>
1022
public bool Websockets = false;
1123
}
1224
}

MLAPI/MLAPI.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
<Compile Include="Properties\AssemblyInfo.cs" />
9696
<Compile Include="Data\NetId.cs" />
9797
<Compile Include="NetworkingManagerComponents\Binary\MessageChunker.cs" />
98+
<Compile Include="Data\MessageType.cs" />
9899
</ItemGroup>
99100
<ItemGroup>
100101
<None Include="packages.config" />

MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ protected void SendToLocalClient(string messageType, string channelName, byte[]
756756
Debug.LogWarning("MLAPI: Sending messages on the internal MLAPI channels is not allowed!");
757757
return;
758758
}
759-
if (!isServer && (!NetworkingManager.singleton.NetworkConfig.AllowPassthroughMessages || !NetworkingManager.singleton.NetworkConfig.PassthroughMessageTypes.Contains(messageType)))
759+
if (!isServer && (!NetworkingManager.singleton.NetworkConfig.AllowPassthroughMessages || !NetworkingManager.singleton.NetworkConfig.PassthroughMessageHashSet.Contains(MessageManager.messageTypes[messageType])))
760760
{
761761
Debug.LogWarning("MLAPI: Invalid Passthrough send. Ensure AllowPassthroughMessages are turned on and that the MessageType " + messageType + " is registered as a passthroughMessageType");
762762
return;
@@ -789,7 +789,7 @@ protected void SendToLocalClientTarget(string messageType, string channelName, b
789789
Debug.LogWarning("MLAPI: Sending messages on the internal MLAPI channels is not allowed!");
790790
return;
791791
}
792-
if (!isServer && (!NetworkingManager.singleton.NetworkConfig.AllowPassthroughMessages || !NetworkingManager.singleton.NetworkConfig.PassthroughMessageTypes.Contains(messageType)))
792+
if (!isServer && (!NetworkingManager.singleton.NetworkConfig.AllowPassthroughMessages || !NetworkingManager.singleton.NetworkConfig.PassthroughMessageHashSet.Contains(MessageManager.messageTypes[messageType])))
793793
{
794794
Debug.LogWarning("MLAPI: Invalid Passthrough send. Ensure AllowPassthroughMessages are turned on and that the MessageType " + messageType + " is registered as a passthroughMessageType");
795795
return;
@@ -889,7 +889,7 @@ protected void SendToClient(uint clientId, string messageType, string channelNam
889889
Debug.LogWarning("MLAPI: Sending messages on the internal MLAPI channels is not allowed!");
890890
return;
891891
}
892-
if (!isServer && (!NetworkingManager.singleton.NetworkConfig.AllowPassthroughMessages || !NetworkingManager.singleton.NetworkConfig.PassthroughMessageTypes.Contains(messageType)))
892+
if (!isServer && (!NetworkingManager.singleton.NetworkConfig.AllowPassthroughMessages || !NetworkingManager.singleton.NetworkConfig.PassthroughMessageHashSet.Contains(MessageManager.messageTypes[messageType])))
893893
{
894894
Debug.LogWarning("MLAPI: Invalid Passthrough send. Ensure AllowPassthroughMessages are turned on and that the MessageType " + messageType + " is registered as a passthroughMessageType");
895895
return;
@@ -924,7 +924,7 @@ protected void SendToClientTarget(uint clientId, string messageType, string chan
924924
Debug.LogWarning("MLAPI: Sending messages on the internal MLAPI channels is not allowed!");
925925
return;
926926
}
927-
if (!isServer && (!NetworkingManager.singleton.NetworkConfig.AllowPassthroughMessages || !NetworkingManager.singleton.NetworkConfig.PassthroughMessageTypes.Contains(messageType)))
927+
if (!isServer && (!NetworkingManager.singleton.NetworkConfig.AllowPassthroughMessages || !NetworkingManager.singleton.NetworkConfig.PassthroughMessageHashSet.Contains(MessageManager.messageTypes[messageType])))
928928
{
929929
Debug.LogWarning("MLAPI: Invalid Passthrough send. Ensure AllowPassthroughMessages are turned on and that the MessageType " + messageType + " is registered as a passthroughMessageType");
930930
return;

0 commit comments

Comments
 (0)