ISession.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using System;
  2. using System.Net.Sockets;
  3. using System.Threading;
  4. using Renci.SshNet.Channels;
  5. using Renci.SshNet.Common;
  6. using Renci.SshNet.Messages;
  7. using Renci.SshNet.Messages.Authentication;
  8. using Renci.SshNet.Messages.Connection;
  9. namespace Renci.SshNet
  10. {
  11. /// <summary>
  12. /// Provides functionality to connect and interact with SSH server.
  13. /// </summary>
  14. internal interface ISession
  15. {
  16. ///// <summary>
  17. ///// Gets or sets the connection info.
  18. ///// </summary>
  19. ///// <value>The connection info.</value>
  20. IConnectionInfo ConnectionInfo { get; }
  21. /// <summary>
  22. /// Gets a value indicating whether the session is connected.
  23. /// </summary>
  24. /// <value>
  25. /// <c>true</c> if the session is connected; otherwise, <c>false</c>.
  26. /// </value>
  27. bool IsConnected { get; }
  28. /// <summary>
  29. /// Gets the next channel number.
  30. /// </summary>
  31. /// <value>
  32. /// The next channel number.
  33. /// </value>
  34. uint NextChannelNumber { get; }
  35. /// <summary>
  36. /// Gets the session semaphore that controls session channels.
  37. /// </summary>
  38. /// <value>
  39. /// The session semaphore.
  40. /// </value>
  41. SemaphoreLight SessionSemaphore { get; }
  42. /// <summary>
  43. /// Gets a <see cref="WaitHandle"/> that can be used to wait for the message listener loop to complete.
  44. /// </summary>
  45. /// <value>
  46. /// A <see cref="WaitHandle"/> that can be used to wait for the message listener loop to complete, or
  47. /// <c>null</c> when the session has not been connected.
  48. /// </value>
  49. WaitHandle MessageListenerCompleted { get; }
  50. /// <summary>
  51. /// Create a new SSH session channel.
  52. /// </summary>
  53. /// <returns>
  54. /// A new SSH session channel.
  55. /// </returns>
  56. IChannelSession CreateChannelSession();
  57. /// <summary>
  58. /// Create a new channel for a locally forwarded TCP/IP port.
  59. /// </summary>
  60. /// <returns>
  61. /// A new channel for a locally forwarded TCP/IP port.
  62. /// </returns>
  63. IChannelDirectTcpip CreateChannelDirectTcpip();
  64. /// <summary>
  65. /// Creates a "forwarded-tcpip" SSH channel.
  66. /// </summary>
  67. /// <returns>
  68. /// A new "forwarded-tcpip" SSH channel.
  69. /// </returns>
  70. IChannelForwardedTcpip CreateChannelForwardedTcpip(uint remoteChannelNumber, uint remoteWindowSize, uint remoteChannelDataPacketSize);
  71. /// <summary>
  72. /// Registers SSH message with the session.
  73. /// </summary>
  74. /// <param name="messageName">The name of the message to register with the session.</param>
  75. void RegisterMessage(string messageName);
  76. /// <summary>
  77. /// Sends a message to the server.
  78. /// </summary>
  79. /// <param name="message">The message to send.</param>
  80. /// <exception cref="SshConnectionException">The client is not connected.</exception>
  81. /// <exception cref="SshOperationTimeoutException">The operation timed out.</exception>
  82. /// <exception cref="InvalidOperationException">The size of the packet exceeds the maximum size defined by the protocol.</exception>
  83. void SendMessage(Message message);
  84. /// <summary>
  85. /// Unregister SSH message from the session.
  86. /// </summary>
  87. /// <param name="messageName">The name of the message to unregister with the session.</param>
  88. void UnRegisterMessage(string messageName);
  89. /// <summary>
  90. /// Waits for the specified handle or the exception handle for the receive thread
  91. /// to signal within the connection timeout.
  92. /// </summary>
  93. /// <param name="waitHandle">The wait handle.</param>
  94. /// <exception cref="SshConnectionException">A received package was invalid or failed the message integrity check.</exception>
  95. /// <exception cref="SshOperationTimeoutException">None of the handles are signaled in time and the session is not disconnecting.</exception>
  96. /// <exception cref="SocketException">A socket error was signaled while receiving messages from the server.</exception>
  97. /// <remarks>
  98. /// When neither handles are signaled in time and the session is not closing, then the
  99. /// session is disconnected.
  100. /// </remarks>
  101. void WaitOnHandle(WaitHandle waitHandle);
  102. /// <summary>
  103. /// Waits for the specified handle or the exception handle for the receive thread
  104. /// to signal within the specified timeout.
  105. /// </summary>
  106. /// <param name="waitHandle">The wait handle.</param>
  107. /// <param name="timeout">The time to wait for any of the handles to become signaled.</param>
  108. /// <exception cref="SshConnectionException">A received package was invalid or failed the message integrity check.</exception>
  109. /// <exception cref="SshOperationTimeoutException">None of the handles are signaled in time and the session is not disconnecting.</exception>
  110. /// <exception cref="SocketException">A socket error was signaled while receiving messages from the server.</exception>
  111. /// <remarks>
  112. /// When neither handles are signaled in time and the session is not closing, then the
  113. /// session is disconnected.
  114. /// </remarks>
  115. void WaitOnHandle(WaitHandle waitHandle, TimeSpan timeout);
  116. /// <summary>
  117. /// Occurs when <see cref="ChannelCloseMessage"/> message received
  118. /// </summary>
  119. event EventHandler<MessageEventArgs<ChannelCloseMessage>> ChannelCloseReceived;
  120. /// <summary>
  121. /// Occurs when <see cref="ChannelDataMessage"/> message received
  122. /// </summary>
  123. event EventHandler<MessageEventArgs<ChannelDataMessage>> ChannelDataReceived;
  124. /// <summary>
  125. /// Occurs when <see cref="ChannelEofMessage"/> message received
  126. /// </summary>
  127. event EventHandler<MessageEventArgs<ChannelEofMessage>> ChannelEofReceived;
  128. /// <summary>
  129. /// Occurs when <see cref="ChannelExtendedDataMessage"/> message received
  130. /// </summary>
  131. event EventHandler<MessageEventArgs<ChannelExtendedDataMessage>> ChannelExtendedDataReceived;
  132. /// <summary>
  133. /// Occurs when <see cref="ChannelFailureMessage"/> message received
  134. /// </summary>
  135. event EventHandler<MessageEventArgs<ChannelFailureMessage>> ChannelFailureReceived;
  136. /// <summary>
  137. /// Occurs when <see cref="ChannelOpenConfirmationMessage"/> message received
  138. /// </summary>
  139. event EventHandler<MessageEventArgs<ChannelOpenConfirmationMessage>> ChannelOpenConfirmationReceived;
  140. /// <summary>
  141. /// Occurs when <see cref="ChannelOpenFailureMessage"/> message received
  142. /// </summary>
  143. event EventHandler<MessageEventArgs<ChannelOpenFailureMessage>> ChannelOpenFailureReceived;
  144. /// <summary>
  145. /// Occurs when <see cref="ChannelOpenMessage"/> message received
  146. /// </summary>
  147. event EventHandler<MessageEventArgs<ChannelOpenMessage>> ChannelOpenReceived;
  148. /// <summary>
  149. /// Occurs when <see cref="ChannelRequestMessage"/> message received
  150. /// </summary>
  151. event EventHandler<MessageEventArgs<ChannelRequestMessage>> ChannelRequestReceived;
  152. /// <summary>
  153. /// Occurs when <see cref="ChannelSuccessMessage"/> message received
  154. /// </summary>
  155. event EventHandler<MessageEventArgs<ChannelSuccessMessage>> ChannelSuccessReceived;
  156. /// <summary>
  157. /// Occurs when <see cref="ChannelWindowAdjustMessage"/> message received
  158. /// </summary>
  159. event EventHandler<MessageEventArgs<ChannelWindowAdjustMessage>> ChannelWindowAdjustReceived;
  160. /// <summary>
  161. /// Occurs when session has been disconnected from the server.
  162. /// </summary>
  163. event EventHandler<EventArgs> Disconnected;
  164. /// <summary>
  165. /// Occurs when an error occurred.
  166. /// </summary>
  167. event EventHandler<ExceptionEventArgs> ErrorOccured;
  168. /// <summary>
  169. /// Occurs when <see cref="RequestSuccessMessage"/> message received
  170. /// </summary>
  171. event EventHandler<MessageEventArgs<RequestSuccessMessage>> RequestSuccessReceived;
  172. /// <summary>
  173. /// Occurs when <see cref="RequestFailureMessage"/> message received
  174. /// </summary>
  175. event EventHandler<MessageEventArgs<RequestFailureMessage>> RequestFailureReceived;
  176. /// <summary>
  177. /// Occurs when <see cref="BannerMessage"/> message is received from the server.
  178. /// </summary>
  179. event EventHandler<MessageEventArgs<BannerMessage>> UserAuthenticationBannerReceived;
  180. }
  181. }