BaseClient.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. /// 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. if (!this.IsConnected)
  99. return;
  100. this.OnDisconnecting();
  101. this.Dispose();
  102. this.OnDisconnected();
  103. }
  104. /// <summary>
  105. /// Sends keep-alive message to the server.
  106. /// </summary>
  107. public void SendKeepAlive()
  108. {
  109. if (this.Session == null)
  110. return;
  111. if (!this.Session.IsConnected)
  112. return;
  113. this.Session.SendKeepAlive();
  114. }
  115. /// <summary>
  116. /// Called when client is connecting to the server.
  117. /// </summary>
  118. protected virtual void OnConnecting()
  119. {
  120. }
  121. /// <summary>
  122. /// Called when client is connected to the server.
  123. /// </summary>
  124. protected virtual void OnConnected()
  125. {
  126. }
  127. /// <summary>
  128. /// Called when client is disconnecting from the server.
  129. /// </summary>
  130. protected virtual void OnDisconnecting()
  131. {
  132. }
  133. /// <summary>
  134. /// Called when client is disconnected from the server.
  135. /// </summary>
  136. protected virtual void OnDisconnected()
  137. {
  138. }
  139. /// <summary>
  140. /// Ensures that client is connected.
  141. /// </summary>
  142. /// <exception cref="Renci.SshNet.Common.SshConnectionException">When client not connected.</exception>
  143. protected void EnsureConnection()
  144. {
  145. if (!this.Session.IsConnected)
  146. throw new SshConnectionException("Client not connected.");
  147. }
  148. private void Session_ErrorOccured(object sender, ExceptionEventArgs e)
  149. {
  150. if (this.ErrorOccurred != null)
  151. {
  152. this.ErrorOccurred(this, e);
  153. }
  154. }
  155. #region IDisposable Members
  156. private bool _isDisposed = false;
  157. /// <summary>
  158. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged ResourceMessages.
  159. /// </summary>
  160. public void Dispose()
  161. {
  162. Dispose(true);
  163. GC.SuppressFinalize(this);
  164. }
  165. /// <summary>
  166. /// Releases unmanaged and - optionally - managed resources
  167. /// </summary>
  168. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged ResourceMessages.</param>
  169. protected virtual void Dispose(bool disposing)
  170. {
  171. // Check to see if Dispose has already been called.
  172. if (!this._isDisposed)
  173. {
  174. // If disposing equals true, dispose all managed
  175. // and unmanaged ResourceMessages.
  176. if (disposing)
  177. {
  178. // Dispose managed ResourceMessages.
  179. this.Session.ErrorOccured -= Session_ErrorOccured;
  180. if (this.Session != null)
  181. {
  182. this.Session.Dispose();
  183. this.Session = null;
  184. }
  185. if (this._keepAliveTimer != null)
  186. {
  187. this._keepAliveTimer.Dispose();
  188. this._keepAliveTimer = null;
  189. }
  190. }
  191. // Note disposing has been done.
  192. _isDisposed = true;
  193. }
  194. }
  195. /// <summary>
  196. /// Releases unmanaged resources and performs other cleanup operations before the
  197. /// <see cref="BaseClient"/> is reclaimed by garbage collection.
  198. /// </summary>
  199. ~BaseClient()
  200. {
  201. // Do not re-create Dispose clean-up code here.
  202. // Calling Dispose(false) is optimal in terms of
  203. // readability and maintainability.
  204. Dispose(false);
  205. }
  206. #endregion
  207. }
  208. }