Skip to content

Commit

Permalink
Merge pull request #163 from PepperDash/hotfix/ssh-object-disposed-ex…
Browse files Browse the repository at this point in the history
…ception

fix: handles an object disposed ex
  • Loading branch information
ngenovese11 authored Aug 23, 2023
2 parents 402af2a + 4f48246 commit e9a2f7a
Showing 1 changed file with 75 additions and 39 deletions.
114 changes: 75 additions & 39 deletions Pepperdash Core/Pepperdash Core/Comm/GenericSshClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,6 @@ public void Connect()
Debug.Console(1, this, "Creating new SshClient");
ConnectionInfo connectionInfo = new ConnectionInfo(Hostname, Port, Username, pauth, kauth);
Client = new SshClient(connectionInfo);

Client.ErrorOccurred -= Client_ErrorOccurred;
Client.ErrorOccurred += Client_ErrorOccurred;

//Attempt to connect
Expand Down Expand Up @@ -320,7 +318,7 @@ public void Disconnect()
if (ReconnectTimer != null)
{
ReconnectTimer.Stop();
ReconnectTimer = null;
// ReconnectTimer = null;
}

KillClient(SocketStatus.SOCKET_STATUS_BROKEN_LOCALLY);
Expand All @@ -333,12 +331,21 @@ private void KillClient(SocketStatus status)
{
KillStream();

if (Client != null)
{
Client.Disconnect();
Client = null;
ClientStatus = status;
Debug.Console(1, this, "Disconnected");
try
{
if (Client != null)
{
Client.ErrorOccurred -= Client_ErrorOccurred;
Client.Disconnect();
Client.Dispose();
Client = null;
ClientStatus = status;
Debug.Console(1, this, "Disconnected");
}
}
catch (Exception ex)
{
Debug.Console(0, this, Debug.ErrorLogLevel.Notice, "Exception in Kill Client:{0}", ex);
}
}

Expand Down Expand Up @@ -375,14 +382,21 @@ void HandleConnectionFailure()
/// </summary>
void KillStream()
{
if (TheStream != null)
{
TheStream.DataReceived -= Stream_DataReceived;
TheStream.Close();
TheStream.Dispose();
TheStream = null;
Debug.Console(1, this, "Disconnected stream");
}
try
{
if (TheStream != null)
{
TheStream.DataReceived -= Stream_DataReceived;
TheStream.Close();
TheStream.Dispose();
TheStream = null;
Debug.Console(1, this, "Disconnected stream");
}
}
catch (Exception ex)
{
Debug.Console(0, this, Debug.ErrorLogLevel.Notice, "Exception in Kill Stream:{0}", ex);
}
}

/// <summary>
Expand Down Expand Up @@ -473,28 +487,39 @@ void OnConnectionChange()
/// <param name="text"></param>
public void SendText(string text)
{
try
{
if (Client != null && TheStream != null && IsConnected)
{
if (StreamDebugging.TxStreamDebuggingIsEnabled)
Debug.Console(0, this, "Sending {0} characters of text: '{1}'", text.Length, ComTextHelper.GetDebugText(text));

TheStream.Write(text);
TheStream.Flush();
try
{
if (Client != null && TheStream != null && IsConnected)
{
if (StreamDebugging.TxStreamDebuggingIsEnabled)
Debug.Console(0,
this,
"Sending {0} characters of text: '{1}'",
text.Length,
ComTextHelper.GetDebugText(text));

TheStream.Write(text);
TheStream.Flush();
}
else
{
Debug.Console(1, this, "Client is null or disconnected. Cannot Send Text");
}
}
catch (ObjectDisposedException ex)
{
Debug.Console(0, this, Debug.ErrorLogLevel.Notice, "Exception: {0}", ex.Message);
Debug.Console(1, this, Debug.ErrorLogLevel.Notice, "Stack Trace: {0}", ex.StackTrace);

}
else
{
Debug.Console(1, this, "Client is null or disconnected. Cannot Send Text");
}
}
KillClient(SocketStatus.SOCKET_STATUS_CONNECT_FAILED);
ReconnectTimer.Reset();
}
catch (Exception ex)
{
Debug.Console(0, "Exception: {0}", ex.Message);
Debug.Console(0, "Stack Trace: {0}", ex.StackTrace);
Debug.Console(0, this, Debug.ErrorLogLevel.Notice, "Exception: {0}", ex.Message);
Debug.Console(1, this, Debug.ErrorLogLevel.Notice, "Stack Trace: {0}", ex.StackTrace);

Debug.Console(1, this, Debug.ErrorLogLevel.Error, "Stream write failed. Disconnected, closing");
Debug.Console(1, this, Debug.ErrorLogLevel.Error, "Stream write failed");
}
}

Expand All @@ -518,10 +543,21 @@ public void SendBytes(byte[] bytes)
{
Debug.Console(1, this, "Client is null or disconnected. Cannot Send Bytes");
}
}
catch
{
Debug.Console(1, this, Debug.ErrorLogLevel.Error, "Stream write failed. Disconnected, closing");
}
catch (ObjectDisposedException ex)
{
Debug.Console(0, this, Debug.ErrorLogLevel.Notice, "Exception: {0}", ex.Message);
Debug.Console(1, this, Debug.ErrorLogLevel.Notice, "Stack Trace: {0}", ex.StackTrace);

KillClient(SocketStatus.SOCKET_STATUS_CONNECT_FAILED);
ReconnectTimer.Reset();
}
catch (Exception ex)
{
Debug.Console(0, this, Debug.ErrorLogLevel.Notice, "Exception: {0}", ex.Message);
Debug.Console(1, this, Debug.ErrorLogLevel.Notice, "Stack Trace: {0}", ex.StackTrace);

Debug.Console(1, this, Debug.ErrorLogLevel.Error, "Stream write failed");
}
}

Expand Down

0 comments on commit e9a2f7a

Please sign in to comment.