SubsystemSession.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using Renci.SshNet.Channels;
  7. using Renci.SshNet.Common;
  8. using System.Diagnostics;
  9. using System.Collections.Generic;
  10. using System.Globalization;
  11. using Renci.SshNet.Sftp.Responses;
  12. using Renci.SshNet.Sftp.Requests;
  13. using Renci.SshNet.Messages.Connection;
  14. namespace Renci.SshNet.Sftp
  15. {
  16. public abstract class SubsystemSession : IDisposable
  17. {
  18. private Session _session;
  19. private string _subsystemName;
  20. private ChannelSession _channel;
  21. private Exception _exception;
  22. private EventWaitHandle _errorOccuredWaitHandle = new AutoResetEvent(false);
  23. protected TimeSpan _operationTimeout;
  24. public event EventHandler<ExceptionEventArgs> ErrorOccured;
  25. protected uint ChannelNumber
  26. {
  27. get
  28. {
  29. return this._channel.RemoteChannelNumber;
  30. }
  31. }
  32. #region SFTP messages
  33. //internal event EventHandler<MessageEventArgs<StatusMessage>> StatusMessageReceived;
  34. //internal event EventHandler<MessageEventArgs<DataMessage>> DataMessageReceived;
  35. //internal event EventHandler<MessageEventArgs<HandleMessage>> HandleMessageReceived;
  36. //internal event EventHandler<MessageEventArgs<NameMessage>> NameMessageReceived;
  37. //internal event EventHandler<MessageEventArgs<AttributesMessage>> AttributesMessageReceived;
  38. #endregion
  39. /// <summary>
  40. /// Initializes a new instance of the SubsystemSession class.
  41. /// </summary>
  42. /// <exception cref="ArgumentNullException"><paramref name="session"/> or <paramref name="subsystemName"/> is null.</exception>
  43. public SubsystemSession(Session session, string subsystemName, TimeSpan operationTimeout)
  44. {
  45. if (session == null)
  46. throw new ArgumentNullException("session");
  47. if (subsystemName == null)
  48. throw new ArgumentNullException("subsystemName");
  49. this._session = session;
  50. this._subsystemName = subsystemName;
  51. this._operationTimeout = operationTimeout;
  52. }
  53. public void Connect()
  54. {
  55. this._channel = this._session.CreateChannel<ChannelSession>();
  56. this._session.ErrorOccured += Session_ErrorOccured;
  57. this._session.Disconnected += Session_Disconnected;
  58. this._channel.DataReceived += Channel_DataReceived;
  59. this._channel.Open();
  60. this._channel.SendSubsystemRequest(_subsystemName);
  61. this.OnChannelOpen();
  62. }
  63. public void Disconnect()
  64. {
  65. this._channel.Close();
  66. }
  67. public void SendData(ChannelDataMessage message)
  68. {
  69. this._session.SendMessage(message);
  70. }
  71. protected abstract void OnChannelOpen();
  72. protected abstract void OnDataReceived(uint dataTypeCode, byte[] data);
  73. protected void RaiseError(Exception error)
  74. {
  75. this._exception = error;
  76. this._errorOccuredWaitHandle.Set();
  77. if (this.ErrorOccured != null)
  78. {
  79. this.ErrorOccured(this, new ExceptionEventArgs(error));
  80. }
  81. }
  82. private void Channel_DataReceived(object sender, Common.ChannelDataEventArgs e)
  83. {
  84. this.OnDataReceived(e.DataTypeCode, e.Data);
  85. }
  86. internal void WaitHandle(WaitHandle waitHandle, TimeSpan operationTimeout)
  87. {
  88. var waitHandles = new WaitHandle[]
  89. {
  90. this._errorOccuredWaitHandle,
  91. waitHandle,
  92. };
  93. var index = EventWaitHandle.WaitAny(waitHandles, operationTimeout);
  94. if (index < 1)
  95. {
  96. throw this._exception;
  97. }
  98. else if (index > 1)
  99. {
  100. // throw time out error
  101. throw new SshOperationTimeoutException(string.Format(CultureInfo.CurrentCulture, "Sftp operation has timed out."));
  102. }
  103. }
  104. private void Session_Disconnected(object sender, EventArgs e)
  105. {
  106. this.RaiseError(new SshException("Connection was lost"));
  107. }
  108. private void Session_ErrorOccured(object sender, ExceptionEventArgs e)
  109. {
  110. this.RaiseError(e.Exception);
  111. }
  112. #region IDisposable Members
  113. private bool _isDisposed = false;
  114. /// <summary>
  115. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  116. /// </summary>
  117. public void Dispose()
  118. {
  119. Dispose(true);
  120. GC.SuppressFinalize(this);
  121. }
  122. /// <summary>
  123. /// Releases unmanaged and - optionally - managed resources
  124. /// </summary>
  125. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  126. protected virtual void Dispose(bool disposing)
  127. {
  128. // Check to see if Dispose has already been called.
  129. if (!this._isDisposed)
  130. {
  131. if (this._channel != null)
  132. {
  133. this._channel.DataReceived -= Channel_DataReceived;
  134. this._channel.Dispose();
  135. this._channel = null;
  136. }
  137. this._session.ErrorOccured -= Session_ErrorOccured;
  138. this._session.Disconnected -= Session_Disconnected;
  139. // If disposing equals true, dispose all managed
  140. // and unmanaged resources.
  141. if (disposing)
  142. {
  143. // Dispose managed resources.
  144. if (this._errorOccuredWaitHandle != null)
  145. {
  146. this._errorOccuredWaitHandle.Dispose();
  147. this._errorOccuredWaitHandle = null;
  148. }
  149. }
  150. // Note disposing has been done.
  151. _isDisposed = true;
  152. }
  153. }
  154. /// <summary>
  155. /// Releases unmanaged resources and performs other cleanup operations before the
  156. /// <see cref="SftpSession"/> is reclaimed by garbage collection.
  157. /// </summary>
  158. ~SubsystemSession()
  159. {
  160. // Do not re-create Dispose clean-up code here.
  161. // Calling Dispose(false) is optimal in terms of
  162. // readability and maintainability.
  163. Dispose(false);
  164. }
  165. #endregion
  166. }
  167. }