Merge commit '7e80a360274abb6d8f8299cdd2f6ada26bb5ce3c' into pascalV3

This commit is contained in:
Pascal Serrarens 2024-12-12 15:46:57 +01:00
commit 87fbddb2b5
8 changed files with 934 additions and 412 deletions

109
Client.cs Normal file
View File

@ -0,0 +1,109 @@
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Net.Sockets;
using System.IO;
namespace Passer.Control {
public class Client {
//public ConnectionMethod connection;
public UdpClient udpClient;
public string ipAddress;
public int port;
public Stream dataStream;
public byte networkId = 0;
public readonly ConcurrentQueue<IMessage> messageQueue = new();
public static Client GetClient(string ipAddress, int port) {
foreach (Client c in clients) {
if (c.ipAddress == ipAddress && c.port == port)
return c;
}
return null;
}
static public List<Client> clients = new List<Client>();
public Client(UdpClient udpClient, int port) {
this.udpClient = udpClient;
this.ipAddress = null;
this.port = port;
this.dataStream = new EchoStream();
clients.Add(this);
}
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;
case NetworkIdMsg networkId:
ProcessNetworkId(networkId);
break;
case InvestigateMsg investigate:
ProcessInvestigate(investigate);
break;
case ThingMsg thing:
ProcessThing(thing);
break;
case NameMsg name:
//UnityEngine.Debug.Log($"Name [{name.networkId}/{name.thingId}] {name.name}");
ProcessName(name);
break;
case ModelUrlMsg modelUrl:
ProcessModelUrl(modelUrl);
break;
case PoseMsg pose:
ProcessPose(pose);
break;
case CustomMsg custom:
ProcessCustom(custom);
break;
case TextMsg text:
ProcessText(text);
break;
case DestroyMsg destroy:
ProcessDestroy(destroy);
break;
default:
return;
}
ForwardMessage(msg);
}
protected virtual void ProcessClient(ClientMsg client) { }
protected virtual void ProcessNetworkId(NetworkIdMsg networkId) { }
protected virtual void ProcessInvestigate(InvestigateMsg investigate) { }
protected virtual void ProcessThing(ThingMsg thing) { }
protected virtual void ProcessName(NameMsg name) { }
protected virtual void ProcessModelUrl(ModelUrlMsg modelUrl) { }
protected virtual void ProcessPose(PoseMsg pose) { }
protected virtual void ProcessCustom(CustomMsg custom) { }
protected virtual void ProcessText(TextMsg text) { }
protected virtual void ProcessDestroy(DestroyMsg destroy) { }
private void ForwardMessage(IMessage msg) {
foreach (Client client in Client.clients) {
if (client == this)
continue;
//UnityEngine.Debug.Log($"---> {client.ipAddress}");
IMessage.SendMsg(client, msg);
}
}
}
}

2
Client.cs.meta Normal file
View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: db9cd79cff119a9438110ead000031c3

173
EchoStream.cs Normal file
View File

