BaseClient.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. using System;
  2. using System.IO;
  3. using System.Threading;
  4. using Renci.SshNet.Common;
  5. namespace Renci.SshNet
  6. {
  7. /// <summary>
  8. /// Serves as base class for client implementations, provides common client functionality.
  9. /// </summary>
  10. public abstract class BaseClient : IDisposable
  11. {
  12. private TimeSpan _keepAliveInterval;
  13. private Timer _keepAliveTimer;
  14. /// <summary>
  15. /// Gets current session.
  16. /// </summary>
  17. protected Session Session { get; private set; }
  18. /// <summary>
  19. /// Gets the connection info.
  20. /// </summary>
  21. public ConnectionInfo ConnectionInfo { get; private set; }
  22. /// <summary>
  23. /// Gets a value indicating whether this client is connected to the server.
  24. /// </summary>
  25. /// <value>
  26. /// <c>true</c> if this client is connected; otherwise, <c>false</c>.
  27. /// </value>
  28. public bool IsConnected
  29. {
  30. get
  31. {
  32. if (this.Session == null)
  33. return false;
  34. else
  35. return this.Session.IsConnected;
  36. }
  37. }
  38. /// <summary>
  39. /// Gets or sets the keep alive interval in seconds.
  40. /// </summary>
  41. /// <value>
  42. /// The keep alive interval in seconds.
  43. /// </value>
  44. public TimeSpan KeepAliveInterval
  45. {
  46. get
  47. {
  48. return this._keepAliveInterval;
  49. }
  50. set
  51. {
  52. this._keepAliveInterval = value;
  53. if (this._keepAliveTimer == null)
  54. {
  55. this._keepAliveTimer = new Timer((state) =>
  56. {
  57. this.SendKeepAlive();
  58. });
  59. }
  60. this._keepAliveTimer.Change(this._keepAliveInterval, this._keepAliveInterval);
  61. }
  62. }
  63. /// <summary>
  64. /// Occurs when an error occurred.
  65. /// </summary>
  66. /// <example>
  67. /// <code source="..\..\Renci.SshNet.Tests\Classes\SshClientTest.cs" region="Example SshClient Connect ErrorOccurred" language="C#" title="Handle ErrorOccurred event" />
  68. /// </example>
  69. public event EventHandler<ExceptionEventArgs> ErrorOccurred;
  70. /// <summary>
  71. /// Occurs when host key received.
  72. /// </summary>
  73. /// <example>
  74. /// <code source="..\..\Renci.SshNet.Tests\Classes\SshClientTest.cs" region="Example SshClient Connect HostKeyReceived" language="C#" title="Handle HostKeyReceived event" />
  75. /// </example>
  76. public event EventHandler<HostKeyEventArgs> HostKeyReceived;
  77. /// <summary>
  78. /// Initializes a new instance of the <see cref="BaseClient"/> class.
  79. /// </summary>
  80. /// <param name="connectionInfo">The connection info.</param>
  81. /// <exception cref="ArgumentNullException"><paramref name="connectionInfo"/> is null.</exception>
  82. public BaseClient(ConnectionInfo connectionInfo)
  83. {
  84. if (connectionInfo == null)
  85. throw new ArgumentNullException("connectionInfo");
  86. this.ConnectionInfo = connectionInfo;
  87. this.Session = new Session(connectionInfo);
  88. }
  89. /// <summary>
  90. /// Connects client to the server.
  91. /// </summary>
  92. public void Connect()
  93. {
  94. this.OnConnecting();
  95. if (this.IsConnected)
  96. {
  97. this.Session.Disconnect();
  98. }
  99. this.Session = new Session(this.ConnectionInfo);
  100. this.Session.HostKeyReceived += Session_HostKeyReceived;
  101. this.Session.ErrorOccured += Session_ErrorOccured;
  102. this.Session.Connect();
  103. this.OnConnected();
  104. }
  105. /// <summary>
  106. /// Disconnects client from the server.
  107. /// </summary>
  108. public void Disconnect()
  109. {
  110. if (!this.IsConnected)
  111. return;
  112. this.OnDisconnecting();
  113. this.Session.Disconnect();
  114. this.OnDisconnected();
  115. }
  116. /// <summary>
  117. /// Sends keep-alive message to the server.
  118. /// </summary>
  119. public void SendKeepAlive()
  120. {
  121. if (this.Session == null)
  122. return;
  123. if (!this.Session.IsConnected)
  124. return;
  125. this.Session.SendKeepAlive();
  126. }
  127. /// <summary>
  128. /// Called when client is connecting to the server.
  129. /// </summary>
  130. protected virtual void OnConnecting()
  131. {
  132. }
  133. /// <summary>
  134. /// Called when client is connected to the server.
  135. /// </summary>
  136. protected virtual void OnConnected()
  137. {
  138. }
  139. /// <summary>
  140. /// Called when client is disconnecting from the server.
  141. /// </summary>
  142. protected virtual void OnDisconnecting()
  143. {
  144. }
  145. /// <summary>
  146. /// Called when client is disconnected from the server.
  147. /// </summary>
  148. protected virtual void OnDisconnected()
  149. {
  150. }
  151. private void Session_ErrorOccured(object sender, ExceptionEventArgs e)
  152. {
  153. var handler = this.ErrorOccurred;
  154. if (handler != null)
  155. {
  156. handler(this, e);
  157. }
  158. }
  159. private void Session_HostKeyReceived(object sender, HostKeyEventArgs e)
  160. {
  161. var handler = this.HostKeyReceived;
  162. if (handler != null)
  163. {
  164. handler(this, e);
  165. }
  166. }
  167. #region IDisposable Members
  168. private bool _isDisposed = false;
  169. /// <summary>
  170. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged ResourceMessages.
  171. /// </summary>
  172. public void Dispose()
  173. {
  174. Dispose(true);
  175. GC.SuppressFinalize(this);
  176. }
  177. /// <summary>
  178. /// Releases unmanaged and - optionally - managed resources
  179. /// </summary>
  180. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged ResourceMessages.</param>
  181. protected virtual void Dispose(bool disposing)
  182. {
  183. // Check to see if Dispose has already been called.
  184. if (!this._isDisposed)
  185. {
  186. // If disposing equals true, dispose all managed
  187. // and unmanaged ResourceMessages.
  188. if (disposing)
  189. {
  190. // Dispose managed ResourceMessages.
  191. this.Session.ErrorOccured -= Session_ErrorOccured;
  192. this.Session.HostKeyReceived -= Session_HostKeyReceived;
  193. if (this.Session != null)
  194. {
  195. this.Session.Dispose();
  196. this.Session = null;
  197. }
  198. if (this._keepAliveTimer != null)
  199. {
  200. this._keepAliveTimer.Dispose();
  201. this._keepAliveTimer = null;
  202. }
  203. }
  204. // Note disposing has been done.
  205. _isDisposed = true;
  206. }
  207. }
  208. /// <summary>
  209. /// Releases unmanaged resources and performs other cleanup operations before the
  210. /// <see cref="BaseClient"/> is reclaimed by garbage collection.
  211. /// </summary>
  212. ~BaseClient()
  213. {
  214. // Do not re-create Dispose clean-up code here.
  215. // Calling Dispose(false) is optimal in terms of
  216. // readability and maintainability.
  217. Dispose(false);
  218. }
  219. #endregion
  220. }
  221. }