< Summary

Information
Class: Renci.SshNet.Connection.ConnectorBase
Assembly: Renci.SshNet
File(s): \home\appveyor\projects\ssh-net\src\Renci.SshNet\Connection\ConnectorBase.cs
Line coverage
85%
Covered lines: 51
Uncovered lines: 9
Coverable lines: 60
Total lines: 155
Line coverage: 85%
Branch coverage
50%
Covered branches: 2
Total branches: 4
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor(...)50%271.42%
get_SocketFactory()100%1100%
SocketConnect(...)100%1100%
SocketConnectAsync()100%175%
SocketReadByte(...)100%1100%
SocketReadByte(...)100%1100%
SocketRead(...)100%1100%
SocketRead(...)50%262.5%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Net;
 3using System.Net.Sockets;
 4using System.Threading;
 5using System.Threading.Tasks;
 6
 7using Renci.SshNet.Abstractions;
 8using Renci.SshNet.Common;
 9using Renci.SshNet.Messages.Transport;
 10
 11namespace Renci.SshNet.Connection
 12{
 13    internal abstract class ConnectorBase : IConnector
 14    {
 229415        protected ConnectorBase(ISocketFactory socketFactory)
 229416        {
 229417            if (socketFactory is null)
 018            {
 019                throw new ArgumentNullException(nameof(socketFactory));
 20            }
 21
 229422            SocketFactory = socketFactory;
 229423        }
 24
 457025        internal ISocketFactory SocketFactory { get; private set; }
 26
 27        public abstract Socket Connect(IConnectionInfo connectionInfo);
 28
 29        public abstract Task<Socket> ConnectAsync(IConnectionInfo connectionInfo, CancellationToken cancellationToken);
 30
 31        /// <summary>
 32        /// Establishes a socket connection to the specified host and port.
 33        /// </summary>
 34        /// <param name="host">The host name of the server to connect to.</param>
 35        /// <param name="port">The port to connect to.</param>
 36        /// <param name="timeout">The maximum time to wait for the connection to be established.</param>
 37        /// <exception cref="SshOperationTimeoutException">The connection failed to establish within the configured <see
 38        /// <exception cref="SocketException">An error occurred trying to establish the connection.</exception>
 39        protected Socket SocketConnect(string host, int port, TimeSpan timeout)
 227440        {
 227441            var ipAddress = DnsAbstraction.GetHostAddresses(host)[0];
 226242            var ep = new IPEndPoint(ipAddress, port);
 43
 226244            DiagnosticAbstraction.Log(string.Format("Initiating connection to '{0}:{1}'.", host, port));
 45
 226246            var socket = SocketFactory.Create(ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
 47
 48            try
 226249            {
 226250                SocketAbstraction.Connect(socket, ep, timeout);
 51
 52                const int socketBufferSize = 10 * Session.MaximumSshPacketSize;
 217853                socket.SendBufferSize = socketBufferSize;
 217854                socket.ReceiveBufferSize = socketBufferSize;
 217855                return socket;
 56            }
 8457            catch (Exception)
 8458            {
 8459                socket.Dispose();
 8460                throw;
 61            }
 217862        }
 63
 64        /// <summary>
 65        /// Establishes a socket connection to the specified host and port.
 66        /// </summary>
 67        /// <param name="host">The host name of the server to connect to.</param>
 68        /// <param name="port">The port to connect to.</param>
 69        /// <param name="cancellationToken">The cancellation token to observe.</param>
 70        /// <exception cref="SshOperationTimeoutException">The connection failed to establish within the configured <see
 71        /// <exception cref="SocketException">An error occurred trying to establish the connection.</exception>
 72        protected async Task<Socket> SocketConnectAsync(string host, int port, CancellationToken cancellationToken)
 873        {
 874            cancellationToken.ThrowIfCancellationRequested();
 75
 876            var ipAddress = (await DnsAbstraction.GetHostAddressesAsync(host).ConfigureAwait(false))[0];
 277            var ep = new IPEndPoint(ipAddress, port);
 78
 279            DiagnosticAbstraction.Log(string.Format("Initiating connection to '{0}:{1}'.", host, port));
 80
 281            var socket = SocketFactory.Create(ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
 82            try
 283            {
 284                await SocketAbstraction.ConnectAsync(socket, ep, cancellationToken).ConfigureAwait(false);
 85
 86                const int socketBufferSize = 2 * Session.MaximumSshPacketSize;
 287                socket.SendBufferSize = socketBufferSize;
 288                socket.ReceiveBufferSize = socketBufferSize;
 289                return socket;
 90            }
 091            catch (Exception)
 092            {
 093                socket.Dispose();
 094                throw;
 95            }
 296        }
 97
 98        protected static byte SocketReadByte(Socket socket)
 28299        {
 282100            var buffer = new byte[1];
 282101            _ = SocketRead(socket, buffer, 0, 1, Session.InfiniteTimeSpan);
 282102            return buffer[0];
 282103        }
 104
 105        protected static byte SocketReadByte(Socket socket, TimeSpan readTimeout)
 129106        {
 129107            var buffer = new byte[1];
 129108            _ = SocketRead(socket, buffer, 0, 1, readTimeout);
 99109            return buffer[0];
 99110        }
 111
 112        /// <summary>
 113        /// Performs a blocking read on the socket until <paramref name="length"/> bytes are received.
 114        /// </summary>
 115        /// <param name="socket">The <see cref="Socket"/> to read from.</param>
 116        /// <param name="buffer">An array of type <see cref="byte"/> that is the storage location for the received data.
 117        /// <param name="offset">The position in <paramref name="buffer"/> parameter to store the received data.</param>
 118        /// <param name="length">The number of bytes to read.</param>
 119        /// <returns>
 120        /// The number of bytes read.
 121        /// </returns>
 122        /// <exception cref="SshConnectionException">The socket is closed.</exception>
 123        /// <exception cref="SocketException">The read failed.</exception>
 124        protected static int SocketRead(Socket socket, byte[] buffer, int offset, int length)
 60125        {
 60126            return SocketRead(socket, buffer, offset, length, Session.InfiniteTimeSpan);
 60127        }
 128
 129        /// <summary>
 130        /// Performs a blocking read on the socket until <paramref name="length"/> bytes are received.
 131        /// </summary>
 132        /// <param name="socket">The <see cref="Socket"/> to read from.</param>
 133        /// <param name="buffer">An array of type <see cref="byte"/> that is the storage location for the received data.
 134        /// <param name="offset">The position in <paramref name="buffer"/> parameter to store the received data.</param>
 135        /// <param name="length">The number of bytes to read.</param>
 136        /// <param name="readTimeout">The maximum time to wait until <paramref name="length"/> bytes have been received.
 137        /// <returns>
 138        /// The number of bytes read.
 139        /// </returns>
 140        /// <exception cref="SshConnectionException">The socket is closed.</exception>
 141        /// <exception cref="SshOperationTimeoutException">The read has timed-out.</exception>
 142        /// <exception cref="SocketException">The read failed.</exception>
 143        protected static int SocketRead(Socket socket, byte[] buffer, int offset, int length, TimeSpan readTimeout)
 534144        {
 534145            var bytesRead = SocketAbstraction.Read(socket, buffer, offset, length, readTimeout);
 471146            if (bytesRead == 0)
 0147            {
 0148                throw new SshConnectionException("An established connection was aborted by the server.",
 0149                                                 DisconnectReason.ConnectionLost);
 150            }
 151
 471152            return bytesRead;
 471153        }
 154    }
 155}