@ -0,0 +1,173 @@
using System;
using System.IO;
using System.Threading.Tasks;
using System.Threading;
using System.Collections.Concurrent;
public class EchoStream : Stream {
public override bool CanTimeout { get; } = true;
public override int ReadTimeout { get; set; } = Timeout.Infinite;
public override int WriteTimeout { get; set; } = Timeout.Infinite;
public override bool CanRead { get; } = true;
public override bool CanSeek { get; } = false;
public override bool CanWrite { get; } = true;
public bool CopyBufferOnWrite { get; set; } = false;
private readonly object _lock = new object();
// Default underlying mechanism for BlockingCollection is ConcurrentQueue<T>, which is what we want
private readonly BlockingCollection<byte[]> _Buffers;
private int _maxQueueDepth = 10;
private byte[] m_buffer = null;
private int m_offset = 0;
private int m_count = 0;
private bool m_Closed = false;
private bool m_FinalZero = false; //after the stream is closed, set to true after returning a 0 for read()
public override void Close() {
m_Closed = true;
// release any waiting writes
_Buffers.CompleteAdding();
}
public bool DataAvailable {
get {
return _Buffers.Count > 0;
}
}
private long _Length = 0L;
public override long Length {
get {
return _Length;
}
}
private long _Position = 0L;
public override long Position {
get {
return _Position;
}
set {
throw new NotImplementedException();
}
}
public EchoStream() : this(10) {
}
public EchoStream(int maxQueueDepth) {
_maxQueueDepth = maxQueueDepth;
_Buffers = new BlockingCollection<byte[]>(_maxQueueDepth);
}
// we override the xxxxAsync functions because the default base class shares state between ReadAsync and WriteAsync, which causes a hang if both are called at once
public new Task WriteAsync(byte[] buffer, int offset, int count) {
return Task.Run(() => Write(buffer, offset, count));
}
// we override the xxxxAsync functions because the default base class shares state between ReadAsync and WriteAsync, which causes a hang if both are called at once
public new Task<int> ReadAsync(byte[] buffer, int offset, int count) {
return Task.Run(() => {
return Read(buffer, offset, count);
});
}
public override void Write(byte[] buffer, int offset, int count) {
if (m_Closed || buffer.Length - offset < count || count <= 0)
return;
byte[] newBuffer;
if (!CopyBufferOnWrite && offset == 0 && count == buffer.Length)
newBuffer = buffer;
else {
newBuffer = new byte[count];
System.Buffer.BlockCopy(buffer, offset, newBuffer, 0, count);
}
if (!_Buffers.TryAdd(newBuffer, WriteTimeout))
throw new TimeoutException("EchoStream Write() Timeout");
_Length += count;
}
public override int Read(byte[] buffer, int offset, int count) {
if (count == 0)
return 0;
lock (_lock) {
if (m_count == 0 && _Buffers.Count == 0) {
if (m_Closed) {
if (!m_FinalZero) {
m_FinalZero = true;
return 0;
}
else {
return -1;
}
}
if (_Buffers.TryTake(out m_buffer, ReadTimeout)) {
m_offset = 0;
m_count = m_buffer.Length;
}
else {
if (m_Closed) {
if (!m_FinalZero) {
m_FinalZero = true;
return 0;
}
else {
return -1;
}
}
else {
return 0;
}
}
}
int returnBytes = 0;
while (count > 0) {
if (m_count == 0) {
if (_Buffers.TryTake(out m_buffer, 0)) {
m_offset = 0;
m_count = m_buffer.Length;
}
else
break;
}
var bytesToCopy = (count < m_count) ? count : m_count;
System.Buffer.BlockCopy(m_buffer, m_offset, buffer, offset, bytesToCopy);
m_offset += bytesToCopy;
m_count -= bytesToCopy;
offset += bytesToCopy;
count -= bytesToCopy;
returnBytes += bytesToCopy;
}
_Position += returnBytes;
return returnBytes;
}
}
public override int ReadByte() {
byte[] returnValue = new byte[1];
return (Read(returnValue, 0, 1) <= 0 ? -1 : (int)returnValue[0]);
}
public override void Flush() {
}
public override long Seek(long offset, SeekOrigin origin) {
throw new NotImplementedException();
}
public override void SetLength(long value) {
throw new NotImplementedException();
}
}

2
EchoStream.cs.meta Normal file
View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 422516a56cbf14d46aaa0b1bc09115bf

View File

