Browse Source

Avoid NRE setting EventWaitHandle.
Remove emty lines.

Gert Driesen 11 years ago
parent
commit
98549ed087
1 changed files with 12 additions and 46 deletions
  1. 12 46
      Renci.SshClient/Renci.SshNet/Channels/ChannelSession.cs

+ 12 - 46
Renci.SshClient/Renci.SshNet/Channels/ChannelSession.cs

@@ -1,8 +1,8 @@
-using System.Threading;
+using System.Collections.Generic;
+using System.Globalization;
+using System.Threading;
 using Renci.SshNet.Common;
 using Renci.SshNet.Messages.Connection;
-using System.Globalization;
-using System.Collections.Generic;
 
 namespace Renci.SshNet.Channels
 {
@@ -51,9 +51,7 @@ namespace Renci.SshNet.Channels
                 }
 
                 if (!this.IsOpen)
-                {
                     throw new SshException(string.Format(CultureInfo.CurrentCulture, "Failed to open a channel after {0} attempts.", this._failedOpenAttempts));
-                }
             }
         }
 
@@ -66,7 +64,6 @@ namespace Renci.SshNet.Channels
         protected override void OnOpenConfirmation(uint remoteChannelNumber, uint initialWindowSize, uint maximumPacketSize)
         {
             base.OnOpenConfirmation(remoteChannelNumber, initialWindowSize, maximumPacketSize);
-
             this._channelOpenResponseWaitHandle.Set();
         }
 
@@ -79,9 +76,7 @@ namespace Renci.SshNet.Channels
         protected override void OnOpenFailure(uint reasonCode, string description, string language)
         {
             this._failedOpenAttempts++;
-
             this.SessionSemaphore.Release();
-
             this._channelOpenResponseWaitHandle.Set();
         }
 
@@ -104,9 +99,7 @@ namespace Renci.SshNet.Channels
             base.Close(wait);
 
             if (!wait)
-            {
                 this.SessionSemaphore.Release();
-            }
         }
 
         /// <summary>
@@ -124,11 +117,8 @@ namespace Renci.SshNet.Channels
         public bool SendPseudoTerminalRequest(string environmentVariable, uint columns, uint rows, uint width, uint height, IDictionary<TerminalModes, uint> terminalModeValues)
         {
             this._channelRequestResponse.Reset();
-
             this.SendMessage(new ChannelRequestMessage(this.RemoteChannelNumber, new PseudoTerminalRequestInfo(environmentVariable, columns, rows, width, height, terminalModeValues)));
-            
             this.WaitOnHandle(this._channelRequestResponse);
-
             return this._channelRequestSucces;
         }
 
@@ -143,11 +133,8 @@ namespace Renci.SshNet.Channels
         public bool SendX11ForwardingRequest(bool isSingleConnection, string protocol, byte[] cookie, uint screenNumber)
         {
             this._channelRequestResponse.Reset();
-
             this.SendMessage(new ChannelRequestMessage(this.RemoteChannelNumber, new X11ForwardingRequestInfo(isSingleConnection, protocol, cookie, screenNumber)));
-
             this.WaitOnHandle(this._channelRequestResponse);
-
             return this._channelRequestSucces;
         }
 
@@ -160,11 +147,8 @@ namespace Renci.SshNet.Channels
         public bool SendEnvironmentVariableRequest(string variableName, string variableValue)
         {
             this._channelRequestResponse.Reset();
-
             this.SendMessage(new ChannelRequestMessage(this.RemoteChannelNumber, new EnvironmentVariableRequestInfo(variableName, variableValue)));
-
             this.WaitOnHandle(this._channelRequestResponse);
-
             return this._channelRequestSucces;
         }
 
@@ -175,11 +159,8 @@ namespace Renci.SshNet.Channels
         public bool SendShellRequest()
         {
             this._channelRequestResponse.Reset();
-
             this.SendMessage(new ChannelRequestMessage(this.RemoteChannelNumber, new ShellRequestInfo()));
-
             this.WaitOnHandle(this._channelRequestResponse);
-
             return this._channelRequestSucces;
         }
 
@@ -191,11 +172,8 @@ namespace Renci.SshNet.Channels
         public bool SendExecRequest(string command)
         {
             this._channelRequestResponse.Reset();
-
             this.SendMessage(new ChannelRequestMessage(this.RemoteChannelNumber, new ExecRequestInfo(command, this.ConnectionInfo.Encoding)));
-
             this.WaitOnHandle(this._channelRequestResponse);
-
             return this._channelRequestSucces;
         }
 
