From 0f0fcfdfbf58244a7878449cabc920a8467d9e50 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Wed, 11 Dec 2024 11:29:01 +0100 Subject: [PATCH] Improved reloading support --- Client.cs | 6 ++++-- Messages.cs | 26 +++++++++++++++++--------- SiteServer.cs | 1 + 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/Client.cs b/Client.cs index 26ad994..e54583e 100644 --- a/Client.cs +++ b/Client.cs @@ -47,6 +47,7 @@ namespace Passer.Control { ProcessNetworkId(networkId); break; case InvestigateMsg investigate: + UnityEngine.Debug.Log($"investigate [{investigate.networkId}/{investigate.thingId}]"); ProcessInvestigate(investigate); break; case ThingMsg thing: @@ -96,11 +97,12 @@ namespace Passer.Control { protected virtual void ProcessDestroy(DestroyMsg destroy) { } - private void ForwardMessage(IMessage thing) { + private void ForwardMessage(IMessage msg) { foreach (Client client in Client.clients) { if (client == this) continue; - IMessage.SendMsg(client, thing); + UnityEngine.Debug.Log($"---> {client.ipAddress}"); + IMessage.SendMsg(client, msg); } } } diff --git a/Messages.cs b/Messages.cs index ecf47f9..c0e852b 100644 --- a/Messages.cs +++ b/Messages.cs @@ -19,6 +19,7 @@ namespace Passer.Control { 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; } @@ -168,8 +169,8 @@ namespace Passer.Control { this.thingId = buffer[ix++]; } - public static bool Send(Client client, byte thingId) { - InvestigateMsg msg = new(client.networkId, thingId); + public static bool Send(Client client, byte networkId, byte thingId) { + InvestigateMsg msg = new(networkId, thingId); return SendMsg(client, msg); } public static async Task Receive(Stream dataStream, Client client, byte packetSize) { @@ -232,6 +233,10 @@ namespace Passer.Control { byte[] buffer = await Receive(dataStream, packetSize); ThingMsg msg = new(buffer); + // Do no process poses with nwid == 0 (== local) + if (msg.networkId == 0) + return true; + client.messageQueue.Enqueue(msg); return true; } @@ -406,13 +411,16 @@ namespace Passer.Control { return SendMsg(client, msg); } public static async Task Receive(Stream dataStream, Client client, byte packetSize) { - UnityEngine.Debug.Log("Receive pose"); 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; } @@ -436,25 +444,25 @@ namespace Passer.Control { } public override byte[] Serialize() { - byte[] buffer = new byte[4 + this.bytes.Length]; + 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; + //buffer[ix++] = (byte)bytes.Length; foreach (byte b in bytes) buffer[ix++] = b; return buffer; } public override void Deserialize(byte[] buffer) { - uint ix = 0; + byte ix = 0; this.networkId = buffer[ix++]; this.thingId = buffer[ix++]; - byte length = buffer[ix++]; + byte length = (byte)(buffer.Length - ix);//buffer[ix++]; this.bytes = new byte[length]; - for (uint bytesIx = 0; ix < length; ix++, bytesIx++) - this.bytes[bytesIx] = buffer[ix]; + for (uint bytesIx = 0; bytesIx < length; bytesIx++) + this.bytes[bytesIx] = buffer[ix++]; } public static void Send(Client client, byte thingId, byte[] bytes) { diff --git a/SiteServer.cs b/SiteServer.cs index a563bf5..57460f3 100644 --- a/SiteServer.cs +++ b/SiteServer.cs @@ -21,6 +21,7 @@ namespace Passer.Control { return; } + //UnityEngine.Debug.Log($"R {msgId} from {client.ipAddress}"); bool result = false; switch (msgId) { case ClientMsg.Id: // 0xA0 / 160