@ -1,7 +1,14 @@
using Passer;
public class LowLevelMessages {
public static Spherical ReceiveSpherical(byte[] data, ref uint ix) {
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) {
float horizontal = ReceiveAngle8(data, ref ix);
float vertical = ReceiveAngle8(data, ref ix);
float distance = ReceiveFloat16(data, ref ix);
@ -9,7 +16,24 @@ public class LowLevelMessages {
return v;
}
public static Quat32 ReceiveQuat32(byte[] data, ref uint ix) {
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) {
qx = -qx;
qy = -qy;
qz = -qz;
qw = -qw;
}
buffer[ix++] = (byte)qx;
buffer[ix++] = (byte)qy;
buffer[ix++] = (byte)qz;
buffer[ix++] = (byte)qw;
}
public static Quat32 ReceiveQuat32(byte[] data, ref byte ix) {
Quat32 q = new(
(data[ix++] - 128.0F) / 127.0F,
(data[ix++] - 128.0F) / 127.0F,
@ -18,18 +42,31 @@ public class LowLevelMessages {
return q;
}
public static float ReceiveAngle8(byte[] data, ref uint ix) {
public static void SendAngle8(byte[] buffer, ref byte ix, float angle) {
// Normalize angle
while (angle >= 180)
angle -= 360;
while (angle < -180)
angle += 360;
buffer[ix++] = (byte)((angle / 360.0f) * 256.0f);
}
public static float ReceiveAngle8(byte[] data, ref byte ix) {
float value = (data[ix++] * 180) / 128.0F;
return value;
}
public static float ReceiveFloat16(byte[] data, ref uint ix) {
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) {
ushort value = (ushort)(data[ix++] << 8 | data[ix++]);
float16 f16 = new();
f16.SetBinary(value);
float f = f16.toFloat();
return f;
}
}

View File

@ -1,410 +1,541 @@
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.IO;
using System.Net.Sockets;
using System.Threading.Tasks;
namespace Passer.Control
{
public partial class Client
{
//public ConnectionMethod connection;
public UdpClient udpClient;
public string ipAddress;
public int port;
public byte networkId;
public readonly ConcurrentQueue<IMessage> messageQueue = new();
public static Client GetClient(string ipAddress, int port)
{
foreach (Client c in clients)
{
if (c.ipAddress == ipAddress && c.port == port)
return c;
}
return null;
}
static public List<Client> clients = new List<Client>();
public static Client NewClient()
{
Client client = new();
clients.Add(client);
client.networkId = 0;
return client;
}
public static Client NewUDPClient(UdpClient udpClient, string ipAddress, int port)
{
Client client = NewClient();
client.ipAddress = null;
client.port = port;
client.udpClient = udpClient;
return client;
}
}
public class IMessage
{
public IMessage() { }
public IMessage(byte[] data)
{
Deserialize(data);
}
public virtual byte[] Serialize() { return null; }
public virtual void Deserialize(byte[] data) { }
public static bool SendMsg(Client client, IMessage msg)
{
return SendMsg(client, msg.Serialize());
}
public static bool SendMsg(Client client, byte[] data)
{
if (client == null || client.ipAddress == null)
return false;
client.udpClient.Send(data, data.Length, client.ipAddress, client.port);
return true;
}
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)
{
// not all bytes have been read, wait and try again
await Task.Delay(1);
byteCount += dataStream.Read(buffer, byteCount, packetSize - 1 - byteCount);
}
return buffer;
}
}
#region Client
public class ClientMsg : IMessage
{
public const byte Id = 0xA0;
public const byte length = 2;
public byte clientId;
public ClientMsg(byte[] data) : base(data) { }
public override void Deserialize(byte[] data)
{
base.Deserialize(data);
uint ix = 0;
clientId = data[ix++];
}
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)
{
client.networkId = (byte)(Client.clients.Count);
NetworkIdMsg.Send(client);
//if (string.IsNullOrEmpty(sceneUrl) == false)
//SendModelUrl(client, sceneUrl);
}
else if (msg.clientId == 0)
{
NetworkIdMsg.Send(client);
//if (string.IsNullOrEmpty(sceneUrl) == false)
//SendModelUrl(client, sceneUrl);
}
return true;
}
}
#endregion Client
#region Network Id
public class NetworkIdMsg : IMessage
{
public const byte Id = 0xA1;
public const byte length = 2;
public static bool Send(Client client)
{
byte[] data = new byte[NetworkIdMsg.length];
data[0] = NetworkIdMsg.Id;
data[1] = client.networkId;
return SendMsg(client, data);
}
}
#endregion Network Id
#region Thing
public class ThingMsg : IMessage
{
public const byte length = 4;
public const byte Id = 0x80;
public byte thingId;
public byte thingType;
public byte parentId;
public ThingMsg(byte[] data) : base(data) { }
public override void Deserialize(byte[] data)
{
uint ix = 0;
thingId = data[ix++];
thingType = data[ix++];
parentId = data[ix];
}
public static bool Send(Client client, byte networkId, byte thingId, byte thingType)
{
byte[] data = new byte[4];
data[0] = ThingMsg.Id;
data[1] = networkId;
data[2] = thingId;
data[3] = thingType;
data[4] = 0x00; // parent not supported yet
return SendMsg(client, data);
}
public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize)
{
if (packetSize != length)
return false;
byte[] buffer = await Receive(dataStream, packetSize);
ThingMsg msg = new(buffer);
client.messageQueue.Enqueue(msg);
return true;
}
}
#endregion Thing
#region Name
public class NameMsg : IMessage
{
public byte networkId = 0;
public byte objectId;
public byte len;
public string name;
public NameMsg(byte[] data) : base(data) { }
public override void Deserialize(byte[] data)
{
uint ix = 0;
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);
client.messageQueue.Enqueue(msg);
return true;
}
}
#endregion
#region Model URL
public class ModelUrlMsg : IMessage
{
public const byte Id = 0x90; // (144) Model URL
public byte objectId;
public Spherical position;
public float scale;
public string url;
public ModelUrlMsg(string url, float scale = 1)
{
this.url = url;
this.scale = scale;
this.position = Spherical.zero;
}
public ModelUrlMsg(byte[] data) : base(data) { }
public override byte[] Serialize()
{
byte[] data = new byte[this.url.Length + 9];
data[0] = ModelUrlMsg.Id;
data[1] = 0x00; // Thing Id
// data[2]..[5] == position 0, 0, 0
data[6] = 0x3C; // Dummy float16 value 1
data[7] = 0x00;
data[8] = (byte)url.Length;
for (int ix = 0; ix < this.url.Length; ix++)
data[9 + ix] = (byte)url[ix];
return data;
}
public override void Deserialize(byte[] data)
{
uint ix = 0;
this.objectId = data[ix++];
this.position = LowLevelMessages.ReceiveSpherical(data, ref ix);
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 async Task<bool> Receive(Stream dataStream, Client client, byte packetSize)
{
byte[] buffer = await Receive(dataStream, packetSize);
ModelUrlMsg msg = new(buffer);
client.messageQueue.Enqueue(msg);
return true;
}
}
#endregion Model URL
#region Pose
public class PoseMsg : IMessage
{
public const byte Id = 0x10;
public const byte length = 3 + 4 + 4;
public byte thingId;
public byte poseType;
public Spherical position;
public Quat32 orientation;
public PoseMsg(byte[] data) : base(data) { }
public override void Deserialize(byte[] data)
{
uint ix = 0;
thingId = data[ix++];
poseType = data[ix++];
//if ((poseType & Pose_Position) != 0)
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;
byte[] buffer = await Receive(dataStream, packetSize);
PoseMsg msg = new(buffer);
client.messageQueue.Enqueue(msg);
return true;
}
}
#endregion Pose
#region Bytes
public class BytesMsg : IMessage
{
public const byte Id = 0xB1;
public byte networkId;
public byte thingId;
public byte[] bytes;
public BytesMsg(byte[] data) : base(data) { }
public BytesMsg(byte networkId, byte thingId, byte[] bytes) : base()
{
this.networkId = networkId;
this.thingId = thingId;
this.bytes = bytes;
}
public override byte[] Serialize()
{
byte[] buffer = new byte[4 + this.bytes.Length];
int ix = 0;
buffer[ix++] = BytesMsg.Id;
buffer[ix++] = this.networkId;
buffer[ix++] = this.thingId;
buffer[ix++] = (byte)bytes.Length;
foreach (byte b in bytes)
buffer[ix++] = b;
return buffer;
}
public override void Deserialize(byte[] data)
{
//this.bytes = data;
uint ix = 0;
this.thingId = 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);
}
// received bytes
public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize)
{
byte[] buffer = await Receive(dataStream, packetSize);
BytesMsg msg = new(buffer);
client.messageQueue.Enqueue(msg);
return true;
}
}
#endregion Bytes
#region Destroy
public class DestroyMsg : IMessage
{
public const byte length = 2;
public byte objectId;
public DestroyMsg(byte[] data) : base(data) { }
public override void Deserialize(byte[] data)
{
objectId = data[0];
}
public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize)
{
if (packetSize != length)
return false;
byte[] buffer = await Receive(dataStream, packetSize);
DestroyMsg msg = new(buffer);
client.messageQueue.Enqueue(msg);
return true;
}
}
#endregion Destroy
namespace Passer.Control {
public class IMessage {
public IMessage() { }
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) {
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 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 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) {
// not all bytes have been read, wait and try again
await Task.Delay(1);
byteCount += dataStream.Read(buffer, byteCount, packetSize - 1 - byteCount);
}
return buffer;
}
}
#region Client
public class ClientMsg : IMessage {
public const byte Id = 0xA0;
public const byte length = 2;
public 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 void Deserialize(byte[] buffer) {
base.Deserialize(buffer);
uint ix = 0;
networkId = buffer[ix];
}
public static bool Send(Client client, byte networkId) {
ClientMsg msg = new(networkId);
return SendMsg(client, msg);
}
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) {
if (packetSize != length)
return false;
byte[] buffer = await Receive(dataStream, packetSize);
ClientMsg msg = new(buffer);
if (client.networkId == 0) {
client.networkId = (byte)(Client.clients.Count);
NetworkIdMsg.Send(client, client.networkId);
client.messageQueue.Enqueue(msg);
}
else if (msg.networkId == 0) {
NetworkIdMsg.Send(client, client.networkId);
client.messageQueue.Enqueue(msg);
}
return true;
}
}
#endregion Client
#region Network Id
public class NetworkIdMsg : IMessage {
public const byte Id = 0xA1;
public const byte length = 2;
public 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 void Deserialize(byte[] buffer) {
uint ix = 0;
this.networkId = buffer[ix];
}
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) {
if (packetSize != length)
return false;
byte[] buffer = await Receive(dataStream, packetSize);
NetworkIdMsg msg = new(buffer);
client.messageQueue.Enqueue(msg);
return true;
}
}
#endregion Network Id
#region Investigate
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) {
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 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}]");
return SendMsg(client, msg);
}
public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize) {
if (packetSize != length)
return false;
byte[] buffer = await Receive(dataStream, packetSize);
InvestigateMsg msg = new(buffer);
//UnityEngine.Debug.Log($"Receive investigate [{msg.networkId}/{msg.thingId}]");
client.messageQueue.Enqueue(msg);
return true;
}
}
#endregion Investigate
#region Thing
public class ThingMsg : IMessage {
public const byte length = 5;
public const byte Id = 0x80;
public byte networkId;
public byte thingId;
public byte thingType;
public byte parentId;
public ThingMsg(byte networkId, byte thingId, byte thingType, byte parentId) {
this.networkId = networkId;
this.thingId = thingId;
this.thingType = thingType;
this.parentId = parentId;
}
public ThingMsg(byte[] buffer) : base(buffer) { }
public override byte[] Serialize() {
byte[] buffer = new byte[ThingMsg.length];
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;
}
public override void Deserialize(byte[] buffer) {
uint ix = 0;
this.networkId = buffer[ix++];
this.thingId = buffer[ix++];
this.thingType = buffer[ix++];
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}]");
return SendMsg(client, msg);
}
public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize) {
if (packetSize != length)
return false;
byte[] buffer = await Receive(dataStream, packetSize);
ThingMsg msg = new(buffer);
//UnityEngine.Debug.Log($"Receive thing [{msg.networkId}/{msg.thingId}]");
// Do no process poses with nwid == 0 (== local)
//if (msg.networkId == 0)
// return true;
client.messageQueue.Enqueue(msg);
return true;
}
}
#endregion Thing
#region Name
public class NameMsg : IMessage {
public const byte Id = 0x91; // 145
public const byte length = 4;
public byte networkId;
public byte thingId;
public byte len;
public 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];
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;
}
public override void Deserialize(byte[] buffer) {
byte ix = 0;
this.networkId = buffer[ix++];
this.thingId = buffer[ix++];
int strlen = buffer[ix++];
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);
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);
client.messageQueue.Enqueue(msg);
return true;
}
}
#endregion
#region Model URL
public class ModelUrlMsg : IMessage {
public const byte Id = 0x90; // (144) Model URL
public byte networkId;
public byte thingId;
public Spherical position;
public float scale;
public string url;
public ModelUrlMsg(byte networkId, byte thingId, string url, float scale = 1) {
this.networkId = networkId;
this.thingId = thingId;
this.url = url;
this.scale = scale;
this.position = Spherical.zero;
}
public ModelUrlMsg(byte[] buffer) : base(buffer) { }
public override byte[] Serialize() {
byte[] buffer = new byte[this.url.Length + 6];
byte ix = 0;
buffer[ix++] = ModelUrlMsg.Id;
buffer[ix++] = this.networkId;
buffer[ix++] = this.thingId; // Thing Id
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;
}
public override void Deserialize(byte[] buffer) {
byte ix = 0;
this.networkId = buffer[ix++];
this.thingId = buffer[ix++];
this.scale = LowLevelMessages.ReceiveFloat16(buffer, ref ix);
int strlen = buffer[ix++];
url = System.Text.Encoding.UTF8.GetString(buffer, (int)ix, strlen);
}
public static bool Send(Client client, byte networkId, byte thingId, string modelUrl) {
ModelUrlMsg msg = new(networkId, thingId, modelUrl);
return SendMsg(client, msg);
}
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);
return true;
}
}
#endregion Model URL
#region Pose
public class PoseMsg : IMessage {
public const byte Id = 0x10;
public const byte length = 4 + 4 + 4;
public byte networkId;
public byte thingId;
public byte poseType;
public const byte Pose_Position = 0x01;
public const byte Pose_Orientation = 0x02;
public Spherical position;
public Quat32 orientation;
public PoseMsg(byte networkId, byte thingId, Spherical position, Quat32 orientation) {
this.networkId = networkId;
this.thingId = thingId;
this.position = position;
this.orientation = orientation;
this.poseType = 0;
if (this.position != null)
this.poseType |= Pose_Position;
else
this.position = new Spherical(0, 0, 0);
if (this.orientation != null)
this.poseType |= Pose_Orientation;
else
this.orientation = new Quat32(0, 0, 0, 1);
}
public PoseMsg(byte[] buffer) : base(buffer) { }
public override byte[] Serialize() {
byte[] buffer = new byte[PoseMsg.length];
byte ix = 0;
buffer[ix++] = PoseMsg.Id;
buffer[ix++] = this.networkId;
buffer[ix++] = this.thingId;
buffer[ix++] = this.poseType;
LowLevelMessages.SendSpherical(buffer, ref ix, this.position);
LowLevelMessages.SendQuat32(buffer, ref ix, this.orientation);
return buffer;
}
public override void Deserialize(byte[] buffer) {
byte ix = 0;
this.networkId = buffer[ix++];
this.thingId = buffer[ix++];
this.poseType = buffer[ix++];
//if ((poseType & Pose_Position) != 0)
this.position = LowLevelMessages.ReceiveSpherical(buffer, ref ix);
//if ((poseType & Pose_Orientation) != 0) {
this.orientation = LowLevelMessages.ReceiveQuat32(buffer, ref ix);
}
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) {
if (packetSize != length)
return false;
byte[] buffer = await Receive(dataStream, packetSize);
PoseMsg msg = new(buffer);
// Do no process poses with nwid == 0 (== local)
if (msg.networkId == 0)
return true;
client.messageQueue.Enqueue(msg);
return true;
}
}
#endregion Pose
#region Custom
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() {
this.networkId = networkId;
this.thingId = thingId;
this.bytes = bytes;
}
public override byte[] Serialize() {
byte[] buffer = new byte[3 + this.bytes.Length];
byte ix = 0;
buffer[ix++] = CustomMsg.Id;
buffer[ix++] = this.networkId;
buffer[ix++] = this.thingId;
//buffer[ix++] = (byte)bytes.Length;
foreach (byte b in bytes)
buffer[ix++] = b;
return buffer;
}
public override void Deserialize(byte[] buffer) {
byte ix = 0;
this.networkId = buffer[ix++];
this.thingId = buffer[ix++];
byte length = (byte)(buffer.Length - ix);//buffer[ix++];
this.bytes = new byte[length];
for (uint bytesIx = 0; bytesIx < length; bytesIx++)
this.bytes[bytesIx] = buffer[ix++];
}
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) {
byte[] buffer = await Receive(dataStream, packetSize);
CustomMsg msg = new(buffer);
client.messageQueue.Enqueue(msg);
return true;
}
}
#endregion Custom
#region Text
public class TextMsg : IMessage {
public const byte Id = 0xB0;
public string text;
public TextMsg(byte[] buffer) : base(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) {
byte[] buffer = await Receive(dataStream, packetSize);
TextMsg msg = new(buffer);
client.messageQueue.Enqueue(msg);
return true;
}
}
#endregion
#region Destroy
public class DestroyMsg : IMessage {
public const byte Id = 0x20;
public const byte length = 2;
public byte networkId;
public byte thingId;
public DestroyMsg(byte[] buffer) : base(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) {
if (packetSize != length)
return false;
byte[] buffer = await Receive(dataStream, packetSize);
DestroyMsg msg = new(buffer);
client.messageQueue.Enqueue(msg);
return true;
}
}
#endregion Destroy
}

