mirror of
https://github.com/PepperDash/PepperDashCore.git
synced 2026-02-15 20:54:46 +00:00
Working through connect/disconnect details
This commit is contained in:
@@ -7,28 +7,9 @@ using Crestron.SimplSharp.Ssh.Common;
|
|||||||
|
|
||||||
namespace PepperDash.Core
|
namespace PepperDash.Core
|
||||||
{
|
{
|
||||||
public class ConnectionChangeEventArgs : EventArgs
|
/// <summary>
|
||||||
{
|
///
|
||||||
public bool IsConnected { get; private set; }
|
/// </summary>
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//*****************************************************************************************************
|
|
||||||
//*****************************************************************************************************
|
|
||||||
|
|
||||||
public class GenericSshClient : Device, IBasicCommunication, IAutoReconnect
|
public class GenericSshClient : Device, IBasicCommunication, IAutoReconnect
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -44,24 +25,39 @@ namespace PepperDash.Core
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Event when the connection status changes.
|
/// Event when the connection status changes.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public event EventHandler<ConnectionChangeEventArgs> ConnectionChange;
|
public event EventHandler<SshConnectionChangeEventArgs> ConnectionChange;
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Address of server
|
||||||
|
/// </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>
|
||||||
|
/// Username for server
|
||||||
|
/// </summary>
|
||||||
public string Username { get; set; }
|
public string Username { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// And... Password for server. That was worth documenting!
|
||||||
|
/// </summary>
|
||||||
public string Password { get; set; }
|
public string Password { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// True when the server is connected - when status == 2.
|
||||||
|
/// </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 UStatus == 2; }
|
get { return UStatus == 2; }
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <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>
|
/// </summary>
|
||||||
public ushort UStatus
|
public ushort UStatus
|
||||||
{
|
{
|
||||||
@@ -101,6 +97,11 @@ namespace PepperDash.Core
|
|||||||
ShellStream TheStream;
|
ShellStream TheStream;
|
||||||
CTimer ReconnectTimer;
|
CTimer ReconnectTimer;
|
||||||
|
|
||||||
|
string PreviousHostname;
|
||||||
|
int PreviousPort;
|
||||||
|
string PreviousUsername;
|
||||||
|
string PreviousPassword;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Typical constructor.
|
/// Typical constructor.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -143,7 +144,7 @@ namespace PepperDash.Core
|
|||||||
{
|
{
|
||||||
if (Client != null)
|
if (Client != null)
|
||||||
{
|
{
|
||||||
Debug.Console(2, this, "Program stopping. Closing connection");
|
Debug.Console(1, this, "Program stopping. Closing connection");
|
||||||
Client.Disconnect();
|
Client.Disconnect();
|
||||||
Client.Dispose();
|
Client.Dispose();
|
||||||
}
|
}
|
||||||
@@ -176,26 +177,43 @@ namespace PepperDash.Core
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//You can do it!
|
|
||||||
UStatus = 1;
|
|
||||||
//IsConnected = false;
|
|
||||||
|
|
||||||
// This handles both password and keyboard-interactive (like on OS-X, 'nixes)
|
// This handles both password and keyboard-interactive (like on OS-X, 'nixes)
|
||||||
KeyboardInteractiveAuthenticationMethod kauth = new KeyboardInteractiveAuthenticationMethod(Username);
|
KeyboardInteractiveAuthenticationMethod kauth = new KeyboardInteractiveAuthenticationMethod(Username);
|
||||||
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);
|
|
||||||
|
|
||||||
// always spin up new client in case parameters have changed
|
// always spin up new client in case parameters have changed
|
||||||
// **** MAY WANT TO CHANGE THIS BECAUSE OF SOCKET LEAKS ****
|
// **** 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();
|
if (Client != null)
|
||||||
Client = null;
|
{
|
||||||
}
|
Debug.Console(2, this, "Cleaning up disconnected client");
|
||||||
Client = new SshClient(connectionInfo);
|
Client.ErrorOccurred -= Client_ErrorOccurred;
|
||||||
|
if(TheStream != null)
|
||||||
Client.ErrorOccurred += Client_ErrorOccurred;
|
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
|
try
|
||||||
{
|
{
|
||||||
Client.Connect();
|
Client.Connect();
|
||||||
@@ -207,7 +225,10 @@ namespace PepperDash.Core
|
|||||||
TheStream.DataReceived += Stream_DataReceived;
|
TheStream.DataReceived += Stream_DataReceived;
|
||||||
Debug.Console(1, this, "Connected");
|
Debug.Console(1, this, "Connected");
|
||||||
UStatus = 2;
|
UStatus = 2;
|
||||||
//IsConnected = true;
|
PreviousHostname = Hostname;
|
||||||
|
PreviousPassword = Password;
|
||||||
|
PreviousPort = Port;
|
||||||
|
PreviousUsername = Username;
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -232,10 +253,9 @@ namespace PepperDash.Core
|
|||||||
Debug.Console(0, this, "Unhandled exception on connect:\r({0})", e);
|
Debug.Console(0, this, "Unhandled exception on connect:\r({0})", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Sucess will not make it this far
|
// Sucess will not make it this far
|
||||||
|
Client.Disconnect();
|
||||||
UStatus = 3;
|
UStatus = 3;
|
||||||
//IsConnected = false;
|
|
||||||
HandleConnectionFailure();
|
HandleConnectionFailure();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -250,34 +270,34 @@ namespace PepperDash.Core
|
|||||||
ReconnectTimer.Stop();
|
ReconnectTimer.Stop();
|
||||||
ReconnectTimer = null;
|
ReconnectTimer = null;
|
||||||
}
|
}
|
||||||
DiscoAndCleanup();
|
if(TheStream != null)
|
||||||
|
TheStream.DataReceived -= Stream_DataReceived;
|
||||||
|
Client.Disconnect();
|
||||||
UStatus = 5;
|
UStatus = 5;
|
||||||
//IsConnected = false;
|
Debug.Console(1, this, "Disconnected");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
void DiscoAndCleanup()
|
//void DiscoAndCleanup()
|
||||||
{
|
//{
|
||||||
if (Client != null)
|
// if (Client != null)
|
||||||
{
|
// {
|
||||||
Client.ErrorOccurred -= Client_ErrorOccurred;
|
// Client.ErrorOccurred -= Client_ErrorOccurred;
|
||||||
TheStream.DataReceived -= Stream_DataReceived;
|
// TheStream.DataReceived -= Stream_DataReceived;
|
||||||
Debug.Console(2, this, "Cleaning up disconnected client");
|
// Debug.Console(2, this, "Cleaning up disconnected client");
|
||||||
Client.Disconnect();
|
// Client.Disconnect();
|
||||||
Client.Dispose();
|
// Client.Dispose();
|
||||||
Client = null;
|
// Client = null;
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Anything to do with reestablishing connection on failures
|
/// Anything to do with reestablishing connection on failures
|
||||||
/// </summary>
|
/// </summary>
|
||||||
void HandleConnectionFailure()
|
void HandleConnectionFailure()
|
||||||
{
|
{
|
||||||
DiscoAndCleanup();
|
|
||||||
|
|
||||||
Debug.Console(2, this, "Checking autoreconnect: {0}, {1}ms",
|
Debug.Console(2, this, "Checking autoreconnect: {0}, {1}ms",
|
||||||
AutoReconnect, AutoReconnectIntervalMs);
|
AutoReconnect, AutoReconnectIntervalMs);
|
||||||
if (AutoReconnect)
|
if (AutoReconnect)
|
||||||
@@ -300,6 +320,12 @@ namespace PepperDash.Core
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool PropertiesHaveChanged()
|
||||||
|
{
|
||||||
|
return Hostname != PreviousHostname || Port != PreviousPort
|
||||||
|
|| Username != PreviousUsername || Password != PreviousPassword;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Handles the keyboard interactive authentication, should it be required.
|
/// Handles the keyboard interactive authentication, should it be required.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -344,11 +370,10 @@ namespace PepperDash.Core
|
|||||||
if (Client != null)
|
if (Client != null)
|
||||||
{
|
{
|
||||||
Client.Disconnect();
|
Client.Disconnect();
|
||||||
Client.Dispose();
|
//Client.Dispose();
|
||||||
Client = null;
|
//Client = null;
|
||||||
}
|
}
|
||||||
UStatus = 4;
|
UStatus = 4;
|
||||||
//IsConnected = false;
|
|
||||||
HandleConnectionFailure();
|
HandleConnectionFailure();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -358,7 +383,7 @@ namespace PepperDash.Core
|
|||||||
void OnConnectionChange()
|
void OnConnectionChange()
|
||||||
{
|
{
|
||||||
if(ConnectionChange != null)
|
if(ConnectionChange != null)
|
||||||
ConnectionChange(this, new ConnectionChangeEventArgs(IsConnected, this));
|
ConnectionChange(this, new SshConnectionChangeEventArgs(IsConnected, this));
|
||||||
}
|
}
|
||||||
|
|
||||||
#region IBasicCommunication Members
|
#region IBasicCommunication Members
|
||||||
@@ -378,7 +403,6 @@ namespace PepperDash.Core
|
|||||||
{
|
{
|
||||||
Debug.Console(1, this, "Stream write failed. Disconnected, closing");
|
Debug.Console(1, this, "Stream write failed. Disconnected, closing");
|
||||||
UStatus = 4;
|
UStatus = 4;
|
||||||
//IsConnected = false;
|
|
||||||
HandleConnectionFailure();
|
HandleConnectionFailure();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -394,11 +418,34 @@ namespace PepperDash.Core
|
|||||||
{
|
{
|
||||||
Debug.Console(1, this, "Stream write failed. Disconnected, closing");
|
Debug.Console(1, this, "Stream write failed. Disconnected, closing");
|
||||||
UStatus = 4;
|
UStatus = 4;
|
||||||
//IsConnected = false;
|
|
||||||
HandleConnectionFailure();
|
HandleConnectionFailure();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -85,7 +85,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>9/22/2016 9:42:40 PM</CompiledOn>
|
<CompiledOn>9/23/2016 9:37:04 AM</CompiledOn>
|
||||||
<AdditionalInfo />
|
<AdditionalInfo />
|
||||||
<EmbedSourceArchive>False</EmbedSourceArchive>
|
<EmbedSourceArchive>False</EmbedSourceArchive>
|
||||||
<CopyTo />
|
<CopyTo />
|
||||||
|
|||||||
@@ -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>
|
||||||
Binary file not shown.
@@ -10,7 +10,7 @@
|
|||||||
<ArchiveName />
|
<ArchiveName />
|
||||||
</RequiredInfo>
|
</RequiredInfo>
|
||||||
<OptionalInfo>
|
<OptionalInfo>
|
||||||
<CompiledOn>9/22/2016 9:42:40 PM</CompiledOn>
|
<CompiledOn>9/23/2016 9:37:04 AM</CompiledOn>
|
||||||
<CompilerRev>1.0.0.37279</CompilerRev>
|
<CompilerRev>1.0.0.15511</CompilerRev>
|
||||||
</OptionalInfo>
|
</OptionalInfo>
|
||||||
</ProgramInfo>
|
</ProgramInfo>
|
||||||
Binary file not shown.
@@ -1,4 +1,4 @@
|
|||||||
MainAssembly=PepperDash_Core.dll:022f5296f4c24102c3cb64cd3a10fa41
|
MainAssembly=PepperDash_Core.dll:ebb9549e48b1f82bec0efdb69443c915
|
||||||
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