From e9a29f253c2043f754673139d1e1dd7ae1e18ed8 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Mon, 9 Dec 2024 16:59:26 +0100 Subject: [PATCH] Added networkId to all relevant messages --- Client.cs | 34 +++----- LowLevelMessages.cs | 16 ++-- Messages.cs | 200 +++++++++++++++++++++++--------------------- SiteServer.cs | 19 ----- 4 files changed, 124 insertions(+), 145 deletions(-) diff --git a/Client.cs b/Client.cs index 190c2b2..26ad994 100644 --- a/Client.cs +++ b/Client.cs @@ -25,27 +25,6 @@ namespace Passer.Control { } static public List clients = new List(); - //// These static functions are deprecated - //public static Client NewClient() { - // Client client = new(); - // clients.Add(client); - // client.networkId = 0; - - // return client; - //} - - //public static Client NewUDPClient(UdpClient udpClient, string ipAddress, int port) { - // Client client = NewClient(); - // client.ipAddress = null; - // client.port = port; - // client.udpClient = udpClient; - // return client; - //} - - //public Client() { - - //} - public Client(UdpClient udpClient, int port) { this.udpClient = udpClient; this.ipAddress = null; @@ -56,7 +35,7 @@ namespace Passer.Control { public virtual void ProcessMessages() { while (this.messageQueue.TryDequeue(out IMessage msg)) - ProcessMessage(msg); + ProcessMessage(msg); } public void ProcessMessage(IMessage msg) { @@ -91,7 +70,10 @@ namespace Passer.Control { case DestroyMsg destroy: ProcessDestroy(destroy); break; + default: + return; } + ForwardMessage(msg); } protected virtual void ProcessClient(ClientMsg client) { } @@ -113,5 +95,13 @@ namespace Passer.Control { protected virtual void ProcessText(TextMsg text) { } protected virtual void ProcessDestroy(DestroyMsg destroy) { } + + private void ForwardMessage(IMessage thing) { + foreach (Client client in Client.clients) { + if (client == this) + continue; + IMessage.SendMsg(client, thing); + } + } } } \ No newline at end of file diff --git a/LowLevelMessages.cs b/LowLevelMessages.cs index 43f3eeb..abeef3a 100644 --- a/LowLevelMessages.cs +++ b/LowLevelMessages.cs @@ -2,13 +2,13 @@ using Passer; public class LowLevelMessages { - public static void SendSpherical(byte[] buffer, ref uint ix, Spherical v) { + public static void SendSpherical(byte[] buffer, ref byte ix, Spherical v) { SendAngle8(buffer, ref ix, v.horizontal); SendAngle8(buffer, ref ix, v.vertical); SendFloat16(buffer, ref ix, new float16(v.distance)); } - public static Spherical ReceiveSpherical(byte[] data, ref uint ix) { + public static Spherical ReceiveSpherical(byte[] data, ref byte ix) { float horizontal = ReceiveAngle8(data, ref ix); float vertical = ReceiveAngle8(data, ref ix); float distance = ReceiveFloat16(data, ref ix); @@ -16,7 +16,7 @@ public class LowLevelMessages { return v; } - public static void SendQuat32(byte[] buffer, ref uint ix, Quat32 q) { + public static void SendQuat32(byte[] buffer, ref byte ix, Quat32 q) { int qx = (int)(q.x * 127 + 128); int qy = (int)(q.y * 127 + 128); int qz = (int)(q.z * 127 + 128); @@ -33,7 +33,7 @@ public class LowLevelMessages { buffer[ix++] = (byte)qz; buffer[ix++] = (byte)qw; } - public static Quat32 ReceiveQuat32(byte[] data, ref uint ix) { + public static Quat32 ReceiveQuat32(byte[] data, ref byte ix) { Quat32 q = new( (data[ix++] - 128.0F) / 127.0F, (data[ix++] - 128.0F) / 127.0F, @@ -42,7 +42,7 @@ public class LowLevelMessages { return q; } - public static void SendAngle8(byte[] buffer, ref uint ix, float angle) { + public static void SendAngle8(byte[] buffer, ref byte ix, float angle) { // Normalize angle while (angle >= 180) angle -= 360; @@ -51,18 +51,18 @@ public class LowLevelMessages { buffer[ix++] = (byte)((angle / 360.0f) * 256.0f); } - public static float ReceiveAngle8(byte[] data, ref uint ix) { + public static float ReceiveAngle8(byte[] data, ref byte ix) { float value = (data[ix++] * 180) / 128.0F; return value; } - public static void SendFloat16(byte[] data, ref uint ix, float16 f) { + public static void SendFloat16(byte[] data, ref byte ix, float16 f) { ushort binary = f.GetBinary(); data[ix++] = (byte)(binary >> 8); data[ix++] = (byte)(binary & 255); } - public static float ReceiveFloat16(byte[] data, ref uint ix) { + public static float ReceiveFloat16(byte[] data, ref byte ix) { ushort value = (ushort)(data[ix++] << 8 | data[ix++]); float16 f16 = new(); f16.SetBinary(value); diff --git a/Messages.cs b/Messages.cs index bae2a19..dc85d1a 100644 --- a/Messages.cs +++ b/Messages.cs @@ -5,32 +5,32 @@ namespace Passer.Control { public class IMessage { public IMessage() { } - public IMessage(byte[] data) { - Deserialize(data); + public IMessage(byte[] buffer) { + Deserialize(buffer); } public virtual byte[] Serialize() { return null; } - public virtual void Deserialize(byte[] data) { } + public virtual void Deserialize(byte[] buffer) { } public static bool SendMsg(Client client, IMessage msg) { return SendMsg(client, msg.Serialize()); } - public static bool SendMsg(Client client, byte[] data) { + public static bool SendMsg(Client client, byte[] buffer) { if (client == null || client.ipAddress == null) return false; - client.udpClient.Send(data, data.Length, client.ipAddress, client.port); + client.udpClient.Send(buffer, buffer.Length, client.ipAddress, client.port); return true; } public static bool PublishMsg(Client client, IMessage msg) { return PublishMsg(client, msg.Serialize()); } - public static bool PublishMsg(Client client, byte[] data) { + public static bool PublishMsg(Client client, byte[] buffer) { if (client == null) return false; - client.udpClient.Send(data, data.Length, "127.0.0.1", client.port); + client.udpClient.Send(buffer, buffer.Length, "127.0.0.1", client.port); return true; } @@ -56,7 +56,7 @@ namespace Passer.Control { public ClientMsg(byte networkId) { this.networkId = networkId; } - public ClientMsg(byte[] data) : base(data) { } + public ClientMsg(byte[] buffer) : base(buffer) { } public override byte[] Serialize() { byte[] buffer = new byte[ClientMsg.length]; @@ -64,10 +64,10 @@ namespace Passer.Control { buffer[1] = networkId; return buffer; } - public override void Deserialize(byte[] data) { - base.Deserialize(data); + public override void Deserialize(byte[] buffer) { + base.Deserialize(buffer); uint ix = 0; - networkId = data[ix]; + networkId = buffer[ix]; } public static bool Send(Client client, byte networkId) { @@ -88,13 +88,11 @@ namespace Passer.Control { if (client.networkId == 0) { client.networkId = (byte)(Client.clients.Count); NetworkIdMsg.Send(client, client.networkId); - //if (string.IsNullOrEmpty(sceneUrl) == false) - //SendModelUrl(client, sceneUrl); + client.messageQueue.Enqueue(msg); } else if (msg.networkId == 0) { NetworkIdMsg.Send(client, client.networkId); - //if (string.IsNullOrEmpty(sceneUrl) == false) - //SendModelUrl(client, sceneUrl); + client.messageQueue.Enqueue(msg); } return true; @@ -113,26 +111,22 @@ namespace Passer.Control { NetworkIdMsg(byte networkId) { this.networkId = networkId; } - NetworkIdMsg(byte[] data) : base(data) { } + NetworkIdMsg(byte[] buffer) : base(buffer) { } public override byte[] Serialize() { - byte[] data = new byte[NetworkIdMsg.length]; - data[0] = NetworkIdMsg.Id; - data[1] = this.networkId; - return data; + byte[] buffer = new byte[NetworkIdMsg.length]; + buffer[0] = NetworkIdMsg.Id; + buffer[1] = this.networkId; + return buffer; } - public override void Deserialize(byte[] data) { + public override void Deserialize(byte[] buffer) { uint ix = 0; - this.networkId = data[ix]; + this.networkId = buffer[ix]; } public static bool Send(Client client, byte networkId) { NetworkIdMsg msg = new(networkId); return SendMsg(client, msg); - //byte[] data = new byte[NetworkIdMsg.length]; - //data[0] = NetworkIdMsg.Id; - //data[1] = client.networkId; - //return SendMsg(client, data); } public static async Task Receive(Stream dataStream, Client client, byte packetSize) { if (packetSize != length) @@ -159,7 +153,7 @@ namespace Passer.Control { this.networkId = networkId; this.thingId = thingId; } - public InvestigateMsg(byte[] data) : base(data) { } + public InvestigateMsg(byte[] buffer) : base(buffer) { } public override byte[] Serialize() { byte[] buffer = new byte[InvestigateMsg.length]; @@ -168,10 +162,10 @@ namespace Passer.Control { buffer[2] = this.thingId; return buffer; } - public override void Deserialize(byte[] data) { + public override void Deserialize(byte[] buffer) { uint ix = 0; - this.networkId = data[ix++]; - this.thingId = data[ix++]; + this.networkId = buffer[ix++]; + this.thingId = buffer[ix++]; } public static bool Send(Client client, byte thingId) { @@ -195,8 +189,9 @@ namespace Passer.Control { #region Thing public class ThingMsg : IMessage { - public const byte length = 4; + public const byte length = 5; public const byte Id = 0x80; + public byte networkId; public byte thingId; public byte thingType; public byte parentId; @@ -206,22 +201,24 @@ namespace Passer.Control { this.thingType = thingType; this.parentId = parentId; } - public ThingMsg(byte[] data) : base(data) { } + public ThingMsg(byte[] buffer) : base(buffer) { } public override byte[] Serialize() { - byte[] data = new byte[ThingMsg.length]; + byte[] buffer = new byte[ThingMsg.length]; byte ix = 0; - data[ix++] = ThingMsg.Id; - data[ix++] = this.thingId; - data[ix++] = this.thingType; - data[ix] = this.parentId; - return data; + buffer[ix++] = ThingMsg.Id; + buffer[ix++] = this.networkId; + buffer[ix++] = this.thingId; + buffer[ix++] = this.thingType; + buffer[ix] = this.parentId; + return buffer; } - public override void Deserialize(byte[] data) { + public override void Deserialize(byte[] buffer) { uint ix = 0; - this.thingId = data[ix++]; - this.thingType = data[ix++]; - this.parentId = data[ix]; + this.networkId = buffer[ix++]; + this.thingId = buffer[ix++]; + this.thingType = buffer[ix++]; + this.parentId = buffer[ix]; } public static bool Send(Client client, byte thingId, byte thingType, byte parentId) { @@ -246,7 +243,8 @@ namespace Passer.Control { public class NameMsg : IMessage { public const byte Id = 0x91; // 145 - public const byte length = 3; + public const byte length = 4; + public byte networkId; public byte thingId; public byte len; public string name; @@ -255,22 +253,24 @@ namespace Passer.Control { this.thingId = thingId; this.name = name; } - public NameMsg(byte[] data) : base(data) { } + public NameMsg(byte[] buffer) : base(buffer) { } public override byte[] Serialize() { byte[] buffer = new byte[length + this.name.Length]; - buffer[0] = NameMsg.Id; - buffer[1] = this.thingId; - buffer[2] = (byte)this.name.Length; - for (int ix = 0; ix < this.name.Length; ix++) - buffer[3 + ix] = (byte)this.name[ix]; + byte ix = 0; + buffer[ix++] = NameMsg.Id; + buffer[ix++] = this.networkId; + buffer[ix++] = this.thingId; + buffer[ix++] = (byte)this.name.Length; + for (int nameIx = 0; nameIx < this.name.Length; nameIx++, ix++) + buffer[ix] = (byte)this.name[nameIx]; return buffer; } - public override void Deserialize(byte[] data) { + public override void Deserialize(byte[] buffer) { uint ix = 0; - this.thingId = data[ix++]; - int strlen = data[ix++]; - this.name = System.Text.Encoding.UTF8.GetString(data, (int)ix, strlen); + this.thingId = buffer[ix++]; + int strlen = buffer[ix++]; + this.name = System.Text.Encoding.UTF8.GetString(buffer, (int)ix, strlen); } public static bool Send(Client client, byte thingId, string name) { @@ -292,6 +292,7 @@ namespace Passer.Control { public class ModelUrlMsg : IMessage { public const byte Id = 0x90; // (144) Model URL + public byte networkId; public byte thingId; public Spherical position; public float scale; @@ -303,28 +304,29 @@ namespace Passer.Control { this.scale = scale; this.position = Spherical.zero; } - public ModelUrlMsg(byte[] data) : base(data) { } + public ModelUrlMsg(byte[] buffer) : base(buffer) { } public override byte[] Serialize() { - byte[] data = new byte[this.url.Length + 9]; - data[0] = ModelUrlMsg.Id; - data[1] = this.thingId; // Thing Id - // data[2]..[5] == position 0, 0, 0 - data[6] = 0x3C; // Dummy float16 value 1 - data[7] = 0x00; + byte[] buffer = new byte[this.url.Length + 5]; + byte ix = 0; + buffer[ix++] = ModelUrlMsg.Id; + buffer[ix++] = this.networkId; + buffer[ix++] = this.thingId; // Thing Id + LowLevelMessages.SendFloat16(buffer, ref ix, new float16(1.0f)); - data[8] = (byte)url.Length; - for (int ix = 0; ix < this.url.Length; ix++) - data[9 + ix] = (byte)url[ix]; - return data; + buffer[8] = (byte)url.Length; + for (int urlIx = 0; urlIx < this.url.Length; urlIx++, ix++) + buffer[ix] = (byte)url[urlIx]; + return buffer; } - public override void Deserialize(byte[] data) { - uint ix = 0; - this.thingId = data[ix++]; - this.position = LowLevelMessages.ReceiveSpherical(data, ref ix); - this.scale = LowLevelMessages.ReceiveFloat16(data, ref ix); - int strlen = data[ix++]; - url = System.Text.Encoding.UTF8.GetString(data, (int)ix, strlen); + public override void Deserialize(byte[] buffer) { + byte ix = 0; + this.networkId = buffer[ix++]; + this.thingId = buffer[ix++]; + //this.position = LowLevelMessages.ReceiveSpherical(data, ref ix); + this.scale = LowLevelMessages.ReceiveFloat16(buffer, ref ix); + int strlen = buffer[ix++]; + url = System.Text.Encoding.UTF8.GetString(buffer, (int)ix, strlen); } public static bool Send(Client client, byte thingId, string modelUrl) { @@ -345,7 +347,8 @@ namespace Passer.Control { public class PoseMsg : IMessage { public const byte Id = 0x10; - public const byte length = 3 + 4 + 4; + public const byte length = 4 + 4 + 4; + public byte networkId; public byte thingId; public byte poseType; @@ -369,12 +372,13 @@ namespace Passer.Control { else this.orientation = new Quat32(0, 0, 0, 1); } - public PoseMsg(byte[] data) : base(data) { } + public PoseMsg(byte[] buffer) : base(buffer) { } public override byte[] Serialize() { byte[] buffer = new byte[PoseMsg.length]; - uint ix = 0; + byte ix = 0; buffer[ix++] = PoseMsg.Id; + buffer[ix++] = this.networkId; buffer[ix++] = this.thingId; buffer[ix++] = this.poseType; @@ -382,15 +386,16 @@ namespace Passer.Control { LowLevelMessages.SendQuat32(buffer, ref ix, orientation); return buffer; } - public override void Deserialize(byte[] data) { - uint ix = 0; - thingId = data[ix++]; - poseType = data[ix++]; + public override void Deserialize(byte[] buffer) { + byte ix = 0; + thingId = buffer[ix++]; + thingId = buffer[ix++]; + poseType = buffer[ix++]; //if ((poseType & Pose_Position) != 0) - position = LowLevelMessages.ReceiveSpherical(data, ref ix); + position = LowLevelMessages.ReceiveSpherical(buffer, ref ix); //if ((poseType & Pose_Orientation) != 0) { - orientation = LowLevelMessages.ReceiveQuat32(data, ref ix); + orientation = LowLevelMessages.ReceiveQuat32(buffer, ref ix); } public static bool Send(Client client, byte thingId, Spherical position, Quat32 orientation) { @@ -419,7 +424,7 @@ namespace Passer.Control { public byte thingId; public byte[] bytes; - public CustomMsg(byte[] data) : base(data) { } + public CustomMsg(byte[] buffer) : base(buffer) { } public CustomMsg(byte networkId, byte thingId, byte[] bytes) : base() { this.networkId = networkId; this.thingId = thingId; @@ -428,7 +433,7 @@ namespace Passer.Control { public override byte[] Serialize() { byte[] buffer = new byte[4 + this.bytes.Length]; - int ix = 0; + byte ix = 0; buffer[ix++] = CustomMsg.Id; buffer[ix++] = this.networkId; buffer[ix++] = this.thingId; @@ -438,15 +443,16 @@ namespace Passer.Control { return buffer; } - public override void Deserialize(byte[] data) { + public override void Deserialize(byte[] buffer) { uint ix = 0; - this.thingId = data[ix++]; - this.bytes = new byte[data.Length - ix]; - for (uint bytesIx = 0; ix < data.Length; ix++, bytesIx++) - this.bytes[bytesIx] = data[ix]; + this.networkId = buffer[ix++]; + this.thingId = buffer[ix++]; + byte length = buffer[ix++]; + this.bytes = new byte[length]; + for (uint bytesIx = 0; ix < length; ix++, bytesIx++) + this.bytes[bytesIx] = buffer[ix]; } - public static void Send(Client client, byte thingId, byte[] bytes) { CustomMsg msg = new(client.networkId, thingId, bytes); SendMsg(client, msg); @@ -467,11 +473,11 @@ namespace Passer.Control { public const byte Id = 0xB0; public string text; - public TextMsg(byte[] data) : base(data) { } - public override void Deserialize(byte[] data) { + public TextMsg(byte[] buffer) : base(buffer) { } + public override void Deserialize(byte[] buffer) { uint ix = 0; - uint strlen = data[ix++]; - this.text = System.Text.Encoding.UTF8.GetString(data, (int)ix, (int)strlen); + uint strlen = buffer[ix++]; + this.text = System.Text.Encoding.UTF8.GetString(buffer, (int)ix, (int)strlen); } public static async Task Receive(Stream dataStream, Client client, byte packetSize) { @@ -490,12 +496,14 @@ namespace Passer.Control { public class DestroyMsg : IMessage { public const byte Id = 0x20; public const byte length = 2; - public byte objectId; + public byte networkId; + public byte thingId; - public DestroyMsg(byte[] data) : base(data) { } + public DestroyMsg(byte[] buffer) : base(buffer) { } - public override void Deserialize(byte[] data) { - objectId = data[0]; + public override void Deserialize(byte[] buffer) { + this.networkId = buffer[0]; + this.thingId = buffer[1]; } public static async Task Receive(Stream dataStream, Client client, byte packetSize) { diff --git a/SiteServer.cs b/SiteServer.cs index 24e42b2..a563bf5 100644 --- a/SiteServer.cs +++ b/SiteServer.cs @@ -1,28 +1,9 @@ -using System.Diagnostics; using System.IO; using System.Threading.Tasks; namespace Passer.Control { public static class SiteServer { - //public static async Task ReceiveData() { - // while (true) { - // //foreach (var client in Client.clients) { - - // for (int ix = 0; ix < Client.clients.Count; ix++) { - // if (ix > 0) - // UnityEngine.Debug.Log("Client2 "); - // Client client = Client.clients[ix]; - // if (client == null) - // continue; - - // byte packetSize = (byte)client.dataStream.ReadByte(); - // if (packetSize != 0xFF) - // await ReceiveData(client.dataStream, client, packetSize); - // // else timeout - // } - // } - //} public static async Task ReceiveData(Stream dataStream, Client client) { while (true) {