mirror of
https://github.com/sshnet/SSH.NET.git
synced 2026-10-17 05:21:15 +08:00
Reduce Session visibility to inherited Channel classes
This commit is contained in:
@@ -3,16 +3,16 @@ namespace Renci.SshClient.Tests
|
||||
{
|
||||
public static class ConnectionData
|
||||
{
|
||||
public static string Host { get { return "you host here"; } }
|
||||
public static string Host { get { return "152.54.8.155"; } }
|
||||
|
||||
public static int Port { get { return 22; } }
|
||||
|
||||
public static string Username { get { return "username"; } }
|
||||
public static string Username { get { return "oleg"; } }
|
||||
|
||||
public static string Password { get { return "password@"; } }
|
||||
public static string Password { get { return "Maya1kap@"; } }
|
||||
|
||||
public static string RsaKeyFilePath { get { return @"path to rsa key file"; } }
|
||||
public static string RsaKeyFilePath { get { return @"H:\My Documents\SSHKeys\oleg-test4.rsa.key"; } }
|
||||
|
||||
public static string DssKeyFilePath { get { return @"path to dss key file"; } }
|
||||
public static string DssKeyFilePath { get { return @"H:\My Documents\SSHKeys\oleg-test4.dsa.key"; } }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ namespace Renci.SshClient.Tests
|
||||
{
|
||||
var client = CreateShellUsingPassword();
|
||||
client.Connect();
|
||||
var port1 = client.AddForwardedPort<ForwardedPortLocal>(8082, "www.renci.org", 80);
|
||||
var port1 = client.AddForwardedPort<ForwardedPortLocal>(8084, "www.renci.org", 80);
|
||||
port1.Exception += delegate(object sender, ExceptionEventArgs e)
|
||||
{
|
||||
Assert.Fail(e.Exception.ToString());
|
||||
@@ -79,7 +79,7 @@ namespace Renci.SshClient.Tests
|
||||
(counter) =>
|
||||
{
|
||||
var start = DateTime.Now;
|
||||
var req = HttpWebRequest.Create("http://localhost:8082");
|
||||
var req = HttpWebRequest.Create("http://localhost:8084");
|
||||
using (var response = req.GetResponse())
|
||||
{
|
||||
|
||||
|
||||
@@ -20,6 +20,8 @@ namespace Renci.SshClient.Channels
|
||||
/// </summary>
|
||||
private bool _closeMessageSent = false;
|
||||
|
||||
private Session _session;
|
||||
|
||||
public abstract ChannelTypes ChannelType { get; }
|
||||
|
||||
public uint LocalChannelNumber { get; private set; }
|
||||
@@ -34,7 +36,18 @@ namespace Renci.SshClient.Channels
|
||||
|
||||
public bool IsOpen { get; private set; }
|
||||
|
||||
protected Session Session { get; private set; }
|
||||
protected bool IsConnected
|
||||
{
|
||||
get { return this._session.IsConnected; }
|
||||
}
|
||||
|
||||
protected ConnectionInfo ConnectionInfo
|
||||
{
|
||||
get
|
||||
{
|
||||
return this._session.ConnectionInfo;
|
||||
}
|
||||
}
|
||||
|
||||
internal Channel()
|
||||
{
|
||||
@@ -45,34 +58,34 @@ namespace Renci.SshClient.Channels
|
||||
this._initialWindowSize = windowSize;
|
||||
this._maximumPacketSize = Math.Max(packetSize, 0x8000); // Ensure minimum maximum packet size of 0x8000 bytes
|
||||
|
||||
this.Session = session;
|
||||
this._session = session;
|
||||
this.LocalWindowSize = this._initialWindowSize; // Initial window size
|
||||
this.PacketSize = this._maximumPacketSize; // Maximum packet size
|
||||
|
||||
this.LocalChannelNumber = session.NextChannelNumber;
|
||||
this.RemoteChannelNumber = serverChannelNumber;
|
||||
|
||||
this.Session.RegisterMessageType<ChannelOpenConfirmationMessage>(MessageTypes.ChannelOpenConfirmation);
|
||||
this.Session.RegisterMessageType<ChannelOpenFailureMessage>(MessageTypes.ChannelOpenFailure);
|
||||
this.Session.RegisterMessageType<ChannelWindowAdjustMessage>(MessageTypes.ChannelWindowAdjust);
|
||||
this.Session.RegisterMessageType<ChannelExtendedDataMessage>(MessageTypes.ChannelExtendedData);
|
||||
this.Session.RegisterMessageType<ChannelRequestMessage>(MessageTypes.ChannelRequest);
|
||||
this.Session.RegisterMessageType<ChannelSuccessMessage>(MessageTypes.ChannelSuccess);
|
||||
this.Session.RegisterMessageType<ChannelDataMessage>(MessageTypes.ChannelData);
|
||||
this.Session.RegisterMessageType<ChannelEofMessage>(MessageTypes.ChannelEof);
|
||||
this.Session.RegisterMessageType<ChannelCloseMessage>(MessageTypes.ChannelClose);
|
||||
this._session.RegisterMessageType<ChannelOpenConfirmationMessage>(MessageTypes.ChannelOpenConfirmation);
|
||||
this._session.RegisterMessageType<ChannelOpenFailureMessage>(MessageTypes.ChannelOpenFailure);
|
||||
this._session.RegisterMessageType<ChannelWindowAdjustMessage>(MessageTypes.ChannelWindowAdjust);
|
||||
this._session.RegisterMessageType<ChannelExtendedDataMessage>(MessageTypes.ChannelExtendedData);
|
||||
this._session.RegisterMessageType<ChannelRequestMessage>(MessageTypes.ChannelRequest);
|
||||
this._session.RegisterMessageType<ChannelSuccessMessage>(MessageTypes.ChannelSuccess);
|
||||
this._session.RegisterMessageType<ChannelDataMessage>(MessageTypes.ChannelData);
|
||||
this._session.RegisterMessageType<ChannelEofMessage>(MessageTypes.ChannelEof);
|
||||
this._session.RegisterMessageType<ChannelCloseMessage>(MessageTypes.ChannelClose);
|
||||
|
||||
this.Session.ChannelOpen += OnChannelOpen;
|
||||
this.Session.ChannelOpenConfirmation += OnChannelOpenConfirmation;
|
||||
this.Session.ChannelOpenFailure += OnChannelOpenFailure;
|
||||
this.Session.ChannelWindowAdjust += OnChannelWindowAdjust;
|
||||
this.Session.ChannelData += OnChannelData;
|
||||
this.Session.ChannelExtendedData += OnChannelExtendedData;
|
||||
this.Session.ChannelEof += OnChannelEof;
|
||||
this.Session.ChannelClose += OnChannelClose;
|
||||
this.Session.ChannelRequest += OnChannelRequest;
|
||||
this.Session.ChannelSuccess += OnChannelSuccess;
|
||||
this.Session.ChannelFailure += OnChannelFailure;
|
||||
this._session.ChannelOpen += OnChannelOpen;
|
||||
this._session.ChannelOpenConfirmation += OnChannelOpenConfirmation;
|
||||
this._session.ChannelOpenFailure += OnChannelOpenFailure;
|
||||
this._session.ChannelWindowAdjust += OnChannelWindowAdjust;
|
||||
this._session.ChannelData += OnChannelData;
|
||||
this._session.ChannelExtendedData += OnChannelExtendedData;
|
||||
this._session.ChannelEof += OnChannelEof;
|
||||
this._session.ChannelClose += OnChannelClose;
|
||||
this._session.ChannelRequest += OnChannelRequest;
|
||||
this._session.ChannelSuccess += OnChannelSuccess;
|
||||
this._session.ChannelFailure += OnChannelFailure;
|
||||
|
||||
}
|
||||
|
||||
@@ -83,7 +96,7 @@ namespace Renci.SshClient.Channels
|
||||
this.SendChannelCloseMessage();
|
||||
|
||||
// Wait for channel to be closed
|
||||
this.Session.WaitHandle(this._channelClosedWaitHandle);
|
||||
this._session.WaitHandle(this._channelClosedWaitHandle);
|
||||
}
|
||||
|
||||
this.CloseCleanup();
|
||||
@@ -151,7 +164,7 @@ namespace Renci.SshClient.Channels
|
||||
|
||||
protected void SendMessage(Message message)
|
||||
{
|
||||
this.Session.SendMessage(message);
|
||||
this._session.SendMessage(message);
|
||||
}
|
||||
|
||||
protected void SendMessage(ChannelDataMessage message)
|
||||
@@ -159,11 +172,11 @@ namespace Renci.SshClient.Channels
|
||||
if (this.ServerWindowSize < 1)
|
||||
{
|
||||
// Wait for window to be adjust
|
||||
this.Session.WaitHandle(this._channelWindowAdjustWaitHandle);
|
||||
this._session.WaitHandle(this._channelWindowAdjustWaitHandle);
|
||||
}
|
||||
|
||||
this.ServerWindowSize -= (uint)message.Data.Length;
|
||||
this.Session.SendMessage(message);
|
||||
this._session.SendMessage(message);
|
||||
}
|
||||
|
||||
protected void SendMessage(ChannelExtendedDataMessage message)
|
||||
@@ -171,11 +184,11 @@ namespace Renci.SshClient.Channels
|
||||
if (this.ServerWindowSize < 1)
|
||||
{
|
||||
// Wait for window to be adjust
|
||||
this.Session.WaitHandle(this._channelWindowAdjustWaitHandle);
|
||||
this._session.WaitHandle(this._channelWindowAdjustWaitHandle);
|
||||
}
|
||||
|
||||
this.ServerWindowSize -= (uint)message.Data.Length;
|
||||
this.Session.SendMessage(message);
|
||||
this._session.SendMessage(message);
|
||||
}
|
||||
|
||||
protected void CloseCleanup()
|
||||
@@ -187,6 +200,11 @@ namespace Renci.SshClient.Channels
|
||||
this.SendChannelCloseMessage();
|
||||
}
|
||||
|
||||
protected void WaitHandle(WaitHandle waitHandle)
|
||||
{
|
||||
this._session.WaitHandle(waitHandle);
|
||||
}
|
||||
|
||||
#region Channel message event handlers
|
||||
|
||||
private void OnChannelOpen(object sender, MessageEventArgs<ChannelOpenMessage> e)
|
||||
|
||||
@@ -36,7 +36,7 @@ namespace Renci.SshClient.Channels
|
||||
|
||||
IPEndPoint ep = socket.RemoteEndPoint as IPEndPoint;
|
||||
|
||||
if (!this.Session.IsConnected)
|
||||
if (!this.IsConnected)
|
||||
{
|
||||
throw new SshException("Session is not connected.");
|
||||
}
|
||||
@@ -55,7 +55,7 @@ namespace Renci.SshClient.Channels
|
||||
});
|
||||
|
||||
// Wait for channel to open
|
||||
this.Session.WaitHandle(this._channelOpen);
|
||||
this.WaitHandle(this._channelOpen);
|
||||
|
||||
// Start reading data from the port and send to channel
|
||||
EventWaitHandle readerTaskError = new AutoResetEvent(false);
|
||||
@@ -66,7 +66,7 @@ namespace Renci.SshClient.Channels
|
||||
{
|
||||
var buffer = new byte[this.PacketSize - 9];
|
||||
|
||||
while (this._socket.Connected || this.Session.IsConnected)
|
||||
while (this._socket.Connected || this.IsConnected)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -112,7 +112,7 @@ namespace Renci.SshClient.Channels
|
||||
|
||||
// Channel was open and we MUST receive EOF notification,
|
||||
// data transfer can take longer then connection specified timeout
|
||||
WaitHandle.WaitAny(new WaitHandle[] { this._channelEof, readerTaskError });
|
||||
System.Threading.WaitHandle.WaitAny(new WaitHandle[] { this._channelEof, readerTaskError });
|
||||
|
||||
this._socket.Close();
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ namespace Renci.SshClient.Channels
|
||||
|
||||
this.ServerWindowSize = this.LocalWindowSize;
|
||||
|
||||
if (!this.Session.IsConnected)
|
||||
if (!this.IsConnected)
|
||||
{
|
||||
throw new SshException("Session is not connected.");
|
||||
}
|
||||
|
||||
@@ -44,10 +44,10 @@ namespace Renci.SshClient.Channels
|
||||
if (!this.IsOpen)
|
||||
{
|
||||
// Try to opend channel several times
|
||||
while (this._failedOpenAttempts < this.Session.ConnectionInfo.RetryAttempts && !this.IsOpen)
|
||||
while (this._failedOpenAttempts < this.ConnectionInfo.RetryAttempts && !this.IsOpen)
|
||||
{
|
||||
this.SendChannelOpenMessage();
|
||||
this.Session.WaitHandle(this._channelOpenResponseWaitHandle);
|
||||
this.WaitHandle(this._channelOpenResponseWaitHandle);
|
||||
}
|
||||
|
||||
if (!this.IsOpen)
|
||||
|
||||
@@ -72,7 +72,7 @@ namespace Renci.SshClient.Channels
|
||||
}
|
||||
|
||||
// Make sure that operation completed if not wait for it to finish
|
||||
this.Session.WaitHandle(this._asyncResult.AsyncWaitHandle);
|
||||
this.WaitHandle(this._asyncResult.AsyncWaitHandle);
|
||||
|
||||
this.Close();
|
||||
|
||||
|
||||
@@ -897,7 +897,7 @@ namespace Renci.SshClient
|
||||
}
|
||||
else
|
||||
{
|
||||
this._socket.Disconnect(true);
|
||||
this._socket.Disconnect(false);
|
||||
throw new SshException("An established connection was aborted by the software in your host machine.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,6 +102,7 @@ namespace Renci.SshClient
|
||||
|
||||
public void Connect()
|
||||
{
|
||||
this._session = new Session(this._connectionInfo);
|
||||
this._session.Connect();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user