mirror of
https://github.com/PepperDash/PepperDashCore.git
synced 2026-02-16 05:04:48 +00:00
Working through ssh disconnect / leak problems
This commit is contained in:
@@ -26,7 +26,7 @@ namespace PepperDash.Core
|
|||||||
//*****************************************************************************************************
|
//*****************************************************************************************************
|
||||||
//*****************************************************************************************************
|
//*****************************************************************************************************
|
||||||
|
|
||||||
public class GenericSshClient : Device, IBasicCommunication
|
public class GenericSshClient : Device, IBasicCommunication, IAutoReconnect
|
||||||
{
|
{
|
||||||
public event EventHandler<GenericCommMethodReceiveBytesArgs> BytesReceived;
|
public event EventHandler<GenericCommMethodReceiveBytesArgs> BytesReceived;
|
||||||
public event EventHandler<GenericCommMethodReceiveTextArgs> TextReceived;
|
public event EventHandler<GenericCommMethodReceiveTextArgs> TextReceived;
|
||||||
@@ -66,12 +66,12 @@ namespace PepperDash.Core
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Millisecond value, determines the timeout period in between reconnect attempts
|
/// Millisecond value, determines the timeout period in between reconnect attempts
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public ushort AutoReconnectIntervalMs { get; set; }
|
public int AutoReconnectIntervalMs { get; set; }
|
||||||
|
|
||||||
SshClient Client;
|
SshClient Client;
|
||||||
ShellStream TheStream;
|
ShellStream TheStream;
|
||||||
CTimer ReconnectTimer;
|
CTimer ReconnectTimer;
|
||||||
bool ReconnectTimerRunning;
|
//bool ReconnectTimerRunning;
|
||||||
|
|
||||||
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)
|
||||||
@@ -82,6 +82,21 @@ namespace PepperDash.Core
|
|||||||
Port = port;
|
Port = port;
|
||||||
Username = username;
|
Username = username;
|
||||||
Password = password;
|
Password = password;
|
||||||
|
CrestronEnvironment.ProgramStatusEventHandler += new ProgramStatusEventHandler(CrestronEnvironment_ProgramStatusEventHandler);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CrestronEnvironment_ProgramStatusEventHandler(eProgramStatusEventType programEventType)
|
||||||
|
{
|
||||||
|
if (programEventType == eProgramStatusEventType.Stopping)
|
||||||
|
{
|
||||||
|
if (Client != null)
|
||||||
|
{
|
||||||
|
Debug.Console(2, this, "Closing connection");
|
||||||
|
Client.Disconnect();
|
||||||
|
Client.Dispose();
|
||||||
|
Debug.Console(2, this, "Connection closed");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -89,58 +104,72 @@ namespace PepperDash.Core
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public void Connect()
|
public void Connect()
|
||||||
{
|
{
|
||||||
ReconnectTimerRunning = false;
|
Debug.Console(1, this, "attempting connect, IsConnected={0}", Client != null ? Client.IsConnected : false);
|
||||||
|
|
||||||
|
//ReconnectTimerRunning = false;
|
||||||
|
if (ReconnectTimer != null)
|
||||||
|
{
|
||||||
|
ReconnectTimer.Stop();
|
||||||
|
ReconnectTimer = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (IsConnected)
|
||||||
|
return;
|
||||||
|
|
||||||
if (Hostname != null && Hostname != string.Empty && Port > 0 &&
|
if (Hostname != null && Hostname != string.Empty && Port > 0 &&
|
||||||
Username != null && Password != null)
|
Username != null && Password != null)
|
||||||
{
|
{
|
||||||
Debug.Console(1, this, "attempting connect, IsConnected={0}", IsConnected);
|
|
||||||
if (!IsConnected)
|
UStatus = 1;
|
||||||
|
IsConnected = false;
|
||||||
|
|
||||||
|
// This handles both password and keyboard-interactive (like on OS-X, 'nixes)
|
||||||
|
KeyboardInteractiveAuthenticationMethod kauth = new KeyboardInteractiveAuthenticationMethod(Username);
|
||||||
|
kauth.AuthenticationPrompt += new EventHandler<AuthenticationPromptEventArgs>(kauth_AuthenticationPrompt);
|
||||||
|
PasswordAuthenticationMethod pauth = new PasswordAuthenticationMethod(Username, Password);
|
||||||
|
ConnectionInfo connectionInfo = new ConnectionInfo(Hostname, Port, Username, pauth, kauth);
|
||||||
|
// always spin up new client in case parameters have changed
|
||||||
|
if (Client != null)
|
||||||
{
|
{
|
||||||
UStatus = 1;
|
Client.Disconnect();
|
||||||
IsConnected = false;
|
Client = null;
|
||||||
|
//Client.Dispose();
|
||||||
|
}
|
||||||
|
Client = new SshClient(connectionInfo);
|
||||||
|
|
||||||
// This handles both password and keyboard-interactive (like on OS-X, 'nixes)
|
Client.ErrorOccurred += Client_ErrorOccurred;
|
||||||
KeyboardInteractiveAuthenticationMethod kauth = new KeyboardInteractiveAuthenticationMethod(Username);
|
try
|
||||||
kauth.AuthenticationPrompt += new EventHandler<AuthenticationPromptEventArgs>(kauth_AuthenticationPrompt);
|
{
|
||||||
PasswordAuthenticationMethod pauth = new PasswordAuthenticationMethod(Username, Password);
|
Client.Connect();
|
||||||
ConnectionInfo connectionInfo = new ConnectionInfo(Hostname, Port, Username, pauth, kauth);
|
if (Client.IsConnected)
|
||||||
Client = new SshClient(connectionInfo);
|
|
||||||
Client.ErrorOccurred += Client_ErrorOccurred;
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
Client.Connect();
|
Client.KeepAliveInterval = TimeSpan.FromSeconds(2);
|
||||||
if (Client.IsConnected)
|
Client.SendKeepAlive();
|
||||||
{
|
IsConnected = true;
|
||||||
Client.KeepAliveInterval = TimeSpan.FromSeconds(2);
|
Debug.Console(1, this, "Connected");
|
||||||
Client.SendKeepAlive();
|
TheStream = Client.CreateShellStream("PDTShell", 100, 80, 100, 200, 65534);
|
||||||
IsConnected = true;
|
TheStream.DataReceived += Stream_DataReceived;
|
||||||
Debug.Console(1, this, "Connected");
|
|
||||||
TheStream = Client.CreateShellStream("PDTShell", 100, 80, 100, 200, 65534);
|
|
||||||
TheStream.DataReceived += Stream_DataReceived;
|
|
||||||
//TheStream.ErrorOccurred += Stream_ErrorOccurred;
|
|
||||||
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
catch (SshConnectionException e)
|
return;
|
||||||
|
}
|
||||||
|
catch (SshConnectionException e)
|
||||||
|
{
|
||||||
|
var ie = e.InnerException; // The details are inside!!
|
||||||
|
string msg;
|
||||||
|
if (ie is SocketException)
|
||||||
|
msg = string.Format("'{0}' CONNECTION failure: Cannot reach host, ({1})", Key, ie.GetType());
|
||||||
|
else if (ie is System.Net.Sockets.SocketException)
|
||||||
|
msg = string.Format("'{0}' Connection failure: Cannot reach host '{1}' on port {2}, ({3})",
|
||||||
|
Key, Hostname, Port, ie.GetType());
|
||||||
|
else if (ie is SshAuthenticationException)
|
||||||
{
|
{
|
||||||
var ie = e.InnerException; // The details are inside!!
|
msg = string.Format("'{0}' Authentication failure for username '{1}', ({2})",
|
||||||
string msg;
|
Username, Key, ie.GetType());
|
||||||
if (ie is SocketException)
|
Debug.Console(0, this, "Authentication failure for username '{0}', ({1})",
|
||||||
msg = string.Format("'{0}' CONNECTION failure: Cannot reach host, ({1})", Key, ie.GetType());
|
Username, ie.GetType());
|
||||||
else if (ie is System.Net.Sockets.SocketException)
|
|
||||||
msg = string.Format("'{0}' Connection failure: Cannot reach host '{1}' on port {2}, ({3})",
|
|
||||||
Key, Hostname, Port, ie.GetType());
|
|
||||||
else if (ie is SshAuthenticationException)
|
|
||||||
{
|
|
||||||
msg = string.Format("'{0}' Authentication failure for username '{1}', ({2})",
|
|
||||||
Username, Key, ie.GetType());
|
|
||||||
Debug.Console(0, this, "Authentication failure for username '{0}', ({1})",
|
|
||||||
Username, ie.GetType());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
Debug.Console(0, this, "Error on connect:\r({0})", e);
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
Debug.Console(0, this, "Error on connect:\r({0})", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -160,7 +189,11 @@ namespace PepperDash.Core
|
|||||||
public void Disconnect()
|
public void Disconnect()
|
||||||
{
|
{
|
||||||
// Stop trying reconnects, if we are
|
// Stop trying reconnects, if we are
|
||||||
if(ReconnectTimer != null) ReconnectTimer.Stop();
|
if (ReconnectTimer != null)
|
||||||
|
{
|
||||||
|
ReconnectTimer.Stop();
|
||||||
|
ReconnectTimer = null;
|
||||||
|
}
|
||||||
// Otherwise just close up
|
// Otherwise just close up
|
||||||
if (Client != null) // && Client.IsConnected) <-- Doesn't always report properly...
|
if (Client != null) // && Client.IsConnected) <-- Doesn't always report properly...
|
||||||
{
|
{
|
||||||
@@ -181,10 +214,13 @@ namespace PepperDash.Core
|
|||||||
AutoReconnect, AutoReconnectIntervalMs);
|
AutoReconnect, AutoReconnectIntervalMs);
|
||||||
if (AutoReconnect)
|
if (AutoReconnect)
|
||||||
{
|
{
|
||||||
if (ReconnectTimer == null || !ReconnectTimerRunning)
|
if (ReconnectTimer == null)// || !ReconnectTimerRunning)
|
||||||
{
|
{
|
||||||
ReconnectTimer = new CTimer(o => Connect(), AutoReconnectIntervalMs);
|
ReconnectTimer = new CTimer(o =>
|
||||||
ReconnectTimerRunning = true;
|
{
|
||||||
|
Connect();
|
||||||
|
ReconnectTimer = null;
|
||||||
|
}, 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));
|
||||||
}
|
}
|
||||||
@@ -251,6 +287,8 @@ namespace PepperDash.Core
|
|||||||
Debug.Console(0, this, "SSH client error: {0}", e.Exception);
|
Debug.Console(0, this, "SSH client error: {0}", e.Exception);
|
||||||
UStatus = 4;
|
UStatus = 4;
|
||||||
}
|
}
|
||||||
|
Client.Disconnect();
|
||||||
|
Client = null;
|
||||||
Debug.Console(1, this, "Disconnected by remote");
|
Debug.Console(1, this, "Disconnected by remote");
|
||||||
IsConnected = false;
|
IsConnected = false;
|
||||||
HandleConnectionFailure();
|
HandleConnectionFailure();
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ using Newtonsoft.Json.Linq;
|
|||||||
|
|
||||||
namespace PepperDash.Core
|
namespace PepperDash.Core
|
||||||
{
|
{
|
||||||
public class GenericTcpIpClient : Device, IBasicCommunication
|
public class GenericTcpIpClient : Device, IBasicCommunication, IAutoReconnect
|
||||||
{
|
{
|
||||||
public event EventHandler<GenericCommMethodReceiveBytesArgs> BytesReceived;
|
public event EventHandler<GenericCommMethodReceiveBytesArgs> BytesReceived;
|
||||||
public event EventHandler<GenericCommMethodReceiveTextArgs> TextReceived;
|
public event EventHandler<GenericCommMethodReceiveTextArgs> TextReceived;
|
||||||
@@ -21,6 +21,9 @@ namespace PepperDash.Core
|
|||||||
public string Status { get { return Client.ClientStatus.ToString(); } }
|
public string Status { get { return Client.ClientStatus.ToString(); } }
|
||||||
public string ConnectionFailure { get { return Client.ClientStatus.ToString(); } }
|
public string ConnectionFailure { get { return Client.ClientStatus.ToString(); } }
|
||||||
|
|
||||||
|
public bool AutoReconnect { get; set; }
|
||||||
|
public int AutoReconnectIntervalMs { get; set; }
|
||||||
|
|
||||||
public bool Connected
|
public bool Connected
|
||||||
{
|
{
|
||||||
get { return Client.ClientStatus == SocketStatus.SOCKET_STATUS_CONNECTED; }
|
get { return Client.ClientStatus == SocketStatus.SOCKET_STATUS_CONNECTED; }
|
||||||
@@ -173,9 +176,21 @@ namespace PepperDash.Core
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public int BufferSize { get; set; }
|
public int BufferSize { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Defaults to true
|
||||||
|
/// </summary>
|
||||||
|
public bool AutoReconnect { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Defaults to 5000ms
|
||||||
|
/// </summary>
|
||||||
|
public int AutoReconnectIntervalMs { get; set; }
|
||||||
|
|
||||||
public TcpIpConfig()
|
public TcpIpConfig()
|
||||||
{
|
{
|
||||||
BufferSize = 32768;
|
BufferSize = 32768;
|
||||||
|
AutoReconnect = true;
|
||||||
|
AutoReconnectIntervalMs = 5000;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,6 +23,12 @@ namespace PepperDash.Core
|
|||||||
void Connect();
|
void Connect();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public interface IAutoReconnect
|
||||||
|
{
|
||||||
|
bool AutoReconnect { get; set; }
|
||||||
|
int AutoReconnectIntervalMs { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -82,7 +82,7 @@
|
|||||||
<Programmer />
|
<Programmer />
|
||||||
<ArchiveFilename>C:\Users\hvolm\Desktop\working\pepperdash-simplsharp-core\Pepperdash Core\Pepperdash Core\bin\PepperDash_Core.clz</ArchiveFilename>
|
<ArchiveFilename>C:\Users\hvolm\Desktop\working\pepperdash-simplsharp-core\Pepperdash Core\Pepperdash Core\bin\PepperDash_Core.clz</ArchiveFilename>
|
||||||
<MinFirmwareVersion>1.007.0017</MinFirmwareVersion>
|
<MinFirmwareVersion>1.007.0017</MinFirmwareVersion>
|
||||||
<CompiledOn>8/3/2016 4:33:09 PM</CompiledOn>
|
<CompiledOn>8/4/2016 12:58:52 PM</CompiledOn>
|
||||||
<AdditionalInfo />
|
<AdditionalInfo />
|
||||||
<EmbedSourceArchive>False</EmbedSourceArchive>
|
<EmbedSourceArchive>False</EmbedSourceArchive>
|
||||||
<CopyTo />
|
<CopyTo />
|
||||||
|
|||||||
Binary file not shown.
@@ -10,7 +10,7 @@
|
|||||||
<ArchiveName />
|
<ArchiveName />
|
||||||
</RequiredInfo>
|
</RequiredInfo>
|
||||||
<OptionalInfo>
|
<OptionalInfo>
|
||||||
<CompiledOn>8/3/2016 4:33:09 PM</CompiledOn>
|
<CompiledOn>8/4/2016 12:58:52 PM</CompiledOn>
|
||||||
<CompilerRev>1.0.0.27993</CompilerRev>
|
<CompilerRev>1.0.0.21565</CompilerRev>
|
||||||
</OptionalInfo>
|
</OptionalInfo>
|
||||||
</ProgramInfo>
|
</ProgramInfo>
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
MainAssembly=PepperDash_Core.dll:ca423b7b3c2fe76c3d6a8b9cb9ff67cd
|
MainAssembly=PepperDash_Core.dll:1e2af81f50d343b460b7e2684b9db8fe
|
||||||
MainAssemblyMinFirmwareVersion=1.007.0017
|
MainAssemblyMinFirmwareVersion=1.007.0017
|
||||||
ü
|
ü
|
||||||
DependencySource=Newtonsoft.Json.Compact.dll:ea996aa2ec65aa1878e7c9d09e37a896
|
DependencySource=Newtonsoft.Json.Compact.dll:ea996aa2ec65aa1878e7c9d09e37a896
|
||||||
|
|||||||
Binary file not shown.
Reference in New Issue
Block a user