2
0

BaseClient.cs 7.8 KB

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