ForwardedPortDynamic.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System;
  2. using System.Threading;
  3. namespace Renci.SshNet
  4. {
  5. /// <summary>
  6. /// Provides functionality for dynamic port forwarding
  7. /// </summary>
  8. public partial class ForwardedPortDynamic : ForwardedPort
  9. {
  10. private EventWaitHandle _listenerCompleted;
  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 or sets a value indicating whether port forwarding is started.
  21. /// </summary>
  22. /// <value>
  23. /// <c>true</c> if port forwarding is started; otherwise, <c>false</c>.
  24. /// </value>
  25. public override bool IsStarted
  26. {
  27. get { return _listenerCompleted != null && !_listenerCompleted.WaitOne(0); }
  28. }
  29. /// <summary>
  30. /// Initializes a new instance of the <see cref="ForwardedPortDynamic"/> class.
  31. /// </summary>
  32. /// <param name="port">The port.</param>
  33. public ForwardedPortDynamic(uint port)
  34. : this(string.Empty, port)
  35. {
  36. }
  37. /// <summary>
  38. /// Initializes a new instance of the <see cref="ForwardedPortDynamic"/> class.
  39. /// </summary>
  40. /// <param name="host">The host.</param>
  41. /// <param name="port">The port.</param>
  42. public ForwardedPortDynamic(string host, uint port)
  43. {
  44. BoundHost = host;
  45. BoundPort = port;
  46. }
  47. /// <summary>
  48. /// Starts local port forwarding.
  49. /// </summary>
  50. protected override void StartPort()
  51. {
  52. InternalStart();
  53. }
  54. /// <summary>
  55. /// Stops local port forwarding, and waits for the specified timeout until all pending
  56. /// requests are processed.
  57. /// </summary>
  58. /// <param name="timeout">The maximum amount of time to wait for pending requests to finish processing.</param>
  59. protected override void StopPort(TimeSpan timeout)
  60. {
  61. if (IsStarted)
  62. {
  63. // prevent new requests from getting processed before we signal existing
  64. // channels that the port is closing
  65. StopListener();
  66. // signal existing channels that the port is closing
  67. base.StopPort(timeout);
  68. }
  69. // wait for open channels to close
  70. InternalStop(timeout);
  71. }
  72. /// <summary>
  73. /// Ensures the current instance is not disposed.
  74. /// </summary>
  75. /// <exception cref="ObjectDisposedException">The current instance is disposed.</exception>
  76. protected override void CheckDisposed()
  77. {
  78. if (_isDisposed)
  79. throw new ObjectDisposedException(GetType().FullName);
  80. }
  81. partial void InternalStart();
  82. /// <summary>
  83. /// Stops the listener.
  84. /// </summary>
  85. partial void StopListener();
  86. /// <summary>
  87. /// Waits for pending requests to finish, and channels to close.
  88. /// </summary>
  89. /// <param name="timeout">The maximum time to wait for the forwarded port to stop.</param>
  90. partial void InternalStop(TimeSpan timeout);
  91. #region IDisposable Members
  92. /// <summary>
  93. /// Holds a value indicating whether the current instance is disposed.
  94. /// </summary>
  95. /// <value>
  96. /// <c>true</c> if the current instance is disposed; otherwise, <c>false</c>.
  97. /// </value>
  98. private bool _isDisposed;
  99. /// <summary>
  100. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged ResourceMessages.
  101. /// </summary>
  102. public void Dispose()
  103. {
  104. Dispose(true);
  105. GC.SuppressFinalize(this);
  106. }
  107. partial void InternalDispose(bool disposing);
  108. /// <summary>
  109. /// Releases unmanaged and - optionally - managed resources
  110. /// </summary>
  111. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged ResourceMessages.</param>
  112. protected override void Dispose(bool disposing)
  113. {
  114. if (!_isDisposed)
  115. {
  116. base.Dispose(disposing);
  117. if (disposing)
  118. {
  119. if (_listenerCompleted != null)
  120. {
  121. _listenerCompleted.Dispose();
  122. _listenerCompleted = null;
  123. }
  124. }
  125. InternalDispose(disposing);
  126. _isDisposed = true;
  127. }
  128. }
  129. /// <summary>
  130. /// Releases unmanaged resources and performs other cleanup operations before the
  131. /// <see cref="ForwardedPortLocal"/> is reclaimed by garbage collection.
  132. /// </summary>
  133. ~ForwardedPortDynamic()
  134. {
  135. // Do not re-create Dispose clean-up code here.
  136. // Calling Dispose(false) is optimal in terms of
  137. // readability and maintainability.
  138. Dispose(false);
  139. }
  140. #endregion
  141. }
  142. }