ForwardedPortLocal.cs 7.8 KB

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