From 3725980462c8f95e9501562435a1cc5c530bc201 Mon Sep 17 00:00:00 2001 From: Alex Johnson Date: Thu, 4 Mar 2021 11:02:19 -0500 Subject: [PATCH 1/4] Adds better stream debugging for SendText methods --- Pepperdash Core/Pepperdash Core/Comm/GenericSshClient.cs | 2 +- Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs | 2 +- Pepperdash Core/Pepperdash Core/Comm/GenericUdpServer.cs | 2 +- Pepperdash Core/Pepperdash Core/CommunicationExtras.cs | 7 ++++++- 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/Pepperdash Core/Pepperdash Core/Comm/GenericSshClient.cs b/Pepperdash Core/Pepperdash Core/Comm/GenericSshClient.cs index 3bc7c21..904f87a 100644 --- a/Pepperdash Core/Pepperdash Core/Comm/GenericSshClient.cs +++ b/Pepperdash Core/Pepperdash Core/Comm/GenericSshClient.cs @@ -432,7 +432,7 @@ namespace PepperDash.Core if (Client != null) { if (StreamDebugging.TxStreamDebuggingIsEnabled) - Debug.Console(0, this, "Sending {0} characters of text: '{1}'", text.Length, text); + Debug.Console(0, this, "Sending {0} characters of text: '{1}'", text.Length, ComTextHelper.GetDebugText(text)); TheStream.Write(text); TheStream.Flush(); diff --git a/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs b/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs index f151b57..9f2102b 100644 --- a/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs +++ b/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs @@ -376,7 +376,7 @@ namespace PepperDash.Core var bytes = Encoding.GetEncoding(28591).GetBytes(text); // Check debug level before processing byte array if (StreamDebugging.TxStreamDebuggingIsEnabled) - Debug.Console(0, this, "Sending {0} bytes: '{1}'", bytes.Length, ComTextHelper.GetEscapedText(bytes)); + Debug.Console(0, this, "Sending {0} characters of text: '{1}'", text.Length, ComTextHelper.GetDebugText(text)); if(Client != null) Client.SendData(bytes, bytes.Length); diff --git a/Pepperdash Core/Pepperdash Core/Comm/GenericUdpServer.cs b/Pepperdash Core/Pepperdash Core/Comm/GenericUdpServer.cs index 1ed3d68..81cf4a2 100644 --- a/Pepperdash Core/Pepperdash Core/Comm/GenericUdpServer.cs +++ b/Pepperdash Core/Pepperdash Core/Comm/GenericUdpServer.cs @@ -329,7 +329,7 @@ namespace PepperDash.Core if (IsConnected && Server != null) { if (StreamDebugging.TxStreamDebuggingIsEnabled) - Debug.Console(0, this, "Sending {0} characters of text: '{1}'", text.Length, text); + Debug.Console(0, this, "Sending {0} characters of text: '{1}'", text.Length, ComTextHelper.GetDebugText(text)); Server.SendData(bytes, bytes.Length); } diff --git a/Pepperdash Core/Pepperdash Core/CommunicationExtras.cs b/Pepperdash Core/Pepperdash Core/CommunicationExtras.cs index c5892a4..6c40979 100644 --- a/Pepperdash Core/Pepperdash Core/CommunicationExtras.cs +++ b/Pepperdash Core/Pepperdash Core/CommunicationExtras.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using Crestron.SimplSharp; using Crestron.SimplSharp.CrestronSockets; - +using System.Text.RegularExpressions; using Newtonsoft.Json; using Newtonsoft.Json.Linq; @@ -138,5 +138,10 @@ namespace PepperDash.Core var bytes = Encoding.GetEncoding(28591).GetBytes(text); return String.Concat(bytes.Select(b => string.Format(@"[{0:X2}]", (int)b)).ToArray()); } + + public static string GetDebugText(string text) + { + return Regex.Replace(text, @"[^\u0000-\u007F]|\p{Cc}", a => string.Format("[{0:X2}]", (byte)a.Value[0])); + } } } \ No newline at end of file From 1ae2483110c55450d281b35908a691c4516980a7 Mon Sep 17 00:00:00 2001 From: Alex Johnson Date: Thu, 4 Mar 2021 11:47:25 -0500 Subject: [PATCH 2/4] Uses existing method to get escaped text for console --- Pepperdash Core/Pepperdash Core/CommunicationExtras.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pepperdash Core/Pepperdash Core/CommunicationExtras.cs b/Pepperdash Core/Pepperdash Core/CommunicationExtras.cs index 6c40979..207f438 100644 --- a/Pepperdash Core/Pepperdash Core/CommunicationExtras.cs +++ b/Pepperdash Core/Pepperdash Core/CommunicationExtras.cs @@ -141,7 +141,7 @@ namespace PepperDash.Core public static string GetDebugText(string text) { - return Regex.Replace(text, @"[^\u0000-\u007F]|\p{Cc}", a => string.Format("[{0:X2}]", (byte)a.Value[0])); + return Regex.Replace(text, @"[^\u0000-\u007F]|\p{Cc}", a => GetEscapedText(a.Value)); } } } \ No newline at end of file From e7d54092d89f0e0a8b01fcb63d36f6bf600806ec Mon Sep 17 00:00:00 2001 From: Alex Johnson Date: Thu, 4 Mar 2021 12:07:07 -0500 Subject: [PATCH 3/4] Simplifies regex for detecting non-printable characters --- Pepperdash Core/Pepperdash Core/CommunicationExtras.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pepperdash Core/Pepperdash Core/CommunicationExtras.cs b/Pepperdash Core/Pepperdash Core/CommunicationExtras.cs index 207f438..9dc4b48 100644 --- a/Pepperdash Core/Pepperdash Core/CommunicationExtras.cs +++ b/Pepperdash Core/Pepperdash Core/CommunicationExtras.cs @@ -141,7 +141,7 @@ namespace PepperDash.Core public static string GetDebugText(string text) { - return Regex.Replace(text, @"[^\u0000-\u007F]|\p{Cc}", a => GetEscapedText(a.Value)); + return Regex.Replace(text, @"[^\u0020-\u007E]", a => GetEscapedText(a.Value)); } } } \ No newline at end of file From 43efa2de189e0370cf5c397d3a2d7af76ed11fba Mon Sep 17 00:00:00 2001 From: Andrew Welker Date: Thu, 1 Jul 2021 14:00:27 -0600 Subject: [PATCH 4/4] fix: (PepperDash Core) Fix issue with stream debugging and bytes received Also fixed some spelling errors and added length info to the received messages for UDP, TCP, and SSH clients --- .../Pepperdash Core/Comm/GenericSshClient.cs | 18 ++++++++++++------ .../Pepperdash Core/Comm/GenericTcpIpClient.cs | 8 +++++++- .../Pepperdash Core/Comm/GenericUdpServer.cs | 12 +++++++----- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/Pepperdash Core/Pepperdash Core/Comm/GenericSshClient.cs b/Pepperdash Core/Pepperdash Core/Comm/GenericSshClient.cs index 904f87a..f81936c 100644 --- a/Pepperdash Core/Pepperdash Core/Comm/GenericSshClient.cs +++ b/Pepperdash Core/Pepperdash Core/Comm/GenericSshClient.cs @@ -379,18 +379,24 @@ namespace PepperDash.Core if (bytes.Length > 0) { var bytesHandler = BytesReceived; - if (bytesHandler != null) - bytesHandler(this, new GenericCommMethodReceiveBytesArgs(bytes)); + if (bytesHandler != null) + { + if (StreamDebugging.RxStreamDebuggingIsEnabled) + { + Debug.Console(0, this, "Received {1} bytes: '{0}'", ComTextHelper.GetEscapedText(bytes), bytes.Length); + } + bytesHandler(this, new GenericCommMethodReceiveBytesArgs(bytes)); + } + var textHandler = TextReceived; if (textHandler != null) { var str = Encoding.GetEncoding(28591).GetString(bytes, 0, bytes.Length); - textHandler(this, new GenericCommMethodReceiveTextArgs(str)); - if (StreamDebugging.RxStreamDebuggingIsEnabled) - Debug.Console(0, this, "Recevied: '{0}'", str); + Debug.Console(0, this, "Received: '{0}'", ComTextHelper.GetDebugText(str)); - } + textHandler(this, new GenericCommMethodReceiveTextArgs(str)); + } } } diff --git a/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs b/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs index 3b41d37..480e4d1 100644 --- a/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs +++ b/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs @@ -364,14 +364,20 @@ namespace PepperDash.Core var bytes = client.IncomingDataBuffer.Take(numBytes).ToArray(); var bytesHandler = BytesReceived; if (bytesHandler != null) + { + if (StreamDebugging.RxStreamDebuggingIsEnabled) + { + Debug.Console(0, this, "Received {1} bytes: '{0}'", ComTextHelper.GetEscapedText(bytes), bytes.Length); + } bytesHandler(this, new GenericCommMethodReceiveBytesArgs(bytes)); + } var textHandler = TextReceived; if (textHandler != null) { var str = Encoding.GetEncoding(28591).GetString(bytes, 0, bytes.Length); if (StreamDebugging.RxStreamDebuggingIsEnabled) - Debug.Console(0, this, "Recevied: '{0}'", str); + Debug.Console(0, this, "Received {1} characters of text: '{0}'", ComTextHelper.GetDebugText(str), str.Length); textHandler(this, new GenericCommMethodReceiveTextArgs(str)); diff --git a/Pepperdash Core/Pepperdash Core/Comm/GenericUdpServer.cs b/Pepperdash Core/Pepperdash Core/Comm/GenericUdpServer.cs index 81cf4a2..2f9769e 100644 --- a/Pepperdash Core/Pepperdash Core/Comm/GenericUdpServer.cs +++ b/Pepperdash Core/Pepperdash Core/Comm/GenericUdpServer.cs @@ -266,19 +266,21 @@ namespace PepperDash.Core Debug.Console(2, this, "Bytes: {0}", bytes.ToString()); var bytesHandler = BytesReceived; if (bytesHandler != null) + { + if (StreamDebugging.RxStreamDebuggingIsEnabled) + { + Debug.Console(0, this, "Received {1} bytes: '{0}'", ComTextHelper.GetEscapedText(bytes), bytes.Length); + } bytesHandler(this, new GenericCommMethodReceiveBytesArgs(bytes)); - else - Debug.Console(2, this, "bytesHandler is null"); + } var textHandler = TextReceived; if (textHandler != null) { if (StreamDebugging.RxStreamDebuggingIsEnabled) - Debug.Console(0, this, "Recevied: '{0}'", str); + Debug.Console(0, this, "Received {1} characters of text: '{0}'", ComTextHelper.GetDebugText(str), str.Length); textHandler(this, new GenericCommMethodReceiveTextArgs(str)); } - else - Debug.Console(2, this, "textHandler is null"); } server.ReceiveDataAsync(Receive);