diff --git a/Renci.SshClient/Renci.SshClient/Session.cs b/Renci.SshClient/Renci.SshClient/Session.cs
index ffbb7e06..1325155a 100644
--- a/Renci.SshClient/Renci.SshClient/Session.cs
+++ b/Renci.SshClient/Renci.SshClient/Session.cs
@@ -588,7 +588,7 @@ namespace Renci.SshClient
///
/// Sends "keep alive" message to keep connection alive.
///
- 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)
{
diff --git a/Renci.SshClient/Renci.SshClient/SshBaseClient.cs b/Renci.SshClient/Renci.SshClient/SshBaseClient.cs
index fb76fc00..7db5ebc2 100644
--- a/Renci.SshClient/Renci.SshClient/SshBaseClient.cs
+++ b/Renci.SshClient/Renci.SshClient/SshBaseClient.cs
@@ -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;
+
///
/// Gets current session.
///
@@ -38,6 +43,34 @@ namespace Renci.SshClient
}
}
+ ///
+ /// Gets or sets the keep alive interval in seconds.
+ ///
+ ///
+ /// The keep alive interval in seconds.
+ ///
+ 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 Authenticating;
///
@@ -85,9 +118,15 @@ namespace Renci.SshClient
///
/// Sends keep-alive message to the server.
///
- public void KeepAlive()
+ public void SendKeepAlive()
{
- this.Session.KeepAlive();
+ if (this.Session == null)
+ return;
+
+ if (!this.Session.IsConnected)
+ return;
+
+ this.Session.SendKeepAlive();
}
///
@@ -155,6 +194,11 @@ namespace Renci.SshClient
{
this.Session.Dispose();
}
+ if (this._keepAliveTimer != null)
+ {
+ this._keepAliveTimer.Dispose();
+ }
+
}
// Note disposing has been done.