ForwardedPort.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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
  9. {
  10. /// <summary>
  11. /// Gets or sets the session.
  12. /// </summary>
  13. /// <value>
  14. /// The session.
  15. /// </value>
  16. internal Session Session { get; set; }
  17. /// <summary>
  18. /// Gets or sets a value indicating whether port forwarding is started.
  19. /// </summary>
  20. /// <value>
  21. /// <c>true</c> if port forwarding is started; otherwise, <c>false</c>.
  22. /// </value>
  23. public bool IsStarted { get; protected set; }
  24. /// <summary>
  25. /// Occurs when an exception is thrown.
  26. /// </summary>
  27. public event EventHandler<ExceptionEventArgs> Exception;
  28. /// <summary>
  29. /// Occurs when a port forwarding request is received.
  30. /// </summary>
  31. public event EventHandler<PortForwardEventArgs> RequestReceived;
  32. /// <summary>
  33. /// Starts port forwarding.
  34. /// </summary>
  35. public virtual void Start()
  36. {
  37. if (this.Session == null)
  38. {
  39. throw new InvalidOperationException("Forwarded port is not added to a client.");
  40. }
  41. if (!this.Session.IsConnected)
  42. {
  43. throw new SshConnectionException("Client not connected.");
  44. }
  45. this.Session.ErrorOccured += Session_ErrorOccured;
  46. }
  47. /// <summary>
  48. /// Stops port forwarding.
  49. /// </summary>
  50. public virtual void Stop()
  51. {
  52. if (this.Session != null)
  53. {
  54. this.Session.ErrorOccured -= Session_ErrorOccured;
  55. }
  56. }
  57. /// <summary>
  58. /// Raises <see cref="Renci.SshNet.ForwardedPort.Exception"/> event.
  59. /// </summary>
  60. /// <param name="exception">The exception.</param>
  61. protected void RaiseExceptionEvent(Exception exception)
  62. {
  63. var handlers = Exception;
  64. if (handlers != null)
  65. {
  66. handlers(this, new ExceptionEventArgs(exception));
  67. }
  68. }
  69. /// <summary>
  70. /// Raises <see cref="Renci.SshNet.ForwardedPort.RequestReceived"/> event.
  71. /// </summary>
  72. /// <param name="host">Request originator host.</param>
  73. /// <param name="port">Request originator port.</param>
  74. protected void RaiseRequestReceived(string host, uint port)
  75. {
  76. var handlers = RequestReceived;
  77. if (handlers != null)
  78. {
  79. RequestReceived(this, new PortForwardEventArgs(host, port));
  80. }
  81. }
  82. /// <summary>
  83. /// Handles session ErrorOccured event.
  84. /// </summary>
  85. /// <param name="sender">The source of the event.</param>
  86. /// <param name="e">The <see cref="ExceptionEventArgs"/> instance containing the event data.</param>
  87. private void Session_ErrorOccured(object sender, ExceptionEventArgs e)
  88. {
  89. RaiseExceptionEvent(e.Exception);
  90. }
  91. }
  92. }