Skip to content

Commit 04df5e4

Browse files
authored
Merge pull request #109 from MidLevel/new-messaging
New messaging
2 parents 6154170 + 468c4f3 commit 04df5e4

27 files changed

+2358
-2591
lines changed

MLAPI-Editor/NetworkingManagerEditor.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ public class NetworkingManagerEditor : Editor
1313
private SerializedProperty LogLevelProperty;
1414
private SerializedProperty NetworkConfigProperty;
1515

16-
17-
18-
1916
private ReorderableList networkPrefabsList;
2017
private ReorderableList channelsList;
2118
private ReorderableList messageTypesList;
@@ -84,6 +81,7 @@ private void OnEnable()
8481
EditorGUI.LabelField(rect, "NetworkedPrefabs (Auto Sorted)");
8582
};
8683

84+
/*
8785
messageTypesList = new ReorderableList(serializedObject, serializedObject.FindProperty("NetworkConfig").FindPropertyRelative("MessageTypes"), true, true, true, true);
8886
messageTypesList.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
8987
{
@@ -110,6 +108,7 @@ private void OnEnable()
110108
messageTypesList.drawHeaderCallback = (Rect rect) => {
111109
EditorGUI.LabelField(rect, "MessageTypes (Auto Sorted)");
112110
};
111+
*/
113112

114113

115114
channelsList = new ReorderableList(serializedObject, serializedObject.FindProperty("NetworkConfig").FindPropertyRelative("Channels"), true, true, true, true);

MLAPI.Tests/NetworkingManagerComponents/Binary/BitWriterTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class BitWriterTest
1212
[Test]
1313
public void TestWritingTrue()
1414
{
15-
BitWriterDeprecated bitWriter = BitWriterDeprecated.Get();
15+
BitWriter bitWriter = BitWriter.Get();
1616

1717
bitWriter.WriteBool(true);
1818

MLAPI/Data/AttributeMessageMode.cs

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,62 @@
1-
namespace MLAPI.Data
1+
using System;
2+
using System.Reflection;
3+
using MLAPI.NetworkingManagerComponents.Binary;
4+
5+
namespace MLAPI.Data
26
{
37
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
48
public enum AttributeMessageMode
59
{
6-
Disabled,
710
WovenTwoByte,
811
WovenFourByte,
912
WovenEightByte
1013
}
14+
15+
public delegate void RpcDelegate(uint clientId, BitReader reader);
16+
17+
public class ReflectionMehtod
18+
{
19+
public MethodInfo method;
20+
public Type[] parameterTypes;
21+
public object[] parameterRefs;
22+
23+
public ReflectionMehtod(MethodInfo methodInfo)
24+
{
25+
method = methodInfo;
26+
ParameterInfo[] parameters = methodInfo.GetParameters();
27+
parameterTypes = new Type[parameters.Length];
28+
parameterRefs = new object[parameters.Length];
29+
30+
for (int i = 0; i < parameters.Length; i++)
31+
{
32+
parameterTypes[i] = parameters[i].ParameterType;
33+
}
34+
}
35+
36+
public void Invoke(object instance, BitReader reader)
37+
{
38+
for (int i = 0; i < parameterTypes.Length; i++)
39+
{
40+
//TODO: BitReader ReadType
41+
parameterRefs[i] = reader.ReadObject(parameterTypes[i]);
42+
}
43+
44+
method.Invoke(instance, parameterRefs);
45+
}
46+
}
47+
48+
public class ServerRPC : Attribute
49+
{
50+
public bool RequireOwnership = true;
51+
internal ReflectionMehtod reflectionMethod;
52+
internal RpcDelegate rpcDelegate;
53+
}
54+
55+
public class ClientRPC : Attribute
56+
{
57+
internal ReflectionMehtod reflectionMethod;
58+
internal RpcDelegate rpcDelegate;
59+
}
60+
1161
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
1262
}

MLAPI/Data/ChannelAttribute.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace MLAPI.Data
7+
{
8+
public class ChannelSetting : Attribute
9+
{
10+
public string channel { get; set; } = "MLAPI_DEFAULT_MESSAGE";
11+
}
12+
}

MLAPI/Data/FieldType.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ internal static bool IsRefType(Type t)
7474
return (!t.IsValueType && t != typeof(string)) || (t.IsArray && IsRefType(t.GetElementType()));
7575
}
7676

77-
internal static void WriteFieldType(BitWriterDeprecated writer, object newValue)
77+
internal static void WriteFieldType(BitWriter writer, object newValue)
7878
{
7979
WriteFieldType(writer, newValue, null);
8080
}
8181

