Add KeepAliveInterval to send keep alive message autmaticlly

SendDisconnect messge when Session is being disposed
This commit is contained in:
olegkap_cp
2010-12-22 20:06:16 +00:00
parent 8aa21ae400
commit 21794bf6d1
2 changed files with 52 additions and 3 deletions
+6 -1
View File
@@ -588,7 +588,7 @@ namespace Renci.SshClient
/// <summary>
/// Sends "keep alive" message to keep connection alive.
/// </summary>
internal void KeepAlive()
internal void SendKeepAlive()
{
this.SendMessage(new GlobalRequestMessage
{
@@ -1465,6 +1465,11 @@ namespace Renci.SshClient
// and unmanaged resources.
if (disposing)
{
if (this.IsConnected)
{
this.Disconnect();
}
// Dispose managed resources.
if (this._socket != null)
{
@@ -6,11 +6,16 @@ using System.IO;
using Renci.SshClient.Sftp;
using Renci.SshClient.Security;
using Renci.SshClient.Common;
using System.Threading;
namespace Renci.SshClient
{
public abstract class SshBaseClient : IDisposable
{
private TimeSpan _keepAliveInterval;
private Timer _keepAliveTimer;
/// <summary>
/// Gets current session.
/// </summary>
@@ -38,6 +43,34 @@ namespace Renci.SshClient
}
}
/// <summary>
/// Gets or sets the keep alive interval in seconds.
/// </summary>
/// <value>
/// The keep alive interval in seconds.
/// </value>
public TimeSpan KeepAliveInterval
{
get
{
return this._keepAliveInterval;
}
set
{
this._keepAliveInterval = value;
if (this._keepAliveTimer == null)
{
this._keepAliveTimer = new Timer((state) =>
{
this.SendKeepAlive();
});
}
this._keepAliveTimer.Change(this._keepAliveInterval, this._keepAliveInterval);
}
}
public event EventHandler<AuthenticationEventArgs> Authenticating;
/// <summary>
@@ -85,9 +118,15 @@ namespace Renci.SshClient
/// <summary>
/// Sends keep-alive message to the server.
/// </summary>
public void KeepAlive()
public void SendKeepAlive()
{
this.Session.KeepAlive();
if (this.Session == null)
return;
if (!this.Session.IsConnected)
return;
this.Session.SendKeepAlive();
}
/// <summary>
@@ -155,6 +194,11 @@ namespace Renci.SshClient
{
this.Session.Dispose();
}
if (this._keepAliveTimer != null)
{
this._keepAliveTimer.Dispose();
}
}
// Note disposing has been done.