ForwardedPortLocal.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using System;
  2. using System.Threading;
  3. using Renci.SshNet.Common;
  4. namespace Renci.SshNet
  5. {
  6. /// <summary>
  7. /// Provides functionality for local port forwarding
  8. /// </summary>
  9. public partial class ForwardedPortLocal : ForwardedPort, IDisposable
  10. {
  11. private EventWaitHandle _listenerTaskCompleted;
  12. /// <summary>
  13. /// Gets the bound host.
  14. /// </summary>
  15. public string BoundHost { get; private set; }
  16. /// <summary>
  17. /// Gets the bound port.
  18. /// </summary>
  19. public uint BoundPort { get; private set; }
  20. /// <summary>
  21. /// Gets the forwarded host.
  22. /// </summary>
  23. public string Host { get; private set; }
  24. /// <summary>
  25. /// Gets the forwarded port.
  26. /// </summary>
  27. public uint Port { get; private set; }
  28. /// <summary>
  29. /// Gets or sets a value indicating whether port forwarding is started.
  30. /// </summary>
  31. /// <value>
  32. /// <c>true</c> if port forwarding is started; otherwise, <c>false</c>.
  33. /// </value>
  34. public override bool IsStarted
  35. {
  36. get { return _listenerTaskCompleted != null && !_listenerTaskCompleted.WaitOne(0); }
  37. }
  38. /// <summary>
  39. /// Initializes a new instance of the <see cref="ForwardedPortLocal"/> class.
  40. /// </summary>
  41. /// <param name="boundPort">The bound port.</param>
  42. /// <param name="host">The host.</param>
  43. /// <param name="port">The port.</param>
  44. /// <exception cref="ArgumentOutOfRangeException"><paramref name="boundPort" /> is greater than <see cref="F:System.Net.IPEndPoint.MaxPort" />.</exception>
  45. /// <exception cref="ArgumentNullException"><paramref name="host"/> is <c>null</c>.</exception>
  46. /// <exception cref="ArgumentOutOfRangeException"><paramref name="port" /> is greater than <see cref="F:System.Net.IPEndPoint.MaxPort" />.</exception>
  47. /// <example>
  48. /// <code source="..\..\Renci.SshNet.Tests\Classes\ForwardedPortLocalTest.cs" region="Example SshClient AddForwardedPort Start Stop ForwardedPortLocal" language="C#" title="Local port forwarding" />
  49. /// </example>
  50. public ForwardedPortLocal(uint boundPort, string host, uint port)
  51. : this(string.Empty, boundPort, host, port)
  52. {
  53. }
  54. /// <summary>
  55. /// Initializes a new instance of the <see cref="ForwardedPortLocal"/> class.
  56. /// </summary>
  57. /// <param name="boundHost">The bound host.</param>
  58. /// <param name="host">The host.</param>
  59. /// <param name="port">The port.</param>
  60. /// <exception cref="ArgumentNullException"><paramref name="boundHost"/> is <c>null</c>.</exception>
  61. /// <exception cref="ArgumentNullException"><paramref name="host"/> is <c>null</c>.</exception>
  62. /// <exception cref="ArgumentOutOfRangeException"><paramref name="port" /> is greater than <see cref="F:System.Net.IPEndPoint.MaxPort" />.</exception>
  63. public ForwardedPortLocal(string boundHost, string host, uint port)
  64. : this(boundHost, 0, host, port)
  65. {
  66. }
  67. /// <summary>
  68. /// Initializes a new instance of the <see cref="ForwardedPortLocal"/> class.
  69. /// </summary>
  70. /// <param name="boundHost">The bound host.</param>
  71. /// <param name="boundPort">The bound port.</param>
  72. /// <param name="host">The host.</param>
  73. /// <param name="port">The port.</param>
  74. /// <exception cref="ArgumentNullException"><paramref name="boundHost"/> is <c>null</c>.</exception>
  75. /// <exception cref="ArgumentNullException"><paramref name="host"/> is <c>null</c>.</exception>
  76. /// <exception cref="ArgumentOutOfRangeException"><paramref name="boundPort" /> is greater than <see cref="F:System.Net.IPEndPoint.MaxPort" />.</exception>
  77. /// <exception cref="ArgumentOutOfRangeException"><paramref name="port" /> is greater than <see cref="F:System.Net.IPEndPoint.MaxPort" />.</exception>
  78. public ForwardedPortLocal(string boundHost, uint boundPort, string host, uint port)
  79. {
  80. if (boundHost == null)
  81. throw new ArgumentNullException("boundHost");
  82. if (host == null)
  83. throw new ArgumentNullException("host");
  84. boundPort.ValidatePort("boundPort");
  85. port.ValidatePort("port");
  86. BoundHost = boundHost;
  87. BoundPort = boundPort;
  88. Host = host;
  89. Port = port;
  90. }
  91. /// <summary>
  92. /// Starts local port forwarding.
  93. /// </summary>
  94. protected override void StartPort()
  95. {
  96. InternalStart();
  97. }
  98. /// <summary>
  99. /// Stops local port forwarding, and waits for the specified timeout until all pending
  100. /// requests are processed.
  101. /// </summary>
  102. /// <param name="timeout">The maximum amount of time to wait for pending requests to finish processing.</param>
  103. protected override void StopPort(TimeSpan timeout)
  104. {
  105. if (IsStarted)
  106. {
  107. // prevent new requests from getting processed before we signal existing
  108. // channels that the port is closing
  109. StopListener();
  110. // signal existing channels that the port is closing
  111. base.StopPort(timeout);
  112. }
  113. // wait for open channels to close
  114. InternalStop(timeout);
  115. }
  116. /// <summary>
  117. /// Ensures the current instance is not disposed.
  118. /// </summary>
  119. /// <exception cref="ObjectDisposedException">The current instance is disposed.</exception>
  120. protected override void CheckDisposed()
  121. {
  122. if (_isDisposed)
  123. throw new ObjectDisposedException(GetType().FullName);
  124. }
  125. partial void InternalStart();
  126. /// <summary>
  127. /// Interrupts the listener, and waits for the listener loop to finish.
  128. /// </summary>
  129. /// <remarks>
  130. /// When the forwarded port is stopped, then any further action is skipped.
  131. /// </remarks>
  132. partial void StopListener();
  133. partial void InternalStop(TimeSpan timeout);
  134. #region IDisposable Members
  135. private bool _isDisposed;
  136. /// <summary>
  137. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  138. /// </summary>
  139. public void Dispose()
  140. {
  141. Dispose(true);
  142. GC.SuppressFinalize(this);
  143. }
  144. /// <summary>
  145. /// Releases unmanaged and - optionally - managed resources
  146. /// </summary>
  147. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged ResourceMessages.</param>
  148. protected override void Dispose(bool disposing)
  149. {
  150. if (!_isDisposed)
  151. {
  152. base.Dispose(disposing);
  153. if (disposing)
  154. {
  155. if (_listenerTaskCompleted != null)
  156. {
  157. _listenerTaskCompleted.Dispose();
  158. _listenerTaskCompleted = null;
  159. }
  160. }
  161. _isDisposed = true;
  162. }
  163. }
  164. /// <summary>
  165. /// Releases unmanaged resources and performs other cleanup operations before the
  166. /// <see cref="ForwardedPortLocal"/> is reclaimed by garbage collection.
  167. /// </summary>
  168. ~ForwardedPortLocal()
  169. {
  170. // Do not re-create Dispose clean-up code here.
  171. // Calling Dispose(false) is optimal in terms of
  172. // readability and maintainability.
  173. Dispose(false);
  174. }
  175. #endregion
  176. }
  177. }