ForwardedPort.cs 5.5 KB

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