Merged in bugfix/pdc-41 (pull request #42)

Bugfix/pdc 41

Approved-by: Neil Dorin <ndorin@pepperdash.com>
This commit is contained in:
Neil Dorin
2020-01-29 17:23:07 +00:00

View File

@@ -175,7 +175,6 @@ namespace PepperDash.Core
{ {
Debug.Console(1, this, "Program stopping. Closing connection"); Debug.Console(1, this, "Program stopping. Closing connection");
Disconnect(); Disconnect();
//Client.Dispose();
} }
} }
} }
@@ -207,29 +206,27 @@ namespace PepperDash.Core
return; return;
} }
// Cleanup the old client if it already exists
if (Client != null)
{
Debug.Console(1, this, "Cleaning up disconnected client");
Client.ErrorOccurred -= Client_ErrorOccurred;
KillClient(SocketStatus.SOCKET_STATUS_BROKEN_LOCALLY);
}
// 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);
if (Client != null)
{
Debug.Console(1, this, "Cleaning up disconnected client");
Client.ErrorOccurred -= Client_ErrorOccurred;
KillStream();
}
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);
if (Client == null)
{
Client = new SshClient(connectionInfo); Client = new SshClient(connectionInfo);
}
Client.ErrorOccurred -= Client_ErrorOccurred; Client.ErrorOccurred -= Client_ErrorOccurred;
Client.ErrorOccurred += Client_ErrorOccurred; Client.ErrorOccurred += Client_ErrorOccurred;
//You can do it! //Attempt to connect
ClientStatus = SocketStatus.SOCKET_STATUS_WAITING; ClientStatus = SocketStatus.SOCKET_STATUS_WAITING;
try try
{ {
@@ -282,13 +279,21 @@ namespace PepperDash.Core
ReconnectTimer = null; ReconnectTimer = null;
} }
KillClient(SocketStatus.SOCKET_STATUS_BROKEN_LOCALLY);
}
/// <summary>
/// Kills the stream, cleans up the client and sets it to null
/// </summary>
private void KillClient(SocketStatus status)
{
KillStream(); KillStream();
if (Client != null) if (Client != null)
{ {
Client.Disconnect(); Client.Disconnect();
Client = null; Client = null;
ClientStatus = SocketStatus.SOCKET_STATUS_BROKEN_LOCALLY; ClientStatus = status;
Debug.Console(1, this, "Disconnected"); Debug.Console(1, this, "Disconnected");
} }
} }
@@ -298,9 +303,9 @@ namespace PepperDash.Core
/// </summary> /// </summary>
void HandleConnectionFailure() void HandleConnectionFailure()
{ {
if (Client != null) KillClient(SocketStatus.SOCKET_STATUS_CONNECT_FAILED);
Client.Disconnect();
KillStream(); Debug.Console(2, this, "Client nulled due to connection failure. AutoReconnect: {0}, ConnectEnabled: {1}", AutoReconnect, ConnectEnabled);
if (AutoReconnect && ConnectEnabled) if (AutoReconnect && ConnectEnabled)
{ {
@@ -324,24 +329,20 @@ namespace PepperDash.Core
} }
} }
/// <summary>
/// Kills the stream
/// </summary>
void KillStream() void KillStream()
{ {
if (TheStream != null) if (TheStream != null)
{ {
TheStream.DataReceived -= Stream_DataReceived; TheStream.DataReceived -= Stream_DataReceived;
//TheStream.ErrorOccurred -= TheStream_ErrorOccurred;
TheStream.Close(); TheStream.Close();
TheStream.Dispose(); TheStream.Dispose();
TheStream = null; TheStream = null;
} }
} }
//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>
@@ -372,17 +373,6 @@ namespace PepperDash.Core
} }
} }
///// <summary>
///// Handler for errors on the stream...
///// </summary>
///// <param name="sender"></param>
///// <param name="e"></param>
//void TheStream_ErrorOccurred(object sender, ExceptionEventArgs e)
//{
// Debug.Console(1, this, "Unhandled SSH STREAM error: {0}", e.Exception);
// ClientStatus = SocketStatus.SOCKET_STATUS_BROKEN_REMOTELY;
// HandleConnectionFailure();
//}
/// <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
@@ -411,15 +401,18 @@ namespace PepperDash.Core
#region IBasicCommunication Members #region IBasicCommunication Members
/// <summary> /// <summary>
/// /// 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.Write(text); TheStream.Write(text);
TheStream.Flush(); TheStream.Flush();
}
} }
catch catch
{ {
@@ -429,12 +422,19 @@ namespace PepperDash.Core
} }
} }
/// <summary>
/// Sends Bytes to the server
/// </summary>
/// <param name="bytes"></param>
public void SendBytes(byte[] bytes) public void SendBytes(byte[] bytes)
{ {
try try
{ {
if (Client != null)
{
TheStream.Write(bytes, 0, bytes.Length); TheStream.Write(bytes, 0, bytes.Length);
TheStream.Flush(); TheStream.Flush();
}
} }
catch catch
{ {
@@ -454,16 +454,36 @@ namespace PepperDash.Core
/// </summary> /// </summary>
public class SshConnectionChangeEventArgs : EventArgs public class SshConnectionChangeEventArgs : EventArgs
{ {
/// <summary>
/// Connection State
/// </summary>
public bool IsConnected { get; private set; } public bool IsConnected { get; private set; }
/// <summary>
/// Connection Status represented as a ushort
/// </summary>
public ushort UIsConnected { get { return (ushort)(Client.IsConnected ? 1 : 0); } } public ushort UIsConnected { get { return (ushort)(Client.IsConnected ? 1 : 0); } }
/// <summary>
/// The client
/// </summary>
public GenericSshClient Client { get; private set; } public GenericSshClient Client { get; private set; }
/// <summary>
/// Socket Status as represented by
/// </summary>
public ushort Status { get { return Client.UStatus; } } public ushort Status { get { return Client.UStatus; } }
// S+ Constructor /// <summary>
/// S+ Constructor
/// </summary>
public SshConnectionChangeEventArgs() { } public SshConnectionChangeEventArgs() { }
/// <summary>
/// EventArgs class
/// </summary>
/// <param name="isConnected">Connection State</param>
/// <param name="client">The Client</param>
public SshConnectionChangeEventArgs(bool isConnected, GenericSshClient client) public SshConnectionChangeEventArgs(bool isConnected, GenericSshClient client)
{ {
IsConnected = isConnected; IsConnected = isConnected;