Skip to content

Commit

Permalink
fix: refactor the ssh handeler to use ShellStream.Read()
Browse files Browse the repository at this point in the history
  • Loading branch information
jtalborough committed Feb 26, 2024
1 parent 0aa095d commit 63f6bb9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
51 changes: 30 additions & 21 deletions Pepperdash Core/Pepperdash Core/Comm/GenericSshClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,11 @@ public void Connect()
{
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;
Expand Down Expand Up @@ -414,29 +419,33 @@ void kauth_AuthenticationPrompt(object sender, AuthenticationPromptEventArgs e)
/// </summary>
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));
}

}


Expand Down
2 changes: 1 addition & 1 deletion Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -564,4 +564,4 @@ public TcpSshPropertiesConfig()

}

}
}

0 comments on commit 63f6bb9

Please sign in to comment.