ForwardedPortDynamic.cs 5.5 KB

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