Refactor Shell and FTP connection

This commit is contained in:
olegkap_cp
2010-08-19 18:09:04 +00:00
parent e6de0af9a7
commit 43556c4085
6 changed files with 153 additions and 124 deletions
@@ -1,96 +0,0 @@
namespace Renci.SshClient
{
public class Connection
{
private Session _session;
public ConnectionInfo ConnectionInfo { get; private set; }
private Shell _shell;
public Shell Shell
{
get
{
if (this._shell == null)
{
this._shell = new Shell(this._session);
}
return this._shell;
}
}
private Sftp _sftp;
public Sftp Sftp
{
get
{
if (this._sftp == null)
{
this._sftp = new Sftp(this._session);
}
return this._sftp;
}
}
public Connection(ConnectionInfo connectionInfo)
{
this.ConnectionInfo = connectionInfo;
this._session = Session.CreateSession(this.ConnectionInfo);
}
public Connection(string host, int port, string username, string password)
: this(new ConnectionInfo
{
Host = host,
Port = port,
Username = username,
Password = password,
})
{
}
public Connection(string host, string username, string password)
: this(new ConnectionInfo
{
Host = host,
Username = username,
Password = password,
})
{
}
public Connection(string host, int port, string username, PrivateKeyFile keyFile)
: this(new ConnectionInfo
{
Host = host,
Port = port,
Username = username,
KeyFile = keyFile,
})
{
}
public Connection(string host, string username, PrivateKeyFile keyFile)
: this(new ConnectionInfo
{
Host = host,
Username = username,
KeyFile = keyFile,
})
{
}
public void Connect()
{
this._session.Connect();
}
public void Disconnect()
{
this._session.Disconnect();
}
}
}
@@ -194,9 +194,9 @@
<Compile Include="SessionSSHv2.cs" />
<Compile Include="Settings.cs" />
<Compile Include="Common\SshData.cs" />
<Compile Include="Connection.cs" />
<Compile Include="Sftp.cs" />
<Compile Include="Shell.cs" />
<Compile Include="SshBase.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="Shell\" />
+6 -1
View File
@@ -143,7 +143,6 @@ namespace Renci.SshClient
this.ClientVersion = string.Format("SSH-2.0-Renci.SshClient.{0}", this.GetType().Assembly.GetName().Version);
this._sockeStream = new NetworkStream(socket);
this.IsConnected = true;
}
private static IDictionary<Type, Func<Session, Channel>> _channels = new Dictionary<Type, Func<Session, Channel>>()
@@ -163,6 +162,12 @@ namespace Renci.SshClient
public void Connect()
{
// If connected dont connect again
if (this.IsConnected)
return;
this.IsConnected = true;
this.Write(Encoding.ASCII.GetBytes(string.Format("{0}\n", this.ClientVersion)));
// Register Transport response messages
+47 -20
View File
@@ -1,70 +1,97 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.IO;
using Renci.SshClient.Channels;
using Renci.SshClient.Common;
namespace Renci.SshClient
{
public class Sftp
public class Sftp : SshBase
{
private Session _session;
private ChannelSftp _channel;
internal Sftp(Session session)
private ChannelSftp Channel
{
this._session = session;
//this._channel = new ChannelSftp(this._session);
this._channel = this._session.CreateChannel<ChannelSftp>();
get
{
if (this._channel == null)
{
this.Session.Connect();
this._channel = this.Session.CreateChannel<ChannelSftp>();
}
return this._channel;
}
}
public Sftp(ConnectionInfo connectionInfo)
: base(connectionInfo)
{
}
public Sftp(string host, int port, string username, string password)
: base(host, port, username, password)
{
}
public Sftp(string host, string username, string password)
: base(host, username, password)
{
}
public Sftp(string host, int port, string username, PrivateKeyFile keyFile)
: base(host, port, username, keyFile)
{
}
public Sftp(string host, string username, PrivateKeyFile keyFile)
: base(host, username, keyFile)
{
}
public IEnumerable<FtpFileInfo> ListDirectory(string path)
{
return this._channel.ListDirectory(path);
return this.Channel.ListDirectory(path);
}
public void UploadFile(Stream source, string fileName)
{
this._channel.UploadFile(source, fileName);
this.Channel.UploadFile(source, fileName);
}
public void UploadFile(string source, string fileName)
{
this._channel.UploadFile(File.OpenRead(source), fileName);
this.Channel.UploadFile(File.OpenRead(source), fileName);
}
public void DownloadFile(string fileName, Stream destination)
{
this._channel.DownloadFile(fileName, destination);
this.Channel.DownloadFile(fileName, destination);
}
public void DownloadFile(string fileName, string destination)
{
var file = File.Create(destination);
this._channel.DownloadFile(fileName, file);
this.Channel.DownloadFile(fileName, File.Create(destination));
}
public void RemoveFile(string fileName)
{
this._channel.RemoveFile(fileName);
this.Channel.RemoveFile(fileName);
}
public void RenameFile(string oldFileName, string newFileName)
{
this._channel.RenameFile(oldFileName, newFileName);
this.Channel.RenameFile(oldFileName, newFileName);
}
public void CreateDirectory(string directoryName)
{
this._channel.CreateDirectory(directoryName);
this.Channel.CreateDirectory(directoryName);
}
public void RemoveDirectory(string directoryName)
{
this._channel.RemoveDirectory(directoryName);
this.Channel.RemoveDirectory(directoryName);
}
}
}
+30 -6
View File
@@ -5,13 +5,31 @@ using System.Text;
using Renci.SshClient.Channels;
namespace Renci.SshClient
{
public class Shell
public class Shell : SshBase
{
private Session _session;
internal Shell(Session session)
public Shell(ConnectionInfo connectionInfo)
: base(connectionInfo)
{
}
public Shell(string host, int port, string username, string password)
: base(host, port, username, password)
{
}
public Shell(string host, string username, string password)
: base(host, username, password)
{
}
public Shell(string host, int port, string username, PrivateKeyFile keyFile)
: base(host, port, username, keyFile)
{
}
public Shell(string host, string username, PrivateKeyFile keyFile)
: base(host, username, keyFile)
{
this._session = session;
}
public string Execute(string command)
@@ -30,6 +48,9 @@ namespace Renci.SshClient
public void Execute(string command, Stream output, Stream extended)
{
// Make sure session is connected
this.Session.Connect();
this.EndExecute(this.BeginExecute(command, output, extended, null, null));
}
@@ -40,7 +61,10 @@ namespace Renci.SshClient
public IAsyncResult BeginExecute(string command, Stream output, Stream extendedOutput, AsyncCallback callback, object state)
{
var channel = this._session.CreateChannel<ChannelExec>();
// Make sure session is connected
this.Session.Connect();
var channel = this.Session.CreateChannel<ChannelExec>();
return channel.BeginExecute(command, output, extendedOutput, callback, state);
}
@@ -0,0 +1,69 @@
namespace Renci.SshClient
{
public abstract class SshBase
{
public ConnectionInfo ConnectionInfo { get; private set; }
internal Session Session { get; private set; }
public SshBase(ConnectionInfo connectionInfo)
{
this.ConnectionInfo = connectionInfo;
this.Session = Session.CreateSession(this.ConnectionInfo);
}
public SshBase(string host, int port, string username, string password)
: this(new ConnectionInfo
{
Host = host,
Port = port,
Username = username,
Password = password,
})
{
}
public SshBase(string host, string username, string password)
: this(new ConnectionInfo
{
Host = host,
Username = username,
Password = password,
})
{
}
public SshBase(string host, int port, string username, PrivateKeyFile keyFile)
: this(new ConnectionInfo
{
Host = host,
Port = port,
Username = username,
KeyFile = keyFile,
})
{
}
public SshBase(string host, string username, PrivateKeyFile keyFile)
: this(new ConnectionInfo
{
Host = host,
Username = username,
KeyFile = keyFile,
})
{
}
public void Connect()
{
this.Session.Connect();
}
public void Disconnect()
{
this.Session.Disconnect();
}
}
}