ForwardedPortLocal.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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; protected set; }
  15. /// <summary>
  16. /// Gets the bound port.
  17. /// </summary>
  18. public uint BoundPort { get; protected set; }
  19. /// <summary>
  20. /// Gets the forwarded host.
  21. /// </summary>
  22. public string Host { get; protected set; }
  23. /// <summary>
  24. /// Gets the forwarded port.
  25. /// </summary>
  26. public uint Port { get; protected set; }
  27. /// <summary>
  28. /// Initializes a new instance of the <see cref="ForwardedPortLocal"/> class.
  29. /// </summary>
  30. /// <param name="boundPort">The bound port.</param>
  31. /// <param name="host">The host.</param>
  32. /// <param name="port">The port.</param>
  33. /// <exception cref="ArgumentOutOfRangeException"><paramref name="boundPort" /> is greater than <see cref="F:System.Net.IPEndPoint.MaxPort" />.</exception>
  34. /// <exception cref="ArgumentNullException"><paramref name="host"/> is <c>null</c>.</exception>
  35. /// <exception cref="ArgumentOutOfRangeException"><paramref name="port" /> is greater than <see cref="F:System.Net.IPEndPoint.MaxPort" />.</exception>
  36. /// <example>
  37. /// <code source="..\..\Renci.SshNet.Tests\Classes\ForwardedPortLocalTest.cs" region="Example SshClient AddForwardedPort Start Stop ForwardedPortLocal" language="C#" title="Local port forwarding" />
  38. /// </example>
  39. public ForwardedPortLocal(uint boundPort, string host, uint port)
  40. : this(string.Empty, boundPort, host, port)
  41. {
  42. }
  43. /// <summary>
  44. /// Initializes a new instance of the <see cref="ForwardedPortLocal"/> class.
  45. /// </summary>
  46. /// <param name="boundHost">The bound host.</param>
  47. /// <param name="host">The host.</param>
  48. /// <param name="port">The port.</param>
  49. /// <exception cref="ArgumentNullException"><paramref name="boundHost"/> is <c>null</c>.</exception>
  50. /// <exception cref="ArgumentNullException"><paramref name="host"/> is <c>null</c>.</exception>
  51. /// <exception cref="ArgumentOutOfRangeException"><paramref name="port" /> is greater than <see cref="F:System.Net.IPEndPoint.MaxPort" />.</exception>
  52. public ForwardedPortLocal(string boundHost, string host, uint port)
  53. : this(boundHost, 0, host, port)
  54. {
  55. }
  56. /// <summary>
  57. /// Initializes a new instance of the <see cref="ForwardedPortLocal"/> class.
  58. /// </summary>
  59. /// <param name="boundHost">The bound host.</param>
  60. /// <param name="boundPort">The bound port.</param>
  61. /// <param name="host">The host.</param>
  62. /// <param name="port">The port.</param>
  63. /// <exception cref="ArgumentNullException"><paramref name="boundHost"/> is <c>null</c>.</exception>
  64. /// <exception cref="ArgumentNullException"><paramref name="host"/> is <c>null</c>.</exception>
  65. /// <exception cref="ArgumentOutOfRangeException"><paramref name="boundPort" /> is greater than <see cref="F:System.Net.IPEndPoint.MaxPort" />.</exception>
  66. /// <exception cref="ArgumentOutOfRangeException"><paramref name="port" /> is greater than <see cref="F:System.Net.IPEndPoint.MaxPort" />.</exception>
  67. public ForwardedPortLocal(string boundHost, uint boundPort, string host, uint port)
  68. {
  69. if (boundHost == null)
  70. throw new ArgumentNullException("boundHost");
  71. if (host == null)
  72. throw new ArgumentNullException("host");
  73. boundPort.ValidatePort("boundPort");
  74. port.ValidatePort("port");
  75. this.BoundHost = boundHost;
  76. this.BoundPort = boundPort;
  77. this.Host = host;
  78. this.Port = port;
  79. }
  80. /// <summary>
  81. /// Starts local port forwarding.
  82. /// </summary>
  83. public override void Start()
  84. {
  85. this.InternalStart();
  86. }
  87. /// <summary>
  88. /// Stops local port forwarding.
  89. /// </summary>
  90. public override void Stop()
  91. {
  92. base.Stop();
  93. this.InternalStop();
  94. }
  95. partial void InternalStart();
  96. partial void InternalStop();
  97. partial void ExecuteThread(Action action);
  98. #region IDisposable Members
  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. /// <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 virtual void Dispose(bool disposing)
  113. {
  114. // Check to see if Dispose has already been called.
  115. if (!this._isDisposed)
  116. {
  117. this.InternalStop();
  118. // If disposing equals true, dispose all managed
  119. // and unmanaged ResourceMessages.
  120. if (disposing)
  121. {
  122. // Dispose managed ResourceMessages.
  123. if (this._listenerTaskCompleted != null)
  124. {
  125. this._listenerTaskCompleted.Dispose();
  126. this._listenerTaskCompleted = null;
  127. }
  128. }
  129. // Note disposing has been done.
  130. _isDisposed = true;
  131. }
  132. }
  133. /// <summary>
  134. /// Releases unmanaged resources and performs other cleanup operations before the
  135. /// <see cref="ForwardedPortLocal"/> is reclaimed by garbage collection.
  136. /// </summary>
  137. ~ForwardedPortLocal()
  138. {
  139. // Do not re-create Dispose clean-up code here.
  140. // Calling Dispose(false) is optimal in terms of
  141. // readability and maintainability.
  142. Dispose(false);
  143. }
  144. #endregion
  145. }
  146. }