Client->Participant

Custom msg is working now
This commit is contained in:
Pascal Serrarens 2024-12-16 12:48:59 +01:00
parent b34c536c68
commit 9b44918eaf
7 changed files with 65 additions and 51 deletions

View File

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

View File

@ -64,6 +64,12 @@ public class LowLevelMessages
return value;
}
public static void SendFloat16(byte[] data, ref byte ix, float f) {
float16 f16 = new(f);
ushort binary = f16.GetBinary();
data[ix++] = (byte)(binary >> 8);
data[ix++] = (byte)(binary & 255);
}
public static void SendFloat16(byte[] data, ref byte ix, float16 f)
{
ushort binary = f.GetBinary();

View File

@ -16,13 +16,18 @@ namespace Passer.Control
public virtual byte Serialize(ref byte[] buffer) { return 0; }
public virtual void Deserialize(byte[] buffer) { }
public static bool SendMsg(Client client, IMessage msg)
public bool Send(Participant client) {
Serialize(ref client.buffer);
return client.SendBuffer();
}
public static bool SendMsg(Participant client, IMessage msg)
{
msg.Serialize(ref client.buffer);
return client.SendBuffer();
}
public static bool PublishMsg(Client client, IMessage msg)
public static bool PublishMsg(Participant client, IMessage msg)
{
msg.Serialize(ref client.buffer);
return client.PublishBuffer();
@ -70,17 +75,17 @@ namespace Passer.Control
networkId = buffer[ix];
}
public static bool Send(Client client, byte networkId)
public static bool Send(Participant client, byte networkId)
{
ClientMsg msg = new(networkId);
return SendMsg(client, msg);
}
public static bool Publish(Client client, byte networkId)
public static bool Publish(Participant 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, Participant client, byte packetSize)
{
if (packetSize != length)
return false;
@ -90,7 +95,7 @@ namespace Passer.Control
if (client.networkId == 0)
{
client.networkId = (byte)(Client.clients.Count);
client.networkId = (byte)(Participant.clients.Count);
NetworkIdMsg.Send(client, client.networkId);
client.messageQueue.Enqueue(msg);
}
@ -133,12 +138,12 @@ namespace Passer.Control
this.networkId = buffer[ix];
}
public static bool Send(Client client, byte networkId)
public static bool Send(Participant 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, Participant client, byte packetSize)
{
if (packetSize != length)
return false;
@ -183,11 +188,11 @@ namespace Passer.Control
this.thingId = buffer[ix++];
}
public static bool Send(Client client, CoreThing thing) {
public static bool Send(Participant 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, Participant client, byte packetSize)
{
if (packetSize != length)
return false;
@ -243,7 +248,7 @@ namespace Passer.Control
this.parentId = buffer[ix];
}
public static bool Send(Client client, CoreThing thing) {
public static bool Send(Participant client, CoreThing thing) {
ThingMsg msg = new(thing.networkId, thing.id, thing.type, thing.parent.id);
return SendMsg(client, msg);
}
@ -253,7 +258,7 @@ namespace Passer.Control
// //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, Participant client, byte packetSize)
{
if (packetSize != length)
return false;
@ -312,7 +317,7 @@ namespace Passer.Control
this.name = System.Text.Encoding.UTF8.GetString(buffer, (int)ix, strlen);
}
public static bool Send(Client client, CoreThing thing) {
public static bool Send(Participant client, CoreThing thing) {
if (string.IsNullOrEmpty(thing.name))
return true; // nothing sent, but still a success!
@ -324,7 +329,7 @@ namespace Passer.Control
// 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, Participant client, byte packetSize)
{
byte[] buffer = await Receive(dataStream, packetSize);
NameMsg msg = new(buffer);
@ -380,14 +385,14 @@ namespace Passer.Control
url = System.Text.Encoding.UTF8.GetString(buffer, (int)ix, strlen);
}
public static bool Send(Client client, CoreThing thing) {
public static bool Send(Participant 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)
public static bool Send(Participant client, byte networkId, byte thingId, string modelUrl)
{
if (string.IsNullOrEmpty(modelUrl))
return true; // nothing sent, but still a success!
@ -395,7 +400,7 @@ namespace Passer.Control
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, Participant client, byte packetSize)
{
byte[] buffer = await Receive(dataStream, packetSize);
ModelUrlMsg msg = new(buffer);
@ -465,12 +470,12 @@ 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(Participant 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, Participant client, byte packetSize)
{
if (packetSize != length)
return false;
@ -498,7 +503,15 @@ namespace Passer.Control
public byte thingId;
public byte[] bytes;
public CustomMsg(byte[] buffer) : base(buffer) { }
public CustomMsg(byte[] buffer) {
byte ix = 0;
this.networkId = buffer[ix++];
this.thingId = buffer[ix++];
byte length = (byte)(buffer.Length - ix);
this.bytes = new byte[length];
for (uint bytesIx = 0; bytesIx < length; bytesIx++)
this.bytes[bytesIx] = buffer[ix++];
}
public CustomMsg(byte networkId, byte thingId, byte[] bytes) : base()
{
this.networkId = networkId;
@ -518,25 +531,11 @@ namespace Passer.Control
return ix;
}
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)
public static async Task<bool> Receive(Stream dataStream, Participant client, byte packetSize)
{
byte[] buffer = await Receive(dataStream, packetSize);
CustomMsg msg = new(buffer);
client.messageQueue.Enqueue(msg);
return true;
@ -560,7 +559,7 @@ namespace Passer.Control
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, Participant client, byte packetSize)
{
byte[] buffer = await Receive(dataStream, packetSize);
TextMsg msg = new(buffer);
@ -589,7 +588,7 @@ namespace Passer.Control
this.thingId = buffer[1];
}
public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize)
public static async Task<bool> Receive(Stream dataStream, Participant client, byte packetSize)
{
if (packetSize != length)
return false;

View File

@ -6,7 +6,7 @@ using System.IO;
namespace Passer.Control
{
public class Client
public class Participant
{
//public ConnectionMethod connection;
public UdpClient udpClient;
@ -21,18 +21,18 @@ namespace Passer.Control
public readonly ConcurrentQueue<IMessage> messageQueue = new();
public static Client GetClient(string ipAddress, int port)
public static Participant GetClient(string ipAddress, int port)
{
foreach (Client c in clients)
foreach (Participant c in clients)
{
if (c.ipAddress == ipAddress && c.port == port)
return c;
}
return null;
}
static public List<Client> clients = new List<Client>();
static public List<Participant> clients = new List<Participant>();
public Client(UdpClient udpClient, int port)
public Participant(UdpClient udpClient, int port)
{
this.udpClient = udpClient;
this.ipAddress = null;
@ -129,7 +129,7 @@ namespace Passer.Control
private void ForwardMessage(IMessage msg)
{
foreach (Client client in Client.clients)
foreach (Participant client in Participant.clients)
{
if (client == this)
continue;

11
Participant.cs.meta Normal file
View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 302ed9b89a108a4429ea274044424a03
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -5,7 +5,7 @@ namespace Passer.Control {
public static class SiteServer {
public static async Task ReceiveData(Stream dataStream, Client client) {
public static async Task ReceiveData(Stream dataStream, Participant client) {
while (true) {
byte packetSize = (byte)dataStream.ReadByte();
if (packetSize != 0xFF)
@ -14,7 +14,7 @@ namespace Passer.Control {
}
}
public static async Task ReceiveData(Stream dataStream, Client client, byte packetSize) {
public static async Task ReceiveData(Stream dataStream, Participant client, byte packetSize) {
byte msgId = (byte)dataStream.ReadByte();
if (msgId == 0xFF) {
// Timeout

View File

@ -5,7 +5,7 @@ namespace Passer.Control
public class CoreThing
{
public Client client;
public Participant participant;
public byte networkId;
public byte id;
public CoreThing parent;
@ -18,9 +18,9 @@ namespace Passer.Control
{
}
public CoreThing(Client client, byte networkId, byte thingId, byte thingType = 0)
public CoreThing(Participant client, byte networkId, byte thingId, byte thingType = 0)
{
this.client = client;
this.participant = client;
this.id = thingId;
this.type = thingType;
this.networkId = networkId;