ForwardedPortLocal.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. /// <example>
  34. /// <code source="..\..\Renci.SshNet.Tests\Classes\ForwardedPortLocalTest.cs" region="Example SshClient AddForwardedPort Start Stop ForwardedPortLocal" language="C#" title="Local port forwarding" />
  35. /// </example>
  36. public ForwardedPortLocal(uint boundPort, string host, uint port)
  37. : this(string.Empty, boundPort, host, port)
  38. {
  39. }
  40. /// <summary>
  41. /// Initializes a new instance of the <see cref="ForwardedPortLocal"/> class.
  42. /// </summary>
  43. /// <param name="boundHost">The bound host.</param>
  44. /// <param name="host">The host.</param>
  45. /// <param name="port">The port.</param>
  46. public ForwardedPortLocal(string boundHost, string host, uint port)
  47. : this(boundHost, 0, host, port)
  48. {
  49. }
  50. /// <summary>
  51. /// Initializes a new instance of the <see cref="ForwardedPortLocal"/> class.
  52. /// </summary>
  53. /// <param name="boundHost">The bound host.</param>
  54. /// <param name="boundPort">The bound port.</param>
  55. /// <param name="host">The host.</param>
  56. /// <param name="port">The port.</param>
  57. public ForwardedPortLocal(string boundHost, uint boundPort, string host, uint port)
  58. {
  59. if (boundHost == null)
  60. throw new ArgumentNullException("boundHost");
  61. if (host == null)
  62. throw new ArgumentNullException("host");
  63. if (!boundHost.IsValidHost())
  64. throw new ArgumentException("boundHost");
  65. if (!boundPort.IsValidPort())
  66. throw new ArgumentOutOfRangeException("boundPort");
  67. if (!host.IsValidHost())
  68. throw new ArgumentException("host");
  69. if (!port.IsValidPort())
  70. throw new ArgumentOutOfRangeException("port");
  71. this.BoundHost = boundHost;
  72. this.BoundPort = boundPort;
  73. this.Host = host;
  74. this.Port = port;
  75. }
  76. /// <summary>
  77. /// Starts local port forwarding.
  78. /// </summary>
  79. public override void Start()
  80. {
  81. this.InternalStart();
  82. }
  83. /// <summary>
  84. /// Stops local port forwarding.
  85. /// </summary>
  86. public override void Stop()
  87. {
  88. base.Stop();
  89. this.InternalStop();
  90. }
  91. partial void InternalStart();
  92. partial void InternalStop();
  93. partial void ExecuteThread(Action action);
  94. #region IDisposable Members
  95. private bool _isDisposed;
  96. /// <summary>
  97. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged ResourceMessages.
  98. /// </summary>
  99. public void Dispose()
  100. {
  101. Dispose(true);
  102. GC.SuppressFinalize(this);
  103. }
  104. /// <summary>
  105. /// Releases unmanaged and - optionally - managed resources
  106. /// </summary>
  107. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged ResourceMessages.</param>
  108. protected virtual void Dispose(bool disposing)
  109. {
  110. // Check to see if Dispose has already been called.
  111. if (!this._isDisposed)
  112. {
  113. this.InternalStop();
  114. // If disposing equals true, dispose all managed
  115. // and unmanaged ResourceMessages.
  116. if (disposing)
  117. {
  118. // Dispose managed ResourceMessages.
  119. if (this._listenerTaskCompleted != null)
  120. {
  121. this._listenerTaskCompleted.Dispose();
  122. this._listenerTaskCompleted = null;
  123. }
  124. }
  125. // Note disposing has been done.
  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. ~ForwardedPortLocal()
  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. }