Ver Fonte

Refactor Dispose to avoid calling any socket related commands from it

olegkap_cp há 14 anos atrás
pai
commit
428b026c48

+ 11 - 11
Renci.SshClient/Renci.SshNet/Channels/Channel.cs

@@ -201,6 +201,15 @@ namespace Renci.SshNet.Channels
         /// </summary>
         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
         /// </summary>
         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

+ 5 - 4
Renci.SshClient/Renci.SshNet/Session.cs

@@ -588,6 +588,11 @@ namespace Renci.SshNet
         /// </summary>
         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;
                     }

+ 6 - 0
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();

+ 2 - 7
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);