< Summary

Information
Class: Renci.SshNet.Connection.ProxyConnector
Assembly: Renci.SshNet
File(s): \home\appveyor\projects\ssh-net\src\Renci.SshNet\Connection\ProxyConnector.cs
Line coverage
47%
Covered lines: 16
Uncovered lines: 18
Coverable lines: 34
Total lines: 97
Line coverage: 47%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor(...)100%1100%
HandleProxyConnectAsync()100%10%
HandleProxyConnectAsync(...)100%10%
Connect(...)100%1100%
ConnectAsync()100%118.18%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Net.Sockets;
 3using System.Threading;
 4using System.Threading.Tasks;
 5
 6namespace Renci.SshNet.Connection
 7{
 8    /// <summary>
 9    /// Represents a connector that uses a proxy server to establish a connection to a given SSH
 10    /// endpoint.
 11    /// </summary>
 12    internal abstract class ProxyConnector : ConnectorBase
 13    {
 14        protected ProxyConnector(ISocketFactory socketFactory)
 42915            : base(socketFactory)
 42916        {
 42917        }
 18
 19        protected abstract void HandleProxyConnect(IConnectionInfo connectionInfo, Socket socket);
 20
 21        // ToDo: Performs async/sync fallback, true async version should be implemented in derived classes
 22        protected virtual
 23#if NET || NETSTANDARD2_1_OR_GREATER
 24        async
 25#endif // NET || NETSTANDARD2_1_OR_GREATER
 26        Task HandleProxyConnectAsync(IConnectionInfo connectionInfo, Socket socket, CancellationToken cancellationToken)
 027        {
 028            cancellationToken.ThrowIfCancellationRequested();
 29
 30#if NET || NETSTANDARD2_1_OR_GREATER
 031            await using (cancellationToken.Register(o => ((Socket)o).Dispose(), socket, useSynchronizationContext: false
 32#else
 033            using (cancellationToken.Register(o => ((Socket) o).Dispose(), socket, useSynchronizationContext: false))
 34#endif // NET || NETSTANDARD2_1_OR_GREATER
 035            {
 36#pragma warning disable MA0042 // Do not use blocking calls in an async method; false positive caused by https://github.
 037                HandleProxyConnect(connectionInfo, socket);
 38#pragma warning restore MA0042 // Do not use blocking calls in an async method
 039            }
 40
 41#if !NET && !NETSTANDARD2_1_OR_GREATER
 042            return Task.CompletedTask;
 43#endif // !NET && !NETSTANDARD2_1_OR_GREATER
 044        }
 45
 46        /// <summary>
 47        /// Connects to a SSH endpoint using the specified <see cref="IConnectionInfo"/>.
 48        /// </summary>
 49        /// <param name="connectionInfo">The <see cref="IConnectionInfo"/> to use to establish a connection to a SSH end
 50        /// <returns>
 51        /// A <see cref="Socket"/> connected to the SSH endpoint represented by the specified <see cref="IConnectionInfo
 52        /// </returns>
 53        public override Socket Connect(IConnectionInfo connectionInfo)
 41754        {
 41755            var socket = SocketConnect(connectionInfo.ProxyHost, connectionInfo.ProxyPort, connectionInfo.Timeout);
 56
 57            try
 34858            {
 34859                HandleProxyConnect(connectionInfo, socket);
 16560                return socket;
 61            }
 18362            catch (Exception)
 18363            {
 18364                socket.Shutdown(SocketShutdown.Both);
 18365                socket.Dispose();
 66
 18367                throw;
 68            }
 16569        }
 70
 71        /// <summary>
 72        /// Asynchronously connects to a SSH endpoint using the specified <see cref="IConnectionInfo"/>.
 73        /// </summary>
 74        /// <param name="connectionInfo">The <see cref="IConnectionInfo"/> to use to establish a connection to a SSH end
 75        /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
 76        /// <returns>
 77        /// A <see cref="Socket"/> connected to the SSH endpoint represented by the specified <see cref="IConnectionInfo
 78        /// </returns>
 79        public override async Task<Socket> ConnectAsync(IConnectionInfo connectionInfo, CancellationToken cancellationTo
 380        {
 381            var socket = await SocketConnectAsync(connectionInfo.ProxyHost, connectionInfo.ProxyPort, cancellationToken)
 82
 83            try
 084            {
 085                await HandleProxyConnectAsync(connectionInfo, socket, cancellationToken).ConfigureAwait(false);
 086                return socket;
 87            }
 088            catch (Exception)
 089            {
 090                socket.Shutdown(SocketShutdown.Both);
 091                socket.Dispose();
 92
 093                throw;
 94            }
 095        }
 96    }
 97}