82-
internal static void WriteFieldType(BitWriterDeprecated writer, object newValue, object oldValue)
82+
internal static void WriteFieldType(BitWriter writer, object newValue, object oldValue)
8383
{
8484
oldValue = null;
8585
Type newValueType = newValue.GetType();
@@ -172,12 +172,12 @@ internal static void WriteFieldType(BitWriterDeprecated writer, object newValue,
172172
}
173173
}
174174

175-
internal static object ReadFieldType(BitReaderDeprecated reader, Type type)
175+
internal static object ReadFieldType(BitReader reader, Type type)
176176
{
177177
return ReadFieldType(reader, type, null);
178178
}
179179

180-
internal static object ReadFieldType(BitReaderDeprecated reader, Type type, object oldObject)
180+
internal static object ReadFieldType(BitReader reader, Type type, object oldObject)
181181
{
182182
CheckForReferenceTypes(type);
183183
if (type.IsArray)

MLAPI/Data/MLAPIConstants.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace MLAPI.Data
2+
{
3+
public static class MLAPIConstants
4+
{
5+
public const string MLAPI_PROTOCOL_VERSION = "2.0.0";
6+
7+
public const ushort MLAPI_CONNECTION_REQUEST = 0;
8+
public const ushort MLAPI_CONNECTION_APPROVED = 1;
9+
public const ushort MLAPI_ADD_OBJECT = 2;
10+
public const ushort MLAPI_CLIENT_DISCONNECT = 3;
11+
public const ushort MLAPI_DESTROY_OBJECT = 4;
12+
public const ushort MLAPI_SWITCH_SCENE = 5;
13+
public const ushort MLAPI_SPAWN_POOL_OBJECT = 6;
14+
public const ushort MLAPI_DESTROY_POOL_OBJECT = 7;
15+
public const ushort MLAPI_CHANGE_OWNER = 8;
16+
public const ushort MLAPI_ADD_OBJECTS = 9;
17+
public const ushort MLAPI_TIME_SYNC = 10;
18+
public const ushort MLAPI_NETWORKED_VAR_DELTA = 11;
19+
public const ushort MLAPI_NETWORKED_VAR_UPDATE = 12;
20+
public const ushort MLAPI_SERVER_RPC = 13;
21+
public const ushort MLAPI_CLIENT_RPC = 14;
22+
public const ushort MLAPI_CUSTOM_MESSAGE = 15;
23+
}
24+
}

MLAPI/Data/NetworkConfig.cs

Lines changed: 7 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,6 @@ public class NetworkConfig
4545
[HideInInspector]
4646
public List<Channel> Channels = new List<Channel>();
4747
/// <summary>
48-
/// Registered MessageTypes
49-
/// </summary>
50-
[HideInInspector]
51-
public List<MessageType> MessageTypes = new List<MessageType>();
52-
internal HashSet<ushort> PassthroughMessageHashSet = new HashSet<ushort>();
53-
internal HashSet<string> EncryptedChannelsHashSet = new HashSet<string>();
54-
internal List<string> EncryptedChannels = new List<string>();
55-
/// <summary>
5648
/// A list of SceneNames that can be used during networked games.
5749
/// </summary>
5850
[HideInInspector]
@@ -73,7 +65,7 @@ public class NetworkConfig
7365
/// <summary>
7466
/// The size of the receive message buffer. This is the max message size.
7567
/// </summary>
76-
public int MessageBufferSize = 65535;
68+
public int MessageBufferSize = 1024;
7769
/// <summary>
7870
/// Amount of times per second the receive queue is emptied and all messages inside are processed.
7971
/// </summary>
@@ -142,10 +134,6 @@ public class NetworkConfig
142134
[TextArea]
143135
public string RSAPublicKey = "<RSAKeyValue><Modulus>vBEvOQki/EftWOgwh4G8/nFRvcDJLylc8P7Dhz5m/hpkkNtAMzizNKYUrGbs7sYWlEuMYBOWrzkIDGOMoOsYc9uCi+8EcmNoHDlIhK5yNfZUexYBF551VbvZ625LSBR7kmBxkyo4IPuA09fYCHeUFm3prt4h6aTD0Hjc7ZsJHUU=</Modulus><Exponent>EQ==</Exponent></RSAKeyValue>"; //CHANGE THESE FOR PRODUCTION!
144136
/// <summary>
145-
/// Wheter or not to allow any type of passthrough messages
146-
/// </summary>
147-
public bool AllowPassthroughMessages = true;
148-
/// <summary>
149137
/// Wheter or not to enable scene switching
150138
/// </summary>
151139
public bool EnableSceneSwitching = true;
@@ -156,11 +144,10 @@ public class NetworkConfig
156144
/// <summary>
157145
/// Decides how many bytes to use for Attribute messaging. Leave this to 2 bytes unless you are facing hash collisions
158146
/// </summary>
159-
public AttributeMessageMode AttributeMessageMode = AttributeMessageMode.Disabled;
147+
public AttributeMessageMode AttributeMessageMode = AttributeMessageMode.WovenTwoByte;
160148

161149
private void Sort()
162150
{
163-
MessageTypes = MessageTypes.OrderBy(x => x.Name).ToList();
164151
Channels = Channels.OrderBy(x => x.Name).ToList();
165152
NetworkedPrefabs = NetworkedPrefabs.OrderBy(x => x.name).ToList();
166153
RegisteredScenes.Sort();
@@ -173,7 +160,7 @@ private void Sort()
173160
public string ToBase64()
174161
{
175162
NetworkConfig config = this;
176-
using (BitWriterDeprecated writer = BitWriterDeprecated.Get())
163+
using (BitWriter writer = BitWriter.Get())
177164
{
178165
writer.WriteUShort(config.ProtocolVersion);
179166
writer.WriteBits((byte)config.Transport, 5);
@@ -186,13 +173,6 @@ public string ToBase64()
186173
writer.WriteBits((byte)config.Channels[i].Type, 5);
187174
}
188175

189-
writer.WriteUShort((ushort)config.MessageTypes.Count);
190-
for (int i = 0; i < config.MessageTypes.Count; i++)
191-
{
192-
writer.WriteString(config.MessageTypes[i].Name);
193-
writer.WriteBool(config.MessageTypes[i].Passthrough);
194-
}
195-
196176
writer.WriteUShort((ushort)config.RegisteredScenes.Count);
197177
for (int i = 0; i < config.RegisteredScenes.Count; i++)
198178
{
@@ -220,7 +200,6 @@ public string ToBase64()
220200
writer.WriteBool(config.HandleObjectSpawning);
221201
writer.WriteBool(config.EnableEncryption);
222202
writer.WriteBool(config.SignKeyExchange);
223-
writer.WriteBool(config.AllowPassthroughMessages);
224203
writer.WriteBool(config.EnableSceneSwitching);
225204
writer.WriteBool(config.EnableTimeResync);
226205
writer.WriteBits((byte)config.AttributeMessageMode, 3);
@@ -238,7 +217,7 @@ public void FromBase64(string base64, bool createDummyObject = false)
238217
{
239218
NetworkConfig config = this;
240219
byte[] binary = Convert.FromBase64String(base64);
241-
using (BitReaderDeprecated reader = BitReaderDeprecated.Get(binary))
220+
using (BitReader reader = BitReader.Get(binary))
242221
{
243222
config.ProtocolVersion = reader.ReadUShort();
244223
config.Transport = (DefaultTransport)reader.ReadBits(5);
@@ -256,18 +235,6 @@ public void FromBase64(string base64, bool createDummyObject = false)
256235
config.Channels.Add(channel);
257236
}
258237

259-
ushort messageTypeCount = reader.ReadUShort();
260-
config.MessageTypes.Clear();
261-
for (int i = 0; i < messageTypeCount; i++)
262-
{
263-
MessageType messageType = new MessageType()
264-
{
265-
Name = reader.ReadString(),
266-
Passthrough = reader.ReadBool()
267-
};
268-
config.MessageTypes.Add(messageType);
269-
}
270-
271238
ushort sceneCount = reader.ReadUShort();
272239
config.RegisteredScenes.Clear();
273240
for (int i = 0; i < sceneCount; i++)
@@ -310,7 +277,6 @@ public void FromBase64(string base64, bool createDummyObject = false)
310277
config.HandleObjectSpawning = reader.ReadBool();
311278
config.EnableEncryption = reader.ReadBool();
312279
config.SignKeyExchange = reader.ReadBool();
313-
config.AllowPassthroughMessages = reader.ReadBool();
314280
config.EnableSceneSwitching = reader.ReadBool();
315281
config.EnableTimeResync = reader.ReadBool();
316282
config.AttributeMessageMode = (AttributeMessageMode)reader.ReadBits(3);
@@ -330,22 +296,18 @@ public ulong GetConfig(bool cache = true)
330296

331297
Sort();
332298

333-
using (BitWriterDeprecated writer = BitWriterDeprecated.Get())
299+
using (BitWriter writer = BitWriter.Get())
334300
{
335301
writer.WriteUShort(ProtocolVersion);
302+
writer.WriteString(MLAPIConstants.MLAPI_PROTOCOL_VERSION);
303+
336304
for (int i = 0; i < Channels.Count; i++)
337305
{
338306
writer.WriteString(Channels[i].Name);
339307
writer.WriteByte((byte)Channels[i].Type);
340308
if (EnableEncryption)
341309
writer.WriteBool(Channels[i].Encrypted);
342310
}
343-
for (int i = 0; i < MessageTypes.Count; i++)
344-
{
345-
writer.WriteString(MessageTypes[i].Name);
346-
if (AllowPassthroughMessages)
347-
writer.WriteBool(MessageTypes[i].Passthrough);
348-
}
349311
if (EnableSceneSwitching)
350312
{
351313
for (int i = 0; i < RegisteredScenes.Count; i++)
@@ -362,7 +324,6 @@ public ulong GetConfig(bool cache = true)
362324
}
363325
writer.WriteBool(HandleObjectSpawning);
364326
writer.WriteBool(EnableEncryption);
365-
writer.WriteBool(AllowPassthroughMessages);
366327
writer.WriteBool(EnableSceneSwitching);
367328
writer.WriteBool(SignKeyExchange);
368329
writer.WriteBits((byte)AttributeMessageMode, 3);

MLAPI/Data/NetworkedCollections/NetworkedDictionary.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public string GetChannel()
7575
}
7676

7777
/// <inheritdoc />
78-
public void ReadDelta(BitReaderDeprecated reader)
78+
public void ReadDelta(BitReader reader)
7979
{
8080
ushort deltaCount = reader.ReadUShort();
8181
for (int i = 0; i < deltaCount; i++)
@@ -124,7 +124,7 @@ public void ReadDelta(BitReaderDeprecated reader)
124124
}
125125

126126
/// <inheritdoc />
127-
public void ReadField(BitReaderDeprecated reader)
127+
public void ReadField(BitReader reader)
128128
{
129129
dictionary.Clear();
130130
ushort entryCount = reader.ReadUShort();
@@ -149,7 +149,7 @@ public bool TryGetValue(TKey key, out TValue value)
149149
}
150150

151151
/// <inheritdoc />
152-
public void WriteDelta(BitWriterDeprecated writer)
152+
public void WriteDelta(BitWriter writer)
153153
{
154154
writer.WriteUShort((ushort)dirtyEvents.Count);
155155
for (int i = 0; i < dirtyEvents.Count; i++)
@@ -192,7 +192,7 @@ public void WriteDelta(BitWriterDeprecated writer)
192192
}
193193

194194
/// <inheritdoc />
195-
public void WriteField(BitWriterDeprecated writer)
195+
public void WriteField(BitWriter writer)
196196
{
197197
writer.WriteUShort((ushort)dictionary.Count);
198198
foreach (KeyValuePair<TKey, TValue> pair in dictionary)

MLAPI/Data/NetworkedCollections/NetworkedList.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public bool CanClientRead(uint clientId)
126126
}
127127

128128
/// <inheritdoc />
129-
public void WriteDelta(BitWriterDeprecated writer)
129+
public void WriteDelta(BitWriter writer)
130130
{
131131
writer.WriteUShort((ushort)dirtyEvents.Count);
132132
for (int i = 0; i < dirtyEvents.Count; i++)
@@ -173,7 +173,7 @@ public void WriteDelta(BitWriterDeprecated writer)
173173
}
174174

175175
/// <inheritdoc />
176-
public void WriteField(BitWriterDeprecated writer)
176+
public void WriteField(BitWriter writer)
177177
{
178178
writer.WriteUShort((ushort)list.Count);
179179
for (int i = 0; i < list.Count; i++)
@@ -183,7 +183,7 @@ public void WriteField(BitWriterDeprecated writer)
183183
}
184184

185185
/// <inheritdoc />
186-
public void ReadField(BitReaderDeprecated reader)
186+
public void ReadField(BitReader reader)
187187
{
188188
list.Clear();
189189
ushort count = reader.ReadUShort();
@@ -194,7 +194,7 @@ public void ReadField(BitReaderDeprecated reader)
194194
}
195195

196196
/// <inheritdoc />
197-
public void ReadDelta(BitReaderDeprecated reader)
197+
public void ReadDelta(BitReader reader)
198198
{
199199
ushort deltaCount = reader.ReadUShort();
200200
for (int i = 0; i < deltaCount; i++)

0 commit comments

Comments
 (0)