ForwardedPortDynamic.cs 3.7 KB

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