Merge commit '17fa48a26673454f22f9c48f76efb5a36c9ffc5a' into V2

This commit is contained in:
Pascal Serrarens 2024-12-14 11:51:19 +01:00
commit e975f219f4
7 changed files with 310 additions and 145 deletions

View File

@ -3,21 +3,28 @@ using System.Collections.Concurrent;
using System.Net.Sockets; using System.Net.Sockets;
using System.IO; using System.IO;
namespace Passer.Control { namespace Passer.Control
{
public class Client { public class Client
{
//public ConnectionMethod connection; //public ConnectionMethod connection;
public UdpClient udpClient; public UdpClient udpClient;
public string ipAddress; public string ipAddress;
public string broadcastIpAddress = "255.255.255.255";
public int port; public int port;
public Stream dataStream; public Stream dataStream;
public byte[] buffer = new byte[256];
public byte networkId = 0; public byte networkId = 0;
public readonly ConcurrentQueue<IMessage> messageQueue = new(); public readonly ConcurrentQueue<IMessage> messageQueue = new();
public static Client GetClient(string ipAddress, int port) { public static Client GetClient(string ipAddress, int port)
foreach (Client c in clients) { {
foreach (Client c in clients)
{
if (c.ipAddress == ipAddress && c.port == port) if (c.ipAddress == ipAddress && c.port == port)
return c; return c;
} }
@ -25,7 +32,8 @@ namespace Passer.Control {
} }
static public List<Client> clients = new List<Client>(); static public List<Client> clients = new List<Client>();
public Client(UdpClient udpClient, int port) { public Client(UdpClient udpClient, int port)
{
this.udpClient = udpClient; this.udpClient = udpClient;
this.ipAddress = null; this.ipAddress = null;
this.port = port; this.port = port;
@ -33,13 +41,35 @@ namespace Passer.Control {
clients.Add(this); clients.Add(this);
} }
public virtual void ProcessMessages() { public bool SendBuffer()
while (this.messageQueue.TryDequeue(out IMessage msg)) {
ProcessMessage(msg); if (this.ipAddress == null)
return false;
//UnityEngine.Debug.Log($"Send msg {buffer[0]} to {ipAddress}");
this.udpClient.Send(this.buffer, this.buffer.Length, this.ipAddress, this.port);
return true;
} }
public void ProcessMessage(IMessage msg) { public bool PublishBuffer()
switch (msg) { {
if (this.broadcastIpAddress == null)
return false;
this.udpClient.Send(this.buffer, this.buffer.Length, this.broadcastIpAddress, this.port);
return true;
}
public virtual void ProcessMessages()
{
while (this.messageQueue.TryDequeue(out IMessage msg))
ProcessMessage(msg);
}
public void ProcessMessage(IMessage msg)
{
switch (msg)
{
case ClientMsg clientMsg: case ClientMsg clientMsg:
ProcessClient(clientMsg); ProcessClient(clientMsg);
break; break;
@ -97,8 +127,10 @@ namespace Passer.Control {
protected virtual void ProcessDestroy(DestroyMsg destroy) { } protected virtual void ProcessDestroy(DestroyMsg destroy) { }
private void ForwardMessage(IMessage msg) { private void ForwardMessage(IMessage msg)
foreach (Client client in Client.clients) { {
foreach (Client client in Client.clients)
{
if (client == this) if (client == this)
continue; continue;
//UnityEngine.Debug.Log($"---> {client.ipAddress}"); //UnityEngine.Debug.Log($"---> {client.ipAddress}");

View File

@ -1,14 +1,17 @@
using Passer; using Passer.LinearAlgebra;
public class LowLevelMessages { public class LowLevelMessages
{
public static void SendSpherical(byte[] buffer, ref byte 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.horizontal);
SendAngle8(buffer, ref ix, v.vertical); SendAngle8(buffer, ref ix, v.vertical);
SendFloat16(buffer, ref ix, new float16(v.distance)); SendFloat16(buffer, ref ix, new float16(v.distance));
} }
public static Spherical ReceiveSpherical(byte[] data, ref byte ix) { public static Spherical ReceiveSpherical(byte[] data, ref byte ix)
{
float horizontal = ReceiveAngle8(data, ref ix); float horizontal = ReceiveAngle8(data, ref ix);
float vertical = ReceiveAngle8(data, ref ix); float vertical = ReceiveAngle8(data, ref ix);
float distance = ReceiveFloat16(data, ref ix); float distance = ReceiveFloat16(data, ref ix);
@ -16,12 +19,14 @@ public class LowLevelMessages {
return v; return v;
} }
public static void SendQuat32(byte[] buffer, ref byte ix, Quat32 q) { public static void SendQuat32(byte[] buffer, ref byte ix, Quat32 q)
{
int qx = (int)(q.x * 127 + 128); int qx = (int)(q.x * 127 + 128);
int qy = (int)(q.y * 127 + 128); int qy = (int)(q.y * 127 + 128);
int qz = (int)(q.z * 127 + 128); int qz = (int)(q.z * 127 + 128);
int qw = (int)(q.w * 255); int qw = (int)(q.w * 255);
if (q.w < 0) { if (q.w < 0)
{
qx = -qx; qx = -qx;
qy = -qy; qy = -qy;
qz = -qz; qz = -qz;
@ -33,7 +38,8 @@ public class LowLevelMessages {
buffer[ix++] = (byte)qz; buffer[ix++] = (byte)qz;
buffer[ix++] = (byte)qw; buffer[ix++] = (byte)qw;
} }
public static Quat32 ReceiveQuat32(byte[] data, ref byte ix) { public static Quat32 ReceiveQuat32(byte[] data, ref byte ix)
{
Quat32 q = new( Quat32 q = new(
(data[ix++] - 128.0F) / 127.0F, (data[ix++] - 128.0F) / 127.0F,
(data[ix++] - 128.0F) / 127.0F, (data[ix++] - 128.0F) / 127.0F,
@ -42,7 +48,8 @@ public class LowLevelMessages {
return q; return q;
} }
public static void SendAngle8(byte[] buffer, ref byte ix, float angle) { public static void SendAngle8(byte[] buffer, ref byte ix, float angle)
{
// Normalize angle // Normalize angle
while (angle >= 180) while (angle >= 180)
angle -= 360; angle -= 360;
@ -51,18 +58,21 @@ public class LowLevelMessages {
buffer[ix++] = (byte)((angle / 360.0f) * 256.0f); buffer[ix++] = (byte)((angle / 360.0f) * 256.0f);
} }
public static float ReceiveAngle8(byte[] data, ref byte ix) { public static float ReceiveAngle8(byte[] data, ref byte ix)
{
float value = (data[ix++] * 180) / 128.0F; float value = (data[ix++] * 180) / 128.0F;
return value; return value;
} }
public static void SendFloat16(byte[] data, ref byte ix, float16 f) { public static void SendFloat16(byte[] data, ref byte ix, float16 f)
{
ushort binary = f.GetBinary(); ushort binary = f.GetBinary();
data[ix++] = (byte)(binary >> 8); data[ix++] = (byte)(binary >> 8);
data[ix++] = (byte)(binary & 255); data[ix++] = (byte)(binary & 255);
} }
public static float ReceiveFloat16(byte[] data, ref byte ix) { public static float ReceiveFloat16(byte[] data, ref byte ix)
{
ushort value = (ushort)(data[ix++] << 8 | data[ix++]); ushort value = (ushort)(data[ix++] << 8 | data[ix++]);
float16 f16 = new(); float16 f16 = new();
f16.SetBinary(value); f16.SetBinary(value);

View File

@ -1,44 +1,39 @@
using System.IO; using System.IO;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Passer.Control { using Passer.LinearAlgebra;
namespace Passer.Control
{
public class IMessage { public class IMessage
{
public IMessage() { } public IMessage() { }
public IMessage(byte[] buffer) { public IMessage(byte[] buffer)
{
Deserialize(buffer); Deserialize(buffer);
} }
public virtual byte[] Serialize() { return null; } public virtual byte Serialize(ref byte[] buffer) { return 0; }
public virtual void Deserialize(byte[] buffer) { } public virtual void Deserialize(byte[] buffer) { }
public static bool SendMsg(Client client, IMessage msg) { public static bool SendMsg(Client client, IMessage msg)
return SendMsg(client, msg.Serialize()); {
} msg.Serialize(ref client.buffer);
public static bool SendMsg(Client client, byte[] buffer) { return client.SendBuffer();
if (client == null || client.ipAddress == null)
return false;
//UnityEngine.Debug.Log($"Send msg {buffer[0]} to {client.ipAddress}");
client.udpClient.Send(buffer, buffer.Length, client.ipAddress, client.port);
return true;
} }
public static bool PublishMsg(Client client, IMessage msg) { public static bool PublishMsg(Client client, IMessage msg)
return PublishMsg(client, msg.Serialize()); {
} msg.Serialize(ref client.buffer);
public static bool PublishMsg(Client client, byte[] buffer) { return client.PublishBuffer();
if (client == null)
return false;
client.udpClient.Send(buffer, buffer.Length, "127.0.0.1", client.port);
return true;
} }
public static async Task<byte[]> Receive(Stream dataStream, byte packetSize) { public static async Task<byte[]> Receive(Stream dataStream, byte packetSize)
{
byte[] buffer = new byte[packetSize - 1]; // without msgId byte[] buffer = new byte[packetSize - 1]; // without msgId
int byteCount = dataStream.Read(buffer, 0, packetSize - 1); int byteCount = dataStream.Read(buffer, 0, packetSize - 1);
while (byteCount < packetSize - 1) { while (byteCount < packetSize - 1)
{
// not all bytes have been read, wait and try again // not all bytes have been read, wait and try again
await Task.Delay(1); await Task.Delay(1);
byteCount += dataStream.Read(buffer, byteCount, packetSize - 1 - byteCount); byteCount += dataStream.Read(buffer, byteCount, packetSize - 1 - byteCount);
@ -49,49 +44,58 @@ namespace Passer.Control {
#region Client #region Client
public class ClientMsg : IMessage { public class ClientMsg : IMessage
{
public const byte Id = 0xA0; public const byte Id = 0xA0;
public const byte length = 2; public const byte length = 2;
public byte networkId; public byte networkId;
public ClientMsg(byte networkId) { public ClientMsg(byte networkId)
{
this.networkId = networkId; this.networkId = networkId;
} }
public ClientMsg(byte[] buffer) : base(buffer) { } public ClientMsg(byte[] buffer) : base(buffer) { }
public override byte[] Serialize() { public override byte Serialize(ref byte[] buffer)
byte[] buffer = new byte[ClientMsg.length]; {
buffer[0] = ClientMsg.Id; byte ix = 0;
buffer[1] = networkId; buffer[ix++] = ClientMsg.Id;
return buffer; buffer[ix++] = networkId;
return ix;
} }
public override void Deserialize(byte[] buffer) { public override void Deserialize(byte[] buffer)
{
base.Deserialize(buffer); base.Deserialize(buffer);
uint ix = 0; uint ix = 0;
networkId = buffer[ix]; networkId = buffer[ix];
} }
public static bool Send(Client client, byte networkId) { public static bool Send(Client client, byte networkId)
{
ClientMsg msg = new(networkId); ClientMsg msg = new(networkId);
return SendMsg(client, msg); return SendMsg(client, msg);
} }
public static bool Publish(Client client, byte networkId) { public static bool Publish(Client client, byte networkId)
{
ClientMsg msg = new(networkId); ClientMsg msg = new(networkId);
return PublishMsg(client, msg); return PublishMsg(client, msg);
} }
public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize) { public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize)
{
if (packetSize != length) if (packetSize != length)
return false; return false;
byte[] buffer = await Receive(dataStream, packetSize); byte[] buffer = await Receive(dataStream, packetSize);
ClientMsg msg = new(buffer); ClientMsg msg = new(buffer);
if (client.networkId == 0) { if (client.networkId == 0)
{
client.networkId = (byte)(Client.clients.Count); client.networkId = (byte)(Client.clients.Count);
NetworkIdMsg.Send(client, client.networkId); NetworkIdMsg.Send(client, client.networkId);
client.messageQueue.Enqueue(msg); client.messageQueue.Enqueue(msg);
} }
else if (msg.networkId == 0) { else if (msg.networkId == 0)
{
NetworkIdMsg.Send(client, client.networkId); NetworkIdMsg.Send(client, client.networkId);
client.messageQueue.Enqueue(msg); client.messageQueue.Enqueue(msg);
} }
@ -104,32 +108,38 @@ namespace Passer.Control {
#region Network Id #region Network Id
public class NetworkIdMsg : IMessage { public class NetworkIdMsg : IMessage
{
public const byte Id = 0xA1; public const byte Id = 0xA1;
public const byte length = 2; public const byte length = 2;
public byte networkId; public byte networkId;
NetworkIdMsg(byte networkId) { NetworkIdMsg(byte networkId)
{
this.networkId = networkId; this.networkId = networkId;
} }
NetworkIdMsg(byte[] buffer) : base(buffer) { } NetworkIdMsg(byte[] buffer) : base(buffer) { }
public override byte[] Serialize() { public override byte Serialize(ref byte[] buffer)
byte[] buffer = new byte[NetworkIdMsg.length]; {
buffer[0] = NetworkIdMsg.Id; byte ix = 0;
buffer[1] = this.networkId; buffer[ix++] = NetworkIdMsg.Id;
return buffer; buffer[ix++] = this.networkId;
return ix;
} }
public override void Deserialize(byte[] buffer) { public override void Deserialize(byte[] buffer)
{
uint ix = 0; uint ix = 0;
this.networkId = buffer[ix]; this.networkId = buffer[ix];
} }
public static bool Send(Client client, byte networkId) { public static bool Send(Client client, byte networkId)
{
NetworkIdMsg msg = new(networkId); NetworkIdMsg msg = new(networkId);
return SendMsg(client, msg); return SendMsg(client, msg);
} }
public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize) { public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize)
{
if (packetSize != length) if (packetSize != length)
return false; return false;
@ -144,37 +154,41 @@ namespace Passer.Control {
#region Investigate #region Investigate
public class InvestigateMsg : IMessage { public class InvestigateMsg : IMessage
{
public const byte Id = 0x81; public const byte Id = 0x81;
public const byte length = 3; public const byte length = 3;
public byte networkId; public byte networkId;
public byte thingId; public byte thingId;
public InvestigateMsg(byte networkId, byte thingId) { public InvestigateMsg(byte networkId, byte thingId)
{
this.networkId = networkId; this.networkId = networkId;
this.thingId = thingId; this.thingId = thingId;
} }
public InvestigateMsg(byte[] buffer) : base(buffer) { } public InvestigateMsg(byte[] buffer) : base(buffer) { }
public override byte[] Serialize() { public override byte Serialize(ref byte[] buffer)
byte[] buffer = new byte[InvestigateMsg.length]; {
buffer[0] = InvestigateMsg.Id; byte ix = 0;
buffer[1] = this.networkId; buffer[ix++] = InvestigateMsg.Id;
buffer[2] = this.thingId; buffer[ix++] = this.networkId;
return buffer; buffer[ix++] = this.thingId;
return ix;
} }
public override void Deserialize(byte[] buffer) { public override void Deserialize(byte[] buffer)
{
uint ix = 0; uint ix = 0;
this.networkId = buffer[ix++]; this.networkId = buffer[ix++];
this.thingId = buffer[ix++]; this.thingId = buffer[ix++];
} }
public static bool Send(Client client, byte networkId, byte thingId) { public static bool Send(Client client, CoreThing thing) {
InvestigateMsg msg = new(networkId, thingId); InvestigateMsg msg = new(thing.networkId, thing.id);
//UnityEngine.Debug.Log($"Send investigate [{msg.networkId}/{msg.thingId}]");
return SendMsg(client, msg); return SendMsg(client, msg);
} }
public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize) { public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize)
{
if (packetSize != length) if (packetSize != length)
return false; return false;
@ -192,7 +206,8 @@ namespace Passer.Control {
#region Thing #region Thing
public class ThingMsg : IMessage { public class ThingMsg : IMessage
{
public const byte length = 5; public const byte length = 5;
public const byte Id = 0x80; public const byte Id = 0x80;
public byte networkId; public byte networkId;
@ -200,7 +215,8 @@ namespace Passer.Control {
public byte thingType; public byte thingType;
public byte parentId; public byte parentId;
public ThingMsg(byte networkId, byte thingId, byte thingType, byte parentId) { public ThingMsg(byte networkId, byte thingId, byte thingType, byte parentId)
{
this.networkId = networkId; this.networkId = networkId;
this.thingId = thingId; this.thingId = thingId;
this.thingType = thingType; this.thingType = thingType;
@ -208,17 +224,18 @@ namespace Passer.Control {
} }
public ThingMsg(byte[] buffer) : base(buffer) { } public ThingMsg(byte[] buffer) : base(buffer) { }
public override byte[] Serialize() { public override byte Serialize(ref byte[] buffer)
byte[] buffer = new byte[ThingMsg.length]; {
byte ix = 0; byte ix = 0;
buffer[ix++] = ThingMsg.Id; buffer[ix++] = ThingMsg.Id;
buffer[ix++] = this.networkId; buffer[ix++] = this.networkId;
buffer[ix++] = this.thingId; buffer[ix++] = this.thingId;
buffer[ix++] = this.thingType; buffer[ix++] = this.thingType;
buffer[ix] = this.parentId; buffer[ix++] = this.parentId;
return buffer; return ix;
} }
public override void Deserialize(byte[] buffer) { public override void Deserialize(byte[] buffer)
{
uint ix = 0; uint ix = 0;
this.networkId = buffer[ix++]; this.networkId = buffer[ix++];
this.thingId = buffer[ix++]; this.thingId = buffer[ix++];
@ -226,12 +243,18 @@ namespace Passer.Control {
this.parentId = buffer[ix]; this.parentId = buffer[ix];
} }
public static bool Send(Client client, byte networkId, byte thingId, byte thingType, byte parentId) { public static bool Send(Client client, CoreThing thing) {
ThingMsg msg = new(networkId, thingId, thingType, parentId); ThingMsg msg = new(thing.networkId, thing.id, thing.type, thing.parent.id);
//UnityEngine.Debug.Log($"Send thing [{msg.networkId}/{msg.thingId}]");
return SendMsg(client, msg); return SendMsg(client, msg);
} }
public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize) { //public static bool Send(Client client, byte networkId, byte thingId, byte thingType, byte parentId)
//{
// ThingMsg msg = new(networkId, thingId, thingType, parentId);
// //UnityEngine.Debug.Log($"Send thing [{msg.networkId}/{msg.thingId}]");
// return SendMsg(client, msg);
//}
public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize)
{
if (packetSize != length) if (packetSize != length)
return false; return false;
@ -252,7 +275,8 @@ namespace Passer.Control {
#region Name #region Name
public class NameMsg : IMessage { public class NameMsg : IMessage
{
public const byte Id = 0x91; // 145 public const byte Id = 0x91; // 145
public const byte length = 4; public const byte length = 4;
public byte networkId; public byte networkId;
@ -260,25 +284,27 @@ namespace Passer.Control {
public byte len; public byte len;
public string name; public string name;
public NameMsg(byte networkId, byte thingId, string name) { public NameMsg(byte networkId, byte thingId, string name)
{
this.networkId = networkId; this.networkId = networkId;
this.thingId = thingId; this.thingId = thingId;
this.name = name; this.name = name;
} }
public NameMsg(byte[] buffer) : base(buffer) { } public NameMsg(byte[] buffer) : base(buffer) { }
public override byte[] Serialize() { public override byte Serialize(ref byte[] buffer)
byte[] buffer = new byte[length + this.name.Length]; {
byte ix = 0; byte ix = 0;
buffer[ix++] = NameMsg.Id; buffer[ix++] = NameMsg.Id;
buffer[ix++] = this.networkId; buffer[ix++] = this.networkId;
buffer[ix++] = this.thingId; buffer[ix++] = this.thingId;
buffer[ix++] = (byte)this.name.Length; buffer[ix++] = (byte)this.name.Length;
for (int nameIx = 0; nameIx < this.name.Length; nameIx++, ix++) for (int nameIx = 0; nameIx < this.name.Length; nameIx++)
buffer[ix] = (byte)this.name[nameIx]; buffer[ix++] = (byte)this.name[nameIx];
return buffer; return ix;
} }
public override void Deserialize(byte[] buffer) { public override void Deserialize(byte[] buffer)
{
byte ix = 0; byte ix = 0;
this.networkId = buffer[ix++]; this.networkId = buffer[ix++];
this.thingId = buffer[ix++]; this.thingId = buffer[ix++];
@ -286,11 +312,20 @@ namespace Passer.Control {
this.name = System.Text.Encoding.UTF8.GetString(buffer, (int)ix, strlen); this.name = System.Text.Encoding.UTF8.GetString(buffer, (int)ix, strlen);
} }
public static bool Send(Client client, byte networkId, byte thingId, string name) { public static bool Send(Client client, CoreThing thing) {
NameMsg msg = new(networkId, thingId, name); if (string.IsNullOrEmpty(thing.name))
return true; // nothing sent, but still a success!
NameMsg msg = new(thing.networkId, thing.id, thing.name);
return SendMsg(client, msg); return SendMsg(client, msg);
} }
public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize) { //public static bool Send(Client client, byte networkId, byte thingId, string name)
//{
// NameMsg msg = new(networkId, thingId, name);
// return SendMsg(client, msg);
//}
public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize)
{
byte[] buffer = await Receive(dataStream, packetSize); byte[] buffer = await Receive(dataStream, packetSize);
NameMsg msg = new(buffer); NameMsg msg = new(buffer);
@ -303,7 +338,8 @@ namespace Passer.Control {
#region Model URL #region Model URL
public class ModelUrlMsg : IMessage { public class ModelUrlMsg : IMessage
{
public const byte Id = 0x90; // (144) Model URL public const byte Id = 0x90; // (144) Model URL
public byte networkId; public byte networkId;
public byte thingId; public byte thingId;
@ -311,7 +347,8 @@ namespace Passer.Control {
public float scale; public float scale;
public string url; public string url;
public ModelUrlMsg(byte networkId, byte thingId, string url, float scale = 1) { public ModelUrlMsg(byte networkId, byte thingId, string url, float scale = 1)
{
this.networkId = networkId; this.networkId = networkId;
this.thingId = thingId; this.thingId = thingId;
this.url = url; this.url = url;
@ -320,8 +357,8 @@ namespace Passer.Control {
} }
public ModelUrlMsg(byte[] buffer) : base(buffer) { } public ModelUrlMsg(byte[] buffer) : base(buffer) { }
public override byte[] Serialize() { public override byte Serialize(ref byte[] buffer)
byte[] buffer = new byte[this.url.Length + 6]; {
byte ix = 0; byte ix = 0;
buffer[ix++] = ModelUrlMsg.Id; buffer[ix++] = ModelUrlMsg.Id;
buffer[ix++] = this.networkId; buffer[ix++] = this.networkId;
@ -329,11 +366,12 @@ namespace Passer.Control {
LowLevelMessages.SendFloat16(buffer, ref ix, new float16(1.0f)); LowLevelMessages.SendFloat16(buffer, ref ix, new float16(1.0f));
buffer[ix++] = (byte)url.Length; buffer[ix++] = (byte)url.Length;
for (int urlIx = 0; urlIx < this.url.Length; urlIx++, ix++) for (int urlIx = 0; urlIx < this.url.Length; urlIx++)
buffer[ix] = (byte)url[urlIx]; buffer[ix++] = (byte)url[urlIx];
return buffer; return ix;
} }
public override void Deserialize(byte[] buffer) { public override void Deserialize(byte[] buffer)
{
byte ix = 0; byte ix = 0;
this.networkId = buffer[ix++]; this.networkId = buffer[ix++];
this.thingId = buffer[ix++]; this.thingId = buffer[ix++];
@ -342,11 +380,23 @@ namespace Passer.Control {
url = System.Text.Encoding.UTF8.GetString(buffer, (int)ix, strlen); url = System.Text.Encoding.UTF8.GetString(buffer, (int)ix, strlen);
} }
public static bool Send(Client client, byte networkId, byte thingId, string modelUrl) { public static bool Send(Client client, CoreThing thing) {
if (string.IsNullOrEmpty(thing.modelUrl))
return true; // nothing sent, but still a success!
ModelUrlMsg msg = new(thing.networkId, thing.id, thing.modelUrl);
return SendMsg(client, msg);
}
public static bool Send(Client client, byte networkId, byte thingId, string modelUrl)
{
if (string.IsNullOrEmpty(modelUrl))
return true; // nothing sent, but still a success!
ModelUrlMsg msg = new(networkId, thingId, modelUrl); ModelUrlMsg msg = new(networkId, thingId, modelUrl);
return SendMsg(client, msg); return SendMsg(client, msg);
} }
public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize) { public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize)
{
byte[] buffer = await Receive(dataStream, packetSize); byte[] buffer = await Receive(dataStream, packetSize);
ModelUrlMsg msg = new(buffer); ModelUrlMsg msg = new(buffer);
client.messageQueue.Enqueue(msg); client.messageQueue.Enqueue(msg);
@ -358,7 +408,8 @@ namespace Passer.Control {
#region Pose #region Pose
public class PoseMsg : IMessage { public class PoseMsg : IMessage
{
public const byte Id = 0x10; public const byte Id = 0x10;
public const byte length = 4 + 4 + 4; public const byte length = 4 + 4 + 4;
public byte networkId; public byte networkId;
@ -371,7 +422,8 @@ namespace Passer.Control {
public Spherical position; public Spherical position;
public Quat32 orientation; public Quat32 orientation;
public PoseMsg(byte networkId, byte thingId, Spherical position, Quat32 orientation) { public PoseMsg(byte networkId, byte thingId, Spherical position, Quat32 orientation)
{
this.networkId = networkId; this.networkId = networkId;
this.thingId = thingId; this.thingId = thingId;
this.position = position; this.position = position;
@ -388,8 +440,8 @@ namespace Passer.Control {
} }
public PoseMsg(byte[] buffer) : base(buffer) { } public PoseMsg(byte[] buffer) : base(buffer) { }
public override byte[] Serialize() { public override byte Serialize(ref byte[] buffer)
byte[] buffer = new byte[PoseMsg.length]; {
byte ix = 0; byte ix = 0;
buffer[ix++] = PoseMsg.Id; buffer[ix++] = PoseMsg.Id;
buffer[ix++] = this.networkId; buffer[ix++] = this.networkId;
@ -398,9 +450,10 @@ namespace Passer.Control {
LowLevelMessages.SendSpherical(buffer, ref ix, this.position); LowLevelMessages.SendSpherical(buffer, ref ix, this.position);
LowLevelMessages.SendQuat32(buffer, ref ix, this.orientation); LowLevelMessages.SendQuat32(buffer, ref ix, this.orientation);
return buffer; return ix;
} }
public override void Deserialize(byte[] buffer) { public override void Deserialize(byte[] buffer)
{
byte ix = 0; byte ix = 0;
this.networkId = buffer[ix++]; this.networkId = buffer[ix++];
this.thingId = buffer[ix++]; this.thingId = buffer[ix++];
@ -412,11 +465,13 @@ namespace Passer.Control {
this.orientation = LowLevelMessages.ReceiveQuat32(buffer, ref ix); this.orientation = LowLevelMessages.ReceiveQuat32(buffer, ref ix);
} }
public static bool Send(Client client, byte thingId, Spherical position, Quat32 orientation) { public static bool Send(Client client, byte thingId, Spherical position, Quat32 orientation)
{
PoseMsg msg = new(client.networkId, thingId, position, orientation); PoseMsg msg = new(client.networkId, thingId, position, orientation);
return SendMsg(client, msg); return SendMsg(client, msg);
} }
public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize) { public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize)
{
if (packetSize != length) if (packetSize != length)
return false; return false;
@ -436,21 +491,23 @@ namespace Passer.Control {
#region Custom #region Custom
public class CustomMsg : IMessage { public class CustomMsg : IMessage
{
public const byte Id = 0xB1; public const byte Id = 0xB1;
public byte networkId; public byte networkId;
public byte thingId; public byte thingId;
public byte[] bytes; public byte[] bytes;
public CustomMsg(byte[] buffer) : base(buffer) { } public CustomMsg(byte[] buffer) : base(buffer) { }
public CustomMsg(byte networkId, byte thingId, byte[] bytes) : base() { public CustomMsg(byte networkId, byte thingId, byte[] bytes) : base()
{
this.networkId = networkId; this.networkId = networkId;
this.thingId = thingId; this.thingId = thingId;
this.bytes = bytes; this.bytes = bytes;
} }
public override byte[] Serialize() { public override byte Serialize(ref byte[] buffer)
byte[] buffer = new byte[3 + this.bytes.Length]; {
byte ix = 0; byte ix = 0;
buffer[ix++] = CustomMsg.Id; buffer[ix++] = CustomMsg.Id;
buffer[ix++] = this.networkId; buffer[ix++] = this.networkId;
@ -459,9 +516,10 @@ namespace Passer.Control {
foreach (byte b in bytes) foreach (byte b in bytes)
buffer[ix++] = b; buffer[ix++] = b;
return buffer; return ix;
} }
public override void Deserialize(byte[] buffer) { public override void Deserialize(byte[] buffer)
{
byte ix = 0; byte ix = 0;
this.networkId = buffer[ix++]; this.networkId = buffer[ix++];
this.thingId = buffer[ix++]; this.thingId = buffer[ix++];
@ -471,11 +529,13 @@ namespace Passer.Control {
this.bytes[bytesIx] = buffer[ix++]; this.bytes[bytesIx] = buffer[ix++];
} }
public static void Send(Client client, byte thingId, byte[] bytes) { public static void Send(Client client, byte thingId, byte[] bytes)
{
CustomMsg msg = new(client.networkId, thingId, bytes); CustomMsg msg = new(client.networkId, thingId, bytes);
SendMsg(client, msg); SendMsg(client, msg);
} }
public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize) { public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize)
{
byte[] buffer = await Receive(dataStream, packetSize); byte[] buffer = await Receive(dataStream, packetSize);
CustomMsg msg = new(buffer); CustomMsg msg = new(buffer);
client.messageQueue.Enqueue(msg); client.messageQueue.Enqueue(msg);
@ -487,18 +547,21 @@ namespace Passer.Control {
#region Text #region Text
public class TextMsg : IMessage { public class TextMsg : IMessage
{
public const byte Id = 0xB0; public const byte Id = 0xB0;
public string text; public string text;
public TextMsg(byte[] buffer) : base(buffer) { } public TextMsg(byte[] buffer) : base(buffer) { }
public override void Deserialize(byte[] buffer) { public override void Deserialize(byte[] buffer)
{
uint ix = 0; uint ix = 0;
uint strlen = buffer[ix++]; uint strlen = buffer[ix++];
this.text = System.Text.Encoding.UTF8.GetString(buffer, (int)ix, (int)strlen); this.text = System.Text.Encoding.UTF8.GetString(buffer, (int)ix, (int)strlen);
} }
public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize) { public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize)
{
byte[] buffer = await Receive(dataStream, packetSize); byte[] buffer = await Receive(dataStream, packetSize);
TextMsg msg = new(buffer); TextMsg msg = new(buffer);
@ -511,7 +574,8 @@ namespace Passer.Control {
#region Destroy #region Destroy
public class DestroyMsg : IMessage { public class DestroyMsg : IMessage
{
public const byte Id = 0x20; public const byte Id = 0x20;
public const byte length = 2; public const byte length = 2;
public byte networkId; public byte networkId;
@ -519,12 +583,14 @@ namespace Passer.Control {
public DestroyMsg(byte[] buffer) : base(buffer) { } public DestroyMsg(byte[] buffer) : base(buffer) { }
public override void Deserialize(byte[] buffer) { public override void Deserialize(byte[] buffer)
{
this.networkId = buffer[0]; this.networkId = buffer[0];
this.thingId = buffer[1]; this.thingId = buffer[1];
} }
public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize) { public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize)
{
if (packetSize != length) if (packetSize != length)
return false; return false;

View File

@ -1,15 +1,18 @@
namespace Passer { namespace Passer.LinearAlgebra
public class Quat32 { {
public class Quat32
{
public float x; public float x;
public float y; public float y;
public float z; public float z;
public float w; public float w;
public Quat32(float x, float y, float z, float w) { public Quat32(float x, float y, float z, float w)
{
this.x = x; this.x = x;
this.y = y; this.y = y;
this.z = z; this.z = z;
this.w = w; this.w = w;
} }
} }
} }

View File

@ -1,12 +1,15 @@
namespace Passer { namespace Passer.LinearAlgebra
public class Spherical { {
public class Spherical
{
public float distance; public float distance;
public float horizontal; public float horizontal;
public float vertical; public float vertical;
public static Spherical zero = new(0, 0, 0); public static Spherical zero = new(0, 0, 0);
public Spherical(float distance, float horizontal, float vertical) { public Spherical(float distance, float horizontal, float vertical)
{
this.distance = distance; this.distance = distance;
this.horizontal = horizontal; this.horizontal = horizontal;
this.vertical = vertical; this.vertical = vertical;

40
Thing.cs Normal file
View File

@ -0,0 +1,40 @@
using System.Collections.Generic;
namespace Passer.Control
{
public class CoreThing
{
public Client client;
public byte networkId;
public byte id;
public CoreThing parent;
public byte type;
public string name;
public string modelUrl;
//protected Sensor sensor;
protected virtual void Init()
{
}
public CoreThing(Client client, byte networkId, byte thingId, byte thingType = 0)
{
this.client = client;
this.id = thingId;
this.type = thingType;
this.networkId = networkId;
this.Init();
allThings.Add(this);
}
private static readonly List<CoreThing> allThings = new();
public static CoreThing Get(byte networkId, byte thingId)
{
CoreThing thing = allThings.Find(aThing => aThing.networkId == networkId && aThing.id == thingId);
return thing;
}
}
}

11
Thing.cs.meta Normal file
View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 0e8b48bc91446304eaaccbfdde4cc4af
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: