Working through connect/disconnect details

This commit is contained in:
Heath Volmer
2016-09-23 10:44:21 -06:00
parent 8aa9f130cd
commit d5970f76ce
8 changed files with 121 additions and 67 deletions

View File

@@ -7,28 +7,9 @@ using Crestron.SimplSharp.Ssh.Common;
namespace PepperDash.Core
{
public class ConnectionChangeEventArgs : EventArgs
{
public bool IsConnected { get; private set; }
public ushort UIsConnected { get { return (ushort)(Client.IsConnected ? 1 : 0); } }
public GenericSshClient Client { get; private set; }
public ushort Status { get { return Client.UStatus; } }
// S+ Constructor
public ConnectionChangeEventArgs() { }
public ConnectionChangeEventArgs(bool isConnected, GenericSshClient client)
{
IsConnected = isConnected;
Client = client;
}
}
//*****************************************************************************************************
//*****************************************************************************************************
/// <summary>
///
/// </summary>
public class GenericSshClient : Device, IBasicCommunication, IAutoReconnect
{
/// <summary>
@@ -44,24 +25,39 @@ namespace PepperDash.Core
/// <summary>
/// Event when the connection status changes.
/// </summary>
public event EventHandler<ConnectionChangeEventArgs> ConnectionChange;
public event EventHandler<SshConnectionChangeEventArgs> ConnectionChange;
/// <summary>
/// Address of server
/// </summary>
public string Hostname { get; set; }
/// <summary>
/// Port on server
/// </summary>
public int Port { get; set; }
/// <summary>
/// Username for server
/// </summary>
public string Username { get; set; }
/// <summary>
/// And... Password for server. That was worth documenting!
/// </summary>
public string Password { get; set; }
/// <summary>
/// True when the server is connected - when status == 2.
/// </summary>
public bool IsConnected
{
// returns false if no client or not connected
get { return UStatus == 2; }
}
/// <summary>
/// Contains the familiar Simpl analog status values
/// Contains the familiar Simpl analog status values. This drives the ConnectionChange event
/// and IsConnected with be true when this == 2.
/// </summary>
public ushort UStatus
{
@@ -101,6 +97,11 @@ namespace PepperDash.Core
ShellStream TheStream;
CTimer ReconnectTimer;
string PreviousHostname;
int PreviousPort;
string PreviousUsername;
string PreviousPassword;
/// <summary>
/// Typical constructor.
/// </summary>
@@ -143,7 +144,7 @@ namespace PepperDash.Core
{
if (Client != null)
{
Debug.Console(2, this, "Program stopping. Closing connection");
Debug.Console(1, this, "Program stopping. Closing connection");
Client.Disconnect();
Client.Dispose();
}
@@ -176,26 +177,43 @@ namespace PepperDash.Core
return;
}
//You can do it!
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
// **** MAY WANT TO CHANGE THIS BECAUSE OF SOCKET LEAKS ****
if (Client != null)
//if (Client != null)
//{
// Client.Disconnect();
// Client = null;
//}
// Make a new client if we need it or things have changed
if (Client == null || PropertiesHaveChanged())
{
Client.Disconnect();
Client = null;
}
Client = new SshClient(connectionInfo);
Client.ErrorOccurred += Client_ErrorOccurred;
if (Client != null)
{
Debug.Console(2, this, "Cleaning up disconnected client");
Client.ErrorOccurred -= Client_ErrorOccurred;
if(TheStream != null)
TheStream.DataReceived -= Stream_DataReceived;
TheStream = null;
}
Debug.Console(2, this, "Creating new SshClient");
ConnectionInfo connectionInfo = new ConnectionInfo(Hostname, Port, Username, pauth, kauth);
Client = new SshClient(connectionInfo);
Client.ErrorOccurred += Client_ErrorOccurred;
}
PreviousHostname = Hostname;
PreviousPassword = Password;
PreviousPort = Port;
PreviousUsername = Username;
//You can do it!
UStatus = 1;
try
{
Client.Connect();
@@ -207,7 +225,10 @@ namespace PepperDash.Core
TheStream.DataReceived += Stream_DataReceived;
Debug.Console(1, this, "Connected");
UStatus = 2;
//IsConnected = true;
PreviousHostname = Hostname;
PreviousPassword = Password;
PreviousPort = Port;
PreviousUsername = Username;
}
return;
}
@@ -232,10 +253,9 @@ namespace PepperDash.Core
Debug.Console(0, this, "Unhandled exception on connect:\r({0})", e);
}
// Sucess will not make it this far
Client.Disconnect();
UStatus = 3;
//IsConnected = false;
HandleConnectionFailure();
}
@@ -250,34 +270,34 @@ namespace PepperDash.Core
ReconnectTimer.Stop();
ReconnectTimer = null;
}
DiscoAndCleanup();
if(TheStream != null)
TheStream.DataReceived -= Stream_DataReceived;
Client.Disconnect();
UStatus = 5;
//IsConnected = false;
Debug.Console(1, this, "Disconnected");
}
/// <summary>
///
/// </summary>
void DiscoAndCleanup()
{
if (Client != null)
{
Client.ErrorOccurred -= Client_ErrorOccurred;
TheStream.DataReceived -= Stream_DataReceived;
Debug.Console(2, this, "Cleaning up disconnected client");
Client.Disconnect();
Client.Dispose();
Client = null;
}
}
//void DiscoAndCleanup()
//{
// if (Client != null)
// {
// Client.ErrorOccurred -= Client_ErrorOccurred;
// TheStream.DataReceived -= Stream_DataReceived;
// Debug.Console(2, this, "Cleaning up disconnected client");
// Client.Disconnect();
// Client.Dispose();
// Client = null;
// }
//}
/// <summary>
/// Anything to do with reestablishing connection on failures
/// </summary>
void HandleConnectionFailure()
{
DiscoAndCleanup();
Debug.Console(2, this, "Checking autoreconnect: {0}, {1}ms",
AutoReconnect, AutoReconnectIntervalMs);
if (AutoReconnect)
@@ -300,6 +320,12 @@ namespace PepperDash.Core
}
}
bool PropertiesHaveChanged()
{
return Hostname != PreviousHostname || Port != PreviousPort
|| Username != PreviousUsername || Password != PreviousPassword;
}
/// <summary>
/// Handles the keyboard interactive authentication, should it be required.
/// </summary>
@@ -344,11 +370,10 @@ namespace PepperDash.Core
if (Client != null)
{
Client.Disconnect();
Client.Dispose();
Client = null;
//Client.Dispose();
//Client = null;
}
UStatus = 4;
//IsConnected = false;
HandleConnectionFailure();
}
@@ -358,7 +383,7 @@ namespace PepperDash.Core
void OnConnectionChange()
{
if(ConnectionChange != null)
ConnectionChange(this, new ConnectionChangeEventArgs(IsConnected, this));
ConnectionChange(this, new SshConnectionChangeEventArgs(IsConnected, this));
}
#region IBasicCommunication Members
@@ -378,7 +403,6 @@ namespace PepperDash.Core
{
Debug.Console(1, this, "Stream write failed. Disconnected, closing");
UStatus = 4;
//IsConnected = false;
HandleConnectionFailure();
}
}
@@ -394,11 +418,34 @@ namespace PepperDash.Core
{
Debug.Console(1, this, "Stream write failed. Disconnected, closing");
UStatus = 4;
//IsConnected = false;
HandleConnectionFailure();
}
}
#endregion
}
//*****************************************************************************************************
//*****************************************************************************************************
/// <summary>
/// Fired when connection changes
/// </summary>
public class SshConnectionChangeEventArgs : EventArgs
{
public bool IsConnected { get; private set; }
public ushort UIsConnected { get { return (ushort)(Client.IsConnected ? 1 : 0); } }
public GenericSshClient Client { get; private set; }
public ushort Status { get { return Client.UStatus; } }
// S+ Constructor
public SshConnectionChangeEventArgs() { }
public SshConnectionChangeEventArgs(bool isConnected, GenericSshClient client)
{
IsConnected = isConnected;
Client = client;
}
}
}

View File

@@ -85,7 +85,7 @@
<Programmer />
<ArchiveFilename>C:\Users\hvolm\Desktop\working\pepperdash-simplsharp-core\Pepperdash Core\Pepperdash Core\bin\PepperDash_Core.clz</ArchiveFilename>
<MinFirmwareVersion>1.007.0017</MinFirmwareVersion>
<CompiledOn>9/22/2016 9:42:40 PM</CompiledOn>
<CompiledOn>9/23/2016 9:37:04 AM</CompiledOn>
<AdditionalInfo />
<EmbedSourceArchive>False</EmbedSourceArchive>
<CopyTo />

View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<ControlSystem>
<Name>MC3 SSH</Name>
<Address>ssh 10.0.0.15</Address>
<ProgramSlot />
<Storage />
</ControlSystem>

View File

@@ -10,7 +10,7 @@
<ArchiveName />
</RequiredInfo>
<OptionalInfo>
<CompiledOn>9/22/2016 9:42:40 PM</CompiledOn>
<CompilerRev>1.0.0.37279</CompilerRev>
<CompiledOn>9/23/2016 9:37:04 AM</CompiledOn>
<CompilerRev>1.0.0.15511</CompilerRev>
</OptionalInfo>
</ProgramInfo>

View File

@@ -1,4 +1,4 @@
MainAssembly=PepperDash_Core.dll:022f5296f4c24102c3cb64cd3a10fa41
MainAssembly=PepperDash_Core.dll:ebb9549e48b1f82bec0efdb69443c915
MainAssemblyMinFirmwareVersion=1.007.0017
ü
DependencySource=Newtonsoft.Json.Compact.dll:ea996aa2ec65aa1878e7c9d09e37a896