BaseClient.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. public event EventHandler<ExceptionEventArgs> ErrorOccurred;
  67. /// <summary>
  68. /// Occurs when host key received.
  69. /// </summary>
  70. public event EventHandler<HostKeyEventArgs> HostKeyReceived;
  71. /// <summary>
  72. /// Initializes a new instance of the <see cref="BaseClient"/> class.
  73. /// </summary>
  74. /// <param name="connectionInfo">The connection info.</param>
  75. /// <exception cref="ArgumentNullException"><paramref name="connectionInfo"/> is null.</exception>
  76. public BaseClient(ConnectionInfo connectionInfo)
  77. {
  78. if (connectionInfo == null)
  79. throw new ArgumentNullException("connectionInfo");
  80. this.ConnectionInfo = connectionInfo;
  81. this.Session = new Session(connectionInfo);
  82. }
  83. /// <summary>
  84. /// Connects client to the server.
  85. /// </summary>
  86. public void Connect()
  87. {
  88. this.OnConnecting();
  89. if (this.IsConnected)
  90. {
  91. this.Session.Disconnect();
  92. }
  93. this.Session = new Session(this.ConnectionInfo);
  94. this.Session.HostKeyReceived += Session_HostKeyReceived;
  95. this.Session.ErrorOccured += Session_ErrorOccured;
  96. this.Session.Connect();
  97. this.OnConnected();
  98. }
  99. /// <summary>
  100. /// Disconnects client from the server.
  101. /// </summary>
  102. public void Disconnect()
  103. {
  104. if (!this.IsConnected)
  105. return;
  106. this.OnDisconnecting();
  107. this.Session.Disconnect();
  108. this.OnDisconnected();
  109. }
  110. /// <summary>
  111. /// Sends keep-alive message to the server.
  112. /// </summary>
  113. public void SendKeepAlive()
  114. {
  115. if (this.Session == null)
  116. return;
  117. if (!this.Session.IsConnected)
  118. return;
  119. this.Session.SendKeepAlive();
  120. }
  121. /// <summary>
  122. /// Called when client is connecting to the server.
  123. /// </summary>
  124. protected virtual void OnConnecting()
  125. {
  126. }
  127. /// <summary>
  128. /// Called when client is connected to the server.
  129. /// </summary>
  130. protected virtual void OnConnected()
  131. {
  132. }
  133. /// <summary>
  134. /// Called when client is disconnecting from the server.
  135. /// </summary>
  136. protected virtual void OnDisconnecting()
  137. {
  138. }
  139. /// <summary>
  140. /// Called when client is disconnected from the server.
  141. /// </summary>
  142. protected virtual void OnDisconnected()
  143. {
  144. }
  145. /// <summary>
  146. /// Ensures that client is connected.
  147. /// </summary>
  148. /// <exception cref="Renci.SshNet.Common.SshConnectionException">When client not connected.</exception>
  149. protected void EnsureConnection()
  150. {
  151. if (!this.Session.IsConnected)
  152. throw new SshConnectionException("Client not connected.");
  153. }
  154. private void Session_ErrorOccured(object sender, ExceptionEventArgs e)
  155. {
  156. if (this.ErrorOccurred != null)
  157. {
  158. this.ErrorOccurred(this, e);
  159. }
  160. }
  161. private void Session_HostKeyReceived(object sender, HostKeyEventArgs e)
  162. {
  163. if (this.HostKeyReceived != null)
  164. {
  165. this.HostKeyReceived(this, e);
  166. }
  167. }
  168. #region IDisposable Members
  169. private bool _isDisposed = false;
  170. /// <summary>
  171. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged ResourceMessages.
  172. /// </summary>
  173. public void Dispose()
  174. {
  175. Dispose(true);
  176. GC.SuppressFinalize(this);
  177. }
  178. /// <summary>
  179. /// Releases unmanaged and - optionally - managed resources
  180. /// </summary>
  181. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged ResourceMessages.</param>
  182. protected virtual void Dispose(bool disposing)
  183. {
  184. // Check to see if Dispose has already been called.
  185. if (!this._isDisposed)
  186. {
  187. // If disposing equals true, dispose all managed
  188. // and unmanaged ResourceMessages.
  189. if (disposing)
  190. {
  191. // Dispose managed ResourceMessages.
  192. this.Session.ErrorOccured -= Session_ErrorOccured;
  193. this.Session.HostKeyReceived -= Session_HostKeyReceived;
  194. if (this.Session != null)
  195. {
  196. this.Session.Dispose();
  197. this.Session = null;
  198. }
  199. if (this._keepAliveTimer != null)
  200. {
  201. this._keepAliveTimer.Dispose();
  202. this._keepAliveTimer = null;
  203. }
  204. }
  205. // Note disposing has been done.
  206. _isDisposed = true;
  207. }
  208. }
  209. /// <summary>
  210. /// Releases unmanaged resources and performs other cleanup operations before the
  211. /// <see cref="BaseClient"/> is reclaimed by garbage collection.
  212. /// </summary>
  213. ~BaseClient()
  214. {
  215. // Do not re-create Dispose clean-up code here.
  216. // Calling Dispose(false) is optimal in terms of
  217. // readability and maintainability.
  218. Dispose(false);
  219. }
  220. #endregion
  221. }
  222. }