@@ -207,11 +185,8 @@ namespace Renci.SshNet.Channels
         public bool SendBreakRequest(uint breakLength)
         {
             this._channelRequestResponse.Reset();
-
             this.SendMessage(new ChannelRequestMessage(this.RemoteChannelNumber, new BreakRequestInfo(breakLength)));
-
             this.WaitOnHandle(this._channelRequestResponse);
-
             return this._channelRequestSucces;
         }
 
@@ -223,11 +198,8 @@ namespace Renci.SshNet.Channels
         public bool SendSubsystemRequest(string subsystem)
         {
             this._channelRequestResponse.Reset();
-
             this.SendMessage(new ChannelRequestMessage(this.RemoteChannelNumber, new SubsystemRequestInfo(subsystem)));
-
             this.WaitOnHandle(this._channelRequestResponse);
-
             return this._channelRequestSucces;
         }
 
@@ -242,7 +214,6 @@ namespace Renci.SshNet.Channels
         public bool SendWindowChangeRequest(uint columns, uint rows, uint width, uint height)
         {
             this.SendMessage(new ChannelRequestMessage(this.RemoteChannelNumber, new WindowChangeRequestInfo(columns, rows, width, height)));
-
             return true;
         }
 
@@ -254,7 +225,6 @@ namespace Renci.SshNet.Channels
         public bool SendLocalFlowRequest(bool clientCanDo)
         {
             this.SendMessage(new ChannelRequestMessage(this.RemoteChannelNumber, new XonXoffRequestInfo(clientCanDo)));
-
             return true;
         }
 
@@ -266,7 +236,6 @@ namespace Renci.SshNet.Channels
         public bool SendSignalRequest(string signalName)
         {
             this.SendMessage(new ChannelRequestMessage(this.RemoteChannelNumber, new SignalRequestInfo(signalName)));
-
             return true;
         }
 
@@ -278,7 +247,6 @@ namespace Renci.SshNet.Channels
         public bool SendExitStatusRequest(uint exitStatus)
         {
             this.SendMessage(new ChannelRequestMessage(this.RemoteChannelNumber, new ExitStatusRequestInfo(exitStatus)));
-
             return true;
         }
 
@@ -293,7 +261,6 @@ namespace Renci.SshNet.Channels
         public bool SendExitSignalRequest(string signalName, bool coreDumped, string errorMessage, string language)
         {
             this.SendMessage(new ChannelRequestMessage(this.RemoteChannelNumber, new ExitSignalRequestInfo(signalName, coreDumped, errorMessage, language)));
-
             return true;
         }
 
@@ -304,11 +271,8 @@ namespace Renci.SshNet.Channels
         public bool SendEndOfWriteRequest()
         {
             this._channelRequestResponse.Reset();
-
             this.SendMessage(new ChannelRequestMessage(this.RemoteChannelNumber, new EndOfWriteRequestInfo()));
-
             this.WaitOnHandle(this._channelRequestResponse);
-
             return this._channelRequestSucces;
         }
 
@@ -319,11 +283,8 @@ namespace Renci.SshNet.Channels
         public bool SendKeepAliveRequest()
         {
             this._channelRequestResponse.Reset();
-
             this.SendMessage(new ChannelRequestMessage(this.RemoteChannelNumber, new KeepAliveRequestInfo()));
-
             this.WaitOnHandle(this._channelRequestResponse);
-
             return this._channelRequestSucces;
         }
 
@@ -334,7 +295,10 @@ namespace Renci.SshNet.Channels
         {
             base.OnSuccess();
             this._channelRequestSucces = true;
-            this._channelRequestResponse.Set();
+
+            var channelRequestResponse = _channelRequestResponse;
+            if (channelRequestResponse != null)
+                channelRequestResponse.Set();
         }
 
         /// <summary>
@@ -343,8 +307,11 @@ namespace Renci.SshNet.Channels
         protected override void OnFailure()
         {
             base.OnFailure();
-            this._channelRequestSucces = false;
-            this._channelRequestResponse.Set();
+            _channelRequestSucces = false;
+
+            var channelRequestResponse = _channelRequestResponse;
+            if (channelRequestResponse != null)
+                channelRequestResponse.Set();
         }
 
         /// <summary>
@@ -356,7 +323,6 @@ namespace Renci.SshNet.Channels
             {
                 //  Ensure that channels are available
                 this.SessionSemaphore.Wait();
-
                 this.SendMessage(new ChannelOpenMessage(this.LocalChannelNumber, this.LocalWindowSize, this.PacketSize, new SessionChannelOpenInfo()));
             }
         }