Add method KeepAlive to SshClient to allow to send dummy KeepAlive requests

Dont allow por forwarding to start or stop if its already started or stopped
This commit is contained in:
olegkap_cp
2010-12-10 14:09:44 +00:00
parent d52655cc1e
commit 2bbb42508b
7 changed files with 76 additions and 30 deletions
@@ -12,6 +12,8 @@ namespace Renci.SshClient
public uint ConnectedPort { get; internal set; }
public bool IsStarted { get; protected set; }
public event EventHandler<ExceptionEventArgs> Exception;
internal ForwardedPort()
@@ -16,6 +16,10 @@ namespace Renci.SshClient
{
base.Start();
// If port already started dont start it again
if (this.IsStarted)
return;
var ep = new IPEndPoint(Dns.GetHostAddresses("localhost")[0], (int)this.BoundPort);
this._listener = new TcpListener(ep);
this._listener.Start();
@@ -53,13 +57,24 @@ namespace Renci.SshClient
{
this.RaiseExceptionEvent(exp);
}
this.Stop();
});
this.IsStarted = true;
}
public override void Stop()
{
// If port not started you cant stop it
if (!this.IsStarted)
return;
this._listener.Stop();
this._listenerTask.Wait();
this.IsStarted = false;
}
}
}
@@ -16,6 +16,10 @@ namespace Renci.SshClient
{
base.Start();
// If port already started dont start it again
if (this.IsStarted)
return;
this.Session.RegisterMessageType<RequestFailureMessage>(Messages.MessageTypes.RequestFailure);
this.Session.RegisterMessageType<RequestSuccessMessage>(Messages.MessageTypes.RequestSuccess);
this.Session.RegisterMessageType<ChannelOpenMessage>(Messages.MessageTypes.ChannelOpen);
@@ -40,10 +44,16 @@ namespace Renci.SshClient
// If request failed dont handle channel opening for this request
this.Session.ChannelOpenReceived -= Session_ChannelOpening;
}
this.IsStarted = true;
}
public override void Stop()
{
// If port not started you cant stop it
if (!this.IsStarted)
return;
// Send global request to cancel direct tcpip
this.Session.SendMessage(new GlobalRequestMessage
{
@@ -58,6 +68,8 @@ namespace Renci.SshClient
this.Session.RequestSuccessReceived -= Session_RequestSuccess;
this.Session.RequestFailureReceived -= Session_RequestFailure;
this.Session.ChannelOpenReceived -= Session_ChannelOpening;
this.IsStarted = false;
}
private void Session_ChannelOpening(object sender, MessageEventArgs<ChannelOpenMessage> e)
@@ -47,12 +47,27 @@ namespace Renci.SshClient.Messages.Connection
case GlobalRequestNames.CancelTcpIpForward:
this.Write("cancel-tcpip-forward");
break;
case GlobalRequestNames.KeepAlive:
this.Write("keep-alive-message-ignore-me");
break;
default:
break;
}
this.Write(this.WantReply);
this.Write(this.AddressToBind);
this.Write(this.PortToBind);
switch (this.RequestName)
{
case GlobalRequestNames.TcpIpForward:
case GlobalRequestNames.CancelTcpIpForward:
this.Write(this.AddressToBind);
this.Write(this.PortToBind);
break;
case GlobalRequestNames.KeepAlive:
break;
default:
break;
}
}
}
}
@@ -9,6 +9,10 @@
/// <summary>
/// cancel-tcpip-forward
/// </summary>
CancelTcpIpForward
CancelTcpIpForward,
/// <summary>
/// keep-alive-message-ignore-me
/// </summary>
KeepAlive
}
}
@@ -568,6 +568,18 @@ namespace Renci.SshClient
return channel;
}
/// <summary>
/// Sends "keep alive" message to keep connection alive.
/// </summary>
internal void KeepAlive()
{
this.SendMessage(new GlobalRequestMessage
{
RequestName = GlobalRequestNames.KeepAlive,
WantReply = false
});
}
/// <summary>
/// Waits for handle to signal while checking other handles as well including timeout check to prevent waiting for ever
/// </summary>
+13 -27
View File
@@ -23,23 +23,6 @@ namespace Renci.SshClient
}
}
//private Sftp _sftp;
///// <summary>
///// Gets the shell.
///// </summary>
///// <value>The shell.</value>
//public Sftp Sftp
//{
// get
// {
// if (this._sftp == null)
// {
// this._sftp = new Sftp(this._session);
// }
// return this._sftp;
// }
//}
public IEnumerable<ForwardedPort> ForwardedPorts
{
get
@@ -128,6 +111,19 @@ namespace Renci.SshClient
return port;
}
public void RemoveForwardedPort(ForwardedPort port)
{
// Stop port forwarding before removing it
port.Stop();
this._forwardedPorts.Remove(port);
}
public void KeepAlive()
{
this._session.KeepAlive();
}
public SshCommand CreateCommand(string commandText)
{
return new SshCommand(this._session, commandText);
@@ -145,16 +141,6 @@ namespace Renci.SshClient
return new Shell(this._session, input, output, extendedOutput, terminalName, columns, rows, width, height, terminalMode);
}
//public Sftp CreateSftp()
//{
// return new Sftp(this._session);
//}
public void RemoveForwardedPort(ForwardedPort port)
{
this._forwardedPorts.Remove(port);
}
#region IDisposable Members
private bool disposed = false;