< Summary

Information
Class: IceRpc.Transports.Tcp.TcpClientTransport
Assembly: IceRpc
File(s): /home/runner/work/icerpc-csharp/icerpc-csharp/src/IceRpc/Transports/Tcp/TcpClientTransport.cs
Tag: 275_13775359185
Line coverage
100%
Covered lines: 54
Uncovered lines: 0
Coverable lines: 54
Total lines: 114
Line coverage: 100%
Branch coverage
97%
Covered branches: 35
Total branches: 36
Branch coverage: 97.2%
Method coverage
100%
Covered methods: 6
Total methods: 6
Method coverage: 100%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Name()100%11100%
.ctor()100%11100%
.ctor(...)100%11100%
CreateConnection(...)95.45%2222100%
CheckParams()100%88100%
IsValidTransportName()100%66100%

File(s)

/home/runner/work/icerpc-csharp/icerpc-csharp/src/IceRpc/Transports/Tcp/TcpClientTransport.cs

#LineLine coverage
 1// Copyright (c) ZeroC, Inc.
 2
 3using IceRpc.Transports.Tcp.Internal;
 4using System.Net.Security;
 5
 6namespace IceRpc.Transports.Tcp;
 7
 8/// <summary>Implements <see cref="IDuplexClientTransport" /> for the tcp transport.</summary>
 9public class TcpClientTransport : IDuplexClientTransport
 10{
 11    /// <inheritdoc/>
 3912    public string Name => TcpName;
 13
 14    private const string SslName = "ssl";
 15    private const string TcpName = "tcp";
 16
 17    private readonly TcpClientTransportOptions _options;
 18
 19    /// <summary>Constructs a <see cref="TcpClientTransport" />.</summary>
 20    public TcpClientTransport()
 4721        : this(new TcpClientTransportOptions())
 4722    {
 4723    }
 24
 25    /// <summary>Constructs a <see cref="TcpClientTransport" />.</summary>
 26    /// <param name="options">The transport options.</param>
 46627    public TcpClientTransport(TcpClientTransportOptions options) => _options = options;
 28
 29    /// <inheritdoc/>
 30    public IDuplexConnection CreateConnection(
 31        ServerAddress serverAddress,
 32        DuplexConnectionOptions options,
 33        SslClientAuthenticationOptions? clientAuthenticationOptions)
 28034    {
 28035        if (serverAddress.Transport is string transport && !IsValidTransportName(transport, serverAddress.Protocol))
 636        {
 637            throw new NotSupportedException(
 638                $"The Tcp client transport does not support server addresses with transport '{transport}'.");
 39        }
 40
 27441        if (!CheckParams(serverAddress))
 1042        {
 1043            throw new ArgumentException(
 1044                $"The server address '{serverAddress}' contains parameters that are not valid for the Tcp client transpo
 1045                nameof(serverAddress));
 46        }
 47
 26448        if (serverAddress.Transport is null)
 1449        {
 1450            serverAddress = serverAddress with { Transport = Name };
 1451        }
 52
 26453        SslClientAuthenticationOptions? authenticationOptions = clientAuthenticationOptions?.Clone() ??
 26454            (serverAddress.Transport == SslName ? new SslClientAuthenticationOptions() : null);
 55
 26456        if (authenticationOptions is not null)
 6257        {
 58            // We are establishing a secure TLS connection. It can rely on system certificates.
 59
 6260            authenticationOptions.TargetHost ??= serverAddress.Host;
 61
 62            // Set ApplicationProtocols to "ice" or "icerpc" in the common situation where the application does not
 63            // specify any application protocol. This way, a proxy server listening on a port shared by multiple
 64            // application protocols can use this ALPN protocol ID to forward all ice/icerpc traffic to an ice/icerpc
 65            // back-end server.
 66            // We do this only when the port is not the default port for ice or icerpc; when we use the IANA-registered
 67            // default port, the server can and should use this port number to identify the application protocol when no
 68            // ALPN protocol ID is provided.
 6269            if (authenticationOptions.ApplicationProtocols is null &&
 6270                serverAddress.Port != serverAddress.Protocol.DefaultPort)
 6271            {
 6272                authenticationOptions.ApplicationProtocols = new List<SslApplicationProtocol>
 6273                {
 6274                    new SslApplicationProtocol(serverAddress.Protocol.Name)
 6275                };
 6276            }
 6277        }
 78
 26479        return new TcpClientConnection(
 26480            serverAddress,
 26481            authenticationOptions,
 26482            options.Pool,
 26483            options.MinSegmentSize,
 26484            _options);
 85
 86        static bool CheckParams(ServerAddress serverAddress)
 27487        {
 27488            if (serverAddress.Protocol == Protocol.Ice)
 2689            {
 9690                foreach (string name in serverAddress.Params.Keys)
 1091                {
 1092                    switch (name)
 93                    {
 94                        case "t":
 95                        case "z":
 96                            // we don't check the value since we ignore it
 897                            break;
 98
 99                        default:
 2100                            return false;
 101                    }
 8102                }
 24103                return true;
 104            }
 105            else
 248106            {
 248107                return serverAddress.Params.Count == 0;
 108            }
 274109        }
 110
 111        static bool IsValidTransportName(string transportName, Protocol protocol) =>
 260112            protocol == Protocol.Ice ? transportName is TcpName or SslName : transportName is TcpName;
 264113    }
 114}