66
SiteServer.cs Normal file
View File

@ -0,0 +1,66 @@
using System.IO;
using System.Threading.Tasks;
namespace Passer.Control {
public static class SiteServer {
public static async Task ReceiveData(Stream dataStream, Client client) {
while (true) {
byte packetSize = (byte)dataStream.ReadByte();
if (packetSize != 0xFF)
await ReceiveData(dataStream, client, packetSize);
// else timeout
}
}
public static async Task ReceiveData(Stream dataStream, Client client, byte packetSize) {
byte msgId = (byte)dataStream.ReadByte();
if (msgId == 0xFF) {
// Timeout
return;
}
//UnityEngine.Debug.Log($"R {msgId} from {client.ipAddress}");
bool result = false;
switch (msgId) {
case ClientMsg.Id: // 0xA0 / 160
result = await ClientMsg.Receive(dataStream, client, packetSize);
break;
case NetworkIdMsg.Id: // 0xA1 / 161
result = await NetworkIdMsg.Receive(dataStream, client, packetSize);
break;
case InvestigateMsg.Id: // 0x81
result = await InvestigateMsg.Receive(dataStream, client, packetSize);
break;
case ThingMsg.Id: // 0x80 / 128
result = await ThingMsg.Receive(dataStream, client, packetSize);
break;
case NameMsg.Id: // 0x91 / 145
result = await NameMsg.Receive(dataStream, client, packetSize);
break;
case ModelUrlMsg.Id: // 0x90 / 144
result = await ModelUrlMsg.Receive(dataStream, client, packetSize);
break;
case PoseMsg.Id: // 0x10 / 16
result = await PoseMsg.Receive(dataStream, client, packetSize);
break;
case CustomMsg.Id: // 0xB1 / 177
result = await CustomMsg.Receive(dataStream, client, packetSize);
break;
case TextMsg.Id: // 0xB0 / 176
result = await TextMsg.Receive(dataStream, client, packetSize);
break;
case DestroyMsg.Id: // 0x20 / 32
result = await DestroyMsg.Receive(dataStream, client, packetSize);
break;
default:
break;
}
if (result == false) {
packetSize = msgId; // skip 1 byte, msgId is possibly a packet size byte
await ReceiveData(dataStream, client, packetSize);
}
}
}
}

2
SiteServer.cs.meta Normal file
View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 53345abb9310d344baa67c19a8d8249f