From 3808aae801705e75ae3a69ba89f036ed0532d3da Mon Sep 17 00:00:00 2001 From: drieseng Date: Sat, 10 Sep 2016 19:40:09 +0200 Subject: [PATCH] Avoid extra IsOpen invocation when attempting to open channel session. --- src/Renci.SshNet/Channels/ChannelSession.cs | 51 +++++++++++---------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/src/Renci.SshNet/Channels/ChannelSession.cs b/src/Renci.SshNet/Channels/ChannelSession.cs index 72edcc25..d078bb61 100644 --- a/src/Renci.SshNet/Channels/ChannelSession.cs +++ b/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 /// public virtual void Open() { - if (!IsOpen) - { - // Try to open channel several times - while (!IsOpen && _failedOpenAttempts < ConnectionInfo.RetryAttempts) - { - SendChannelOpenMessage(); - try - { - WaitOnHandle(_channelOpenResponseWaitHandle); - } - catch (Exception) - { - // avoid leaking session semaphore - ReleaseSemaphore(); - throw; - } - } + if (IsOpen) + return; - if (!IsOpen) - throw new SshException(string.Format(CultureInfo.CurrentCulture, "Failed to open a channel after {0} attempts.", _failedOpenAttempts)); - } + // Try to open channel several times + do + { + SendChannelOpenMessage(); + try + { + WaitOnHandle(_channelOpenResponseWaitHandle); + } + catch (Exception) + { + // 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)); } /// @@ -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(); } } }