| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- using System;
- using Renci.SshNet.Common;
- namespace Renci.SshNet
- {
- /// <summary>
- /// Base class for port forwarding functionality.
- /// </summary>
- public abstract class ForwardedPort : IForwardedPort
- {
- private EventHandler _closingEvent;
- /// <summary>
- /// Gets or sets the session.
- /// </summary>
- /// <value>
- /// The session.
- /// </value>
- internal ISession Session { get; set; }
- /// <summary>
- /// The <see cref="IForwardedPort.Closing"/> event occurs as the forward port is being stopped.
- /// </summary>
- event EventHandler IForwardedPort.Closing
- {
- add { _closingEvent += value; }
- remove { _closingEvent -= value; }
- }
- /// <summary>
- /// Gets or sets a value indicating whether port forwarding is started.
- /// </summary>
- /// <value>
- /// <c>true</c> if port forwarding is started; otherwise, <c>false</c>.
- /// </value>
- public bool IsStarted { get; protected set; }
- /// <summary>
- /// Occurs when an exception is thrown.
- /// </summary>
- public event EventHandler<ExceptionEventArgs> Exception;
- /// <summary>
- /// Occurs when a port forwarding request is received.
- /// </summary>
- public event EventHandler<PortForwardEventArgs> RequestReceived;
- /// <summary>
- /// Starts port forwarding.
- /// </summary>
- public virtual void Start()
- {
- if (this.Session == null)
- {
- throw new InvalidOperationException("Forwarded port is not added to a client.");
- }
- if (!this.Session.IsConnected)
- {
- throw new SshConnectionException("Client not connected.");
- }
- this.Session.ErrorOccured += Session_ErrorOccured;
- }
- /// <summary>
- /// Stops port forwarding.
- /// </summary>
- public virtual void Stop()
- {
- RaiseClosing();
- if (this.Session != null)
- {
- this.Session.ErrorOccured -= Session_ErrorOccured;
- }
- }
- /// <summary>
- /// Raises <see cref="Renci.SshNet.ForwardedPort.Exception"/> event.
- /// </summary>
- /// <param name="exception">The exception.</param>
- protected void RaiseExceptionEvent(Exception exception)
- {
- var handlers = Exception;
- if (handlers != null)
- {
- handlers(this, new ExceptionEventArgs(exception));
- }
- }
- /// <summary>
- /// Raises <see cref="Renci.SshNet.ForwardedPort.RequestReceived"/> event.
- /// </summary>
- /// <param name="host">Request originator host.</param>
- /// <param name="port">Request originator port.</param>
- protected void RaiseRequestReceived(string host, uint port)
- {
- var handlers = RequestReceived;
- if (handlers != null)
- {
- RequestReceived(this, new PortForwardEventArgs(host, port));
- }
- }
- /// <summary>
- /// Raises the <see cref="IForwardedPort.Closing"/> event.
- /// </summary>
- private void RaiseClosing()
- {
- var handlers = _closingEvent;
- if (handlers != null)
- {
- handlers(this, new EventArgs());
- }
- }
- /// <summary>
- /// Handles session ErrorOccured event.
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="ExceptionEventArgs"/> instance containing the event data.</param>
- private void Session_ErrorOccured(object sender, ExceptionEventArgs e)
- {
- RaiseExceptionEvent(e.Exception);
- }
- }
- }
|