ForwardedPort.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using Renci.SshNet.Common;
  3. namespace Renci.SshNet
  4. {
  5. /// <summary>
  6. /// Base class for port forwarding functionality.
  7. /// </summary>
  8. public abstract class ForwardedPort : IForwardedPort
  9. {
  10. private EventHandler _closingEvent;
  11. /// <summary>
  12. /// Gets or sets the session.
  13. /// </summary>
  14. /// <value>
  15. /// The session.
  16. /// </value>
  17. internal ISession Session { get; set; }
  18. /// <summary>
  19. /// The <see cref="IForwardedPort.Closing"/> event occurs as the forward port is being stopped.
  20. /// </summary>
  21. event EventHandler IForwardedPort.Closing
  22. {
  23. add { _closingEvent += value; }
  24. remove { _closingEvent -= value; }
  25. }
  26. /// <summary>
  27. /// Gets or sets a value indicating whether port forwarding is started.
  28. /// </summary>
  29. /// <value>
  30. /// <c>true</c> if port forwarding is started; otherwise, <c>false</c>.
  31. /// </value>
  32. public bool IsStarted { get; protected set; }
  33. /// <summary>
  34. /// Occurs when an exception is thrown.
  35. /// </summary>
  36. public event EventHandler<ExceptionEventArgs> Exception;
  37. /// <summary>
  38. /// Occurs when a port forwarding request is received.
  39. /// </summary>
  40. public event EventHandler<PortForwardEventArgs> RequestReceived;
  41. /// <summary>
  42. /// Starts port forwarding.
  43. /// </summary>
  44. public virtual void Start()
  45. {
  46. if (this.Session == null)
  47. {
  48. throw new InvalidOperationException("Forwarded port is not added to a client.");
  49. }
  50. if (!this.Session.IsConnected)
  51. {
  52. throw new SshConnectionException("Client not connected.");
  53. }
  54. this.Session.ErrorOccured += Session_ErrorOccured;
  55. }
  56. /// <summary>
  57. /// Stops port forwarding.
  58. /// </summary>
  59. public virtual void Stop()
  60. {
  61. RaiseClosing();
  62. if (this.Session != null)
  63. {
  64. this.Session.ErrorOccured -= Session_ErrorOccured;
  65. }
  66. }
  67. /// <summary>
  68. /// Raises <see cref="Renci.SshNet.ForwardedPort.Exception"/> event.
  69. /// </summary>
  70. /// <param name="exception">The exception.</param>
  71. protected void RaiseExceptionEvent(Exception exception)
  72. {
  73. var handlers = Exception;
  74. if (handlers != null)
  75. {
  76. handlers(this, new ExceptionEventArgs(exception));
  77. }
  78. }
  79. /// <summary>
  80. /// Raises <see cref="Renci.SshNet.ForwardedPort.RequestReceived"/> event.
  81. /// </summary>
  82. /// <param name="host">Request originator host.</param>
  83. /// <param name="port">Request originator port.</param>
  84. protected void RaiseRequestReceived(string host, uint port)
  85. {
  86. var handlers = RequestReceived;
  87. if (handlers != null)
  88. {
  89. RequestReceived(this, new PortForwardEventArgs(host, port));
  90. }
  91. }
  92. /// <summary>
  93. /// Raises the <see cref="IForwardedPort.Closing"/> event.
  94. /// </summary>
  95. private void RaiseClosing()
  96. {
  97. var handlers = _closingEvent;
  98. if (handlers != null)
  99. {
  100. handlers(this, new EventArgs());
  101. }
  102. }
  103. /// <summary>
  104. /// Handles session ErrorOccured event.
  105. /// </summary>
  106. /// <param name="sender">The source of the event.</param>
  107. /// <param name="e">The <see cref="ExceptionEventArgs"/> instance containing the event data.</param>
  108. private void Session_ErrorOccured(object sender, ExceptionEventArgs e)
  109. {
  110. RaiseExceptionEvent(e.Exception);
  111. }
  112. }
  113. }