ForwardedPort.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 started.
  19. /// </summary>
  20. /// <value>
  21. /// <c>true</c> if port forwarding started; otherwise, <c>false</c>.
  22. /// </value>
  23. public bool IsStarted { get; protected set; }
  24. /// <summary>
  25. /// Occurs when exception is thrown.
  26. /// </summary>
  27. public event EventHandler<ExceptionEventArgs> Exception;
  28. /// <summary>
  29. /// Occurs when port forwarding request 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("Session property is null.");
  40. }
  41. if (!this.Session.IsConnected)
  42. {
  43. throw new SshConnectionException("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="execption">The exception.</param>
  61. protected void RaiseExceptionEvent(Exception execption)
  62. {
  63. if (this.Exception != null)
  64. {
  65. this.Exception(this, new ExceptionEventArgs(execption));
  66. }
  67. }
  68. /// <summary>
  69. /// Raises <see cref="Renci.SshNet.ForwardedPort.RequestReceived"/> event.
  70. /// </summary>
  71. /// <param name="host">Request originator host.</param>
  72. /// <param name="port">Request originator port.</param>
  73. protected void RaiseRequestReceived(string host, uint port)
  74. {
  75. if (this.RequestReceived != null)
  76. {
  77. this.RequestReceived(this, new PortForwardEventArgs(host, port));
  78. }
  79. }
  80. /// <summary>
  81. /// Handles session ErrorOccured event.
  82. /// </summary>
  83. /// <param name="sender">The source of the event.</param>
  84. /// <param name="e">The <see cref="ExceptionEventArgs"/> instance containing the event data.</param>
  85. private void Session_ErrorOccured(object sender, ExceptionEventArgs e)
  86. {
  87. this.RaiseExceptionEvent(e.Exception);
  88. }
  89. }
  90. }