ForwardedPort.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. this.Session.ErrorOccured -= Session_ErrorOccured;
  53. }
  54. /// <summary>
  55. /// Raises <see cref="Renci.SshNet.ForwardedPort.Exception"/> event.
  56. /// </summary>
  57. /// <param name="execption">The exception.</param>
  58. protected void RaiseExceptionEvent(Exception execption)
  59. {
  60. if (this.Exception != null)
  61. {
  62. this.Exception(this, new ExceptionEventArgs(execption));
  63. }
  64. }
  65. /// <summary>
  66. /// Raises <see cref="Renci.SshNet.ForwardedPort.RequestReceived"/> event.
  67. /// </summary>
  68. /// <param name="host">Request originator host.</param>
  69. /// <param name="port">Request originator port.</param>
  70. protected void RaiseRequestReceived(string host, uint port)
  71. {
  72. if (this.RequestReceived != null)
  73. {
  74. this.RequestReceived(this, new PortForwardEventArgs(host, port));
  75. }
  76. }
  77. /// <summary>
  78. /// Handles session ErrorOccured event.
  79. /// </summary>
  80. /// <param name="sender">The source of the event.</param>
  81. /// <param name="e">The <see cref="ExceptionEventArgs"/> instance containing the event data.</param>
  82. private void Session_ErrorOccured(object sender, ExceptionEventArgs e)
  83. {
  84. this.RaiseExceptionEvent(e.Exception);
  85. }
  86. }
  87. }