diff --git a/Renci.SshClient/Renci.SshNet/Channels/Channel.cs b/Renci.SshClient/Renci.SshNet/Channels/Channel.cs index 02a86a17..29d842f8 100644 --- a/Renci.SshClient/Renci.SshNet/Channels/Channel.cs +++ b/Renci.SshClient/Renci.SshNet/Channels/Channel.cs @@ -201,6 +201,15 @@ namespace Renci.SshNet.Channels /// public virtual void Close() { + // Send EOF message first when channel need to be closed + this.SendMessage(new ChannelEofMessage(this.RemoteChannelNumber)); + + // Send message to close the channel on the server + this.SendMessage(new ChannelCloseMessage(this.RemoteChannelNumber)); + + // Wait for channel to be closed + this._session.WaitHandle(this._channelClosedWaitHandle); + this.Dispose(); } @@ -598,7 +607,7 @@ namespace Renci.SshNet.Channels /// public void Dispose() { - Dispose(true); + this.Dispose(true); GC.SuppressFinalize(this); } @@ -616,15 +625,6 @@ namespace Renci.SshNet.Channels // and unmanaged resources. if (disposing) { - // Send EOF message first when channel need to be closed - this.SendMessage(new ChannelEofMessage(this.RemoteChannelNumber)); - - // Send message to close the channel on the server - this.SendMessage(new ChannelCloseMessage(this.RemoteChannelNumber)); - - // Wait for channel to be closed - this._session.WaitHandle(this._channelClosedWaitHandle); - // Dispose managed resources. if (this._channelClosedWaitHandle != null) { @@ -678,7 +678,7 @@ namespace Renci.SshNet.Channels // Do not re-create Dispose clean-up code here. // Calling Dispose(false) is optimal in terms of // readability and maintainability. - Dispose(false); + this.Dispose(false); } #endregion diff --git a/Renci.SshClient/Renci.SshNet/Session.cs b/Renci.SshClient/Renci.SshNet/Session.cs index 4867b352..d52c4a1c 100644 --- a/Renci.SshClient/Renci.SshNet/Session.cs +++ b/Renci.SshClient/Renci.SshNet/Session.cs @@ -588,6 +588,11 @@ namespace Renci.SshNet /// public void Disconnect() { + this._isDisconnecting = true; + + // If socket still open try to send disconnect message to the server + this.SendDisconnect(DisconnectReason.ByApplication, "Connection terminated by the client."); + this.Dispose(); } @@ -1579,13 +1584,9 @@ namespace Renci.SshNet // and unmanaged ResourceMessages. if (disposing) { - this._isDisconnecting = true; if (this._socket != null) { - // If socket still open try to send disconnect message to the server - this.SendDisconnect(DisconnectReason.ByApplication, "Connection terminated by the client."); - this._socket.Dispose(); this._socket = null; } diff --git a/Renci.SshClient/Renci.SshNet/Shell.cs b/Renci.SshClient/Renci.SshNet/Shell.cs index de97f55b..e0aa1022 100644 --- a/Renci.SshClient/Renci.SshNet/Shell.cs +++ b/Renci.SshClient/Renci.SshNet/Shell.cs @@ -306,6 +306,12 @@ namespace Renci.SshNet this._channelClosedWaitHandle = null; } + if (this._channel != null) + { + this._channel.Dispose(); + this._channel = null; + } + if (this._dataReaderTaskCompleted != null) { this._dataReaderTaskCompleted.Dispose(); diff --git a/Renci.SshClient/Renci.SshNet/SubsystemSession.cs b/Renci.SshClient/Renci.SshNet/SubsystemSession.cs index e4691fce..08d9e2b7 100644 --- a/Renci.SshClient/Renci.SshNet/SubsystemSession.cs +++ b/Renci.SshClient/Renci.SshNet/SubsystemSession.cs @@ -14,7 +14,7 @@ using Renci.SshNet.Messages.Connection; namespace Renci.SshNet.Sftp { - internal abstract class SubsystemSession : IDisposable + public abstract class SubsystemSession : IDisposable { private Session _session; @@ -86,14 +86,9 @@ namespace Renci.SshNet.Sftp public void Disconnect() { - this.Dispose(); + this._channel.Close(); } - //public void SendData(byte[] data) - //{ - // this._session.SendMessage(new ChannelDataMessage(this._channel.RemoteChannelNumber, data)); - //} - public void SendData(ChannelDataMessage message) { this._session.SendMessage(message);