Working through ssh disconnect / leak problems

This commit is contained in:
Heath Volmer
2016-08-04 13:14:32 -06:00
parent cb5d36de51
commit 0f0b61afa2
8 changed files with 117 additions and 58 deletions

View File

@@ -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,13 +104,22 @@ 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; UStatus = 1;
IsConnected = false; IsConnected = false;
@@ -104,7 +128,15 @@ namespace PepperDash.Core
kauth.AuthenticationPrompt += new EventHandler<AuthenticationPromptEventArgs>(kauth_AuthenticationPrompt); kauth.AuthenticationPrompt += new EventHandler<AuthenticationPromptEventArgs>(kauth_AuthenticationPrompt);
PasswordAuthenticationMethod pauth = new PasswordAuthenticationMethod(Username, Password); PasswordAuthenticationMethod pauth = new PasswordAuthenticationMethod(Username, Password);
ConnectionInfo connectionInfo = new ConnectionInfo(Hostname, Port, Username, pauth, kauth); ConnectionInfo connectionInfo = new ConnectionInfo(Hostname, Port, Username, pauth, kauth);
// always spin up new client in case parameters have changed
if (Client != null)
{
Client.Disconnect();
Client = null;
//Client.Dispose();
}
Client = new SshClient(connectionInfo); Client = new SshClient(connectionInfo);
Client.ErrorOccurred += Client_ErrorOccurred; Client.ErrorOccurred += Client_ErrorOccurred;
try try
{ {
@@ -117,8 +149,6 @@ namespace PepperDash.Core
Debug.Console(1, this, "Connected"); Debug.Console(1, this, "Connected");
TheStream = Client.CreateShellStream("PDTShell", 100, 80, 100, 200, 65534); TheStream = Client.CreateShellStream("PDTShell", 100, 80, 100, 200, 65534);
TheStream.DataReceived += Stream_DataReceived; TheStream.DataReceived += Stream_DataReceived;
//TheStream.ErrorOccurred += Stream_ErrorOccurred;
} }
return; return;
} }
@@ -142,7 +172,6 @@ namespace PepperDash.Core
Debug.Console(0, this, "Error on connect:\r({0})", e); Debug.Console(0, this, "Error on connect:\r({0})", e);
} }
} }
}
else else
{ {
Debug.Console(0, this, "Connect failed. Check hostname, port, username and password are set or not null"); Debug.Console(0, this, "Connect failed. Check hostname, port, username and password are set or not null");
@@ -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();

View File

@@ -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;
} }
} }

View File

@@ -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>

View File

@@ -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 />

View File

@@ -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>

View File

@@ -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