Remove initilize method and make sure there is always one session per connection when connected to the server

Correct fail command re-execution test
This commit is contained in:
olegkap_cp
2010-11-08 16:01:54 +00:00
parent ba8f01a661
commit f9d6c34b59
4 changed files with 19 additions and 54 deletions
@@ -173,7 +173,7 @@ namespace Renci.SshClient.Tests
}
catch (SshException exp)
{
Assert.AreEqual(exp.ExitStatus, (uint)2);
Assert.IsTrue(exp.ExitStatus > 0);
}
s.Disconnect();
}
@@ -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();
}
}
+14 -47
View File
@@ -76,12 +76,7 @@ namespace Renci.SshClient
/// <summary>
/// WaitHandle to signal that listner ended
/// </summary>
private EventWaitHandle _listenerWaitHandle = new ManualResetEvent(false);
/// <summary>
/// Keeps track of all session channels
/// </summary>
private Dictionary<uint, Channel> _sessionChannels = new Dictionary<uint, Channel>();
private EventWaitHandle _listenerWaitHandle = new AutoResetEvent(false);
/// <summary>
/// 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();
/// <summary>
/// Sends packet message to the server.
/// </summary>
@@ -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
/// <summary>
/// Initializes this session.
/// </summary>
/// <remarks>Initialization required when same session object being reconnected</remarks>
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;
}
/// <summary>
/// Perfom neccesary cleanup when client disconects from the server
/// </summary>
@@ -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)
{
@@ -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<T>(uint boundPort, string connectedHost, uint connectedPort) where T : ForwardedPort, new()