ForwardedPortLocal.cs 4.9 KB

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