mirror of
https://github.com/PepperDash/PepperDashCore.git
synced 2026-02-16 05:04:48 +00:00
fix: Copies SSH updates from 1.3.3-hotfix-390
This commit is contained in:
@@ -8,167 +8,149 @@ using Crestron.SimplSharp.Ssh.Common;
|
|||||||
|
|
||||||
namespace PepperDash.Core
|
namespace PepperDash.Core
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class GenericSshClient : Device, ISocketStatusWithStreamDebugging, IAutoReconnect
|
public class GenericSshClient : Device, ISocketStatusWithStreamDebugging, IAutoReconnect
|
||||||
{
|
{
|
||||||
private const string SPlusKey = "Uninitialized SshClient";
|
private const string SPlusKey = "Uninitialized SshClient";
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Object to enable stream debugging
|
/// Object to enable stream debugging
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public CommunicationStreamDebugging StreamDebugging { get; private set; }
|
public CommunicationStreamDebugging StreamDebugging { get; private set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Event that fires when data is received. Delivers args with byte array
|
/// Event that fires when data is received. Delivers args with byte array
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public event EventHandler<GenericCommMethodReceiveBytesArgs> BytesReceived;
|
public event EventHandler<GenericCommMethodReceiveBytesArgs> BytesReceived;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Event that fires when data is received. Delivered as text.
|
/// Event that fires when data is received. Delivered as text.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public event EventHandler<GenericCommMethodReceiveTextArgs> TextReceived;
|
public event EventHandler<GenericCommMethodReceiveTextArgs> TextReceived;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Event when the connection status changes.
|
/// Event when the connection status changes.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public event EventHandler<GenericSocketStatusChageEventArgs> ConnectionChange;
|
public event EventHandler<GenericSocketStatusChageEventArgs> ConnectionChange;
|
||||||
|
|
||||||
///// <summary>
|
///// <summary>
|
||||||
/////
|
/////
|
||||||
///// </summary>
|
///// </summary>
|
||||||
//public event GenericSocketStatusChangeEventDelegate SocketStatusChange;
|
//public event GenericSocketStatusChangeEventDelegate SocketStatusChange;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Address of server
|
/// Address of server
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string Hostname { get; set; }
|
public string Hostname { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Port on server
|
/// Port on server
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int Port { get; set; }
|
public int Port { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Username for server
|
/// Username for server
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string Username { get; set; }
|
public string Username { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// And... Password for server. That was worth documenting!
|
/// And... Password for server. That was worth documenting!
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string Password { get; set; }
|
public string Password { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// True when the server is connected - when status == 2.
|
/// True when the server is connected - when status == 2.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool IsConnected
|
public bool IsConnected
|
||||||
{
|
{
|
||||||
// returns false if no client or not connected
|
// returns false if no client or not connected
|
||||||
get { return Client != null && ClientStatus == SocketStatus.SOCKET_STATUS_CONNECTED; }
|
get { return Client != null && ClientStatus == SocketStatus.SOCKET_STATUS_CONNECTED; }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// S+ helper for IsConnected
|
/// S+ helper for IsConnected
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public ushort UIsConnected
|
public ushort UIsConnected
|
||||||
{
|
{
|
||||||
get { return (ushort)(IsConnected ? 1 : 0); }
|
get { return (ushort)(IsConnected ? 1 : 0); }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public SocketStatus ClientStatus
|
public SocketStatus ClientStatus
|
||||||
{
|
{
|
||||||
get { return _ClientStatus; }
|
get { return _ClientStatus; }
|
||||||
private set
|
private set
|
||||||
{
|
{
|
||||||
if (_ClientStatus == value)
|
if (_ClientStatus == value)
|
||||||
return;
|
return;
|
||||||
_ClientStatus = value;
|
_ClientStatus = value;
|
||||||
OnConnectionChange();
|
OnConnectionChange();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
SocketStatus _ClientStatus;
|
SocketStatus _ClientStatus;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Contains the familiar Simpl analog status values. This drives the ConnectionChange event
|
/// Contains the familiar Simpl analog status values. This drives the ConnectionChange event
|
||||||
/// and IsConnected with be true when this == 2.
|
/// and IsConnected with be true when this == 2.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public ushort UStatus
|
public ushort UStatus
|
||||||
{
|
{
|
||||||
get { return (ushort)_ClientStatus; }
|
get { return (ushort)_ClientStatus; }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Determines whether client will attempt reconnection on failure. Default is true
|
/// Determines whether client will attempt reconnection on failure. Default is true
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool AutoReconnect { get; set; }
|
public bool AutoReconnect { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Will be set and unset by connect and disconnect only
|
/// Will be set and unset by connect and disconnect only
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool ConnectEnabled { get; private set; }
|
public bool ConnectEnabled { get; private set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// S+ helper for AutoReconnect
|
/// S+ helper for AutoReconnect
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public ushort UAutoReconnect
|
public ushort UAutoReconnect
|
||||||
{
|
{
|
||||||
get { return (ushort)(AutoReconnect ? 1 : 0); }
|
get { return (ushort)(AutoReconnect ? 1 : 0); }
|
||||||
set { AutoReconnect = value == 1; }
|
set { AutoReconnect = value == 1; }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Millisecond value, determines the timeout period in between reconnect attempts.
|
/// Millisecond value, determines the timeout period in between reconnect attempts.
|
||||||
/// Set to 5000 by default
|
/// Set to 5000 by default
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int AutoReconnectIntervalMs { get; set; }
|
public int AutoReconnectIntervalMs { get; set; }
|
||||||
|
|
||||||
SshClient Client;
|
SshClient Client;
|
||||||
|
|
||||||
ShellStream TheStream;
|
ShellStream TheStream;
|
||||||
|
|
||||||
CTimer ReconnectTimer;
|
CTimer ReconnectTimer;
|
||||||
|
|
||||||
//Lock object to prevent simulatneous connect/disconnect operations
|
//Lock object to prevent simulatneous connect/disconnect operations
|
||||||
private CCriticalSection connectLock = new CCriticalSection();
|
private CCriticalSection connectLock = new CCriticalSection();
|
||||||
|
|
||||||
private bool DisconnectLogged = false;
|
private bool DisconnectLogged = false;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Typical constructor.
|
/// Typical constructor.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public GenericSshClient(string key, string hostname, int port, string username, string password) :
|
public GenericSshClient(string key, string hostname, int port, string username, string password) :
|
||||||
base(key)
|
base(key)
|
||||||
{
|
{
|
||||||
StreamDebugging = new CommunicationStreamDebugging(key);
|
StreamDebugging = new CommunicationStreamDebugging(key);
|
||||||
CrestronEnvironment.ProgramStatusEventHandler += new ProgramStatusEventHandler(CrestronEnvironment_ProgramStatusEventHandler);
|
CrestronEnvironment.ProgramStatusEventHandler += new ProgramStatusEventHandler(CrestronEnvironment_ProgramStatusEventHandler);
|
||||||
Key = key;
|
Key = key;
|
||||||
Hostname = hostname;
|
Hostname = hostname;
|
||||||
Port = port;
|
Port = port;
|
||||||
Username = username;
|
Username = username;
|
||||||
Password = password;
|
Password = password;
|
||||||
AutoReconnectIntervalMs = 5000;
|
AutoReconnectIntervalMs = 5000;
|
||||||
|
|
||||||
ReconnectTimer = new CTimer(o =>
|
|
||||||
{
|
|
||||||
if (ConnectEnabled)
|
|
||||||
{
|
|
||||||
Connect();
|
|
||||||
}
|
|
||||||
}, Timeout.Infinite);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// S+ Constructor - Must set all properties before calling Connect
|
|
||||||
/// </summary>
|
|
||||||
public GenericSshClient()
|
|
||||||
: base(SPlusKey)
|
|
||||||
{
|
|
||||||
CrestronEnvironment.ProgramStatusEventHandler += new ProgramStatusEventHandler(CrestronEnvironment_ProgramStatusEventHandler);
|
|
||||||
AutoReconnectIntervalMs = 5000;
|
|
||||||
|
|
||||||
ReconnectTimer = new CTimer(o =>
|
ReconnectTimer = new CTimer(o =>
|
||||||
{
|
{
|
||||||
@@ -177,35 +159,53 @@ namespace PepperDash.Core
|
|||||||
Connect();
|
Connect();
|
||||||
}
|
}
|
||||||
}, Timeout.Infinite);
|
}, Timeout.Infinite);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Just to help S+ set the key
|
/// S+ Constructor - Must set all properties before calling Connect
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Initialize(string key)
|
public GenericSshClient()
|
||||||
{
|
: base(SPlusKey)
|
||||||
Key = key;
|
{
|
||||||
}
|
CrestronEnvironment.ProgramStatusEventHandler += new ProgramStatusEventHandler(CrestronEnvironment_ProgramStatusEventHandler);
|
||||||
|
AutoReconnectIntervalMs = 5000;
|
||||||
|
|
||||||
/// <summary>
|
ReconnectTimer = new CTimer(o =>
|
||||||
/// Handles closing this up when the program shuts down
|
{
|
||||||
/// </summary>
|
if (ConnectEnabled)
|
||||||
void CrestronEnvironment_ProgramStatusEventHandler(eProgramStatusEventType programEventType)
|
{
|
||||||
{
|
Connect();
|
||||||
if (programEventType == eProgramStatusEventType.Stopping)
|
}
|
||||||
{
|
}, Timeout.Infinite);
|
||||||
if (Client != null)
|
}
|
||||||
{
|
|
||||||
Debug.Console(1, this, "Program stopping. Closing connection");
|
/// <summary>
|
||||||
|
/// Just to help S+ set the key
|
||||||
|
/// </summary>
|
||||||
|
public void Initialize(string key)
|
||||||
|
{
|
||||||
|
Key = key;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Handles closing this up when the program shuts down
|
||||||
|
/// </summary>
|
||||||
|
void CrestronEnvironment_ProgramStatusEventHandler(eProgramStatusEventType programEventType)
|
||||||
|
{
|
||||||
|
if (programEventType == eProgramStatusEventType.Stopping)
|
||||||
|
{
|
||||||
|
if (Client != null)
|
||||||
|
{
|
||||||
|
Debug.Console(1, this, "Program stopping. Closing connection");
|
||||||
Disconnect();
|
Disconnect();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Connect to the server, using the provided properties.
|
/// Connect to the server, using the provided properties.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Connect()
|
public void Connect()
|
||||||
{
|
{
|
||||||
// Don't go unless everything is here
|
// Don't go unless everything is here
|
||||||
if (string.IsNullOrEmpty(Hostname) || Port < 1 || Port > 65535
|
if (string.IsNullOrEmpty(Hostname) || Port < 1 || Port > 65535
|
||||||
@@ -229,7 +229,10 @@ namespace PepperDash.Core
|
|||||||
Debug.Console(1, this, "Attempting connect");
|
Debug.Console(1, this, "Attempting connect");
|
||||||
|
|
||||||
// Cancel reconnect if running.
|
// Cancel reconnect if running.
|
||||||
ReconnectTimer.Stop();
|
if (ReconnectTimer != null)
|
||||||
|
{
|
||||||
|
ReconnectTimer.Stop();
|
||||||
|
}
|
||||||
|
|
||||||
// Cleanup the old client if it already exists
|
// Cleanup the old client if it already exists
|
||||||
if (Client != null)
|
if (Client != null)
|
||||||
@@ -246,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
|
||||||
@@ -256,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;
|
||||||
@@ -307,21 +313,21 @@ namespace PepperDash.Core
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Disconnect the clients and put away it's resources.
|
/// Disconnect the clients and put away it's resources.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Disconnect()
|
public void Disconnect()
|
||||||
{
|
{
|
||||||
ConnectEnabled = false;
|
ConnectEnabled = false;
|
||||||
// Stop trying reconnects, if we are
|
// Stop trying reconnects, if we are
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Kills the stream, cleans up the client and sets it to null
|
/// Kills the stream, cleans up the client and sets it to null
|
||||||
@@ -330,105 +336,125 @@ 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Anything to do with reestablishing connection on failures
|
/// Anything to do with reestablishing connection on failures
|
||||||
/// </summary>
|
/// </summary>
|
||||||
void HandleConnectionFailure()
|
void HandleConnectionFailure()
|
||||||
{
|
{
|
||||||
KillClient(SocketStatus.SOCKET_STATUS_CONNECT_FAILED);
|
KillClient(SocketStatus.SOCKET_STATUS_CONNECT_FAILED);
|
||||||
|
|
||||||
Debug.Console(1, this, "Client nulled due to connection failure. AutoReconnect: {0}, ConnectEnabled: {1}", AutoReconnect, ConnectEnabled);
|
Debug.Console(1, this, "Client nulled due to connection failure. AutoReconnect: {0}, ConnectEnabled: {1}", AutoReconnect, ConnectEnabled);
|
||||||
if (AutoReconnect && ConnectEnabled)
|
if (AutoReconnect && ConnectEnabled)
|
||||||
{
|
{
|
||||||
Debug.Console(1, this, "Checking autoreconnect: {0}, {1}ms", AutoReconnect, AutoReconnectIntervalMs);
|
Debug.Console(1, this, "Checking autoreconnect: {0}, {1}ms", AutoReconnect, AutoReconnectIntervalMs);
|
||||||
if (ReconnectTimer == null)
|
if (ReconnectTimer == null)
|
||||||
{
|
{
|
||||||
ReconnectTimer = new CTimer(o =>
|
ReconnectTimer = new CTimer(o =>
|
||||||
{
|
{
|
||||||
Connect();
|
Connect();
|
||||||
}, AutoReconnectIntervalMs);
|
}, AutoReconnectIntervalMs);
|
||||||
Debug.Console(1, this, "Attempting connection in {0} seconds",
|
Debug.Console(1, this, "Attempting connection in {0} seconds",
|
||||||
(float) (AutoReconnectIntervalMs/1000));
|
(float)(AutoReconnectIntervalMs / 1000));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Debug.Console(1, this, "{0} second reconnect cycle running",
|
Debug.Console(1, this, "{0} second reconnect cycle running",
|
||||||
(float) (AutoReconnectIntervalMs/1000));
|
(float)(AutoReconnectIntervalMs / 1000));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Kills the stream
|
/// Kills the stream
|
||||||
/// </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");
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Handles the keyboard interactive authentication, should it be required.
|
|
||||||
/// </summary>
|
|
||||||
void kauth_AuthenticationPrompt(object sender, AuthenticationPromptEventArgs e)
|
|
||||||
{
|
|
||||||
foreach (AuthenticationPrompt prompt in e.Prompts)
|
|
||||||
if (prompt.Request.IndexOf("Password:", StringComparison.InvariantCultureIgnoreCase) != -1)
|
|
||||||
prompt.Response = Password;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Handler for data receive on ShellStream. Passes data across to queue for line parsing.
|
|
||||||
/// </summary>
|
|
||||||
void Stream_DataReceived(object sender, Crestron.SimplSharp.Ssh.Common.ShellDataEventArgs e)
|
|
||||||
{
|
|
||||||
var bytes = e.Data;
|
|
||||||
if (bytes.Length > 0)
|
|
||||||
{
|
|
||||||
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));
|
|
||||||
|
|
||||||
textHandler(this, new GenericCommMethodReceiveTextArgs(str));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Debug.Console(0, this, Debug.ErrorLogLevel.Notice, "Exception in Kill Stream:{0}", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Handles the keyboard interactive authentication, should it be required.
|
||||||
|
/// </summary>
|
||||||
|
void kauth_AuthenticationPrompt(object sender, AuthenticationPromptEventArgs e)
|
||||||
|
{
|
||||||
|
foreach (AuthenticationPrompt prompt in e.Prompts)
|
||||||
|
if (prompt.Request.IndexOf("Password:", StringComparison.InvariantCultureIgnoreCase) != -1)
|
||||||
|
prompt.Response = Password;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Handler for data receive on ShellStream. Passes data across to queue for line parsing.
|
||||||
|
/// </summary>
|
||||||
|
void Stream_DataReceived(object sender, Crestron.SimplSharp.Ssh.Common.ShellDataEventArgs e)
|
||||||
|
{
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
if (StreamDebugging.RxStreamDebuggingIsEnabled)
|
||||||
|
Debug.Console(0, this, "Received: '{0}'", ComTextHelper.GetDebugText(response));
|
||||||
|
|
||||||
|
textHandler(this, new GenericCommMethodReceiveTextArgs(response));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Error event handler for client events - disconnect, etc. Will forward those events via ConnectionChange
|
/// Error event handler for client events - disconnect, etc. Will forward those events via ConnectionChange
|
||||||
/// event
|
/// event
|
||||||
/// </summary>
|
/// </summary>
|
||||||
void Client_ErrorOccurred(object sender, Crestron.SimplSharp.Ssh.Common.ExceptionEventArgs e)
|
void Client_ErrorOccurred(object sender, Crestron.SimplSharp.Ssh.Common.ExceptionEventArgs e)
|
||||||
{
|
{
|
||||||
CrestronInvoke.BeginInvoke(o =>
|
CrestronInvoke.BeginInvoke(o =>
|
||||||
{
|
{
|
||||||
if (e.Exception is SshConnectionException || e.Exception is System.Net.Sockets.SocketException)
|
if (e.Exception is SshConnectionException || e.Exception is System.Net.Sockets.SocketException)
|
||||||
@@ -451,58 +477,69 @@ namespace PepperDash.Core
|
|||||||
ReconnectTimer.Reset(AutoReconnectIntervalMs);
|
ReconnectTimer.Reset(AutoReconnectIntervalMs);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Helper for ConnectionChange event
|
/// Helper for ConnectionChange event
|
||||||
/// </summary>
|
/// </summary>
|
||||||
void OnConnectionChange()
|
void OnConnectionChange()
|
||||||
{
|
{
|
||||||
if (ConnectionChange != null)
|
if (ConnectionChange != null)
|
||||||
ConnectionChange(this, new GenericSocketStatusChageEventArgs(this));
|
ConnectionChange(this, new GenericSocketStatusChageEventArgs(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
#region IBasicCommunication Members
|
#region IBasicCommunication Members
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sends text to the server
|
/// Sends text to the server
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <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
|
else
|
||||||
{
|
{
|
||||||
Debug.Console(1, this, "Client is null or disconnected. Cannot Send Text");
|
Debug.Console(1, this, "Client is null or disconnected. Cannot Send Text");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (ObjectDisposedException 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");
|
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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sends Bytes to the server
|
/// Sends Bytes to the server
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="bytes"></param>
|
/// <param name="bytes"></param>
|
||||||
public void SendBytes(byte[] bytes)
|
public void SendBytes(byte[] bytes)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (Client != null && TheStream != null && IsConnected)
|
if (Client != null && TheStream != null && IsConnected)
|
||||||
{
|
{
|
||||||
if (StreamDebugging.TxStreamDebuggingIsEnabled)
|
if (StreamDebugging.TxStreamDebuggingIsEnabled)
|
||||||
@@ -515,23 +552,34 @@ 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);
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
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");
|
||||||
//*****************************************************************************************************
|
}
|
||||||
/// <summary>
|
}
|
||||||
/// Fired when connection changes
|
|
||||||
/// </summary>
|
#endregion
|
||||||
public class SshConnectionChangeEventArgs : EventArgs
|
}
|
||||||
{
|
|
||||||
|
//*****************************************************************************************************
|
||||||
|
//*****************************************************************************************************
|
||||||
|
/// <summary>
|
||||||
|
/// Fired when connection changes
|
||||||
|
/// </summary>
|
||||||
|
public class SshConnectionChangeEventArgs : EventArgs
|
||||||
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Connection State
|
/// Connection State
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -552,10 +600,10 @@ namespace PepperDash.Core
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public ushort Status { get { return Client.UStatus; } }
|
public ushort Status { get { return Client.UStatus; } }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// S+ Constructor
|
/// S+ Constructor
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public SshConnectionChangeEventArgs() { }
|
public SshConnectionChangeEventArgs() { }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// EventArgs class
|
/// EventArgs class
|
||||||
@@ -563,9 +611,9 @@ namespace PepperDash.Core
|
|||||||
/// <param name="isConnected">Connection State</param>
|
/// <param name="isConnected">Connection State</param>
|
||||||
/// <param name="client">The Client</param>
|
/// <param name="client">The Client</param>
|
||||||
public SshConnectionChangeEventArgs(bool isConnected, GenericSshClient client)
|
public SshConnectionChangeEventArgs(bool isConnected, GenericSshClient client)
|
||||||
{
|
{
|
||||||
IsConnected = isConnected;
|
IsConnected = isConnected;
|
||||||
Client = client;
|
Client = client;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user