From 8357c5d6231947a3ecd32a81b723506640fff441 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Fri, 11 Apr 2025 11:54:54 +0200 Subject: [PATCH] Improved debug logging --- src/Messages/BinaryMsg.cs | 4 ++- src/Messages/ModelUrlMsg.cs | 23 +++++++++----- src/Messages/NameMsg.cs | 3 ++ src/Messages/NetworkIdMsg.cs | 5 +++- src/Messages/TextMsg.cs | 15 ++++++++-- src/Messages/ThingMsg.cs | 3 ++ src/ParticipantUDP.cs | 58 +++++++++++++++++++++++++++--------- 7 files changed, 86 insertions(+), 25 deletions(-) diff --git a/src/Messages/BinaryMsg.cs b/src/Messages/BinaryMsg.cs index 408ba94..d9ab9f2 100644 --- a/src/Messages/BinaryMsg.cs +++ b/src/Messages/BinaryMsg.cs @@ -60,7 +60,9 @@ namespace RoboidControl { if (buffer.Length < BinaryMsg.length + this.data.Length || this.data.Length == 0) return 0; - System.Console.Write($"sending Binary [{this.networkId}/{this.thingId}]"); +#if DEBUG + System.Console.WriteLine($"Send BinaryMsg [{this.networkId}/{this.thingId}] {this.dataLength}"); +#endif byte ix = 0; buffer[ix++] = BinaryMsg.Id; buffer[ix++] = this.networkId; diff --git a/src/Messages/ModelUrlMsg.cs b/src/Messages/ModelUrlMsg.cs index 3a2d3fc..64efac5 100644 --- a/src/Messages/ModelUrlMsg.cs +++ b/src/Messages/ModelUrlMsg.cs @@ -21,6 +21,10 @@ namespace RoboidControl { /// public byte thingId; /// + /// The length of the url string, excluding the null terminator + /// + public byte urlLength; + /// /// The URL of the model /// public string url = null; @@ -33,6 +37,7 @@ namespace RoboidControl { public ModelUrlMsg(byte networkId, Thing thing) { this.networkId = networkId; this.thingId = thing.id; + this.urlLength = (byte)thing.modelUrl.Length; this.url = thing.modelUrl; } /// @@ -41,19 +46,20 @@ namespace RoboidControl { /// The network ID of the thing /// The ID of the thing /// The URL to send - public ModelUrlMsg(byte networkId, byte thingId, string url) { - this.networkId = networkId; - this.thingId = thingId; - this.url = url; - } + // public ModelUrlMsg(byte networkId, byte thingId, string url) { + // this.networkId = networkId; + // this.thingId = thingId; + // this.urlLength = (byte)url.Length; + // this.url = url; + // } /// @copydoc Passer::RoboidControl::IMessage::IMessage(byte[] buffer) public ModelUrlMsg(byte[] buffer) { byte ix = 1; this.networkId = buffer[ix++]; this.thingId = buffer[ix++]; - int strlen = buffer[ix++]; - url = System.Text.Encoding.UTF8.GetString(buffer, (int)ix, strlen); + this.urlLength = buffer[ix++]; + this.url = System.Text.Encoding.UTF8.GetString(buffer, (int)ix, this.urlLength); } /// @copydoc Passer::RoboidControl::IMessage::Serialize @@ -61,6 +67,9 @@ namespace RoboidControl { if (string.IsNullOrEmpty(this.url)) return 0; +#if DEBUG + System.Console.WriteLine($"Send ModelUrlMsg [{this.networkId}/{this.thingId}] {this.urlLength} {this.url}"); +#endif byte ix = 0; buffer[ix++] = ModelUrlMsg.Id; buffer[ix++] = this.networkId; diff --git a/src/Messages/NameMsg.cs b/src/Messages/NameMsg.cs index c1b5faa..04bd745 100644 --- a/src/Messages/NameMsg.cs +++ b/src/Messages/NameMsg.cs @@ -66,6 +66,9 @@ namespace RoboidControl { if (buffer.Length < NameMsg.length + this.name.Length || string.IsNullOrEmpty(this.name)) return 0; +#if DEBUG + System.Console.WriteLine($"Send NameMsg [{this.networkId}/{this.thingId}] {this.nameLength} {this.name}"); +#endif byte ix = 0; buffer[ix++] = NameMsg.Id; buffer[ix++] = this.networkId; diff --git a/src/Messages/NetworkIdMsg.cs b/src/Messages/NetworkIdMsg.cs index 8d81e27..ed51018 100644 --- a/src/Messages/NetworkIdMsg.cs +++ b/src/Messages/NetworkIdMsg.cs @@ -33,7 +33,10 @@ namespace RoboidControl { public override byte Serialize(ref byte[] buffer) { if (buffer.Length < NetworkIdMsg.length) return 0; - + +#if DEBUG + System.Console.WriteLine($"Send NetworkIdMsg {this.networkId}"); +#endif buffer[0] = NetworkIdMsg.Id; buffer[1] = this.networkId; return NetworkIdMsg.length; diff --git a/src/Messages/TextMsg.cs b/src/Messages/TextMsg.cs index a1de990..6fdfc0c 100644 --- a/src/Messages/TextMsg.cs +++ b/src/Messages/TextMsg.cs @@ -13,6 +13,10 @@ namespace RoboidControl { /// public const byte length = 2; /// + /// The length of the text without the null terminator + /// + public byte textLength; + /// /// The text /// public string text = ""; @@ -22,20 +26,27 @@ namespace RoboidControl { /// /// The text to send public TextMsg(string text) { + this.textLength = (byte)text.Length; this.text = text; } /// @copydoc Passer::RoboidControl::IMessage::IMessage(byte[] buffer) - public TextMsg(byte[] buffer) : base(buffer) { } + public TextMsg(byte[] buffer) : base(buffer) { + this.textLength = buffer[0]; + this.text = System.Text.Encoding.UTF8.GetString(buffer, 1, this.textLength); + } /// @copydoc Passer::RoboidControl::IMessage::Serialize public override byte Serialize(ref byte[] buffer) { if (buffer.Length < TextMsg.length + this.text.Length || this.text.Length == 0) return 0; +#if DEBUG + System.Console.WriteLine($"Send TextMsg {this.textLength} {this.text}"); +#endif byte ix = 0; buffer[ix++] = TextMsg.Id; - buffer[ix++] = (byte)this.text.Length; + buffer[ix++] = this.textLength; for (int textIx = 0; textIx < this.text.Length; textIx++) buffer[ix++] = (byte)this.text[textIx]; return ix; diff --git a/src/Messages/ThingMsg.cs b/src/Messages/ThingMsg.cs index 9725e23..bf93a75 100644 --- a/src/Messages/ThingMsg.cs +++ b/src/Messages/ThingMsg.cs @@ -77,6 +77,9 @@ namespace RoboidControl { if (buffer.Length < ThingMsg.length) return 0; +#if DEBUG + System.Console.WriteLine($"Send ThingMsg [{this.networkId}/{this.thingId}] {this.thingType} {this.parentId}"); +#endif byte ix = 0; buffer[ix++] = ThingMsg.id; buffer[ix++] = this.networkId; diff --git a/src/ParticipantUDP.cs b/src/ParticipantUDP.cs index 9021123..01071b5 100644 --- a/src/ParticipantUDP.cs +++ b/src/ParticipantUDP.cs @@ -189,7 +189,7 @@ namespace RoboidControl { return true; IPEndPoint participantEndpoint = new IPEndPoint(IPAddress.Parse(owner.ipAddress), owner.port); - Console.WriteLine($"msg to {participantEndpoint.Address.ToString()} {participantEndpoint.Port}"); + // Console.WriteLine($"msg to {participantEndpoint.Address.ToString()} {participantEndpoint.Port}"); this.udpClient?.Send(this.buffer, bufferSize, participantEndpoint); return true; } @@ -241,7 +241,6 @@ namespace RoboidControl { return; } - System.Console.Write($"Receive msg {msgId}"); switch (msgId) { case ParticipantMsg.Id: // 0xA0 / 160 this.Process(sender, new ParticipantMsg(data)); @@ -284,11 +283,16 @@ namespace RoboidControl { #region Process protected virtual void Process(Participant sender, ParticipantMsg msg) { - Console.WriteLine($"{this.name} Process participant {msg.networkId}"); +#if DEBUG + Console.WriteLine($"{this.name} Process participantMsg {msg.networkId}"); +#endif } protected virtual void Process(Participant sender, NetworkIdMsg msg) { - Console.WriteLine($"{this.name} Process network id {this.networkId} {msg.networkId}"); +#if DEBUG + Console.WriteLine($"{this.name} Process SiteMsg {this.networkId} -> {msg.networkId}"); +#endif + if (this.networkId != msg.networkId) { this.networkId = msg.networkId; foreach (Thing thing in this.things) //Thing.GetAllThings()) @@ -296,34 +300,48 @@ namespace RoboidControl { } } - protected virtual void Process(Participant sender, InvestigateMsg msg) { } + protected virtual void Process(Participant sender, InvestigateMsg msg) { +#if DEBUG + Console.WriteLine($"Participant: InvestigateMsg [{msg.networkId}/{msg.thingId}]"); +#endif + } protected virtual void Process(Participant sender, ThingMsg msg) { - Console.WriteLine($"Participant: Process thing [{msg.networkId}/{msg.thingId}]"); +#if DEBUG + Console.WriteLine($"Participant: Process ThingMsg [{msg.networkId}/{msg.thingId}] {msg.thingType} {msg.parentId}"); +#endif } protected virtual void Process(Participant sender, NameMsg msg) { - Console.WriteLine($"Participant: Process name [{msg.networkId}/{msg.thingId}] {msg.name}"); +#if DEBUG + Console.WriteLine($"Participant: Process NameMsg [{msg.networkId}/{msg.thingId}] {msg.nameLength} {msg.name}"); +#endif + Thing thing = sender.Get(msg.networkId, msg.thingId); if (thing != null) thing.name = msg.name; } protected virtual void Process(Participant sender, ModelUrlMsg msg) { - Console.WriteLine($"Participant: Process model [{msg.networkId}/{msg.thingId}] {msg.url}"); +#if DEBUG + Console.WriteLine($"Participant: Process ModelUrlMsg [{msg.networkId}/{msg.thingId}] {msg.urlLength} {msg.url}"); +#endif + Thing thing = sender.Get(msg.networkId, msg.thingId); if (thing != null) thing.modelUrl = msg.url; } protected virtual void Process(Participant sender, PoseMsg msg) { - // Console.WriteLine($"Participant: Process pose [{msg.networkId}/{msg.thingId}] {msg.poseType}"); +#if DEBUG + Console.WriteLine($"Participant: Process PoseMsg [{msg.networkId}/{msg.thingId}] {msg.poseType}"); +#endif Thing thing = sender.Get(msg.networkId, msg.thingId); if (thing != null) { if ((msg.poseType & PoseMsg.Pose_Position) != 0) - thing.position = msg.position; + thing.position = msg.position; if ((msg.poseType & PoseMsg.Pose_Orientation) != 0) - thing.orientation = msg.orientation; + thing.orientation = msg.orientation; if ((msg.poseType & PoseMsg.Pose_LinearVelocity) != 0) thing.linearVelocity = msg.linearVelocity; if ((msg.poseType & PoseMsg.Pose_AngularVelocity) != 0) @@ -332,14 +350,26 @@ namespace RoboidControl { } protected virtual void Process(Participant sender, BinaryMsg msg) { - Console.WriteLine($"Participant: Process binary [{msg.networkId}/{msg.thingId}]"); +#if DEBUG + Console.WriteLine($"Participant: Process BinaryMsg [{msg.networkId}/{msg.thingId}] {msg.dataLength}"); +#endif Thing thing = sender.Get(msg.networkId, msg.thingId); thing?.ProcessBinary(msg.data); } - protected virtual void Process(Participant sender, TextMsg temsgxt) { } + protected virtual void Process(Participant sender, TextMsg msg) { +#if DEBUG + Console.WriteLine($"Participant: Process TextMsg {msg.textLength} {msg.text}"); +#endif - protected virtual void Process(Participant sender, DestroyMsg msg) { } + } + + protected virtual void Process(Participant sender, DestroyMsg msg) { +#if DEBUG + Console.WriteLine($"Participant: Process ThingMsg [{msg.networkId}/{msg.thingId}]"); +#endif + + } private void ForwardMessage(IMessage msg) { // foreach (Participant client in senders) {