< Summary

Information
Class: Renci.SshNet.ForwardedPort
Assembly: Renci.SshNet
File(s): \home\appveyor\projects\ssh-net\src\Renci.SshNet\ForwardedPort.cs
Line coverage
100%
Covered lines: 56
Uncovered lines: 0
Coverable lines: 56
Total lines: 174
Line coverage: 100%
Branch coverage
90%
Covered branches: 18
Total branches: 20
Branch coverage: 90%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
get_Session()100%1100%
Start()100%6100%
Stop()100%2100%
Dispose()100%1100%
StopPort(...)100%2100%
Dispose(...)100%4100%
RaiseExceptionEvent(...)50%2100%
RaiseRequestReceived(...)50%2100%
RaiseClosing()100%2100%
Session_ErrorOccured(...)100%1100%

File(s)

\home\appveyor\projects\ssh-net\src\Renci.SshNet\ForwardedPort.cs

#LineLine coverage
 1using System;
 2
 3using Renci.SshNet.Common;
 4
 5namespace Renci.SshNet
 6{
 7    /// <summary>
 8    /// Base class for port forwarding functionality.
 9    /// </summary>
 10    public abstract class ForwardedPort : IForwardedPort
 11    {
 12        /// <summary>
 13        /// Gets or sets the session.
 14        /// </summary>
 15        /// <value>
 16        /// The session.
 17        /// </value>
 806718        internal ISession Session { get; set; }
 19
 20        /// <summary>
 21        /// Gets a value indicating whether port forwarding is started.
 22        /// </summary>
 23        /// <value>
 24        /// <see langword="true"/> if port forwarding is started; otherwise, <see langword="false"/>.
 25        /// </value>
 26        public abstract bool IsStarted { get; }
 27
 28        /// <summary>
 29        /// The <see cref="Closing"/> event occurs as the forwarded port is being stopped.
 30        /// </summary>
 31        public event EventHandler Closing;
 32
 33        /// <summary>
 34        /// Occurs when an exception is thrown.
 35        /// </summary>
 36        public event EventHandler<ExceptionEventArgs> Exception;
 37
 38        /// <summary>
 39        /// Occurs when a port forwarding request is received.
 40        /// </summary>
 41        public event EventHandler<PortForwardEventArgs> RequestReceived;
 42
 43        /// <summary>
 44        /// Starts port forwarding.
 45        /// </summary>
 46        /// <exception cref="InvalidOperationException">The current <see cref="ForwardedPort"/> is already started -or- 
 47        /// <exception cref="SshConnectionException">The client is not connected.</exception>
 48        public virtual void Start()
 68749        {
 68750            CheckDisposed();
 51
 66052            if (IsStarted)
 4553            {
 4554                throw new InvalidOperationException("Forwarded port is already started.");
 55            }
 56
 61557            if (Session is null)
 4558            {
 4559                throw new InvalidOperationException("Forwarded port is not added to a client.");
 60            }
 61
 57062            if (!Session.IsConnected)
 4563            {
 4564                throw new SshConnectionException("Client not connected.");
 65            }
 66
 52567            Session.ErrorOccured += Session_ErrorOccured;
 52568            StartPort();
 52569        }
 70
 71        /// <summary>
 72        /// Stops port forwarding.
 73        /// </summary>
 74#pragma warning disable CA1716 // Identifiers should not match keywords
 75        public virtual void Stop()
 76#pragma warning restore CA1716 // Identifiers should not match keywords
 35977        {
 35978            if (IsStarted)
 25279            {
 25280                StopPort(Session.ConnectionInfo.Timeout);
 25281            }
 35982        }
 83
 84        /// <summary>
 85        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
 86        /// </summary>
 87        public void Dispose()
 98088        {
 98089            Dispose(disposing: true);
 98090            GC.SuppressFinalize(this);
 98091        }
 92
 93        /// <summary>
 94        /// Starts port forwarding.
 95        /// </summary>
 96        protected abstract void StartPort();
 97
 98        /// <summary>
 99        /// Stops port forwarding, and waits for the specified timeout until all pending
 100        /// requests are processed.
 101        /// </summary>
 102        /// <param name="timeout">The maximum amount of time to wait for pending requests to finish processing.</param>
 103        protected virtual void StopPort(TimeSpan timeout)
 525104        {
 525105            RaiseClosing();
 106
 525107            var session = Session;
 525108            if (session is not null)
 525109            {
 525110                session.ErrorOccured -= Session_ErrorOccured;
 525111            }
 525112        }
 113
 114        /// <summary>
 115        /// Releases unmanaged and - optionally - managed resources.
 116        /// </summary>
 117        /// <param name="disposing"><see langowrd="true"/> to release both managed and unmanaged resources; <see langowr
 118        protected virtual void Dispose(bool disposing)
 744119        {
 744120            if (disposing)
 684121            {
 684122                var session = Session;
 684123                if (session is not null)
 550124                {
 550125                    StopPort(session.ConnectionInfo.Timeout);
 550126                    Session = null;
 550127                }
 684128            }
 744129        }
 130
 131        /// <summary>
 132        /// Ensures the current instance is not disposed.
 133        /// </summary>
 134        /// <exception cref="ObjectDisposedException">The current instance is disposed.</exception>
 135        protected abstract void CheckDisposed();
 136
 137        /// <summary>
 138        /// Raises <see cref="Exception"/> event.
 139        /// </summary>
 140        /// <param name="exception">The exception.</param>
 141        protected void RaiseExceptionEvent(Exception exception)
 49142        {
 49143            Exception?.Invoke(this, new ExceptionEventArgs(exception));
 49144        }
 145
 146        /// <summary>
 147        /// Raises <see cref="RequestReceived"/> event.
 148        /// </summary>
 149        /// <param name="host">Request originator host.</param>
 150        /// <param name="port">Request originator port.</param>
 151        protected void RaiseRequestReceived(string host, uint port)
 207152        {
 207153            RequestReceived?.Invoke(this, new PortForwardEventArgs(host, port));
 207154        }
 155
 156        /// <summary>
 157        /// Raises the <see cref="IForwardedPort.Closing"/> event.
 158        /// </summary>
 159        private void RaiseClosing()
 525160        {
 525161            Closing?.Invoke(this, EventArgs.Empty);
 525162        }
 163
 164        /// <summary>
 165        /// Handles session ErrorOccured event.
 166        /// </summary>
 167        /// <param name="sender">The source of the event.</param>
 168        /// <param name="e">The <see cref="ExceptionEventArgs"/> instance containing the event data.</param>
 169        private void Session_ErrorOccured(object sender, ExceptionEventArgs e)
 27170        {
 27171            RaiseExceptionEvent(e.Exception);
 27172        }
 173    }
 174}