ForwardedPortLocal.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. /// Starts local port forwarding.
  13. /// </summary>
  14. public override void Start()
  15. {
  16. this.InternalStart();
  17. }
  18. /// <summary>
  19. /// Stops local port forwarding.
  20. /// </summary>
  21. public override void Stop()
  22. {
  23. base.Stop();
  24. this.InternalStop();
  25. }
  26. partial void InternalStart();
  27. partial void InternalStop();
  28. partial void ExecuteThread(Action action);
  29. #region IDisposable Members
  30. private bool _isDisposed = false;
  31. /// <summary>
  32. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged ResourceMessages.
  33. /// </summary>
  34. public void Dispose()
  35. {
  36. Dispose(true);
  37. GC.SuppressFinalize(this);
  38. }
  39. /// <summary>
  40. /// Releases unmanaged and - optionally - managed resources
  41. /// </summary>
  42. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged ResourceMessages.</param>
  43. protected virtual void Dispose(bool disposing)
  44. {
  45. // Check to see if Dispose has already been called.
  46. if (!this._isDisposed)
  47. {
  48. // If disposing equals true, dispose all managed
  49. // and unmanaged ResourceMessages.
  50. if (disposing)
  51. {
  52. // Dispose managed ResourceMessages.
  53. if (this._listenerTaskCompleted != null)
  54. {
  55. this._listenerTaskCompleted.Dispose();
  56. this._listenerTaskCompleted = null;
  57. }
  58. }
  59. // Note disposing has been done.
  60. _isDisposed = true;
  61. }
  62. }
  63. /// <summary>
  64. /// Releases unmanaged resources and performs other cleanup operations before the
  65. /// <see cref="ForwardedPortLocal"/> is reclaimed by garbage collection.
  66. /// </summary>
  67. ~ForwardedPortLocal()
  68. {
  69. // Do not re-create Dispose clean-up code here.
  70. // Calling Dispose(false) is optimal in terms of
  71. // readability and maintainability.
  72. Dispose(false);
  73. }
  74. #endregion
  75. }
  76. }