ForwardedPort.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 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. if (IsStarted)
  65. {
  66. StopPort(Session.ConnectionInfo.Timeout);
  67. }
  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. var session = Session;
  82. if (session != null)
  83. {
  84. session.ErrorOccured -= Session_ErrorOccured;
  85. }
  86. }
  87. /// <summary>
  88. /// Releases unmanaged and - optionally - managed resources
  89. /// </summary>
  90. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  91. protected virtual void Dispose(bool disposing)
  92. {
  93. if (disposing)
  94. {
  95. var session = Session;
  96. if (session != null)
  97. {
  98. StopPort(session.ConnectionInfo.Timeout);
  99. Session = null;
  100. }
  101. }
  102. }
  103. /// <summary>
  104. /// Ensures the current instance is not disposed.
  105. /// </summary>
  106. /// <exception cref="ObjectDisposedException">The current instance is disposed.</exception>
  107. protected abstract void CheckDisposed();
  108. /// <summary>
  109. /// Raises <see cref="Renci.SshNet.ForwardedPort.Exception"/> event.
  110. /// </summary>
  111. /// <param name="exception">The exception.</param>
  112. protected void RaiseExceptionEvent(Exception exception)
  113. {
  114. var handlers = Exception;
  115. if (handlers != null)
  116. {
  117. handlers(this, new ExceptionEventArgs(exception));
  118. }
  119. }
  120. /// <summary>
  121. /// Raises <see cref="Renci.SshNet.ForwardedPort.RequestReceived"/> event.
  122. /// </summary>
  123. /// <param name="host">Request originator host.</param>
  124. /// <param name="port">Request originator port.</param>
  125. protected void RaiseRequestReceived(string host, uint port)
  126. {
  127. var handlers = RequestReceived;
  128. if (handlers != null)
  129. {
  130. handlers(this, new PortForwardEventArgs(host, port));
  131. }
  132. }
  133. /// <summary>
  134. /// Raises the <see cref="IForwardedPort.Closing"/> event.
  135. /// </summary>
  136. private void RaiseClosing()
  137. {
  138. var handlers = Closing;
  139. if (handlers != null)
  140. {
  141. handlers(this, EventArgs.Empty);
  142. }
  143. }
  144. /// <summary>
  145. /// Handles session ErrorOccured event.
  146. /// </summary>
  147. /// <param name="sender">The source of the event.</param>
  148. /// <param name="e">The <see cref="ExceptionEventArgs"/> instance containing the event data.</param>
  149. private void Session_ErrorOccured(object sender, ExceptionEventArgs e)
  150. {
  151. RaiseExceptionEvent(e.Exception);
  152. }
  153. }
  154. }