Merge commit '17fa48a26673454f22f9c48f76efb5a36c9ffc5a' into V2
This commit is contained in:
commit
e975f219f4
56
Client.cs
56
Client.cs
@ -3,21 +3,28 @@ 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 string broadcastIpAddress = "255.255.255.255";
|
||||
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 +32,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 +41,35 @@ namespace Passer.Control {
|
||||
clients.Add(this);
|
||||
}
|
||||
|
||||
public virtual void ProcessMessages() {
|
||||
while (this.messageQueue.TryDequeue(out IMessage msg))
|
||||
ProcessMessage(msg);
|
||||
public bool SendBuffer()
|
||||
{
|
||||
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) {
|
||||
switch (msg) {
|
||||
public bool PublishBuffer()
|
||||
{
|
||||
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:
|
||||
ProcessClient(clientMsg);
|
||||
break;
|
||||
@ -97,8 +127,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}");
|
||||
|
@ -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.vertical);
|
||||
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 vertical = ReceiveAngle8(data, ref ix);
|
||||
float distance = ReceiveFloat16(data, ref ix);
|
||||
@ -16,12 +19,14 @@ public class LowLevelMessages {
|
||||
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 qy = (int)(q.y * 127 + 128);
|
||||
int qz = (int)(q.z * 127 + 128);
|
||||
int qw = (int)(q.w * 255);
|
||||
if (q.w < 0) {
|
||||
if (q.w < 0)
|
||||
{
|
||||
qx = -qx;
|
||||
qy = -qy;
|
||||
qz = -qz;
|
||||
@ -33,7 +38,8 @@ public class LowLevelMessages {
|
||||
buffer[ix++] = (byte)qz;
|
||||
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(
|
||||
(data[ix++] - 128.0F) / 127.0F,
|
||||
(data[ix++] - 128.0F) / 127.0F,
|
||||
@ -42,7 +48,8 @@ public class LowLevelMessages {
|
||||
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
|
||||
while (angle >= 180)
|
||||
angle -= 360;
|
||||
@ -51,18 +58,21 @@ public class LowLevelMessages {
|
||||
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;
|
||||
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();
|
||||
data[ix++] = (byte)(binary >> 8);
|
||||
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++]);
|
||||
float16 f16 = new();
|
||||
f16.SetBinary(value);
|
||||
|
296
Messages.cs
296
Messages.cs
@ -1,44 +1,39 @@
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Passer.Control {
|
||||
using Passer.LinearAlgebra;
|
||||
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 byte Serialize(ref byte[] buffer) { return 0; }
|
||||
public virtual void Deserialize(byte[] buffer) { }
|
||||
|
||||
public static bool SendMsg(Client client, IMessage msg) {
|
||||
return SendMsg(client, msg.Serialize());
|
||||
}
|
||||
public static bool SendMsg(Client client, byte[] buffer) {
|
||||
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 SendMsg(Client client, IMessage msg)
|
||||
{
|
||||
msg.Serialize(ref client.buffer);
|
||||
return client.SendBuffer();
|
||||
}
|
||||
|
||||
public static bool PublishMsg(Client client, IMessage msg) {
|
||||
return PublishMsg(client, msg.Serialize());
|
||||
}
|
||||
public static bool PublishMsg(Client client, byte[] buffer) {
|
||||
if (client == null)
|
||||
return false;
|
||||
|
||||
client.udpClient.Send(buffer, buffer.Length, "127.0.0.1", client.port);
|
||||
return true;
|
||||
public static bool PublishMsg(Client client, IMessage msg)
|
||||
{
|
||||
msg.Serialize(ref client.buffer);
|
||||
return client.PublishBuffer();
|
||||
}
|
||||
|
||||
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 +44,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() {
|
||||
byte[] buffer = new byte[ClientMsg.length];
|
||||
buffer[0] = ClientMsg.Id;
|
||||
buffer[1] = networkId;
|
||||
return buffer;
|
||||
public override byte Serialize(ref byte[] buffer)
|
||||
{
|
||||
byte ix = 0;
|
||||
buffer[ix++] = ClientMsg.Id;
|
||||
buffer[ix++] = networkId;
|
||||
return ix;
|
||||
}
|
||||
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 +108,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() {
|
||||
byte[] buffer = new byte[NetworkIdMsg.length];
|
||||
buffer[0] = NetworkIdMsg.Id;
|
||||
buffer[1] = this.networkId;
|
||||
return buffer;
|
||||
public override byte Serialize(ref byte[] buffer)
|
||||
{
|
||||
byte ix = 0;
|
||||
buffer[ix++] = NetworkIdMsg.Id;
|
||||
buffer[ix++] = this.networkId;
|
||||
return ix;
|
||||
}
|
||||
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 +154,41 @@ 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() {
|
||||
byte[] buffer = new byte[InvestigateMsg.length];
|
||||
buffer[0] = InvestigateMsg.Id;
|
||||
buffer[1] = this.networkId;
|
||||
buffer[2] = this.thingId;
|
||||
return buffer;
|
||||
public override byte Serialize(ref byte[] buffer)
|
||||
{
|
||||
byte ix = 0;
|
||||
buffer[ix++] = InvestigateMsg.Id;
|
||||
buffer[ix++] = this.networkId;
|
||||
buffer[ix++] = this.thingId;
|
||||
return ix;
|
||||
}
|
||||
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) {
|
||||
InvestigateMsg msg = new(networkId, thingId);
|
||||
//UnityEngine.Debug.Log($"Send investigate [{msg.networkId}/{msg.thingId}]");
|
||||
public static bool Send(Client client, CoreThing thing) {
|
||||
InvestigateMsg msg = new(thing.networkId, thing.id);
|
||||
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 +206,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 +215,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,17 +224,18 @@ namespace Passer.Control {
|
||||
}
|
||||
public ThingMsg(byte[] buffer) : base(buffer) { }
|
||||
|
||||
public override byte[] Serialize() {
|
||||
byte[] buffer = new byte[ThingMsg.length];
|
||||
public override byte Serialize(ref byte[] buffer)
|
||||
{
|
||||
byte ix = 0;
|
||||
buffer[ix++] = ThingMsg.Id;
|
||||
buffer[ix++] = this.networkId;
|
||||
buffer[ix++] = this.thingId;
|
||||
buffer[ix++] = this.thingType;
|
||||
buffer[ix] = this.parentId;
|
||||
return buffer;
|
||||
buffer[ix++] = this.parentId;
|
||||
return ix;
|
||||
}
|
||||
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 +243,18 @@ namespace Passer.Control {
|
||||
this.parentId = buffer[ix];
|
||||
}
|
||||
|
||||
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}]");
|
||||
public static bool Send(Client client, CoreThing thing) {
|
||||
ThingMsg msg = new(thing.networkId, thing.id, thing.type, thing.parent.id);
|
||||
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)
|
||||
return false;
|
||||
|
||||
@ -252,7 +275,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,25 +284,27 @@ 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() {
|
||||
byte[] buffer = new byte[length + this.name.Length];
|
||||
public override byte Serialize(ref byte[] buffer)
|
||||
{
|
||||
byte ix = 0;
|
||||
buffer[ix++] = NameMsg.Id;
|
||||
buffer[ix++] = this.networkId;
|
||||
buffer[ix++] = this.thingId;
|
||||
buffer[ix++] = (byte)this.name.Length;
|
||||
for (int nameIx = 0; nameIx < this.name.Length; nameIx++, ix++)
|
||||
buffer[ix] = (byte)this.name[nameIx];
|
||||
return buffer;
|
||||
for (int nameIx = 0; nameIx < this.name.Length; nameIx++)
|
||||
buffer[ix++] = (byte)this.name[nameIx];
|
||||
return ix;
|
||||
}
|
||||
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 +312,20 @@ 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) {
|
||||
NameMsg msg = new(networkId, thingId, name);
|
||||
public static bool Send(Client client, CoreThing thing) {
|
||||
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);
|
||||
}
|
||||
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);
|
||||
NameMsg msg = new(buffer);
|
||||
|
||||
@ -303,7 +338,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 +347,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,8 +357,8 @@ namespace Passer.Control {
|
||||
}
|
||||
public ModelUrlMsg(byte[] buffer) : base(buffer) { }
|
||||
|
||||
public override byte[] Serialize() {
|
||||
byte[] buffer = new byte[this.url.Length + 6];
|
||||
public override byte Serialize(ref byte[] buffer)
|
||||
{
|
||||
byte ix = 0;
|
||||
buffer[ix++] = ModelUrlMsg.Id;
|
||||
buffer[ix++] = this.networkId;
|
||||
@ -329,11 +366,12 @@ namespace Passer.Control {
|
||||
LowLevelMessages.SendFloat16(buffer, ref ix, new float16(1.0f));
|
||||
|
||||
buffer[ix++] = (byte)url.Length;
|
||||
for (int urlIx = 0; urlIx < this.url.Length; urlIx++, ix++)
|
||||
buffer[ix] = (byte)url[urlIx];
|
||||
return buffer;
|
||||
for (int urlIx = 0; urlIx < this.url.Length; urlIx++)
|
||||
buffer[ix++] = (byte)url[urlIx];
|
||||
return ix;
|
||||
}
|
||||
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 +380,23 @@ 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, 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);
|
||||
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 +408,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 +422,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,8 +440,8 @@ namespace Passer.Control {
|
||||
}
|
||||
public PoseMsg(byte[] buffer) : base(buffer) { }
|
||||
|
||||
public override byte[] Serialize() {
|
||||
byte[] buffer = new byte[PoseMsg.length];
|
||||
public override byte Serialize(ref byte[] buffer)
|
||||
{
|
||||
byte ix = 0;
|
||||
buffer[ix++] = PoseMsg.Id;
|
||||
buffer[ix++] = this.networkId;
|
||||
@ -398,9 +450,10 @@ namespace Passer.Control {
|
||||
|
||||
LowLevelMessages.SendSpherical(buffer, ref ix, this.position);
|
||||
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;
|
||||
this.networkId = buffer[ix++];
|
||||
this.thingId = buffer[ix++];
|
||||
@ -412,11 +465,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,21 +491,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() {
|
||||
byte[] buffer = new byte[3 + this.bytes.Length];
|
||||
public override byte Serialize(ref byte[] buffer)
|
||||
{
|
||||
byte ix = 0;
|
||||
buffer[ix++] = CustomMsg.Id;
|
||||
buffer[ix++] = this.networkId;
|
||||
@ -459,9 +516,10 @@ namespace Passer.Control {
|
||||
foreach (byte b in bytes)
|
||||
buffer[ix++] = b;
|
||||
|
||||
return buffer;
|
||||
return ix;
|
||||
}
|
||||
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 +529,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 +547,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 +574,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 +583,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;
|
||||
|
||||
|
11
Quat32.cs
11
Quat32.cs
@ -1,15 +1,18 @@
|
||||
namespace Passer {
|
||||
public class Quat32 {
|
||||
namespace Passer.LinearAlgebra
|
||||
{
|
||||
public class Quat32
|
||||
{
|
||||
public float x;
|
||||
public float y;
|
||||
public float z;
|
||||
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.y = y;
|
||||
this.z = z;
|
||||
this.w = w;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,12 +1,15 @@
|
||||
namespace Passer {
|
||||
public class Spherical {
|
||||
namespace Passer.LinearAlgebra
|
||||
{
|
||||
public class Spherical
|
||||
{
|
||||
public float distance;
|
||||
public float horizontal;
|
||||
public float vertical;
|
||||
|
||||
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.horizontal = horizontal;
|
||||
this.vertical = vertical;
|
||||
|
40
Thing.cs
Normal file
40
Thing.cs
Normal 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
11
Thing.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0e8b48bc91446304eaaccbfdde4cc4af
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
x
Reference in New Issue
Block a user