2
0
Эх сурвалжийг харах

Avoid extra IsOpen invocation when attempting to open channel session.

drieseng 9 жил өмнө
parent
commit
3808aae801

+ 23 - 22
src/Renci.SshNet/Channels/ChannelSession.cs

@@ -2,7 +2,6 @@
 using System.Collections.Generic;
 using System.Globalization;
 using System.Threading;
-using Renci.SshNet.Abstractions;
 using Renci.SshNet.Common;
 using Renci.SshNet.Messages.Connection;
 
@@ -65,27 +64,27 @@ namespace Renci.SshNet.Channels
         /// </summary>
         public virtual void Open()
         {
-            if (!IsOpen)
+            if (IsOpen)
+                return;
+
+            //  Try to open channel several times
+            do
             {
-                //  Try to open channel several times
-                while (!IsOpen && _failedOpenAttempts < ConnectionInfo.RetryAttempts)
+                SendChannelOpenMessage();
+                try
+                {
+                    WaitOnHandle(_channelOpenResponseWaitHandle);
+                }
+                catch (Exception)
                 {
-                    SendChannelOpenMessage();
-                    try
-                    {
-                        WaitOnHandle(_channelOpenResponseWaitHandle);
-                    }
-                    catch (Exception)
-                    {
-                        // avoid leaking session semaphore
-                        ReleaseSemaphore();
-                        throw;
-                    }
+                    // avoid leaking session semaphore
+                    ReleaseSemaphore();
+                    throw;
                 }
+            } while (!IsOpen && _failedOpenAttempts < ConnectionInfo.RetryAttempts);
 
-                if (!IsOpen)
-                    throw new SshException(string.Format(CultureInfo.CurrentCulture, "Failed to open a channel after {0} attempts.", _failedOpenAttempts));
-            }
+            if (!IsOpen)
+                throw new SshException(string.Format(CultureInfo.CurrentCulture, "Failed to open a channel after {0} attempts.", _failedOpenAttempts));
         }
 
         /// <summary>
@@ -386,16 +385,18 @@ namespace Renci.SshNet.Channels
 
             if (disposing)
             {
-                if (_channelOpenResponseWaitHandle != null)
+                var channelOpenResponseWaitHandle = _channelOpenResponseWaitHandle;
+                if (channelOpenResponseWaitHandle != null)
                 {
-                    _channelOpenResponseWaitHandle.Dispose();
                     _channelOpenResponseWaitHandle = null;
+                    channelOpenResponseWaitHandle.Dispose();
                 }
 
-                if (_channelRequestResponse != null)
+                var channelRequestResponse = _channelRequestResponse;
+                if (channelRequestResponse != null)
                 {
-                    _channelRequestResponse.Dispose();
                     _channelRequestResponse = null;
+                    channelRequestResponse.Dispose();
                 }
             }
         }