< Summary

Information
Class: Ice.Internal.TcpEndpointFactory
Assembly: Ice
File(s): /home/runner/work/ice/ice/csharp/src/Ice/Internal/TcpEndpointI.cs
Tag: 71_18251537082
Line coverage
100%
Covered lines: 9
Uncovered lines: 0
Coverable lines: 9
Total lines: 285
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage
100%
Covered methods: 7
Total methods: 7
Method coverage: 100%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
initialize()100%11100%
type()100%11100%
protocol()100%11100%
create(...)100%11100%
read(...)100%11100%
clone(...)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)
 22        : base(instance, ho, po, sourceAddr, conId)
 23    {
 24        _timeout = ti;
 25        _compress = co;
 26    }
 27
 28    public TcpEndpointI(ProtocolInstance instance)
 29        : base(instance)
 30    {
 31        // This timeout is not used in Ice 3.8 or greater.
 32        _timeout = defaultTimeout;
 33        _compress = false;
 34    }
 35
 36    public TcpEndpointI(ProtocolInstance instance, Ice.InputStream s)
 37        : base(instance, s)
 38    {
 39        _timeout = s.readInt();
 40        _compress = s.readBool();
 41    }
 42
 43    public override void streamWriteImpl(Ice.OutputStream s)
 44    {
 45        base.streamWriteImpl(s);
 46        s.writeInt(_timeout);
 47        s.writeBool(_compress);
 48    }
 49
 50    public override EndpointInfo getInfo() =>
 51        new TCPEndpointInfo(
 52            _compress,
 53            host_,
 54            port_,
 55            Network.endpointAddressToString(sourceAddr_),
 56            type(),
 57            secure());
 58
 59    public override int timeout() => _timeout;
 60
 61    public override EndpointI timeout(int timeout)
 62    {
 63        if (timeout == _timeout)
 64        {
 65            return this;
 66        }
 67        else
 68        {
 69            return new TcpEndpointI(instance_, host_, port_, sourceAddr_, timeout, connectionId_, _compress);
 70        }
 71    }
 72
 73    public override bool compress() => _compress;
 74
 75    public override EndpointI compress(bool compress)
 76    {
 77        if (compress == _compress)
 78        {
 79            return this;
 80        }
 81        else
 82        {
 83            return new TcpEndpointI(instance_, host_, port_, sourceAddr_, _timeout, connectionId_, compress);
 84        }
 85    }
 86
 87    public override bool datagram() => false;
 88
 89    public override Transceiver transceiver() => null;
 90
 91    public override Acceptor acceptor(string adapterName, SslServerAuthenticationOptions serverAuthenticationOptions)
 92    {
 93        Debug.Assert(serverAuthenticationOptions is null);
 94        return new TcpAcceptor(this, instance_, host_, port_);
 95    }
 96
 97    public TcpEndpointI endpoint(TcpAcceptor acceptor)
 98    {
 99        int port = acceptor.effectivePort();
 100        if (port == port_)
 101        {
 102            return this;
 103        }
 104        else
 105        {
 106            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        //
 119        string s = base.options();
 120
 121        if (_timeout != defaultTimeout)
 122        {
 123            if (_timeout == -1)
 124            {
 125                s += " -t infinite";
 126            }
 127            else
 128            {
 129                s += " -t " + _timeout;
 130            }
 131        }
 132
 133        if (_compress)
 134        {
 135            s += " -z";
 136        }
 137
 138        return s;
 139    }
 140
 141    public override int CompareTo(EndpointI obj)
 142    {
 143        if (!(obj is TcpEndpointI))
 144        {
 145            return type() < obj.type() ? -1 : 1;
 146        }
 147
 148        var p = (TcpEndpointI)obj;
 149        if (this == p)
 150        {
 151            return 0;
 152        }
 153
 154        if (_timeout < p._timeout)
 155        {
 156            return -1;
 157        }
 158        else if (p._timeout < _timeout)
 159        {
 160            return 1;
 161        }
 162
 163        if (!_compress && p._compress)
 164        {
 165            return -1;
 166        }
 167        else if (!p._compress && _compress)
 168        {
 169            return 1;
 170        }
 171
 172        return base.CompareTo(p);
 173    }
 174
 175    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
 182        if (publishedHost.Length == 0)
 183        {
 184            return this;
 185        }
 186        else
 187        {
 188            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    {
 194        if (base.checkOption(option, argument, endpoint))
 195        {
 196            return true;
 197        }
 198
 199        switch (option[1])
 200        {
 201            case 't':
 202            {
 203                if (argument == null)
 204                {
 205                    throw new ParseException($"no argument provided for -t option in endpoint '{endpoint}'");
 206                }
 207
 208                if (argument == "infinite")
 209                {
 210                    _timeout = -1;
 211                }
 212                else
 213                {
 214                    try
 215                    {
 216                        _timeout = int.Parse(argument, CultureInfo.InvariantCulture);
 217                        if (_timeout < 1)
 218                        {
 219                            throw new ParseException($"invalid timeout value '{argument}' in endpoint '{endpoint}'");
 220                        }
 221                    }
 222                    catch (System.FormatException ex)
 223                    {
 224                        throw new ParseException($"invalid timeout value '{argument}' in endpoint '{endpoint}'", ex);
 225                    }
 226                }
 227
 228                return true;
 229            }
 230
 231            case 'z':
 232            {
 233                if (argument != null)
 234                {
 235                    throw new ParseException(
 236                        $"unexpected argument '{argument}' provided for -z option in endpoint '{endpoint}'");
 237                }
 238
 239                _compress = true;
 240
 241                return true;
 242            }
 243
 244            default:
 245            {
 246                return false;
 247            }
 248        }
 249    }
 250
 251    protected override Connector createConnector(EndPoint addr, NetworkProxy proxy) =>
 252        new TcpConnector(instance_, addr, proxy, sourceAddr_, _timeout, connectionId_);
 253
 254    protected override IPEndpointI createEndpoint(string host, int port, string connectionId) =>
 255        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{
 1263    internal TcpEndpointFactory(ProtocolInstance instance) => _instance = instance;
 264
 265    public void initialize()
 266    {
 1267    }
 268
 1269    public short type() => _instance.type();
 270
 1271    public string protocol() => _instance.protocol();
 272
 273    public EndpointI create(List<string> args, bool oaEndpoint)
 274    {
 1275        IPEndpointI endpt = new TcpEndpointI(_instance);
 1276        endpt.initWithOptions(args, oaEndpoint);
 1277        return endpt;
 278    }
 279
 1280    public EndpointI read(Ice.InputStream s) => new TcpEndpointI(_instance, s);
 281
 1282    public EndpointFactory clone(ProtocolInstance instance) => new TcpEndpointFactory(instance);
 283
 284    private readonly ProtocolInstance _instance;
 285}