diff --git a/ClientMsg.cs.meta b/ClientMsg.cs.meta deleted file mode 100644 index a491f9d..0000000 --- a/ClientMsg.cs.meta +++ /dev/null @@ -1,2 +0,0 @@ -fileFormatVersion: 2 -guid: f73ac1131d4d7bd4a8f5b62500069867 \ No newline at end of file diff --git a/CustomMsg.cs b/CustomMsg.cs index 5b491b1..48691ec 100644 --- a/CustomMsg.cs +++ b/CustomMsg.cs @@ -1,3 +1,4 @@ +/* namespace Passer.Control.Core { public class CustomMsg : IMessage { @@ -42,4 +43,5 @@ namespace Passer.Control.Core { //} } -} \ No newline at end of file +} +*/ \ No newline at end of file diff --git a/EchoStream.cs b/EchoStream.cs index c9ae949..02b0781 100644 --- a/EchoStream.cs +++ b/EchoStream.cs @@ -1,3 +1,4 @@ +/* using System; using System.IO; using System.Threading.Tasks; @@ -170,4 +171,5 @@ public class EchoStream : Stream { public override void SetLength(long value) { throw new NotImplementedException(); } -} \ No newline at end of file +} +*/ \ No newline at end of file diff --git a/EchoStream.cs.meta b/EchoStream.cs.meta deleted file mode 100644 index 063a4f2..0000000 --- a/EchoStream.cs.meta +++ /dev/null @@ -1,2 +0,0 @@ -fileFormatVersion: 2 -guid: 422516a56cbf14d46aaa0b1bc09115bf \ No newline at end of file diff --git a/LinearAlgebra.meta b/LinearAlgebra.meta deleted file mode 100644 index a68ed37..0000000 --- a/LinearAlgebra.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: 028f10636fdaa594a9a9969924e5ff18 -folderAsset: yes -DefaultImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/LowLevelMessages.cs.meta b/LowLevelMessages.cs.meta deleted file mode 100644 index 9980c38..0000000 --- a/LowLevelMessages.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 157cf85b523c1f648adc007539f8b736 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Messages.cs.meta b/Messages.cs.meta deleted file mode 100644 index 60016bd..0000000 --- a/Messages.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: d3f26f22a5422a44997b39a1844ab2ad -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/ClientMsg.cs b/Messages/ClientMsg.cs similarity index 100% rename from ClientMsg.cs rename to Messages/ClientMsg.cs diff --git a/Messages/CustomMsg.cs b/Messages/CustomMsg.cs new file mode 100644 index 0000000..5b491b1 --- /dev/null +++ b/Messages/CustomMsg.cs @@ -0,0 +1,45 @@ +namespace Passer.Control.Core { + + public class CustomMsg : IMessage { + public const byte Id = 0xB1; + public byte networkId; + public byte thingId; + public byte[] bytes; + + public CustomMsg(byte[] buffer) { + byte ix = 1; + this.networkId = buffer[ix++]; + this.thingId = buffer[ix++]; + byte length = (byte)(buffer.Length - ix); + this.bytes = new byte[length]; + for (uint bytesIx = 0; bytesIx < length; bytesIx++) + this.bytes[bytesIx] = buffer[ix++]; + } + public CustomMsg(byte networkId, byte thingId, byte[] bytes) : base() { + this.networkId = networkId; + this.thingId = thingId; + this.bytes = bytes; + } + + public override byte Serialize(ref byte[] buffer) { + byte ix = 0; + buffer[ix++] = CustomMsg.Id; + buffer[ix++] = this.networkId; + buffer[ix++] = this.thingId; + //buffer[ix++] = (byte)bytes.Length; + foreach (byte b in bytes) + buffer[ix++] = b; + + return ix; + } + + //public static async Task Receive(Stream dataStream, Participant client, byte packetSize) { + // byte[] buffer = await Receive(dataStream, packetSize); + + // CustomMsg msg = new(buffer); + // client.messageQueue.Enqueue(msg); + // return true; + //} + } + +} \ No newline at end of file diff --git a/LowLevelMessages.cs b/Messages/LowLevelMessages.cs similarity index 100% rename from LowLevelMessages.cs rename to Messages/LowLevelMessages.cs diff --git a/Messages.cs b/Messages/Messages.cs similarity index 100% rename from Messages.cs rename to Messages/Messages.cs diff --git a/NameMsg.cs b/Messages/NameMsg.cs similarity index 100% rename from NameMsg.cs rename to Messages/NameMsg.cs diff --git a/NetworkIdMsg.cs b/Messages/NetworkIdMsg.cs similarity index 100% rename from NetworkIdMsg.cs rename to Messages/NetworkIdMsg.cs diff --git a/NameMsg.cs.meta b/NameMsg.cs.meta deleted file mode 100644 index 1c4a0f5..0000000 --- a/NameMsg.cs.meta +++ /dev/null @@ -1,2 +0,0 @@ -fileFormatVersion: 2 -guid: 462c645008baabe46b81f5d0ab83e26e \ No newline at end of file diff --git a/NetworkIdMsg.cs.meta b/NetworkIdMsg.cs.meta deleted file mode 100644 index afedff4..0000000 --- a/NetworkIdMsg.cs.meta +++ /dev/null @@ -1,2 +0,0 @@ -fileFormatVersion: 2 -guid: c51dae79b8c6f5a428349e1f4d34982c \ No newline at end of file diff --git a/Participant.cs b/Participant.cs index b237fa4..d04c2a9 100644 --- a/Participant.cs +++ b/Participant.cs @@ -234,16 +234,16 @@ namespace Passer.Control.Core { Console.WriteLine($"received name {msg.url}"); } - protected virtual void ProcessPoseMsg(PoseMsg msg) { } + protected virtual void Process(PoseMsg msg) { } protected virtual void Process(CustomMsg msg) { Thing thing = Thing.Get(msg.networkId, msg.thingId); thing?.ProcessBytes(msg.bytes); } - protected virtual void ProcessTextMsg(TextMsg temsgxt) { } + protected virtual void Process(TextMsg temsgxt) { } - protected virtual void ProcessDestroyMsg(DestroyMsg msg) { } + protected virtual void Process(DestroyMsg msg) { } private void ForwardMessage(IMessage msg) { foreach (Participant client in others) { diff --git a/Participant.cs.meta b/Participant.cs.meta deleted file mode 100644 index 541b236..0000000 --- a/Participant.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 302ed9b89a108a4429ea274044424a03 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/README.md.meta b/README.md.meta deleted file mode 100644 index f345b71..0000000 --- a/README.md.meta +++ /dev/null @@ -1,7 +0,0 @@ -fileFormatVersion: 2 -guid: 3b79b5b373e9ced4abe72eb4d2f83c6a -TextScriptImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Sensors/TemperatureSensor.cs b/Sensors/TemperatureSensor.cs index 7a54051..fdc5215 100644 --- a/Sensors/TemperatureSensor.cs +++ b/Sensors/TemperatureSensor.cs @@ -1,3 +1,5 @@ +using System; + namespace Passer.Control.Core { public class TemperatureSensor : Thing { @@ -7,7 +9,7 @@ namespace Passer.Control.Core { public override void ProcessBytes(byte[] bytes) { byte ix = 0; float temp = LowLevelMessages.ReceiveFloat16(bytes, ref ix); - UnityEngine.Debug.Log($"temperature {this.name} = {temp} C"); + Console.WriteLine($"temperature {this.name} = {temp} C"); } } diff --git a/SiteServer.cs b/SiteServer.cs index ab35567..0962e28 100644 --- a/SiteServer.cs +++ b/SiteServer.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Net; using System.Net.Sockets; @@ -14,9 +15,11 @@ namespace Passer.Control.Core { this.udpClient.Client.Bind(new IPEndPoint(IPAddress.Any, port)); this.udpClient.BeginReceive(new AsyncCallback(result => ReceiveUDP(result)), null); this.name = "Site Server"; + + Register((byte)Thing.Type.TemperatureSensor); } - public override void Update(long currentTimeMs) { + public override void Update(long currentTimeMs) { Thing.UpdateAll(currentTimeMs); } @@ -30,17 +33,28 @@ namespace Passer.Control.Core { protected override void Process(Participant sender, NetworkIdMsg msg) { } + + delegate Thing ThingConstructor(byte networkId, byte thingId); + readonly Dictionary thingMsgProcessors = new(); + + public void Register(byte thingType) where ThingClass : Thing { + thingMsgProcessors[thingType] = (byte networkId, byte thingId) => { + if (Activator.CreateInstance(typeof(Thing), networkId, thingId) is not ThingClass instance) + throw new InvalidOperationException($"Could not created an instance of {(typeof(ThingClass))}."); + return instance; + }; + Console.WriteLine($"Registering {typeof(ThingClass)} for thing type {thingType}"); + } + protected override void Process(ThingMsg msg) { Thing thing = Thing.Get(msg.networkId, msg.thingId); if (thing == null) { - switch ((Thing.Type) msg.thingType) { - case Thing.Type.TemperatureSensor: - new TemperatureSensor(msg.networkId, msg.thingId); - break; - default: - new Thing(this, msg.networkId, msg.thingId, msg.thingType); - break; - } + if (thingMsgProcessors.TryGetValue(msg.thingType, out ThingConstructor thingConstructor)) { + thingConstructor(msg.networkId, msg.thingId); + } + else { + new Thing(this, msg.networkId, msg.thingId, msg.thingType); + } } } diff --git a/SiteServer.cs.meta b/SiteServer.cs.meta deleted file mode 100644 index 02fd171..0000000 --- a/SiteServer.cs.meta +++ /dev/null @@ -1,2 +0,0 @@ -fileFormatVersion: 2 -guid: 53345abb9310d344baa67c19a8d8249f \ No newline at end of file diff --git a/Thing.cs b/Thing.cs index 1b3de79..a5dd1b3 100644 --- a/Thing.cs +++ b/Thing.cs @@ -193,7 +193,7 @@ namespace Passer.Control.Core { allThings.Add(thing); if (invokeEvent) OnNewThing?.Invoke(thing); - UnityEngine.Debug.Log($"Add thing [{thing.networkId}/{thing.id}] {thing.name}"); + //UnityEngine.Debug.Log($"Add thing [{thing.networkId}/{thing.id}] {thing.name}"); } } diff --git a/Thing.cs.meta b/Thing.cs.meta deleted file mode 100644 index 0d02170..0000000 --- a/Thing.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 7429282ee0e367445bd1e2111631b27d -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/test/UnitTest1.cs b/test/UnitTest1.cs index d41d268..859f9e6 100644 --- a/test/UnitTest1.cs +++ b/test/UnitTest1.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Threading; using NUnit.Framework;