Reimplemented almost everything in C#

This commit is contained in:
Pascal Serrarens 2024-12-05 18:06:19 +01:00
parent 9919aa6578
commit 673fd3d258

View File

@ -4,9 +4,10 @@ using System.IO;
using System.Net.Sockets; using System.Net.Sockets;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Passer.Control { namespace Passer.Control
{
public class Client { public partial class Client
{
//public ConnectionMethod connection; //public ConnectionMethod connection;
public UdpClient udpClient; public UdpClient udpClient;
public string ipAddress; public string ipAddress;
@ -16,8 +17,10 @@ namespace Passer.Control {
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 +28,8 @@ namespace Passer.Control {
} }
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;
@ -33,7 +37,8 @@ namespace Passer.Control {
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;
@ -42,29 +47,35 @@ namespace Passer.Control {
} }
} }
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);
@ -75,31 +86,37 @@ namespace Passer.Control {
#region Client #region Client
public class ClientMsg : IMessage { public class ClientMsg : IMessage
{
public const byte Id = 0xA0;
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);
@ -113,11 +130,13 @@ 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 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;
@ -129,22 +148,25 @@ namespace Passer.Control {
#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 thingId;
public byte objectType; public byte thingType;
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++]; thingId = data[ix++];
objectType = data[ix++]; thingType = data[ix++];
parentId = data[ix]; parentId = data[ix];
} }
public static bool Send(Client client, byte networkId, byte thingId, byte thingType) { public static bool Send(Client client, byte networkId, byte thingId, byte thingType)
{
byte[] data = new byte[4]; byte[] data = new byte[4];
data[0] = ThingMsg.Id; data[0] = ThingMsg.Id;
data[1] = networkId; data[1] = networkId;
@ -153,7 +175,8 @@ namespace Passer.Control {
data[4] = 0x00; // parent not supported yet data[4] = 0x00; // parent not supported yet
return SendMsg(client, data); return SendMsg(client, data);
} }
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;
@ -169,21 +192,24 @@ namespace Passer.Control {
#region Name #region Name
public class NameMsg : IMessage { public class NameMsg : IMessage
{
public byte networkId = 0; public byte networkId = 0;
public byte objectId; public byte objectId;
public byte len; public byte len;
public string name; public string name;
public NameMsg(byte[] data) : base(data) { } public NameMsg(byte[] data) : base(data) { }
public override void Deserialize(byte[] data) { public override void Deserialize(byte[] data)
{
uint ix = 0; uint ix = 0;
this.objectId = data[ix++]; this.objectId = data[ix++];
int strlen = data[ix++]; int strlen = data[ix++];
this.name = System.Text.Encoding.UTF8.GetString(data, (int)ix, strlen); this.name = System.Text.Encoding.UTF8.GetString(data, (int)ix, 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);
NameMsg msg = new(buffer); NameMsg msg = new(buffer);
@ -196,25 +222,28 @@ 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 objectId; public byte objectId;
public Spherical position; public Spherical position;
public float scale; public float scale;
public string url; public string url;
public ModelUrlMsg(string url, float scale = 1) { public ModelUrlMsg(string url, float scale = 1)
{
this.url = url; this.url = url;
this.scale = scale; this.scale = scale;
this.position = Spherical.zero; this.position = Spherical.zero;
} }
public ModelUrlMsg(byte[] data) : base(data) { } public ModelUrlMsg(byte[] data) : base(data) { }
public override byte[] Serialize() { public override byte[] Serialize()
{
byte[] data = new byte[this.url.Length + 9]; byte[] data = new byte[this.url.Length + 9];
data[0] = ModelUrlMsg.Id; data[0] = ModelUrlMsg.Id;
data[1] = 0x00; // Thing Id data[1] = 0x00; // Thing Id
// data[2]..[5] == position 0, 0, 0 // data[2]..[5] == position 0, 0, 0
data[6] = 0x3C; // Dummy float16 value 1 data[6] = 0x3C; // Dummy float16 value 1
data[7] = 0x00; data[7] = 0x00;
@ -223,7 +252,8 @@ namespace Passer.Control {
data[9 + ix] = (byte)url[ix]; data[9 + ix] = (byte)url[ix];
return data; return data;
} }
public override void Deserialize(byte[] data) { public override void Deserialize(byte[] data)
{
uint ix = 0; uint ix = 0;
this.objectId = data[ix++]; this.objectId = data[ix++];
this.position = LowLevelMessages.ReceiveSpherical(data, ref ix); this.position = LowLevelMessages.ReceiveSpherical(data, ref ix);
@ -232,11 +262,13 @@ namespace Passer.Control {
url = System.Text.Encoding.UTF8.GetString(data, (int)ix, strlen); url = System.Text.Encoding.UTF8.GetString(data, (int)ix, strlen);
} }
public static bool Send(Client client, string modelUrl) { public static bool Send(Client client, string modelUrl)
{
ModelUrlMsg msg = new(modelUrl); ModelUrlMsg msg = new(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);
@ -248,7 +280,9 @@ namespace Passer.Control {
#region Pose #region Pose
public class PoseMsg : IMessage { public class PoseMsg : IMessage
{
public const byte Id = 0x10;
public const byte length = 3 + 4 + 4; public const byte length = 3 + 4 + 4;
public byte thingId; public byte thingId;
public byte poseType; public byte poseType;
@ -258,7 +292,8 @@ namespace Passer.Control {
public PoseMsg(byte[] data) : base(data) { } public PoseMsg(byte[] data) : base(data) { }
public override void Deserialize(byte[] data) { public override void Deserialize(byte[] data)
{
uint ix = 0; uint ix = 0;
thingId = data[ix++]; thingId = data[ix++];
poseType = data[ix++]; poseType = data[ix++];
@ -269,7 +304,8 @@ namespace Passer.Control {
orientation = LowLevelMessages.ReceiveQuat32(data, ref ix); orientation = LowLevelMessages.ReceiveQuat32(data, ref 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;
@ -286,19 +322,22 @@ namespace Passer.Control {
#region Bytes #region Bytes
public class BytesMsg : IMessage { public class BytesMsg : 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 BytesMsg(byte[] data) : base(data) { } public BytesMsg(byte[] data) : base(data) { }
public BytesMsg(byte networkId, byte thingId, byte[] bytes) : base() { public BytesMsg(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()
{
byte[] buffer = new byte[4 + this.bytes.Length]; byte[] buffer = new byte[4 + this.bytes.Length];
int ix = 0; int ix = 0;
buffer[ix++] = BytesMsg.Id; buffer[ix++] = BytesMsg.Id;
@ -310,7 +349,8 @@ namespace Passer.Control {
return buffer; return buffer;
} }
public override void Deserialize(byte[] data) { public override void Deserialize(byte[] data)
{
//this.bytes = data; //this.bytes = data;
uint ix = 0; uint ix = 0;
this.thingId = data[ix++]; this.thingId = data[ix++];
@ -320,13 +360,15 @@ namespace Passer.Control {
} }
public static void Send(Client client, byte thingId, byte[] bytes) { public static void Send(Client client, byte thingId, byte[] bytes)
{
BytesMsg msg = new(client.networkId, thingId, bytes); BytesMsg msg = new(client.networkId, thingId, bytes);
SendMsg(client, msg); SendMsg(client, msg);
} }
// received bytes // received bytes
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);
BytesMsg msg = new(buffer); BytesMsg msg = new(buffer);
client.messageQueue.Enqueue(msg); client.messageQueue.Enqueue(msg);
@ -338,17 +380,20 @@ namespace Passer.Control {
#region Destroy #region Destroy
public class DestroyMsg : IMessage { public class DestroyMsg : IMessage
{
public const byte length = 2; public const byte length = 2;
public byte objectId; public byte objectId;
public DestroyMsg(byte[] data) : base(data) { } 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;