From 9919aa6578dac8462626e7ad4129cac1392775ee Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Thu, 5 Dec 2024 14:11:58 +0100 Subject: [PATCH 1/3] Replaced use of Thing type with the values --- Messages.cs | 713 ++++++++++++++++++++++++++-------------------------- 1 file changed, 355 insertions(+), 358 deletions(-) diff --git a/Messages.cs b/Messages.cs index eacfea6..b517f4d 100644 --- a/Messages.cs +++ b/Messages.cs @@ -6,363 +6,360 @@ using System.Threading.Tasks; namespace Passer.Control { - public class Client { - //public ConnectionMethod connection; - public UdpClient udpClient; - public string ipAddress; - public int port; - - public byte networkId; - - public readonly ConcurrentQueue messageQueue = new(); - - public static Client GetClient(string ipAddress, int port) { - foreach (Client c in clients) { - if (c.ipAddress == ipAddress && c.port == port) - return c; - } - return null; - } - static public List clients = new List(); - - 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 class IMessage { - public IMessage() { } - public IMessage(byte[] data) { - Deserialize(data); - } - - public virtual byte[] Serialize() { return null; } - public virtual void Deserialize(byte[] data) { } - - public static bool SendMsg(Client client, IMessage msg) { - return SendMsg(client, msg.Serialize()); - } - public static bool SendMsg(Client client, byte[] data) { - if (client == null || client.ipAddress == null) - return false; - - client.udpClient.Send(data, data.Length, client.ipAddress, client.port); - return true; - } - public static async Task Receive(Stream dataStream, byte packetSize) { - byte[] buffer = new byte[packetSize - 1]; // without msgId - int byteCount = dataStream.Read(buffer, 0, packetSize - 1); - while (byteCount < packetSize - 1) { - // not all bytes have been read, wait and try again - await Task.Delay(1); - byteCount += dataStream.Read(buffer, byteCount, packetSize - 1 - byteCount); - } - return buffer; - } - } - - #region Client - - public class ClientMsg : IMessage { - public const byte length = 2; - public byte clientId; - - public ClientMsg(byte[] data) : base(data) { } - public override void Deserialize(byte[] data) { - base.Deserialize(data); - uint ix = 0; - clientId = data[ix++]; - } - - public static async Task Receive(Stream dataStream, Client client, byte packetSize) { - if (packetSize != length) - return false; - - byte[] buffer = await Receive(dataStream, packetSize); - ClientMsg msg = new(buffer); - - if (client.networkId == 0) { - client.networkId = (byte)(Client.clients.Count); - NetworkIdMsg.Send(client); - //if (string.IsNullOrEmpty(sceneUrl) == false) - //SendModelUrl(client, sceneUrl); - } - else if (msg.clientId == 0) { - NetworkIdMsg.Send(client); - //if (string.IsNullOrEmpty(sceneUrl) == false) - //SendModelUrl(client, sceneUrl); - } - - return true; - } - } - - #endregion Client - - #region Network Id - - public class NetworkIdMsg : IMessage { - public const byte Id = 0xA1; - public const byte length = 2; - - public static bool Send(Client client) { - byte[] data = new byte[NetworkIdMsg.length]; - data[0] = NetworkIdMsg.Id; - data[1] = client.networkId; - return SendMsg(client, data); - } - } - - #endregion Network Id - - #region Thing - - public class ThingMsg : IMessage { - public const byte length = 4; - public const byte Id = 0x80; - public byte objectId; - public byte objectType; - public byte parentId; - - public ThingMsg(byte[] data) : base(data) { } - public override void Deserialize(byte[] data) { - uint ix = 0; - objectId = data[ix++]; - objectType = data[ix++]; - parentId = data[ix]; - } - - public static bool Send(Client client, Thing obj) { - if (obj == null) - return false; - - byte[] data = new byte[4]; - data[0] = ThingMsg.Id; - data[1] = obj.networkId; - data[2] = obj.thingId; - data[3] = obj.objectType; - data[4] = 0x00; // not supported yet - return SendMsg(client, data); - } - public static async Task Receive(Stream dataStream, Client client, byte packetSize) { - if (packetSize != length) - return false; - - byte[] buffer = await Receive(dataStream, packetSize); - ThingMsg msg = new(buffer); - - client.messageQueue.Enqueue(msg); - return true; - } - } - - #endregion Thing - - #region Name - - public class NameMsg : IMessage { - public byte networkId = 0; - public byte objectId; - public byte len; - public string name; - - public NameMsg(byte[] data) : base(data) { } - public override void Deserialize(byte[] data) { - uint ix = 0; - this.objectId = data[ix++]; - int strlen = data[ix++]; - this.name = System.Text.Encoding.UTF8.GetString(data, (int)ix, strlen); - } - - public static async Task Receive(Stream dataStream, Client client, byte packetSize) { - byte[] buffer = await Receive(dataStream, packetSize); - NameMsg msg = new(buffer); - - client.messageQueue.Enqueue(msg); - return true; - } - } - - #endregion - - #region Model URL - - public class ModelUrlMsg : IMessage { - public const byte Id = 0x90; // (144) Model URL - public byte objectId; - public Spherical position; - public float scale; - public string url; - - public ModelUrlMsg(string url, float scale = 1) { - this.url = url; - this.scale = scale; - this.position = Spherical.zero; - } - public ModelUrlMsg(byte[] data) : base(data) { } - - public override byte[] Serialize() { - byte[] data = new byte[this.url.Length + 9]; - data[0] = ModelUrlMsg.Id; - data[1] = 0x00; // Thing Id - // data[2]..[5] == position 0, 0, 0 - data[6] = 0x3C; // Dummy float16 value 1 - data[7] = 0x00; - - data[8] = (byte)url.Length; - for (int ix = 0; ix < this.url.Length; ix++) - data[9 + ix] = (byte)url[ix]; - return data; - } - public override void Deserialize(byte[] data) { - uint ix = 0; - this.objectId = 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 static bool Send(Client client, string modelUrl) { - ModelUrlMsg msg = new(modelUrl); - return SendMsg(client, msg); - } - public static async Task Receive(Stream dataStream, Client client, byte packetSize) { - byte[] buffer = await Receive(dataStream, packetSize); - ModelUrlMsg msg = new(buffer); - client.messageQueue.Enqueue(msg); - return true; - } - } - - #endregion Model URL - - #region Pose - - public class PoseMsg : IMessage { - public const byte length = 3 + 4 + 4; - public byte thingId; - public byte poseType; - - public Spherical position; - public Quat32 orientation; - - public PoseMsg(byte[] data) : base(data) { } - - public override void Deserialize(byte[] data) { - uint ix = 0; - thingId = data[ix++]; - poseType = data[ix++]; - - //if ((poseType & Pose_Position) != 0) - position = LowLevelMessages.ReceiveSpherical(data, ref ix); - //if ((poseType & Pose_Orientation) != 0) { - orientation = LowLevelMessages.ReceiveQuat32(data, ref ix); - } - - public static async Task Receive(Stream dataStream, Client client, byte packetSize) { - if (packetSize != length) - return false; - - byte[] buffer = await Receive(dataStream, packetSize); - PoseMsg msg = new(buffer); - - client.messageQueue.Enqueue(msg); - return true; - - } - } - - #endregion Pose - - #region Bytes - - public class BytesMsg : IMessage { - public const byte Id = 0xB1; - public byte networkId; - public byte thingId; - public byte[] bytes; - - public BytesMsg(byte[] data) : base(data) { } - public BytesMsg(byte networkId, byte thingId, byte[] bytes) : base() { - this.networkId = networkId; - this.thingId = thingId; - this.bytes = bytes; - } - public override byte[] Serialize() { - byte[] buffer = new byte[4 + this.bytes.Length]; - int ix = 0; - buffer[ix++] = BytesMsg.Id; - buffer[ix++] = this.networkId; - buffer[ix++] = this.thingId; - buffer[ix++] = (byte)bytes.Length; - foreach (byte b in bytes) - buffer[ix++] = b; - - return buffer; - } - public override void Deserialize(byte[] data) { - //this.bytes = data; - 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]; - } - - - public static void Send(Client client, byte thingId, byte[] bytes) { - BytesMsg msg = new(client.networkId, thingId, bytes); - SendMsg(client, msg); - } - - // received bytes - public static async Task Receive(Stream dataStream, Client client, byte packetSize) { - byte[] buffer = await Receive(dataStream, packetSize); - BytesMsg msg = new(buffer); - client.messageQueue.Enqueue(msg); - return true; - } - } - - #endregion Bytes - - #region Destroy - - public class DestroyMsg : IMessage { - public const byte length = 2; - public byte objectId; - - public DestroyMsg(byte[] data) : base(data) { } + public class Client { + //public ConnectionMethod connection; + public UdpClient udpClient; + public string ipAddress; + public int port; + + public byte networkId; + + public readonly ConcurrentQueue messageQueue = new(); + + public static Client GetClient(string ipAddress, int port) { + foreach (Client c in clients) { + if (c.ipAddress == ipAddress && c.port == port) + return c; + } + return null; + } + static public List clients = new List(); + + 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 class IMessage { + public IMessage() { } + public IMessage(byte[] data) { + Deserialize(data); + } + + public virtual byte[] Serialize() { return null; } + public virtual void Deserialize(byte[] data) { } + + public static bool SendMsg(Client client, IMessage msg) { + return SendMsg(client, msg.Serialize()); + } + public static bool SendMsg(Client client, byte[] data) { + if (client == null || client.ipAddress == null) + return false; + + client.udpClient.Send(data, data.Length, client.ipAddress, client.port); + return true; + } + public static async Task Receive(Stream dataStream, byte packetSize) { + byte[] buffer = new byte[packetSize - 1]; // without msgId + int byteCount = dataStream.Read(buffer, 0, packetSize - 1); + while (byteCount < packetSize - 1) { + // not all bytes have been read, wait and try again + await Task.Delay(1); + byteCount += dataStream.Read(buffer, byteCount, packetSize - 1 - byteCount); + } + return buffer; + } + } + + #region Client + + public class ClientMsg : IMessage { + public const byte length = 2; + public byte clientId; + + public ClientMsg(byte[] data) : base(data) { } + public override void Deserialize(byte[] data) { + base.Deserialize(data); + uint ix = 0; + clientId = data[ix++]; + } + + public static async Task Receive(Stream dataStream, Client client, byte packetSize) { + if (packetSize != length) + return false; + + byte[] buffer = await Receive(dataStream, packetSize); + ClientMsg msg = new(buffer); + + if (client.networkId == 0) { + client.networkId = (byte)(Client.clients.Count); + NetworkIdMsg.Send(client); + //if (string.IsNullOrEmpty(sceneUrl) == false) + //SendModelUrl(client, sceneUrl); + } + else if (msg.clientId == 0) { + NetworkIdMsg.Send(client); + //if (string.IsNullOrEmpty(sceneUrl) == false) + //SendModelUrl(client, sceneUrl); + } + + return true; + } + } + + #endregion Client + + #region Network Id + + public class NetworkIdMsg : IMessage { + public const byte Id = 0xA1; + public const byte length = 2; + + public static bool Send(Client client) { + byte[] data = new byte[NetworkIdMsg.length]; + data[0] = NetworkIdMsg.Id; + data[1] = client.networkId; + return SendMsg(client, data); + } + } + + #endregion Network Id + + #region Thing + + public class ThingMsg : IMessage { + public const byte length = 4; + public const byte Id = 0x80; + public byte objectId; + public byte objectType; + public byte parentId; + + public ThingMsg(byte[] data) : base(data) { } + public override void Deserialize(byte[] data) { + uint ix = 0; + objectId = data[ix++]; + objectType = data[ix++]; + parentId = data[ix]; + } + + public static bool Send(Client client, byte networkId, byte thingId, byte thingType) { + byte[] data = new byte[4]; + data[0] = ThingMsg.Id; + data[1] = networkId; + data[2] = thingId; + data[3] = thingType; + data[4] = 0x00; // parent not supported yet + return SendMsg(client, data); + } + public static async Task Receive(Stream dataStream, Client client, byte packetSize) { + if (packetSize != length) + return false; + + byte[] buffer = await Receive(dataStream, packetSize); + ThingMsg msg = new(buffer); + + client.messageQueue.Enqueue(msg); + return true; + } + } + + #endregion Thing + + #region Name + + public class NameMsg : IMessage { + public byte networkId = 0; + public byte objectId; + public byte len; + public string name; + + public NameMsg(byte[] data) : base(data) { } + public override void Deserialize(byte[] data) { + uint ix = 0; + this.objectId = data[ix++]; + int strlen = data[ix++]; + this.name = System.Text.Encoding.UTF8.GetString(data, (int)ix, strlen); + } + + public static async Task Receive(Stream dataStream, Client client, byte packetSize) { + byte[] buffer = await Receive(dataStream, packetSize); + NameMsg msg = new(buffer); + + client.messageQueue.Enqueue(msg); + return true; + } + } + + #endregion + + #region Model URL + + public class ModelUrlMsg : IMessage { + public const byte Id = 0x90; // (144) Model URL + public byte objectId; + public Spherical position; + public float scale; + public string url; + + public ModelUrlMsg(string url, float scale = 1) { + this.url = url; + this.scale = scale; + this.position = Spherical.zero; + } + public ModelUrlMsg(byte[] data) : base(data) { } + + public override byte[] Serialize() { + byte[] data = new byte[this.url.Length + 9]; + data[0] = ModelUrlMsg.Id; + data[1] = 0x00; // Thing Id + // data[2]..[5] == position 0, 0, 0 + data[6] = 0x3C; // Dummy float16 value 1 + data[7] = 0x00; + + data[8] = (byte)url.Length; + for (int ix = 0; ix < this.url.Length; ix++) + data[9 + ix] = (byte)url[ix]; + return data; + } + public override void Deserialize(byte[] data) { + uint ix = 0; + this.objectId = 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 static bool Send(Client client, string modelUrl) { + ModelUrlMsg msg = new(modelUrl); + return SendMsg(client, msg); + } + public static async Task Receive(Stream dataStream, Client client, byte packetSize) { + byte[] buffer = await Receive(dataStream, packetSize); + ModelUrlMsg msg = new(buffer); + client.messageQueue.Enqueue(msg); + return true; + } + } + + #endregion Model URL + + #region Pose + + public class PoseMsg : IMessage { + public const byte length = 3 + 4 + 4; + public byte thingId; + public byte poseType; + + public Spherical position; + public Quat32 orientation; + + public PoseMsg(byte[] data) : base(data) { } + + public override void Deserialize(byte[] data) { + uint ix = 0; + thingId = data[ix++]; + poseType = data[ix++]; + + //if ((poseType & Pose_Position) != 0) + position = LowLevelMessages.ReceiveSpherical(data, ref ix); + //if ((poseType & Pose_Orientation) != 0) { + orientation = LowLevelMessages.ReceiveQuat32(data, ref ix); + } + + public static async Task Receive(Stream dataStream, Client client, byte packetSize) { + if (packetSize != length) + return false; + + byte[] buffer = await Receive(dataStream, packetSize); + PoseMsg msg = new(buffer); + + client.messageQueue.Enqueue(msg); + return true; + + } + } + + #endregion Pose + + #region Bytes + + public class BytesMsg : IMessage { + public const byte Id = 0xB1; + public byte networkId; + public byte thingId; + public byte[] bytes; + + public BytesMsg(byte[] data) : base(data) { } + public BytesMsg(byte networkId, byte thingId, byte[] bytes) : base() { + this.networkId = networkId; + this.thingId = thingId; + this.bytes = bytes; + } + public override byte[] Serialize() { + byte[] buffer = new byte[4 + this.bytes.Length]; + int ix = 0; + buffer[ix++] = BytesMsg.Id; + buffer[ix++] = this.networkId; + buffer[ix++] = this.thingId; + buffer[ix++] = (byte)bytes.Length; + foreach (byte b in bytes) + buffer[ix++] = b; + + return buffer; + } + public override void Deserialize(byte[] data) { + //this.bytes = data; + 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]; + } + + + public static void Send(Client client, byte thingId, byte[] bytes) { + BytesMsg msg = new(client.networkId, thingId, bytes); + SendMsg(client, msg); + } + + // received bytes + public static async Task Receive(Stream dataStream, Client client, byte packetSize) { + byte[] buffer = await Receive(dataStream, packetSize); + BytesMsg msg = new(buffer); + client.messageQueue.Enqueue(msg); + return true; + } + } + + #endregion Bytes + + #region Destroy + + public class DestroyMsg : IMessage { + public const byte length = 2; + public byte objectId; + + public DestroyMsg(byte[] data) : base(data) { } - public override void Deserialize(byte[] data) { - objectId = data[0]; - } - - public static async Task Receive(Stream dataStream, Client client, byte packetSize) { - if (packetSize != length) - return false; - - byte[] buffer = await Receive(dataStream, packetSize); - DestroyMsg msg = new(buffer); - - client.messageQueue.Enqueue(msg); - return true; - } - } - - - #endregion Destroy + public override void Deserialize(byte[] data) { + objectId = data[0]; + } + + public static async Task Receive(Stream dataStream, Client client, byte packetSize) { + if (packetSize != length) + return false; + + byte[] buffer = await Receive(dataStream, packetSize); + DestroyMsg msg = new(buffer); + + client.messageQueue.Enqueue(msg); + return true; + } + } + + + #endregion Destroy } From d8fc41f1c4657a762d4827d05d4006348ae53210 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Fri, 6 Dec 2024 15:39:36 +0100 Subject: [PATCH 2/3] First step for ControlCore support --- EchoStream.cs | 173 ++++++++++++++++++++++++++++++++++++++++++++ EchoStream.cs.meta | 2 + Messages.cs | 175 ++++++++++++++++++++++++++++++++++++++------- SiteServer.cs | 85 ++++++++++++++++++++++ SiteServer.cs.meta | 2 + 5 files changed, 410 insertions(+), 27 deletions(-) create mode 100644 EchoStream.cs create mode 100644 EchoStream.cs.meta create mode 100644 SiteServer.cs create mode 100644 SiteServer.cs.meta diff --git a/EchoStream.cs b/EchoStream.cs new file mode 100644 index 0000000..c9ae949 --- /dev/null +++ b/EchoStream.cs @@ -0,0 +1,173 @@ +using System; +using System.IO; +using System.Threading.Tasks; +using System.Threading; +using System.Collections.Concurrent; + +public class EchoStream : Stream { + public override bool CanTimeout { get; } = true; + public override int ReadTimeout { get; set; } = Timeout.Infinite; + public override int WriteTimeout { get; set; } = Timeout.Infinite; + public override bool CanRead { get; } = true; + public override bool CanSeek { get; } = false; + public override bool CanWrite { get; } = true; + + public bool CopyBufferOnWrite { get; set; } = false; + + private readonly object _lock = new object(); + + // Default underlying mechanism for BlockingCollection is ConcurrentQueue, which is what we want + private readonly BlockingCollection _Buffers; + private int _maxQueueDepth = 10; + + private byte[] m_buffer = null; + private int m_offset = 0; + private int m_count = 0; + + private bool m_Closed = false; + private bool m_FinalZero = false; //after the stream is closed, set to true after returning a 0 for read() + public override void Close() { + m_Closed = true; + + // release any waiting writes + _Buffers.CompleteAdding(); + } + + public bool DataAvailable { + get { + return _Buffers.Count > 0; + } + } + + private long _Length = 0L; + public override long Length { + get { + return _Length; + } + } + + private long _Position = 0L; + public override long Position { + get { + return _Position; + } + set { + throw new NotImplementedException(); + } + } + + public EchoStream() : this(10) { + } + + public EchoStream(int maxQueueDepth) { + _maxQueueDepth = maxQueueDepth; + _Buffers = new BlockingCollection(_maxQueueDepth); + } + + // we override the xxxxAsync functions because the default base class shares state between ReadAsync and WriteAsync, which causes a hang if both are called at once + public new Task WriteAsync(byte[] buffer, int offset, int count) { + return Task.Run(() => Write(buffer, offset, count)); + } + + // we override the xxxxAsync functions because the default base class shares state between ReadAsync and WriteAsync, which causes a hang if both are called at once + public new Task ReadAsync(byte[] buffer, int offset, int count) { + return Task.Run(() => { + return Read(buffer, offset, count); + }); + } + + public override void Write(byte[] buffer, int offset, int count) { + if (m_Closed || buffer.Length - offset < count || count <= 0) + return; + + byte[] newBuffer; + if (!CopyBufferOnWrite && offset == 0 && count == buffer.Length) + newBuffer = buffer; + else { + newBuffer = new byte[count]; + System.Buffer.BlockCopy(buffer, offset, newBuffer, 0, count); + } + if (!_Buffers.TryAdd(newBuffer, WriteTimeout)) + throw new TimeoutException("EchoStream Write() Timeout"); + + _Length += count; + } + + public override int Read(byte[] buffer, int offset, int count) { + if (count == 0) + return 0; + lock (_lock) { + if (m_count == 0 && _Buffers.Count == 0) { + if (m_Closed) { + if (!m_FinalZero) { + m_FinalZero = true; + return 0; + } + else { + return -1; + } + } + + if (_Buffers.TryTake(out m_buffer, ReadTimeout)) { + m_offset = 0; + m_count = m_buffer.Length; + } + else { + if (m_Closed) { + if (!m_FinalZero) { + m_FinalZero = true; + return 0; + } + else { + return -1; + } + } + else { + return 0; + } + } + } + + int returnBytes = 0; + while (count > 0) { + if (m_count == 0) { + if (_Buffers.TryTake(out m_buffer, 0)) { + m_offset = 0; + m_count = m_buffer.Length; + } + else + break; + } + + var bytesToCopy = (count < m_count) ? count : m_count; + System.Buffer.BlockCopy(m_buffer, m_offset, buffer, offset, bytesToCopy); + m_offset += bytesToCopy; + m_count -= bytesToCopy; + offset += bytesToCopy; + count -= bytesToCopy; + + returnBytes += bytesToCopy; + } + + _Position += returnBytes; + + return returnBytes; + } + } + + public override int ReadByte() { + byte[] returnValue = new byte[1]; + return (Read(returnValue, 0, 1) <= 0 ? -1 : (int)returnValue[0]); + } + + public override void Flush() { + } + + public override long Seek(long offset, SeekOrigin origin) { + throw new NotImplementedException(); + } + + public override void SetLength(long value) { + throw new NotImplementedException(); + } +} \ No newline at end of file diff --git a/EchoStream.cs.meta b/EchoStream.cs.meta new file mode 100644 index 0000000..063a4f2 --- /dev/null +++ b/EchoStream.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 422516a56cbf14d46aaa0b1bc09115bf \ No newline at end of file diff --git a/Messages.cs b/Messages.cs index b517f4d..acbecad 100644 --- a/Messages.cs +++ b/Messages.cs @@ -3,6 +3,7 @@ using System.Collections.Concurrent; using System.IO; using System.Net.Sockets; using System.Threading.Tasks; +using System; namespace Passer.Control { @@ -76,16 +77,25 @@ namespace Passer.Control { #region Client public class ClientMsg : IMessage { + public const byte Id = 0xA0; public const byte length = 2; - public byte clientId; + public byte networkId; + public ClientMsg(byte networkId) { + this.networkId = networkId; + } public ClientMsg(byte[] data) : base(data) { } + public override void Deserialize(byte[] data) { base.Deserialize(data); uint ix = 0; - clientId = data[ix++]; + networkId = data[ix]; } + public static bool Send(Client client, byte networkId) { + ClientMsg msg = new(networkId); + return SendMsg(client, msg); + } public static async Task Receive(Stream dataStream, Client client, byte packetSize) { if (packetSize != length) return false; @@ -95,12 +105,12 @@ namespace Passer.Control { if (client.networkId == 0) { client.networkId = (byte)(Client.clients.Count); - NetworkIdMsg.Send(client); + NetworkIdMsg.Send(client, client.networkId); //if (string.IsNullOrEmpty(sceneUrl) == false) //SendModelUrl(client, sceneUrl); } - else if (msg.clientId == 0) { - NetworkIdMsg.Send(client); + else if (msg.networkId == 0) { + NetworkIdMsg.Send(client, client.networkId); //if (string.IsNullOrEmpty(sceneUrl) == false) //SendModelUrl(client, sceneUrl); } @@ -116,20 +126,92 @@ namespace Passer.Control { public class NetworkIdMsg : IMessage { public const byte Id = 0xA1; public const byte length = 2; + public byte networkId; - public static bool Send(Client client) { - byte[] data = new byte[NetworkIdMsg.length]; - data[0] = NetworkIdMsg.Id; - data[1] = client.networkId; - return SendMsg(client, data); + NetworkIdMsg(byte networkId) { + this.networkId = networkId; + } + NetworkIdMsg(byte[] data) : base(data) { } + + public override byte[] Serialize() { + byte[] data = new byte[NetworkIdMsg.length]; + data[0] = NetworkIdMsg.Id; + data[1] = this.networkId; + return data; + } + public override void Deserialize(byte[] data) { + uint ix = 0; + this.networkId = data[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) + return false; + + byte[] buffer = await Receive(dataStream, packetSize); + NetworkIdMsg msg = new(buffer); + client.messageQueue.Enqueue(msg); + return true; + } } - #endregion Network Id + #endregion Network Id - #region Thing + #region Investigate - public class ThingMsg : IMessage { + class InvestigateMsg : IMessage { + public const byte Id = 0x81; + public const byte length = 3; + public byte networkId; + public byte thingId; + + public InvestigateMsg(byte networkId, byte thingId) { + this.networkId = networkId; + this.thingId = thingId; + } + public InvestigateMsg(byte[] data) : base(data) { } + + public override byte[] Serialize() { + byte[] buffer = new byte[InvestigateMsg.length]; + buffer[0] = InvestigateMsg.Id; + buffer[1] = this.networkId; + buffer[2] = this.thingId; + return buffer; + } + public override void Deserialize(byte[] data) { + uint ix = 0; + this.networkId = data[ix++]; + this.thingId = data[ix++]; + } + + public static bool Send(Client client, byte thingId) { + InvestigateMsg msg = new(client.networkId, thingId); + return SendMsg(client, msg); + } + public static async Task Receive(Stream dataStream, Client client, byte packetSize) { + if (packetSize != length) + return false; + + byte[] buffer = await Receive(dataStream, packetSize); + InvestigateMsg msg = new(buffer); + client.messageQueue.Enqueue(msg); + return true; + + } + } + + #endregion Investigate + #region Thing + + public class ThingMsg : IMessage { public const byte length = 4; public const byte Id = 0x80; public byte objectId; @@ -144,13 +226,13 @@ namespace Passer.Control { parentId = data[ix]; } - public static bool Send(Client client, byte networkId, byte thingId, byte thingType) { + public static bool Send(Client client, byte thingId, byte thingType, byte parentId) { byte[] data = new byte[4]; data[0] = ThingMsg.Id; - data[1] = networkId; + data[1] = client.networkId; data[2] = thingId; data[3] = thingType; - data[4] = 0x00; // parent not supported yet + data[4] = parentId; return SendMsg(client, data); } public static async Task Receive(Stream dataStream, Client client, byte packetSize) { @@ -170,19 +252,39 @@ namespace Passer.Control { #region Name public class NameMsg : IMessage { + public const byte Id = 0x91; // 145 + public const byte length = 3; public byte networkId = 0; - public byte objectId; + public byte thingId; public byte len; public string name; + public NameMsg(byte thingId, string name) { + this.thingId = thingId; + this.name = name; + } public NameMsg(byte[] data) : base(data) { } - public override void Deserialize(byte[] data) { + + public override byte[] Serialize() { + byte[] data = new byte[length + this.name.Length]; + data[0] = NameMsg.Id; + data[1] = this.thingId; + data[2] = (byte)this.name.Length; + for (int ix = 0; ix < this.name.Length; ix++) + data[3 + ix] = (byte)this.name[ix]; + return data; + } + public override void Deserialize(byte[] data) { uint ix = 0; - this.objectId = data[ix++]; + this.thingId = data[ix++]; int strlen = data[ix++]; this.name = System.Text.Encoding.UTF8.GetString(data, (int)ix, strlen); } + public static bool Send(Client client, byte thingId, string name) { + NameMsg msg = new NameMsg(thingId, name); + return SendMsg(client, msg); + } public static async Task Receive(Stream dataStream, Client client, byte packetSize) { byte[] buffer = await Receive(dataStream, packetSize); NameMsg msg = new(buffer); @@ -198,12 +300,13 @@ namespace Passer.Control { public class ModelUrlMsg : IMessage { public const byte Id = 0x90; // (144) Model URL - public byte objectId; + public byte thingId; public Spherical position; public float scale; public string url; - public ModelUrlMsg(string url, float scale = 1) { + public ModelUrlMsg(byte thingId, string url, float scale = 1) { + this.thingId = thingId; this.url = url; this.scale = scale; this.position = Spherical.zero; @@ -213,7 +316,7 @@ namespace Passer.Control { public override byte[] Serialize() { byte[] data = new byte[this.url.Length + 9]; data[0] = ModelUrlMsg.Id; - data[1] = 0x00; // Thing Id + data[1] = this.thingId; // Thing Id // data[2]..[5] == position 0, 0, 0 data[6] = 0x3C; // Dummy float16 value 1 data[7] = 0x00; @@ -225,15 +328,15 @@ namespace Passer.Control { } public override void Deserialize(byte[] data) { uint ix = 0; - this.objectId = data[ix++]; + 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 static bool Send(Client client, string modelUrl) { - ModelUrlMsg msg = new(modelUrl); + public static bool Send(Client client, byte thingId, string modelUrl) { + ModelUrlMsg msg = new(thingId, modelUrl); return SendMsg(client, msg); } public static async Task Receive(Stream dataStream, Client client, byte packetSize) { @@ -249,14 +352,28 @@ namespace Passer.Control { #region Pose public class PoseMsg : IMessage { + public const byte Id = 0x10; public const byte length = 3 + 4 + 4; public byte thingId; + public byte poseType; + public const byte Pose_Position = 0x01; + public const byte Pose_Orientation = 0x02; public Spherical position; public Quat32 orientation; - public PoseMsg(byte[] data) : base(data) { } + public PoseMsg(byte thingId, Spherical position, Quat32 orientation) { + this.thingId = thingId; + this.position = position; + this.orientation = orientation; + this.poseType = 0; + if (this.position != null) + this.poseType |= Pose_Position; + if (this.orientation != null) + this.poseType |= Pose_Orientation; + } + public PoseMsg(byte[] data) : base(data) { } public override void Deserialize(byte[] data) { uint ix = 0; @@ -269,6 +386,10 @@ namespace Passer.Control { orientation = LowLevelMessages.ReceiveQuat32(data, ref ix); } + public static bool Send(Client client, byte thingId, Spherical position, Quat32 orientation) { + PoseMsg msg = new(thingId, position, orientation); + return SendMsg(client, msg); + } public static async Task Receive(Stream dataStream, Client client, byte packetSize) { if (packetSize != length) return false; @@ -278,7 +399,6 @@ namespace Passer.Control { client.messageQueue.Enqueue(msg); return true; - } } @@ -339,6 +459,7 @@ namespace Passer.Control { #region Destroy public class DestroyMsg : IMessage { + public const byte Id = 0x20; public const byte length = 2; public byte objectId; diff --git a/SiteServer.cs b/SiteServer.cs new file mode 100644 index 0000000..cd01666 --- /dev/null +++ b/SiteServer.cs @@ -0,0 +1,85 @@ + +using System.IO; +using System.Threading.Tasks; + +namespace Passer.Control { + + public static class SiteServer { + public static async Task ReceiveData(Stream dataStream, Client client) { + while (true) { + byte packetSize = (byte)dataStream.ReadByte(); + if (packetSize == 0xFF) + //Debug.Log("Receive timeout"); + // Timeout + ; + else + await ReceiveData(dataStream, client, packetSize); + } + } + + public static async Task ReceiveData(Stream dataStream, Client client, byte packetSize) { + byte msgId = (byte)dataStream.ReadByte(); + if (msgId == 0xFF) { + // Timeout + return; + } + + bool result = false; + switch (msgId) { + case PoseMsg.Id: // Object Pose (16) + result = await PoseMsg.Receive(dataStream, client, packetSize); + break; + case DestroyMsg.Id: // Destroy object (32) + result = await DestroyMsg.Receive(dataStream, client, packetSize); + break; + case ThingMsg.Id: + result = await ThingMsg.Receive(dataStream, client, packetSize); + break; + case InvestigateMsg.Id: + result = await InvestigateMsg.Receive(dataStream, client, packetSize); + break; + case ModelUrlMsg.Id: // Model URL (144) + result = await BytesMsg.Receive(dataStream, client, packetSize); + break; + case NameMsg.Id: // Object Name (145) + result = await NameMsg.Receive(dataStream, client, packetSize); + break; + case ClientMsg.Id: + result = await ClientMsg.Receive(dataStream, client, packetSize); + break; + case NetworkIdMsg.Id: + result = await NetworkIdMsg.Receive(dataStream, client, packetSize); + break; + //case TextMsg.Id: // Text (176) + // result = await TextMsg.Receive(dataStream, client, packetSize); + // break; + case BytesMsg.Id: + result = await BytesMsg.Receive(dataStream, client, packetSize); + break; + default: + break; + } + if (result == false) { + packetSize = msgId; // skip 1 byte, msgId is possibly a packet size byte + await ReceiveData(dataStream, client, packetSize); + } + } + + public static void ProcessMessage(ISiteServer site, Client client, IMessage msg) { + switch (msg) { + case NetworkIdMsg networkId: + site.ProcessNetworkId(client, networkId); + break; + case ModelUrlMsg modelUrl: + site.ProcessModelUrl(client, modelUrl); + break; + } + } + } + + public interface ISiteServer { + + public void ProcessNetworkId(Client client, NetworkIdMsg networkId); + public void ProcessModelUrl(Client client, ModelUrlMsg modelUrl); + } +} \ No newline at end of file diff --git a/SiteServer.cs.meta b/SiteServer.cs.meta new file mode 100644 index 0000000..02fd171 --- /dev/null +++ b/SiteServer.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 53345abb9310d344baa67c19a8d8249f \ No newline at end of file From a48ae12fc2f6d4a99119c128e78bf4b103e607c3 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Fri, 6 Dec 2024 16:30:24 +0100 Subject: [PATCH 3/3] ControlCore mostly works (but I don't see a model on the site server yet) --- LowLevelMessages.cs | 41 +++++++++++++++++++++++++++++++++++++++-- Messages.cs | 45 +++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 80 insertions(+), 6 deletions(-) diff --git a/LowLevelMessages.cs b/LowLevelMessages.cs index a976ddf..f279dbc 100644 --- a/LowLevelMessages.cs +++ b/LowLevelMessages.cs @@ -1,6 +1,13 @@ using Passer; public class LowLevelMessages { + + public static void SendSpherical(byte[] buffer, ref uint ix, Spherical v) { + SendFloat16(buffer, ref ix, new float16(v.distance)); + SendAngle8(buffer, ref ix, v.horizontal); + SendAngle8(buffer, ref ix, v.vertical); + } + public static Spherical ReceiveSpherical(byte[] data, ref uint ix) { float horizontal = ReceiveAngle8(data, ref ix); float vertical = ReceiveAngle8(data, ref ix); @@ -9,6 +16,23 @@ public class LowLevelMessages { return v; } + public static void SendQuat32(byte[] buffer, ref uint ix, Quat32 q) { + int qx = (int)(q.x * 127 + 128); + int qy = (int)(q.y * 127 + 128); + int qz = (int)(q.z * 127 + 128); + int qw = (int)(q.w * 255); + if (q.w < 0) { + qx = -qx; + qy = -qy; + qz = -qz; + qw = -qw; + } + + buffer[ix++] = (byte)qx; + buffer[ix++] = (byte)qy; + buffer[ix++] = (byte)qz; + buffer[ix++] = (byte)qw; + } public static Quat32 ReceiveQuat32(byte[] data, ref uint ix) { Quat32 q = new( (data[ix++] - 128.0F) / 127.0F, @@ -18,11 +42,26 @@ public class LowLevelMessages { return q; } + public static void SendAngle8(byte[] buffer, ref uint ix, float angle) { + // Normalize angle + while (angle >= 180) + angle -= 360; + while (angle < -180) + angle += 360; + buffer[ix++] = (byte)((angle / 360.0f) * 256.0f); + } + public static float ReceiveAngle8(byte[] data, ref uint ix) { float value = (data[ix++] * 180) / 128.0F; return value; } + public static void SendFloat16(byte[] data, ref uint 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) { ushort value = (ushort)(data[ix++] << 8 | data[ix++]); float16 f16 = new(); @@ -30,6 +69,4 @@ public class LowLevelMessages { float f = f16.toFloat(); return f; } - - } diff --git a/Messages.cs b/Messages.cs index acbecad..b9c7e2b 100644 --- a/Messages.cs +++ b/Messages.cs @@ -62,6 +62,18 @@ namespace Passer.Control { client.udpClient.Send(data, data.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) { + if (client == null) + return false; + + client.udpClient.Send(data, data.Length, "127.0.0.1", client.port); + return true; + } + public static async Task Receive(Stream dataStream, byte packetSize) { byte[] buffer = new byte[packetSize - 1]; // without msgId int byteCount = dataStream.Read(buffer, 0, packetSize - 1); @@ -86,7 +98,13 @@ namespace Passer.Control { } public ClientMsg(byte[] data) : base(data) { } - public override void Deserialize(byte[] data) { + public override byte[] Serialize() { + byte[] buffer = new byte[ClientMsg.length]; + buffer[0] = ClientMsg.Id; + buffer[1] = networkId; + return buffer; + } + public override void Deserialize(byte[] data) { base.Deserialize(data); uint ix = 0; networkId = data[ix]; @@ -96,6 +114,10 @@ namespace Passer.Control { ClientMsg msg = new(networkId); return SendMsg(client, msg); } + public static bool Publish(Client client, byte networkId) { + ClientMsg msg = new(networkId); + return PublishMsg(client, msg); + } public static async Task Receive(Stream dataStream, Client client, byte packetSize) { if (packetSize != length) return false; @@ -212,7 +234,7 @@ 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 objectId; public byte objectType; @@ -227,7 +249,7 @@ namespace Passer.Control { } public static bool Send(Client client, byte thingId, byte thingType, byte parentId) { - byte[] data = new byte[4]; + byte[] data = new byte[ThingMsg.length]; data[0] = ThingMsg.Id; data[1] = client.networkId; data[2] = thingId; @@ -370,12 +392,27 @@ namespace Passer.Control { this.poseType = 0; if (this.position != null) this.poseType |= Pose_Position; + else + this.position = new Spherical(0, 0, 0); if (this.orientation != null) this.poseType |= Pose_Orientation; + else + this.orientation = new Quat32(0, 0, 0, 1); } public PoseMsg(byte[] data) : base(data) { } - public override void Deserialize(byte[] data) { + public override byte[] Serialize() { + byte[] buffer = new byte[PoseMsg.length]; + uint ix = 0; + buffer[ix++] = PoseMsg.Id; + buffer[ix++] = this.thingId; + buffer[ix++] = this.poseType; + + LowLevelMessages.SendSpherical(buffer, ref ix, position); + LowLevelMessages.SendQuat32(buffer, ref ix, orientation); + return buffer; + } + public override void Deserialize(byte[] data) { uint ix = 0; thingId = data[ix++]; poseType = data[ix++];