ForwardedPort.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. /// <summary>
  11. /// Gets or sets the session.
  12. /// </summary>
  13. /// <value>
  14. /// The session.
  15. /// </value>
  16. internal ISession Session { get; set; }
  17. /// <summary>
  18. /// The <see cref="Closing"/> event occurs as the forwarded port is being stopped.
  19. /// </summary>
  20. internal event EventHandler Closing;
  21. /// <summary>
  22. /// The <see cref="IForwardedPort.Closing"/> event occurs as the forwarded port is being stopped.
  23. /// </summary>
  24. event EventHandler IForwardedPort.Closing
  25. {
  26. add { Closing += value; }
  27. remove { Closing -= value; }
  28. }
  29. /// <summary>
  30. /// Gets or sets a value indicating whether port forwarding is started.
  31. /// </summary>
  32. /// <value>
  33. /// <c>true</c> if port forwarding is started; otherwise, <c>false</c>.
  34. /// </value>
  35. public abstract bool IsStarted { get; }
  36. /// <summary>
  37. /// Occurs when an exception is thrown.
  38. /// </summary>
  39. public event EventHandler<ExceptionEventArgs> Exception;
  40. /// <summary>
  41. /// Occurs when a port forwarding request is received.
  42. /// </summary>
  43. public event EventHandler<PortForwardEventArgs> RequestReceived;
  44. /// <summary>
  45. /// Starts port forwarding.
  46. /// </summary>
  47. public virtual void Start()
  48. {
  49. CheckDisposed();
  50. if (IsStarted)
  51. throw new InvalidOperationException("Forwarded port is already started.");
  52. if (Session == null)
  53. throw new InvalidOperationException("Forwarded port is not added to a client.");
  54. if (!Session.IsConnected)
  55. throw new SshConnectionException("Client not connected.");
  56. Session.ErrorOccured += Session_ErrorOccured;
  57. StartPort();
  58. }
  59. /// <summary>
  60. /// Stops port forwarding.
  61. /// </summary>
  62. public virtual void Stop()
  63. {
  64. CheckDisposed();
  65. if (!IsStarted)
  66. return;
  67. StopPort(Session.ConnectionInfo.Timeout);
  68. }
  69. /// <summary>
  70. /// Starts port forwarding.
  71. /// </summary>
  72. protected abstract void StartPort();
  73. /// <summary>
  74. /// Stops port forwarding, and waits for the specified timeout until all pending
  75. /// requests are processed.
  76. /// </summary>
  77. /// <param name="timeout">The maximum amount of time to wait for pending requests to finish processing.</param>
  78. protected virtual void StopPort(TimeSpan timeout)
  79. {
  80. RaiseClosing();
  81. if (Session != null)
  82. {
  83. Session.ErrorOccured -= Session_ErrorOccured;
  84. }
  85. }
  86. /// <summary>
  87. /// Releases unmanaged and - optionally - managed resources
  88. /// </summary>
  89. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged ResourceMessages.</param>
  90. protected virtual void Dispose(bool disposing)
  91. {
  92. if (disposing)
  93. {
  94. if (Session != null)
  95. {
  96. Session.ErrorOccured -= Session_ErrorOccured;
  97. StopPort(Session.ConnectionInfo.Timeout);
  98. Session = null;
  99. }
  100. }
  101. }
  102. /// <summary>
  103. /// Ensures the current instance is not disposed.
  104. /// </summary>
  105. /// <exception cref="ObjectDisposedException">The current instance is disposed.</exception>
  106. protected abstract void CheckDisposed();
  107. /// <summary>
  108. /// Raises <see cref="Renci.SshNet.ForwardedPort.Exception"/> event.
  109. /// </summary>
  110. /// <param name="exception">The exception.</param>
  111. protected void RaiseExceptionEvent(Exception exception)
  112. {
  113. var handlers = Exception;
  114. if (handlers != null)
  115. {
  116. handlers(this, new ExceptionEventArgs(exception));
  117. }
  118. }
  119. /// <summary>
  120. /// Raises <see cref="Renci.SshNet.ForwardedPort.RequestReceived"/> event.
  121. /// </summary>
  122. /// <param name="host">Request originator host.</param>
  123. /// <param name="port">Request originator port.</param>
  124. protected void RaiseRequestReceived(string host, uint port)
  125. {
  126. var handlers = RequestReceived;
  127. if (handlers != null)
  128. {
  129. RequestReceived(this, new PortForwardEventArgs(host, port));
  130. }
  131. }
  132. /// <summary>
  133. /// Raises the <see cref="IForwardedPort.Closing"/> event.
  134. /// </summary>
  135. private void RaiseClosing()
  136. {
  137. var handlers = Closing;
  138. if (handlers != null)
  139. {
  140. handlers(this, EventArgs.Empty);
  141. }
  142. }
  143. /// <summary>
  144. /// Handles session ErrorOccured event.
  145. /// </summary>
  146. /// <param name="sender">The source of the event.</param>
  147. /// <param name="e">The <see cref="ExceptionEventArgs"/> instance containing the event data.</param>
  148. private void Session_ErrorOccured(object sender, ExceptionEventArgs e)
  149. {
  150. RaiseExceptionEvent(e.Exception);
  151. }
  152. }
  153. }