From 751d157f07b9c136d10d1974c848429e1bba65aa Mon Sep 17 00:00:00 2001 From: Nick Genovese Date: Wed, 16 Aug 2023 10:08:06 -0400 Subject: [PATCH 1/4] fix: handles an object disposed ex - the SSH Stream is occasionally disposed, when this happens we catch and recreate the client - removed the line where we set the Timer to null at disconnect, as there is no other place that the timer gets recreated - added try/catch w/ logging in the KillClient method for safety's sake --- .../Pepperdash Core/Comm/GenericSshClient.cs | 112 ++++++++++++------ 1 file changed, 74 insertions(+), 38 deletions(-) diff --git a/Pepperdash Core/Pepperdash Core/Comm/GenericSshClient.cs b/Pepperdash Core/Pepperdash Core/Comm/GenericSshClient.cs index 0728cfc..26da1ef 100644 --- a/Pepperdash Core/Pepperdash Core/Comm/GenericSshClient.cs +++ b/Pepperdash Core/Pepperdash Core/Comm/GenericSshClient.cs @@ -249,8 +249,6 @@ namespace PepperDash.Core Debug.Console(1, this, "Creating new SshClient"); ConnectionInfo connectionInfo = new ConnectionInfo(Hostname, Port, Username, pauth, kauth); Client = new SshClient(connectionInfo); - - Client.ErrorOccurred -= Client_ErrorOccurred; Client.ErrorOccurred += Client_ErrorOccurred; //Attempt to connect @@ -320,7 +318,7 @@ namespace PepperDash.Core if (ReconnectTimer != null) { ReconnectTimer.Stop(); - ReconnectTimer = null; + // ReconnectTimer = null; } KillClient(SocketStatus.SOCKET_STATUS_BROKEN_LOCALLY); @@ -333,12 +331,21 @@ namespace PepperDash.Core { KillStream(); - if (Client != null) - { - Client.Disconnect(); - Client = null; - ClientStatus = status; - Debug.Console(1, this, "Disconnected"); + try + { + if (Client != null) + { + Client.ErrorOccurred -= Client_ErrorOccurred; + Client.Disconnect(); + Client.Dispose(); + Client = null; + ClientStatus = status; + Debug.Console(1, this, "Disconnected"); + } + } + catch (Exception ex) + { + Debug.Console(0, this, Debug.ErrorLogLevel.Notice, "Exception in Kill Client:{0}", ex); } } @@ -375,14 +382,21 @@ namespace PepperDash.Core /// void KillStream() { - if (TheStream != null) - { - TheStream.DataReceived -= Stream_DataReceived; - TheStream.Close(); - TheStream.Dispose(); - TheStream = null; - Debug.Console(1, this, "Disconnected stream"); - } + try + { + if (TheStream != null) + { + TheStream.DataReceived -= Stream_DataReceived; + TheStream.Close(); + TheStream.Dispose(); + TheStream = null; + Debug.Console(1, this, "Disconnected stream"); + } + } + catch (Exception ex) + { + Debug.Console(0, this, Debug.ErrorLogLevel.Notice, "Exception in Kill Stream:{0}", ex); + } } /// @@ -473,28 +487,39 @@ namespace PepperDash.Core /// public void SendText(string text) { - try - { - if (Client != null && TheStream != null && IsConnected) - { - if (StreamDebugging.TxStreamDebuggingIsEnabled) - Debug.Console(0, this, "Sending {0} characters of text: '{1}'", text.Length, ComTextHelper.GetDebugText(text)); + try + { + if (Client != null && TheStream != null && IsConnected) + { + if (StreamDebugging.TxStreamDebuggingIsEnabled) + Debug.Console(0, + this, + "Sending {0} characters of text: '{1}'", + text.Length, + ComTextHelper.GetDebugText(text)); - TheStream.Write(text); - TheStream.Flush(); + TheStream.Write(text); + TheStream.Flush(); + } + else + { + Debug.Console(1, this, "Client is null or disconnected. Cannot Send Text"); + } + } + catch (ObjectDisposedException ex) + { + Debug.Console(0, this, "Exception: {0}", ex.Message); + Debug.Console(0, this, "Stack Trace: {0}", ex.StackTrace); - } - else - { - Debug.Console(1, this, "Client is null or disconnected. Cannot Send Text"); - } - } + KillClient(SocketStatus.SOCKET_STATUS_CONNECT_FAILED); + ReconnectTimer.Reset(); + } catch (Exception ex) { - Debug.Console(0, "Exception: {0}", ex.Message); - Debug.Console(0, "Stack Trace: {0}", ex.StackTrace); + Debug.Console(0, this, "Exception: {0}", ex.Message); + Debug.Console(0, this, "Stack Trace: {0}", ex.StackTrace); - Debug.Console(1, this, Debug.ErrorLogLevel.Error, "Stream write failed. Disconnected, closing"); + Debug.Console(1, this, Debug.ErrorLogLevel.Error, "Stream write failed"); } } @@ -518,10 +543,21 @@ namespace PepperDash.Core { Debug.Console(1, this, "Client is null or disconnected. Cannot Send Bytes"); } - } - catch - { - Debug.Console(1, this, Debug.ErrorLogLevel.Error, "Stream write failed. Disconnected, closing"); + } + catch (ObjectDisposedException ex) + { + Debug.Console(0, this, "Exception: {0}", ex.Message); + Debug.Console(0, this, "Stack Trace: {0}", ex.StackTrace); + + KillClient(SocketStatus.SOCKET_STATUS_CONNECT_FAILED); + ReconnectTimer.Reset(); + } + catch (Exception ex) + { + Debug.Console(0, this, "Exception: {0}", ex.Message); + Debug.Console(0, this, "Stack Trace: {0}", ex.StackTrace); + + Debug.Console(1, this, Debug.ErrorLogLevel.Error, "Stream write failed"); } } From 4f482460614db1c9d5ad209f901eb98714e6d0fb Mon Sep 17 00:00:00 2001 From: Nick Genovese Date: Wed, 23 Aug 2023 14:01:13 -0400 Subject: [PATCH 2/4] fix: cleaned up some debug - in both 'send' methods, print and log the ex message at level 0 - in both 'send' methods, print and log the stack trace at level 1 --- .../Pepperdash Core/Comm/GenericSshClient.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Pepperdash Core/Pepperdash Core/Comm/GenericSshClient.cs b/Pepperdash Core/Pepperdash Core/Comm/GenericSshClient.cs index 26da1ef..e13c284 100644 --- a/Pepperdash Core/Pepperdash Core/Comm/GenericSshClient.cs +++ b/Pepperdash Core/Pepperdash Core/Comm/GenericSshClient.cs @@ -508,16 +508,16 @@ namespace PepperDash.Core } catch (ObjectDisposedException ex) { - Debug.Console(0, this, "Exception: {0}", ex.Message); - Debug.Console(0, this, "Stack Trace: {0}", ex.StackTrace); + Debug.Console(0, this, Debug.ErrorLogLevel.Notice, "Exception: {0}", ex.Message); + Debug.Console(1, this, Debug.ErrorLogLevel.Notice, "Stack Trace: {0}", ex.StackTrace); KillClient(SocketStatus.SOCKET_STATUS_CONNECT_FAILED); ReconnectTimer.Reset(); } catch (Exception ex) { - Debug.Console(0, this, "Exception: {0}", ex.Message); - Debug.Console(0, this, "Stack Trace: {0}", ex.StackTrace); + Debug.Console(0, this, Debug.ErrorLogLevel.Notice, "Exception: {0}", ex.Message); + Debug.Console(1, this, Debug.ErrorLogLevel.Notice, "Stack Trace: {0}", ex.StackTrace); Debug.Console(1, this, Debug.ErrorLogLevel.Error, "Stream write failed"); } @@ -546,16 +546,16 @@ namespace PepperDash.Core } catch (ObjectDisposedException ex) { - Debug.Console(0, this, "Exception: {0}", ex.Message); - Debug.Console(0, this, "Stack Trace: {0}", ex.StackTrace); + Debug.Console(0, this, Debug.ErrorLogLevel.Notice, "Exception: {0}", ex.Message); + Debug.Console(1, this, Debug.ErrorLogLevel.Notice, "Stack Trace: {0}", ex.StackTrace); KillClient(SocketStatus.SOCKET_STATUS_CONNECT_FAILED); ReconnectTimer.Reset(); } catch (Exception ex) { - Debug.Console(0, this, "Exception: {0}", ex.Message); - Debug.Console(0, this, "Stack Trace: {0}", ex.StackTrace); + Debug.Console(0, this, Debug.ErrorLogLevel.Notice, "Exception: {0}", ex.Message); + Debug.Console(1, this, Debug.ErrorLogLevel.Notice, "Stack Trace: {0}", ex.StackTrace); Debug.Console(1, this, Debug.ErrorLogLevel.Error, "Stream write failed"); } From 86acb09782e46362982477b9712b3bc4f8f50b70 Mon Sep 17 00:00:00 2001 From: Nick Genovese Date: Thu, 24 Aug 2023 12:36:21 -0400 Subject: [PATCH 3/4] fix: StreamDebugging in default constructor - creates a StreamDebugging class with the default s+ key - this shouldn't matter since in a simpl windows env you can't use console commands anyhow --- Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs b/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs index 8cfaad2..024221f 100644 --- a/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs +++ b/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs @@ -220,7 +220,8 @@ namespace PepperDash.Core /// public GenericTcpIpClient() : base(SplusKey) - { + { + StreamDebugging = new CommunicationStreamDebugging(SplusKey); CrestronEnvironment.ProgramStatusEventHandler += new ProgramStatusEventHandler(CrestronEnvironment_ProgramStatusEventHandler); AutoReconnectIntervalMs = 5000; BufferSize = 2000; From 63f6bb9cf2c10c8f765dde9dc85c2972f29bf697 Mon Sep 17 00:00:00 2001 From: jtalborough Date: Mon, 26 Feb 2024 12:58:42 -0500 Subject: [PATCH 4/4] fix: refactor the ssh handeler to use ShellStream.Read() --- .../Pepperdash Core/Comm/GenericSshClient.cs | 51 +++++++++++-------- .../Comm/GenericTcpIpClient.cs | 2 +- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/Pepperdash Core/Pepperdash Core/Comm/GenericSshClient.cs b/Pepperdash Core/Pepperdash Core/Comm/GenericSshClient.cs index e13c284..f27a104 100644 --- a/Pepperdash Core/Pepperdash Core/Comm/GenericSshClient.cs +++ b/Pepperdash Core/Pepperdash Core/Comm/GenericSshClient.cs @@ -257,6 +257,11 @@ namespace PepperDash.Core { Client.Connect(); TheStream = Client.CreateShellStream("PDTShell", 100, 80, 100, 200, 65534); + if (TheStream.DataAvailable) + { + // empty the buffer if there is data + string str = TheStream.Read(); + } TheStream.DataReceived += Stream_DataReceived; Debug.Console(1, this, Debug.ErrorLogLevel.Notice, "Connected"); ClientStatus = SocketStatus.SOCKET_STATUS_CONNECTED; @@ -414,29 +419,33 @@ namespace PepperDash.Core /// void Stream_DataReceived(object sender, Crestron.SimplSharp.Ssh.Common.ShellDataEventArgs e) { - var bytes = e.Data; - if (bytes.Length > 0) + if (((ShellStream)sender).Length <= 0L) + { + return; + } + var response = ((ShellStream)sender).Read(); + + var bytesHandler = BytesReceived; + + if (bytesHandler != null) + { + var bytes = Encoding.UTF8.GetBytes(response); + 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 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, "Received: '{0}'", ComTextHelper.GetDebugText(str)); + if (StreamDebugging.RxStreamDebuggingIsEnabled) + Debug.Console(0, this, "Received: '{0}'", ComTextHelper.GetDebugText(response)); - textHandler(this, new GenericCommMethodReceiveTextArgs(str)); - } - } + textHandler(this, new GenericCommMethodReceiveTextArgs(response)); + } + } diff --git a/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs b/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs index 024221f..1094fe3 100644 --- a/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs +++ b/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs @@ -564,4 +564,4 @@ namespace PepperDash.Core } -} +}