Replaced use of Thing type with the values

This commit is contained in:
Pascal Serrarens 2024-12-05 14:11:58 +01:00
parent 6f12854d4f
commit 9919aa6578

View File

@ -6,363 +6,360 @@ using System.Threading.Tasks;
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 int port; public int port;
public byte networkId; public byte networkId;
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;
} }
return null; return null;
} }
static public List<Client> clients = new List<Client>(); static public List<Client> clients = new List<Client>();
public static Client NewClient() { public static Client NewClient() {
Client client = new(); Client client = new();
clients.Add(client); clients.Add(client);
client.networkId = 0; client.networkId = 0;
return client; return client;
} }
public static Client NewUDPClient(UdpClient udpClient, string ipAddress, int port) { public static Client NewUDPClient(UdpClient udpClient, string ipAddress, int port) {
Client client = NewClient(); Client client = NewClient();
client.ipAddress = null; client.ipAddress = null;
client.port = port; client.port = port;
client.udpClient = udpClient; client.udpClient = udpClient;
return client; return client;
} }
} }
public class IMessage { public class IMessage {
public IMessage() { } public IMessage() { }
public IMessage(byte[] data) { public IMessage(byte[] data) {
Deserialize(data); Deserialize(data);
} }
public virtual byte[] Serialize() { return null; } public virtual byte[] Serialize() { return null; }
public virtual void Deserialize(byte[] data) { } public virtual void Deserialize(byte[] data) { }
public static bool SendMsg(Client client, IMessage msg) { public static bool SendMsg(Client client, IMessage msg) {
return SendMsg(client, msg.Serialize()); return SendMsg(client, msg.Serialize());
} }
public static bool SendMsg(Client client, byte[] data) { public static bool SendMsg(Client client, byte[] data) {
if (client == null || client.ipAddress == null) if (client == null || client.ipAddress == null)
return false; return false;
client.udpClient.Send(data, data.Length, client.ipAddress, client.port); client.udpClient.Send(data, data.Length, client.ipAddress, client.port);
return true; 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);
} }
return buffer; return buffer;
} }
} }
#region Client #region Client
public class ClientMsg : IMessage { public class ClientMsg : IMessage {
public const byte length = 2; public const byte length = 2;
public byte clientId; public byte clientId;
public ClientMsg(byte[] data) : base(data) { } public ClientMsg(byte[] data) : base(data) { }
public override void Deserialize(byte[] data) { public override void Deserialize(byte[] data) {
base.Deserialize(data); base.Deserialize(data);
uint ix = 0; uint ix = 0;
clientId = data[ix++]; clientId = data[ix++];
} }
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); NetworkIdMsg.Send(client);
//if (string.IsNullOrEmpty(sceneUrl) == false) //if (string.IsNullOrEmpty(sceneUrl) == false)
//SendModelUrl(client, sceneUrl); //SendModelUrl(client, sceneUrl);
} }
else if (msg.clientId == 0) { else if (msg.clientId == 0) {
NetworkIdMsg.Send(client); NetworkIdMsg.Send(client);
//if (string.IsNullOrEmpty(sceneUrl) == false) //if (string.IsNullOrEmpty(sceneUrl) == false)
//SendModelUrl(client, sceneUrl); //SendModelUrl(client, sceneUrl);
} }
return true; return true;
} }
} }
#endregion Client #endregion Client
#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 static bool Send(Client client) { public static bool Send(Client client) {
byte[] data = new byte[NetworkIdMsg.length]; byte[] data = new byte[NetworkIdMsg.length];
data[0] = NetworkIdMsg.Id; data[0] = NetworkIdMsg.Id;
data[1] = client.networkId; data[1] = client.networkId;
return SendMsg(client, data); return SendMsg(client, data);
} }
} }
#endregion Network Id #endregion Network Id
#region Thing #region Thing
public class ThingMsg : IMessage { public class ThingMsg : IMessage {
public const byte length = 4; public const byte length = 4;
public const byte Id = 0x80; public const byte Id = 0x80;
public byte objectId; public byte objectId;
public byte objectType; public byte objectType;
public byte parentId; public byte parentId;
public ThingMsg(byte[] data) : base(data) { } public ThingMsg(byte[] data) : base(data) { }
public override void Deserialize(byte[] data) { public override void Deserialize(byte[] data) {
uint ix = 0; uint ix = 0;
objectId = data[ix++]; objectId = data[ix++];
objectType = data[ix++]; objectType = data[ix++];
parentId = data[ix]; parentId = data[ix];
} }
public static bool Send(Client client, Thing obj) { public static bool Send(Client client, byte networkId, byte thingId, byte thingType) {
if (obj == null) byte[] data = new byte[4];
return false; data[0] = ThingMsg.Id;
data[1] = networkId;
byte[] data = new byte[4]; data[2] = thingId;
data[0] = ThingMsg.Id; data[3] = thingType;
data[1] = obj.networkId; data[4] = 0x00; // parent not supported yet
data[2] = obj.thingId; return SendMsg(client, data);
data[3] = obj.objectType; }
data[4] = 0x00; // not supported yet public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize) {
return SendMsg(client, data); if (packetSize != length)
} return false;
public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize) {
if (packetSize != length) byte[] buffer = await Receive(dataStream, packetSize);
return false; ThingMsg msg = new(buffer);
byte[] buffer = await Receive(dataStream, packetSize); client.messageQueue.Enqueue(msg);
ThingMsg msg = new(buffer); return true;
}
client.messageQueue.Enqueue(msg); }
return true;
} #endregion Thing
}
#region Name
#endregion Thing
public class NameMsg : IMessage {
#region Name public byte networkId = 0;
public byte objectId;
public class NameMsg : IMessage { public byte len;
public byte networkId = 0; public string name;
public byte objectId;
public byte len; public NameMsg(byte[] data) : base(data) { }
public string name; public override void Deserialize(byte[] data) {
uint ix = 0;
public NameMsg(byte[] data) : base(data) { } this.objectId = data[ix++];
public override void Deserialize(byte[] data) { int strlen = data[ix++];
uint ix = 0; this.name = System.Text.Encoding.UTF8.GetString(data, (int)ix, strlen);
this.objectId = data[ix++]; }
int strlen = data[ix++];
this.name = System.Text.Encoding.UTF8.GetString(data, (int)ix, strlen); public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize) {
} byte[] buffer = await Receive(dataStream, packetSize);
NameMsg msg = new(buffer);
public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize) {
byte[] buffer = await Receive(dataStream, packetSize); client.messageQueue.Enqueue(msg);
NameMsg msg = new(buffer); return true;
}
client.messageQueue.Enqueue(msg); }
return true;
} #endregion
}
#region Model URL
#endregion
public class ModelUrlMsg : IMessage {
#region Model URL public const byte Id = 0x90; // (144) Model URL
public byte objectId;
public class ModelUrlMsg : IMessage { public Spherical position;
public const byte Id = 0x90; // (144) Model URL public float scale;
public byte objectId; public string url;
public Spherical position;
public float scale; public ModelUrlMsg(string url, float scale = 1) {
public string url; this.url = url;
this.scale = scale;
public ModelUrlMsg(string url, float scale = 1) { this.position = Spherical.zero;
this.url = url; }
this.scale = scale; public ModelUrlMsg(byte[] data) : base(data) { }
this.position = Spherical.zero;
} public override byte[] Serialize() {
public ModelUrlMsg(byte[] data) : base(data) { } byte[] data = new byte[this.url.Length + 9];
data[0] = ModelUrlMsg.Id;
public override byte[] Serialize() { data[1] = 0x00; // Thing Id
byte[] data = new byte[this.url.Length + 9]; // data[2]..[5] == position 0, 0, 0
data[0] = ModelUrlMsg.Id; data[6] = 0x3C; // Dummy float16 value 1
data[1] = 0x00; // Thing Id data[7] = 0x00;
// data[2]..[5] == position 0, 0, 0
data[6] = 0x3C; // Dummy float16 value 1 data[8] = (byte)url.Length;
data[7] = 0x00; for (int ix = 0; ix < this.url.Length; ix++)
data[9 + ix] = (byte)url[ix];
data[8] = (byte)url.Length; return data;
for (int ix = 0; ix < this.url.Length; ix++) }
data[9 + ix] = (byte)url[ix]; public override void Deserialize(byte[] data) {
return data; uint ix = 0;
} this.objectId = data[ix++];
public override void Deserialize(byte[] data) { this.position = LowLevelMessages.ReceiveSpherical(data, ref ix);
uint ix = 0; this.scale = LowLevelMessages.ReceiveFloat16(data, ref ix);
this.objectId = data[ix++]; int strlen = data[ix++];
this.position = LowLevelMessages.ReceiveSpherical(data, ref ix); url = System.Text.Encoding.UTF8.GetString(data, (int)ix, strlen);
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 bool Send(Client client, string modelUrl) { }
ModelUrlMsg msg = new(modelUrl); public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize) {
return SendMsg(client, msg); byte[] buffer = await Receive(dataStream, packetSize);
} ModelUrlMsg msg = new(buffer);
public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize) { client.messageQueue.Enqueue(msg);
byte[] buffer = await Receive(dataStream, packetSize); return true;
ModelUrlMsg msg = new(buffer); }
client.messageQueue.Enqueue(msg); }
return true;
} #endregion Model URL
}
#region Pose
#endregion Model URL
public class PoseMsg : IMessage {
#region Pose public const byte length = 3 + 4 + 4;
public byte thingId;
public class PoseMsg : IMessage { public byte poseType;
public const byte length = 3 + 4 + 4;
public byte thingId; public Spherical position;
public byte poseType; public Quat32 orientation;
public Spherical position; public PoseMsg(byte[] data) : base(data) { }
public Quat32 orientation;
public override void Deserialize(byte[] data) {
public PoseMsg(byte[] data) : base(data) { } uint ix = 0;
thingId = data[ix++];
public override void Deserialize(byte[] data) { poseType = data[ix++];
uint ix = 0;
thingId = data[ix++]; //if ((poseType & Pose_Position) != 0)
poseType = data[ix++]; position = LowLevelMessages.ReceiveSpherical(data, ref ix);
//if ((poseType & Pose_Orientation) != 0) {
//if ((poseType & Pose_Position) != 0) orientation = LowLevelMessages.ReceiveQuat32(data, ref ix);
position = LowLevelMessages.ReceiveSpherical(data, ref ix); }
//if ((poseType & Pose_Orientation) != 0) {
orientation = LowLevelMessages.ReceiveQuat32(data, ref ix); public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize) {
} if (packetSize != length)
return false;
public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize) {
if (packetSize != length) byte[] buffer = await Receive(dataStream, packetSize);
return false; PoseMsg msg = new(buffer);
byte[] buffer = await Receive(dataStream, packetSize); client.messageQueue.Enqueue(msg);
PoseMsg msg = new(buffer); return true;
client.messageQueue.Enqueue(msg); }
return true; }
} #endregion Pose
}
#region Bytes
#endregion Pose
public class BytesMsg : IMessage {
#region Bytes public const byte Id = 0xB1;
public byte networkId;
public class BytesMsg : IMessage { public byte thingId;
public const byte Id = 0xB1; public byte[] bytes;
public byte networkId;
public byte thingId; public BytesMsg(byte[] data) : base(data) { }
public byte[] bytes; public BytesMsg(byte networkId, byte thingId, byte[] bytes) : base() {
this.networkId = networkId;
public BytesMsg(byte[] data) : base(data) { } this.thingId = thingId;
public BytesMsg(byte networkId, byte thingId, byte[] bytes) : base() { this.bytes = bytes;
this.networkId = networkId; }
this.thingId = thingId; public override byte[] Serialize() {
this.bytes = bytes; byte[] buffer = new byte[4 + this.bytes.Length];
} int ix = 0;
public override byte[] Serialize() { buffer[ix++] = BytesMsg.Id;
byte[] buffer = new byte[4 + this.bytes.Length]; buffer[ix++] = this.networkId;
int ix = 0; buffer[ix++] = this.thingId;
buffer[ix++] = BytesMsg.Id; buffer[ix++] = (byte)bytes.Length;
buffer[ix++] = this.networkId; foreach (byte b in bytes)
buffer[ix++] = this.thingId; buffer[ix++] = b;
buffer[ix++] = (byte)bytes.Length;
foreach (byte b in bytes) return buffer;
buffer[ix++] = b; }
public override void Deserialize(byte[] data) {
return buffer; //this.bytes = data;
} uint ix = 0;
public override void Deserialize(byte[] data) { this.thingId = data[ix++];
//this.bytes = data; this.bytes = new byte[data.Length - ix];
uint ix = 0; for (uint bytesIx = 0; ix < data.Length; ix++, bytesIx++)
this.thingId = data[ix++]; this.bytes[bytesIx] = 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);
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<bool> Receive(Stream dataStream, Client client, byte packetSize) {
byte[] buffer = await Receive(dataStream, packetSize);
// received bytes BytesMsg msg = new(buffer);
public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize) { client.messageQueue.Enqueue(msg);
byte[] buffer = await Receive(dataStream, packetSize); return true;
BytesMsg msg = new(buffer); }
client.messageQueue.Enqueue(msg); }
return true;
} #endregion Bytes
}
#region Destroy
#endregion Bytes
public class DestroyMsg : IMessage {
#region Destroy public const byte length = 2;
public byte objectId;
public class DestroyMsg : IMessage {
public const byte length = 2; public DestroyMsg(byte[] data) : base(data) { }
public byte objectId;
public DestroyMsg(byte[] data) : base(data) { }
public override void Deserialize(byte[] data) { public override void Deserialize(byte[] data) {
objectId = data[0]; objectId = data[0];
} }
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);
DestroyMsg msg = new(buffer); DestroyMsg msg = new(buffer);
client.messageQueue.Enqueue(msg); client.messageQueue.Enqueue(msg);
return true; return true;
} }
} }
#endregion Destroy #endregion Destroy
} }