< Summary

Information
Class: Ice.Internal.TcpEndpointI
Assembly: Ice
File(s): /home/runner/work/ice/ice/csharp/src/Ice/Internal/TcpEndpointI.cs
Tag: 71_18251537082
Line coverage
84%
Covered lines: 73
Uncovered lines: 13
Coverable lines: 86
Total lines: 285
Line coverage: 84.8%
Branch coverage
73%
Covered branches: 34
Total branches: 46
Branch coverage: 73.9%
Method coverage
100%
Covered methods: 20
Total methods: 20
Method coverage: 100%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
.ctor(...)100%11100%
.ctor(...)100%11100%
streamWriteImpl(...)100%11100%
getInfo()100%11100%
timeout()100%11100%
timeout(...)50%2.15266.67%
compress()100%11100%
compress(...)100%22100%
datagram()100%11100%
transceiver()100%11100%
acceptor(...)100%11100%
endpoint(...)100%22100%
options()100%66100%
CompareTo(...)61.11%32.751864.29%
GetHashCode()100%11100%
toPublishedEndpoint(...)100%22100%
checkOption(...)71.43%22.41465%
createConnector(...)100%11100%
createEndpoint(...)100%11100%

File(s)

/home/runner/work/ice/ice/csharp/src/Ice/Internal/TcpEndpointI.cs

