BaseClient.cs 7.1 KB

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