Spawned a GLB ant!

This commit is contained in:
Pascal Serrarens 2024-12-12 17:42:45 +01:00
parent 87fbddb2b5
commit 0d022c26ef
3 changed files with 198 additions and 77 deletions

View File

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

View File

@ -1,21 +1,26 @@
using System.IO;
using System.Threading.Tasks;
namespace Passer.Control {
namespace Passer.Control
{
public class IMessage {
public class IMessage
{
public IMessage() { }
public IMessage(byte[] buffer) {
public IMessage(byte[] buffer)
{
Deserialize(buffer);
}
public virtual byte[] Serialize() { return null; }
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());
}
public static bool SendMsg(Client client, byte[] buffer) {
public static bool SendMsg(Client client, byte[] buffer)
{
if (client == null || client.ipAddress == null)
return false;
@ -24,10 +29,12 @@ namespace Passer.Control {
return true;
}
public static bool PublishMsg(Client client, IMessage msg) {
public static bool PublishMsg(Client client, IMessage msg)
{
return PublishMsg(client, msg.Serialize());
}
public static bool PublishMsg(Client client, byte[] buffer) {
public static bool PublishMsg(Client client, byte[] buffer)
{
if (client == null)
return false;
@ -35,10 +42,12 @@ namespace Passer.Control {
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
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
await Task.Delay(1);
byteCount += dataStream.Read(buffer, byteCount, packetSize - 1 - byteCount);
@ -49,49 +58,58 @@ namespace Passer.Control {
#region Client
public class ClientMsg : IMessage {
public class ClientMsg : IMessage
{
public const byte Id = 0xA0;
public const byte length = 2;
public byte networkId;
public ClientMsg(byte networkId) {
public ClientMsg(byte networkId)
{
this.networkId = networkId;
}
public ClientMsg(byte[] buffer) : base(buffer) { }
public override byte[] Serialize() {
public override byte[] Serialize()
{
byte[] buffer = new byte[ClientMsg.length];
buffer[0] = ClientMsg.Id;
buffer[1] = networkId;
return buffer;
}
public override void Deserialize(byte[] buffer) {
public override void Deserialize(byte[] buffer)
{
base.Deserialize(buffer);
uint ix = 0;
networkId = buffer[ix];
}
public static bool Send(Client client, byte networkId) {
public static bool Send(Client client, byte networkId)
{
ClientMsg msg = new(networkId);
return SendMsg(client, msg);
}
public static bool Publish(Client client, byte networkId) {
public static bool Publish(Client client, byte networkId)
{
ClientMsg msg = new(networkId);
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)
return false;
byte[] buffer = await Receive(dataStream, packetSize);
ClientMsg msg = new(buffer);
if (client.networkId == 0) {
if (client.networkId == 0)
{
client.networkId = (byte)(Client.clients.Count);
NetworkIdMsg.Send(client, client.networkId);
client.messageQueue.Enqueue(msg);
}
else if (msg.networkId == 0) {
else if (msg.networkId == 0)
{
NetworkIdMsg.Send(client, client.networkId);
client.messageQueue.Enqueue(msg);
}
@ -104,32 +122,38 @@ namespace Passer.Control {
#region Network Id
public class NetworkIdMsg : IMessage {
public class NetworkIdMsg : IMessage
{
public const byte Id = 0xA1;
public const byte length = 2;
public byte networkId;
NetworkIdMsg(byte networkId) {
NetworkIdMsg(byte networkId)
{
this.networkId = networkId;
}
NetworkIdMsg(byte[] buffer) : base(buffer) { }
public override byte[] Serialize() {
public override byte[] Serialize()
{
byte[] buffer = new byte[NetworkIdMsg.length];
buffer[0] = NetworkIdMsg.Id;
buffer[1] = this.networkId;
return buffer;
}
public override void Deserialize(byte[] buffer) {
public override void Deserialize(byte[] buffer)
{
uint ix = 0;
this.networkId = buffer[ix];
}
public static bool Send(Client client, byte networkId) {
public static bool Send(Client client, byte networkId)
{
NetworkIdMsg msg = new(networkId);
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)
return false;
@ -144,37 +168,43 @@ namespace Passer.Control {
#region Investigate
public class InvestigateMsg : IMessage {
public 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) {
public InvestigateMsg(byte networkId, byte thingId)
{
this.networkId = networkId;
this.thingId = thingId;
}
public InvestigateMsg(byte[] buffer) : base(buffer) { }
public override byte[] Serialize() {
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[] buffer) {
public override void Deserialize(byte[] buffer)
{
uint ix = 0;
this.networkId = buffer[ix++];
this.thingId = buffer[ix++];
}
public static bool Send(Client client, byte networkId, byte thingId) {
public static bool Send(Client client, byte networkId, byte thingId)
{
InvestigateMsg msg = new(networkId, thingId);
//UnityEngine.Debug.Log($"Send investigate [{msg.networkId}/{msg.thingId}]");
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)
return false;
@ -192,7 +222,8 @@ namespace Passer.Control {
#region Thing
public class ThingMsg : IMessage {
public class ThingMsg : IMessage
{
public const byte length = 5;
public const byte Id = 0x80;
public byte networkId;
@ -200,7 +231,8 @@ namespace Passer.Control {
public byte thingType;
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.thingId = thingId;
this.thingType = thingType;
@ -208,7 +240,8 @@ namespace Passer.Control {
}
public ThingMsg(byte[] buffer) : base(buffer) { }
public override byte[] Serialize() {
public override byte[] Serialize()
{
byte[] buffer = new byte[ThingMsg.length];
byte ix = 0;
buffer[ix++] = ThingMsg.Id;
@ -218,7 +251,8 @@ namespace Passer.Control {
buffer[ix] = this.parentId;
return buffer;
}
public override void Deserialize(byte[] buffer) {
public override void Deserialize(byte[] buffer)
{
uint ix = 0;
this.networkId = buffer[ix++];
this.thingId = buffer[ix++];
@ -226,12 +260,14 @@ namespace Passer.Control {
this.parentId = buffer[ix];
}
public static bool Send(Client client, byte networkId, byte thingId, byte thingType, byte parentId) {
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) {
public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize)
{
if (packetSize != length)
return false;
@ -252,7 +288,8 @@ namespace Passer.Control {
#region Name
public class NameMsg : IMessage {
public class NameMsg : IMessage
{
public const byte Id = 0x91; // 145
public const byte length = 4;
public byte networkId;
@ -260,14 +297,16 @@ namespace Passer.Control {
public byte len;
public string name;
public NameMsg(byte networkId, byte thingId, string name) {
public NameMsg(byte networkId, byte thingId, string name)
{
this.networkId = networkId;
this.thingId = thingId;
this.name = name;
}
public NameMsg(byte[] buffer) : base(buffer) { }
public override byte[] Serialize() {
public override byte[] Serialize()
{
byte[] buffer = new byte[length + this.name.Length];
byte ix = 0;
buffer[ix++] = NameMsg.Id;
@ -278,7 +317,8 @@ namespace Passer.Control {
buffer[ix] = (byte)this.name[nameIx];
return buffer;
}
public override void Deserialize(byte[] buffer) {
public override void Deserialize(byte[] buffer)
{
byte ix = 0;
this.networkId = buffer[ix++];
this.thingId = buffer[ix++];
@ -286,11 +326,13 @@ namespace Passer.Control {
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, 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) {
public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize)
{
byte[] buffer = await Receive(dataStream, packetSize);
NameMsg msg = new(buffer);
@ -303,7 +345,8 @@ namespace Passer.Control {
#region Model URL
public class ModelUrlMsg : IMessage {
public class ModelUrlMsg : IMessage
{
public const byte Id = 0x90; // (144) Model URL
public byte networkId;
public byte thingId;
@ -311,7 +354,8 @@ namespace Passer.Control {
public float scale;
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.thingId = thingId;
this.url = url;
@ -320,7 +364,8 @@ namespace Passer.Control {
}
public ModelUrlMsg(byte[] buffer) : base(buffer) { }
public override byte[] Serialize() {
public override byte[] Serialize()
{
byte[] buffer = new byte[this.url.Length + 6];
byte ix = 0;
buffer[ix++] = ModelUrlMsg.Id;
@ -333,7 +378,8 @@ namespace Passer.Control {
buffer[ix] = (byte)url[urlIx];
return buffer;
}
public override void Deserialize(byte[] buffer) {
public override void Deserialize(byte[] buffer)
{
byte ix = 0;
this.networkId = buffer[ix++];
this.thingId = buffer[ix++];
@ -342,11 +388,16 @@ namespace Passer.Control {
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, byte networkId, byte thingId, string modelUrl)
{
if (string.IsNullOrEmpty(modelUrl))
return true; // nothing sent, but still a success!
ModelUrlMsg msg = new(networkId, thingId, modelUrl);
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);
ModelUrlMsg msg = new(buffer);
client.messageQueue.Enqueue(msg);
@ -358,7 +409,8 @@ namespace Passer.Control {
#region Pose
public class PoseMsg : IMessage {
public class PoseMsg : IMessage
{
public const byte Id = 0x10;
public const byte length = 4 + 4 + 4;
public byte networkId;
@ -371,7 +423,8 @@ namespace Passer.Control {
public Spherical position;
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.thingId = thingId;
this.position = position;
@ -388,7 +441,8 @@ namespace Passer.Control {
}
public PoseMsg(byte[] buffer) : base(buffer) { }
public override byte[] Serialize() {
public override byte[] Serialize()
{
byte[] buffer = new byte[PoseMsg.length];
byte ix = 0;
buffer[ix++] = PoseMsg.Id;
@ -400,7 +454,8 @@ namespace Passer.Control {
LowLevelMessages.SendQuat32(buffer, ref ix, this.orientation);
return buffer;
}
public override void Deserialize(byte[] buffer) {
public override void Deserialize(byte[] buffer)
{
byte ix = 0;
this.networkId = buffer[ix++];
this.thingId = buffer[ix++];
@ -412,11 +467,13 @@ namespace Passer.Control {
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);
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)
return false;
@ -436,20 +493,23 @@ namespace Passer.Control {
#region Custom
public class CustomMsg : IMessage {
public class CustomMsg : IMessage
{
public const byte Id = 0xB1;
public byte networkId;
public byte thingId;
public byte[] bytes;
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.thingId = thingId;
this.bytes = bytes;
}
public override byte[] Serialize() {
public override byte[] Serialize()
{
byte[] buffer = new byte[3 + this.bytes.Length];
byte ix = 0;
buffer[ix++] = CustomMsg.Id;
@ -461,7 +521,8 @@ namespace Passer.Control {
return buffer;
}
public override void Deserialize(byte[] buffer) {
public override void Deserialize(byte[] buffer)
{
byte ix = 0;
this.networkId = buffer[ix++];
this.thingId = buffer[ix++];
@ -471,11 +532,13 @@ namespace Passer.Control {
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);
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);
CustomMsg msg = new(buffer);
client.messageQueue.Enqueue(msg);
@ -487,18 +550,21 @@ namespace Passer.Control {
#region Text
public class TextMsg : IMessage {
public class TextMsg : IMessage
{
public const byte Id = 0xB0;
public string text;
public TextMsg(byte[] buffer) : base(buffer) { }
public override void Deserialize(byte[] buffer) {
public override void Deserialize(byte[] buffer)
{
uint ix = 0;
uint strlen = buffer[ix++];
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);
TextMsg msg = new(buffer);
@ -511,7 +577,8 @@ namespace Passer.Control {
#region Destroy
public class DestroyMsg : IMessage {
public class DestroyMsg : IMessage
{
public const byte Id = 0x20;
public const byte length = 2;
public byte networkId;
@ -519,12 +586,14 @@ namespace Passer.Control {
public DestroyMsg(byte[] buffer) : base(buffer) { }
public override void Deserialize(byte[] buffer) {
public override void Deserialize(byte[] buffer)
{
this.networkId = buffer[0];
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)
return false;

40
Thing.cs Normal file
View File

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