#LineLine coverage
 1// Copyright (c) ZeroC, Inc.
 2
 3using System.Diagnostics;
 4using System.Globalization;
 5using System.Net;
 6using System.Net.Security;
 7
 8namespace Ice.Internal;
 9
 10internal sealed class TcpEndpointI : IPEndpointI
 11{
 12    private const int defaultTimeout = 60_000; // 60,000 milliseconds (1 minute)
 13
 14    public TcpEndpointI(
 15        ProtocolInstance instance,
 16        string ho,
 17        int po,
 18        EndPoint sourceAddr,
 19        int ti,
 20        string conId,
 21        bool co)
 122        : base(instance, ho, po, sourceAddr, conId)
 23    {
 124        _timeout = ti;
 125        _compress = co;
 126    }
 27
 28    public TcpEndpointI(ProtocolInstance instance)
 129        : base(instance)
 30    {
 31        // This timeout is not used in Ice 3.8 or greater.
 132        _timeout = defaultTimeout;
 133        _compress = false;
 134    }
 35
 36    public TcpEndpointI(ProtocolInstance instance, Ice.InputStream s)
 137        : base(instance, s)
 38    {
 139        _timeout = s.readInt();
 140        _compress = s.readBool();
 141    }
 42
 43    public override void streamWriteImpl(Ice.OutputStream s)
 44    {
 145        base.streamWriteImpl(s);
 146        s.writeInt(_timeout);
 147        s.writeBool(_compress);
 148    }
 49
 50    public override EndpointInfo getInfo() =>
 151        new TCPEndpointInfo(
 152            _compress,
 153            host_,
 154            port_,
 155            Network.endpointAddressToString(sourceAddr_),
 156            type(),
 157            secure());
 58
 159    public override int timeout() => _timeout;
 60
 61    public override EndpointI timeout(int timeout)
 62    {
 163        if (timeout == _timeout)
 64        {
 065            return this;
 66        }
 67        else
 68        {
 169            return new TcpEndpointI(instance_, host_, port_, sourceAddr_, timeout, connectionId_, _compress);
 70        }
 71    }
 72
 173    public override bool compress() => _compress;
 74
 75    public override EndpointI compress(bool compress)
 76    {
 177        if (compress == _compress)
 78        {
 179            return this;
 80        }
 81        else
 82        {
 183            return new TcpEndpointI(instance_, host_, port_, sourceAddr_, _timeout, connectionId_, compress);
 84        }
 85    }
 86
 187    public override bool datagram() => false;
 88
 189    public override Transceiver transceiver() => null;
 90
 91    public override Acceptor acceptor(string adapterName, SslServerAuthenticationOptions serverAuthenticationOptions)
 92    {
 93        Debug.Assert(serverAuthenticationOptions is null);
 194        return new TcpAcceptor(this, instance_, host_, port_);
 95    }
 96
 97    public TcpEndpointI endpoint(TcpAcceptor acceptor)
 98    {
 199        int port = acceptor.effectivePort();
 1100        if (port == port_)
 101        {
 1102            return this;
 103        }
 104        else
 105        {
 1106            return new TcpEndpointI(instance_, host_, port, sourceAddr_, _timeout, connectionId_, _compress);
 107        }
 108    }
 109
 110    public override string options()
 111    {
 112        //
 113        // WARNING: Certain features, such as proxy validation in Glacier2,
 114        // depend on the format of proxy strings. Changes to toString() and
 115        // methods called to generate parts of the reference string could break
 116        // these features. Please review for all features that depend on the
 117        // format of proxyToString() before changing this and related code.
 118        //
 1119        string s = base.options();
 120
 1121        if (_timeout != defaultTimeout)
 122        {
 1123            if (_timeout == -1)
 124            {
 1125                s += " -t infinite";
 126            }
 127            else
 128            {
 1129                s += " -t " + _timeout;
 130            }
 131        }
 132
 1133        if (_compress)
 134        {
 1135            s += " -z";
 136        }
 137
 1138        return s;
 139    }
 140
 141    public override int CompareTo(EndpointI obj)
 142    {
 1143        if (!(obj is TcpEndpointI))
 144        {
 0145            return type() < obj.type() ? -1 : 1;
 146        }
 147
 1148        var p = (TcpEndpointI)obj;
 1149        if (this == p)
 150        {
 1151            return 0;
 152        }
 153
 1154        if (_timeout < p._timeout)
 155        {
 0156            return -1;
 157        }
 1158        else if (p._timeout < _timeout)
 159        {
 0160            return 1;
 161        }
 162
 1163        if (!_compress && p._compress)
 164        {
 0165            return -1;
 166        }
 1167        else if (!p._compress && _compress)
 168        {
 0169            return 1;
 170        }
 171
 1172        return base.CompareTo(p);
 173    }
 174
 1175    public override int GetHashCode() => HashCode.Combine(base.GetHashCode(), _timeout, _compress);
 176
 177    public override EndpointI toPublishedEndpoint(string publishedHost)
 178    {
 179        // A server endpoint can't have a source address or connection ID.
 180        Debug.Assert(sourceAddr_ is null && connectionId_.Length == 0);
 181
 1182        if (publishedHost.Length == 0)
 183        {
 1184            return this;
 185        }
 186        else
 187        {
 1188            return new TcpEndpointI(instance_, publishedHost, port_, sourceAddr: null, _timeout, conId: "", _compress);
 189        }
 190    }
 191
 192    protected override bool checkOption(string option, string argument, string endpoint)
 193    {
 1194        if (base.checkOption(option, argument, endpoint))
 195        {
 1196            return true;
 197        }
 198
 1199        switch (option[1])
 200        {
 201            case 't':
 202            {
 1203                if (argument == null)
 204                {
 0205                    throw new ParseException($"no argument provided for -t option in endpoint '{endpoint}'");
 206                }
 207
 1208                if (argument == "infinite")
 209                {
 0210                    _timeout = -1;
 211                }
 212                else
 213                {
 214                    try
 215                    {
 1216                        _timeout = int.Parse(argument, CultureInfo.InvariantCulture);
 1217                        if (_timeout < 1)
 218                        {
 0219                            throw new ParseException($"invalid timeout value '{argument}' in endpoint '{endpoint}'");
 220                        }
 1221                    }
 0222                    catch (System.FormatException ex)
 223                    {
 0224                        throw new ParseException($"invalid timeout value '{argument}' in endpoint '{endpoint}'", ex);
 225                    }
 226                }
 227
 1228                return true;
 229            }
 230
 231            case 'z':
 232            {
 1233                if (argument != null)
 234                {
 0235                    throw new ParseException(
 0236                        $"unexpected argument '{argument}' provided for -z option in endpoint '{endpoint}'");
 237                }
 238
 1239                _compress = true;
 240
 1241                return true;
 242            }
 243
 244            default:
 245            {
 1246                return false;
 247            }
 248        }
 249    }
 250
 251    protected override Connector createConnector(EndPoint addr, NetworkProxy proxy) =>
 1252        new TcpConnector(instance_, addr, proxy, sourceAddr_, _timeout, connectionId_);
 253
 254    protected override IPEndpointI createEndpoint(string host, int port, string connectionId) =>
 1255        new TcpEndpointI(instance_, host, port, sourceAddr_, _timeout, connectionId, _compress);
 256
 257    private int _timeout;
 258    private bool _compress;
 259}
 260
 261internal sealed class TcpEndpointFactory : EndpointFactory
 262{
 263    internal TcpEndpointFactory(ProtocolInstance instance) => _instance = instance;
 264
 265    public void initialize()
 266    {
 267    }
 268
 269    public short type() => _instance.type();
 270
 271    public string protocol() => _instance.protocol();
 272
 273    public EndpointI create(List<string> args, bool oaEndpoint)
 274    {
 275        IPEndpointI endpt = new TcpEndpointI(_instance);
 276        endpt.initWithOptions(args, oaEndpoint);
 277        return endpt;
 278    }
 279
 280    public EndpointI read(Ice.InputStream s) => new TcpEndpointI(_instance, s);
 281
 282    public EndpointFactory clone(ProtocolInstance instance) => new TcpEndpointFactory(instance);
 283
 284    private readonly ProtocolInstance _instance;
 285}