Fix possible nullable path (breaking change from previouse checking)

Fix disconnect logic to avoid ObjectDisposed exception
This commit is contained in:
olegkap_cp
2011-05-11 21:40:51 +00:00
parent 2f42d5a31e
commit 61a8edead9
5 changed files with 42 additions and 30 deletions
@@ -98,6 +98,27 @@ namespace Renci.SshNet.Tests.SftpClientTests
}
}
[TestMethod]
[TestCategory("Sftp")]
public void Test_Sftp_ListDirectory_Empty()
{
using (var sftp = new SftpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
{
sftp.Connect();
var files = sftp.ListDirectory(string.Empty);
Assert.IsTrue(files.Count() > 0);
foreach (var file in files)
{
Debug.WriteLine(file.FullName);
}
sftp.Disconnect();
}
}
[TestMethod]
[TestCategory("Sftp")]
public void Test_Sftp_ListDirectory_HugeDirectory()
@@ -111,6 +111,9 @@ namespace Renci.SshNet
/// </summary>
public void Disconnect()
{
if (!this.IsConnected)
return;
this.OnDisconnecting();
this.Dispose();
+16 -28
View File
@@ -141,12 +141,14 @@ namespace Renci.SshNet
}
}
private bool _isDisconnectMessageSent;
private uint _nextChannelNumber;
/// <summary>
/// Gets the next channel number.
/// </summary>
/// <value>The next channel number.</value>
public uint NextChannelNumber
internal uint NextChannelNumber
{
get
{
@@ -805,13 +807,15 @@ namespace Renci.SshNet
private void SendDisconnect(DisconnectReason reasonCode, string message)
{
// If disconnect message was sent already dont send it again
if (this._isDisconnectMessageSent)
return;
var disconnectMessage = new DisconnectMessage(reasonCode, message);
this.SendMessage(disconnectMessage);
// Handle disconnect message as if it was sent by the server
// TODO: Review this code and may be handle it separately
this.HandleMessage(disconnectMessage);
this._isDisconnectMessageSent = true;
}
/// <summary>
@@ -1583,12 +1587,11 @@ namespace Renci.SshNet
{
this.ErrorOccured(this, new ErrorEventArgs(exp));
}
var disconnectReason = DisconnectReason.ByApplication;
if (connectionException != null)
disconnectReason = connectionException.DisconnectReason;
this.SendDisconnect(disconnectReason, exp.ToString());
if (connectionException != null && connectionException.DisconnectReason != DisconnectReason.ConnectionLost)
{
this.SendDisconnect(connectionException.DisconnectReason, exp.ToString());
}
}
#region IDisposable Members
@@ -1622,26 +1625,11 @@ namespace Renci.SshNet
if (this._socket != null)
{
lock (this._socket)
{
// TODO: Not sure if it makes sense to do locking in Dispose but just want to check it for now as possible reason, need to remove later.
// If socket still open try to send disconnect message to the server
this.SendDisconnect(DisconnectReason.ByApplication, "Connection terminated by the client.");
if (this._socket != null)
{
try
{
// If socket still open try to send disconnect message to the server
this.SendMessage(new DisconnectMessage(DisconnectReason.ByApplication, "Connection terminated by the client."));
}
catch (Exception)
{
// Do nothing as this is not required to send disconnect message correctly.
}
this._socket.Dispose();
this._socket = null;
}
}
this._socket.Dispose();
this._socket = null;
}
if (this._messageListener != null)
@@ -47,7 +47,7 @@ namespace Renci.SshNet.Sftp
base.OnName(files);
var seperator = "/";
if (this._path[this._path.Length - 1] == '/')
if (!string.IsNullOrEmpty(this._path) && this._path[this._path.Length - 1] == '/')
seperator = string.Empty;
var sftpFiles = from f in files
@@ -113,7 +113,7 @@ namespace Renci.SshNet.Sftp
{
var fullPath = path;
if (path[0] != '/' && this.WorkingDirectory != null)
if (!string.IsNullOrEmpty(path) && path[0] != '/' && this.WorkingDirectory != null)
{
if (this.WorkingDirectory[this.WorkingDirectory.Length - 1] == '/')
{