Merge pull request #180 from PepperDash/hotfix/ssh-fix

Hotfix/ssh fix
This commit is contained in:
Neil Dorin
2024-10-31 11:03:52 -06:00
committed by GitHub
2 changed files with 107 additions and 61 deletions

View File

@@ -249,8 +249,6 @@ namespace PepperDash.Core
Debug.Console(1, this, "Creating new SshClient"); Debug.Console(1, this, "Creating new SshClient");
ConnectionInfo connectionInfo = new ConnectionInfo(Hostname, Port, Username, pauth, kauth); ConnectionInfo connectionInfo = new ConnectionInfo(Hostname, Port, Username, pauth, kauth);
Client = new SshClient(connectionInfo); Client = new SshClient(connectionInfo);
Client.ErrorOccurred -= Client_ErrorOccurred;
Client.ErrorOccurred += Client_ErrorOccurred; Client.ErrorOccurred += Client_ErrorOccurred;
//Attempt to connect //Attempt to connect
@@ -259,6 +257,11 @@ namespace PepperDash.Core
{ {
Client.Connect(); Client.Connect();
TheStream = Client.CreateShellStream("PDTShell", 100, 80, 100, 200, 65534); 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; TheStream.DataReceived += Stream_DataReceived;
Debug.Console(1, this, Debug.ErrorLogLevel.Notice, "Connected"); Debug.Console(1, this, Debug.ErrorLogLevel.Notice, "Connected");
ClientStatus = SocketStatus.SOCKET_STATUS_CONNECTED; ClientStatus = SocketStatus.SOCKET_STATUS_CONNECTED;
@@ -320,7 +323,7 @@ namespace PepperDash.Core
if (ReconnectTimer != null) if (ReconnectTimer != null)
{ {
ReconnectTimer.Stop(); ReconnectTimer.Stop();
ReconnectTimer = null; // ReconnectTimer = null;
} }
KillClient(SocketStatus.SOCKET_STATUS_BROKEN_LOCALLY); KillClient(SocketStatus.SOCKET_STATUS_BROKEN_LOCALLY);
@@ -333,12 +336,21 @@ namespace PepperDash.Core
{ {
KillStream(); KillStream();
if (Client != null) try
{ {
Client.Disconnect(); if (Client != null)
Client = null; {
ClientStatus = status; Client.ErrorOccurred -= Client_ErrorOccurred;
Debug.Console(1, this, "Disconnected"); 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 +387,21 @@ namespace PepperDash.Core
/// </summary> /// </summary>
void KillStream() void KillStream()
{ {
if (TheStream != null) try
{ {
TheStream.DataReceived -= Stream_DataReceived; if (TheStream != null)
TheStream.Close(); {
TheStream.Dispose(); TheStream.DataReceived -= Stream_DataReceived;
TheStream = null; TheStream.Close();
Debug.Console(1, this, "Disconnected stream"); 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);
}
} }
/// <summary> /// <summary>
@@ -400,29 +419,33 @@ namespace PepperDash.Core
/// </summary> /// </summary>
void Stream_DataReceived(object sender, Crestron.SimplSharp.Ssh.Common.ShellDataEventArgs e) void Stream_DataReceived(object sender, Crestron.SimplSharp.Ssh.Common.ShellDataEventArgs e)
{ {
var bytes = e.Data; if (((ShellStream)sender).Length <= 0L)
if (bytes.Length > 0) {
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 (StreamDebugging.RxStreamDebuggingIsEnabled)
if (bytesHandler != null) Debug.Console(0, this, "Received: '{0}'", ComTextHelper.GetDebugText(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; textHandler(this, new GenericCommMethodReceiveTextArgs(response));
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));
textHandler(this, new GenericCommMethodReceiveTextArgs(str));
}
}
} }
@@ -473,28 +496,39 @@ namespace PepperDash.Core
/// <param name="text"></param> /// <param name="text"></param>
public void SendText(string text) public void SendText(string text)
{ {
try try
{ {
if (Client != null && TheStream != null && IsConnected) if (Client != null && TheStream != null && IsConnected)
{ {
if (StreamDebugging.TxStreamDebuggingIsEnabled) if (StreamDebugging.TxStreamDebuggingIsEnabled)
Debug.Console(0, this, "Sending {0} characters of text: '{1}'", text.Length, ComTextHelper.GetDebugText(text)); Debug.Console(0,
this,
"Sending {0} characters of text: '{1}'",
text.Length,
ComTextHelper.GetDebugText(text));
TheStream.Write(text); TheStream.Write(text);
TheStream.Flush(); TheStream.Flush();
}
else
{
Debug.Console(1, this, "Client is null or disconnected. Cannot Send Text");
}
}
catch (ObjectDisposedException ex)
{
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);
else ReconnectTimer.Reset();
{ }
Debug.Console(1, this, "Client is null or disconnected. Cannot Send Text");
}
}
catch (Exception ex) catch (Exception ex)
{ {
Debug.Console(0, "Exception: {0}", ex.Message); Debug.Console(0, this, Debug.ErrorLogLevel.Notice, "Exception: {0}", ex.Message);
Debug.Console(0, "Stack Trace: {0}", ex.StackTrace); Debug.Console(1, this, Debug.ErrorLogLevel.Notice, "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 +552,21 @@ namespace PepperDash.Core
{ {
Debug.Console(1, this, "Client is null or disconnected. Cannot Send Bytes"); Debug.Console(1, this, "Client is null or disconnected. Cannot Send Bytes");
} }
} }
catch catch (ObjectDisposedException ex)
{ {
Debug.Console(1, this, Debug.ErrorLogLevel.Error, "Stream write failed. Disconnected, closing"); 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, 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");
} }
} }

View File

@@ -220,7 +220,8 @@ namespace PepperDash.Core
/// </summary> /// </summary>
public GenericTcpIpClient() public GenericTcpIpClient()
: base(SplusKey) : base(SplusKey)
{ {
StreamDebugging = new CommunicationStreamDebugging(SplusKey);
CrestronEnvironment.ProgramStatusEventHandler += new ProgramStatusEventHandler(CrestronEnvironment_ProgramStatusEventHandler); CrestronEnvironment.ProgramStatusEventHandler += new ProgramStatusEventHandler(CrestronEnvironment_ProgramStatusEventHandler);
AutoReconnectIntervalMs = 5000; AutoReconnectIntervalMs = 5000;
BufferSize = 2000; BufferSize = 2000;