ForwardedPortDynamic.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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, 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. /// Initializes a new instance of the <see cref="ForwardedPortDynamic"/> class.
  21. /// </summary>
  22. /// <param name="port">The port.</param>
  23. public ForwardedPortDynamic(uint port)
  24. : this(string.Empty, port)
  25. {
  26. }
  27. /// <summary>
  28. /// Initializes a new instance of the <see cref="ForwardedPortDynamic"/> class.
  29. /// </summary>
  30. /// <param name="host">The host.</param>
  31. /// <param name="port">The port.</param>
  32. public ForwardedPortDynamic(string host, uint port)
  33. {
  34. this.BoundHost = host;
  35. this.BoundPort = port;
  36. }
  37. /// <summary>
  38. /// Starts local port forwarding.
  39. /// </summary>
  40. public override void Start()
  41. {
  42. this.InternalStart();
  43. }
  44. /// <summary>
  45. /// Stops local port forwarding.
  46. /// </summary>
  47. public override void Stop()
  48. {
  49. base.Stop();
  50. this.InternalStop();
  51. }
  52. partial void InternalStart();
  53. partial void InternalStop();
  54. partial void ExecuteThread(Action action);
  55. #region IDisposable Members
  56. private bool _isDisposed;
  57. /// <summary>
  58. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged ResourceMessages.
  59. /// </summary>
  60. public void Dispose()
  61. {
  62. Dispose(true);
  63. GC.SuppressFinalize(this);
  64. }
  65. /// <summary>
  66. /// Releases unmanaged and - optionally - managed resources
  67. /// </summary>
  68. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged ResourceMessages.</param>
  69. protected virtual void Dispose(bool disposing)
  70. {
  71. // Check to see if Dispose has already been called.
  72. if (!this._isDisposed)
  73. {
  74. this.InternalStop();
  75. // If disposing equals true, dispose all managed
  76. // and unmanaged ResourceMessages.
  77. if (disposing)
  78. {
  79. // Dispose managed ResourceMessages.
  80. if (this._listenerTaskCompleted != null)
  81. {
  82. this._listenerTaskCompleted.Dispose();
  83. this._listenerTaskCompleted = null;
  84. }
  85. }
  86. // Note disposing has been done.
  87. _isDisposed = true;
  88. }
  89. }
  90. /// <summary>
  91. /// Releases unmanaged resources and performs other cleanup operations before the
  92. /// <see cref="ForwardedPortLocal"/> is reclaimed by garbage collection.
  93. /// </summary>
  94. ~ForwardedPortDynamic()
  95. {
  96. // Do not re-create Dispose clean-up code here.
  97. // Calling Dispose(false) is optimal in terms of
  98. // readability and maintainability.
  99. Dispose(false);
  100. }
  101. #endregion
  102. }
  103. }