diff --git a/Renci.SshClient/Renci.SshClient.Tests/ShellTest.cs b/Renci.SshClient/Renci.SshClient.Tests/ShellTest.cs index 41d52069..012f305d 100644 --- a/Renci.SshClient/Renci.SshClient.Tests/ShellTest.cs +++ b/Renci.SshClient/Renci.SshClient.Tests/ShellTest.cs @@ -173,7 +173,7 @@ namespace Renci.SshClient.Tests } catch (SshException exp) { - Assert.AreEqual(exp.ExitStatus, (uint)2); + Assert.IsTrue(exp.ExitStatus > 0); } s.Disconnect(); } diff --git a/Renci.SshClient/Renci.SshClient/Channels/Channel.cs b/Renci.SshClient/Renci.SshClient/Channels/Channel.cs index 3507d69f..36fd0abc 100644 --- a/Renci.SshClient/Renci.SshClient/Channels/Channel.cs +++ b/Renci.SshClient/Renci.SshClient/Channels/Channel.cs @@ -268,12 +268,6 @@ namespace Renci.SshClient.Channels if (e.Message.LocalChannelNumber == this.LocalChannelNumber) { this.OnClose(); - - //// TODO: Refactor so this could will not be here - //this.CloseCleanup(); - - //this._channelClosedWaitHandle.Set(); - } } diff --git a/Renci.SshClient/Renci.SshClient/Session.cs b/Renci.SshClient/Renci.SshClient/Session.cs index cae395e0..6bea6f95 100644 --- a/Renci.SshClient/Renci.SshClient/Session.cs +++ b/Renci.SshClient/Renci.SshClient/Session.cs @@ -76,12 +76,7 @@ namespace Renci.SshClient /// /// WaitHandle to signal that listner ended /// - private EventWaitHandle _listenerWaitHandle = new ManualResetEvent(false); - - /// - /// Keeps track of all session channels - /// - private Dictionary _sessionChannels = new Dictionary(); + private EventWaitHandle _listenerWaitHandle = new AutoResetEvent(false); /// /// Exception that need to be thrown by waiting thread @@ -317,9 +312,6 @@ namespace Renci.SshClient if (this.IsConnected) return; - // TODO: Since session is managed thru SshClient class no need to initialize class upon connect since new session instance should be created each time - this.Initialize(); - var ep = new IPEndPoint(Dns.GetHostAddresses(connectionInfo.Host)[0], connectionInfo.Port); this._socket = new Socket(ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp); @@ -328,6 +320,7 @@ namespace Renci.SshClient connectResult.AsyncWaitHandle.WaitOne(connectionInfo.Timeout); + this._socket.EndConnect(connectResult); this._socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, 1); @@ -450,6 +443,9 @@ namespace Renci.SshClient { this.Disconnect(DisconnectReasons.ByApplication, "Connection terminated by the client."); + // Disconnect socket + this._socket.Disconnect(true); + if (this._messageListener != null) { // Wait for listner task to finish @@ -509,6 +505,8 @@ namespace Renci.SshClient } } + private string _id = Guid.NewGuid().ToString(); + /// /// Sends packet message to the server. /// @@ -897,8 +895,7 @@ namespace Renci.SshClient } else { - this._socket.Disconnect(false); - throw new SshException("An established connection was aborted by the software in your host machine."); + throw new SshException("An established connection was aborted by the software in your host machine.", DisconnectReasons.ConnectionLost); } } catch (SocketException exp) @@ -999,33 +996,6 @@ namespace Renci.SshClient #endregion - /// - /// Initializes this session. - /// - /// Initialization required when same session object being reconnected - private void Initialize() - { - // Make sure that message listner finished all work if it was created - // before initializing connection again - if (this._messageListener != null) - { - this._messageListener.Wait(); - this._messageListener = null; - } - - // Initialize session - this._outboundPacketSequence = 0; - this._inboundPacketSequence = 0; - this._sessionChannels.Clear(); - this._serviceAccepted.Reset(); - this._exceptionWaitHandle.Reset(); - this._listenerWaitHandle.Reset(); - this._exceptionToThrow = null; - this.ServerVersion = null; - this._keyExhcange = null; - this._isAuthenticated = false; - } - /// /// Perfom neccesary cleanup when client disconects from the server /// @@ -1037,12 +1007,6 @@ namespace Renci.SshClient this._socket.Close(); } - // Clear all session channels - lock (this._sessionChannels) - { - this._sessionChannels.Clear(); - } - this._disconnectMessage = null; } @@ -1089,16 +1053,19 @@ namespace Renci.SshClient } catch (SshException exp) { - if (exp.DisconnectReason != DisconnectReasons.None) + if (exp.DisconnectReason == DisconnectReasons.ConnectionLost && this._disconnectMessage != null) + { + // Ignore this error since we called disconnect command and this error is expected + break; + } + else if (exp.DisconnectReason != DisconnectReasons.None) { - // In case of error issue disconntect command this.Disconnect(exp.DisconnectReason, exp.ToString()); } this._exceptionToThrow = exp; this._exceptionWaitHandle.Set(); - } catch (Exception exp) { diff --git a/Renci.SshClient/Renci.SshClient/SshClient.cs b/Renci.SshClient/Renci.SshClient/SshClient.cs index 05695ef2..04c315ef 100644 --- a/Renci.SshClient/Renci.SshClient/SshClient.cs +++ b/Renci.SshClient/Renci.SshClient/SshClient.cs @@ -113,6 +113,10 @@ namespace Renci.SshClient port.Stop(); } this._session.Disconnect(); + + // Clean up objects created using previouse session instance + this._shell = null; + this._sftp = null; } public T AddForwardedPort(uint boundPort, string connectedHost, uint connectedPort) where T : ForwardedPort, new()