< Summary

Information
Class: Ice.Internal.UdpEndpointI
Assembly: Ice
File(s): /home/runner/work/ice/ice/csharp/src/Ice/Internal/UdpEndpointI.cs
Tag: 71_18251537082
Line coverage
76%
Covered lines: 111
Uncovered lines: 35
Coverable lines: 146
Total lines: 391
Line coverage: 76%
Branch coverage
58%
Covered branches: 41
Total branches: 70
Branch coverage: 58.5%
Method coverage
90%
Covered methods: 19
Total methods: 21
Method coverage: 90.4%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
.ctor(...)100%11100%
.ctor(...)50%2.26260%
getInfo()100%11100%
timeout()100%210%
timeout(...)100%11100%
compress()100%11100%
compress(...)100%22100%
datagram()100%11100%
transceiver()100%11100%
acceptor(...)100%210%
initWithOptions(...)25%6450%
endpoint(...)100%22100%
options()90%10.041092.86%
CompareTo(...)60%37.582064.71%
streamWriteImpl(...)50%2.15266.67%
GetHashCode()100%11100%
toPublishedEndpoint(...)100%22100%
checkOption(...)42.31%149.032643.33%
createConnector(...)100%11100%
createEndpoint(...)100%11100%

File(s)

/home/runner/work/ice/ice/csharp/src/Ice/Internal/UdpEndpointI.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 UdpEndpointI : IPEndpointI
 11{
 12    public UdpEndpointI(
 13        ProtocolInstance instance,
 14        string host,
 15        int port,
 16        EndPoint sourceAddr,
 17        string mcastInterface,
 18        int mttl,
 19        string connectionId,
 20        bool compress)
 121        : base(instance, host, port, sourceAddr, connectionId)
 22    {
 123        _mcastInterface = mcastInterface;
 124        _mcastTtl = mttl;
 125        _compress = compress;
 126    }
 27
 28    public UdpEndpointI(ProtocolInstance instance)
 129        : base(instance)
 30    {
 131    }
 32
 33    public UdpEndpointI(ProtocolInstance instance, Ice.InputStream s)
 134        : base(instance, s)
 35    {
 136        if (s.getEncoding().Equals(Ice.Util.Encoding_1_0))
 37        {
 038            s.readByte();
 039            s.readByte();
 040            s.readByte();
 041            s.readByte();
 42        }
 143        _compress = s.readBool();
 144    }
 45
 46    //
 47    // Return the endpoint information.
 48    //
 49    public override EndpointInfo getInfo()
 50    {
 51        Debug.Assert(!secure());
 52        Debug.Assert(type() == UDPEndpointType.value);
 53
 154        return new UDPEndpointInfo(
 155            _compress,
 156            host_,
 157            port_,
 158            Network.endpointAddressToString(sourceAddr_),
 159            _mcastInterface,
 160            _mcastTtl);
 61    }
 62
 63    //
 64    // Return the timeout for the endpoint in milliseconds. 0 means
 65    // non-blocking, -1 means no timeout.
 66    //
 067    public override int timeout() => -1;
 68
 69    //
 70    // Return a new endpoint with a different timeout value, provided
 71    // that timeouts are supported by the endpoint. Otherwise the same
 72    // endpoint is returned.
 73    //
 174    public override EndpointI timeout(int timeout) => this;
 75
 76    //
 77    // Return true if the endpoints support bzip2 compress, or false
 78    // otherwise.
 79    //
 180    public override bool compress() => _compress;
 81
 82    //
 83    // Return a new endpoint with a different compression value,
 84    // provided that compression is supported by the
 85    // endpoint. Otherwise the same endpoint is returned.
 86    //
 87    public override EndpointI compress(bool compress)
 88    {
 189        if (compress == _compress)
 90        {
 191            return this;
 92        }
 93        else
 94        {
 195            return new UdpEndpointI(
 196                instance_,
 197                host_,
 198                port_,
 199                sourceAddr_,
 1100                _mcastInterface,
 1101                _mcastTtl,
 1102                connectionId_,
 1103                compress);
 104        }
 105    }
 106
 107    //
 108    // Return true if the endpoint is datagram-based.
 109    //
 1110    public override bool datagram() => true;
 111
 112    //
 113    // Return a server side transceiver for this endpoint, or null if a
 114    // transceiver can only be created by an acceptor.
 115    //
 1116    public override Transceiver transceiver() => new UdpTransceiver(this, instance_, host_, port_, _mcastInterface);
 117
 118    //
 119    // Return an acceptor for this endpoint, or null if no acceptors
 120    // is available.
 121    //
 122    public override Acceptor acceptor(string adapterName, SslServerAuthenticationOptions serverAuthenticationOptions)
 123    {
 124        Debug.Assert(serverAuthenticationOptions is null);
 0125        return null;
 126    }
 127
 128    public override void initWithOptions(List<string> args, bool oaEndpoint)
 129    {
 1130        base.initWithOptions(args, oaEndpoint);
 131
 1132        if (_mcastInterface == "*")
 133        {
 0134            if (oaEndpoint)
 135            {
 0136                _mcastInterface = "";
 137            }
 138            else
 139            {
 0140                throw new ParseException($"'--interface *' not valid for proxy endpoint '{this}'");
 141            }
 142        }
 1143    }
 144
 145    public UdpEndpointI endpoint(UdpTransceiver transceiver)
 146    {
 1147        int port = transceiver.effectivePort();
 1148        if (port == port_)
 149        {
 1150            return this;
 151        }
 152        else
 153        {
 1154            return new UdpEndpointI(
 1155                instance_,
 1156                host_,
 1157                port,
 1158                sourceAddr_,
 1159                _mcastInterface,
 1160                _mcastTtl,
 1161                connectionId_,
 1162                _compress);
 163        }
 164    }
 165
 166    public override string options()
 167    {
 168        //
 169        // WARNING: Certain features, such as proxy validation in Glacier2,
 170        // depend on the format of proxy strings. Changes to toString() and
 171        // methods called to generate parts of the reference string could break
 172        // these features. Please review for all features that depend on the
 173        // format of proxyToString() before changing this and related code.
 174        //
 1175        string s = base.options();
 176
 1177        if (_mcastInterface.Length != 0)
 178        {
 1179            bool addQuote = _mcastInterface.Contains(':', StringComparison.Ordinal);
 1180            s += " --interface ";
 1181            if (addQuote)
 182            {
 1183                s += "\"";
 184            }
 1185            s += _mcastInterface;
 1186            if (addQuote)
 187            {
 1188                s += "\"";
 189            }
 190        }
 191
 1192        if (_mcastTtl != -1)
 193        {
 0194            s += " --ttl " + _mcastTtl;
 195        }
 196
 1197        if (_compress)
 198        {
 1199            s += " -z";
 200        }
 201
 1202        return s;
 203    }
 204
 205    //
 206    // Compare endpoints for sorting purposes
 207    //
 208    public override int CompareTo(EndpointI obj)
 209    {
 1210        if (!(obj is UdpEndpointI))
 211        {
 0212            return type() < obj.type() ? -1 : 1;
 213        }
 214
 1215        var p = (UdpEndpointI)obj;
 1216        if (this == p)
 217        {
 1218            return 0;
 219        }
 220
 1221        if (!_compress && p._compress)
 222        {
 0223            return -1;
 224        }
 1225        else if (!p._compress && _compress)
 226        {
 0227            return 1;
 228        }
 229
 1230        int rc = string.Compare(_mcastInterface, p._mcastInterface, StringComparison.Ordinal);
 1231        if (rc != 0)
 232        {
 0233            return rc;
 234        }
 235
 1236        if (_mcastTtl < p._mcastTtl)
 237        {
 0238            return -1;
 239        }
 1240        else if (p._mcastTtl < _mcastTtl)
 241        {
 0242            return 1;
 243        }
 244
 1245        return base.CompareTo(p);
 246    }
 247
 248    //
 249    // Marshal the endpoint
 250    //
 251    public override void streamWriteImpl(Ice.OutputStream s)
 252    {
 1253        base.streamWriteImpl(s);
 1254        if (s.getEncoding().Equals(Ice.Util.Encoding_1_0))
 255        {
 0256            ProtocolVersion.ice_write(s, Ice.Util.Protocol_1_0);
 0257            EncodingVersion.ice_write(s, Ice.Util.Encoding_1_0);
 258        }
 1259        s.writeBool(_compress);
 1260    }
 261
 1262    public override int GetHashCode() => HashCode.Combine(base.GetHashCode(), _mcastInterface, _mcastTtl, _compress);
 263
 264    public override EndpointI toPublishedEndpoint(string publishedHost) =>
 1265        new UdpEndpointI(
 1266            instance_,
 1267            publishedHost.Length > 0 ? publishedHost : host_,
 1268            port_,
 1269            sourceAddr: null,
 1270            mcastInterface: "",
 1271            mttl: -1,
 1272            connectionId: "",
 1273            _compress);
 274
 275    protected override bool checkOption(string option, string argument, string endpoint)
 276    {
 1277        if (base.checkOption(option, argument, endpoint))
 278        {
 1279            return true;
 280        }
 281
 1282        if (option == "-z")
 283        {
 0284            if (argument != null)
 285            {
 0286                throw new ParseException(
 0287                    $"unexpected argument '{argument}' provided for -z option in endpoint '{endpoint}'");
 288            }
 289
 0290            _compress = true;
 291        }
 1292        else if (option == "-v" || option == "-e")
 293        {
 0294            if (argument == null)
 295            {
 0296                throw new ParseException($"no argument provided for {option} option in endpoint '{endpoint}'");
 297            }
 298
 299            try
 300            {
 0301                Ice.EncodingVersion v = Ice.Util.stringToEncodingVersion(argument);
 0302                if (v.major != 1 || v.minor != 0)
 303                {
 0304                    instance_.logger().warning("deprecated udp endpoint option: " + option);
 305                }
 0306            }
 0307            catch (ParseException ex)
 308            {
 0309                throw new ParseException($"invalid version '{argument}' in endpoint '{endpoint}'", ex);
 310            }
 311        }
 1312        else if (option == "--ttl")
 313        {
 1314            if (argument == null)
 315            {
 0316                throw new ParseException($"no argument provided for --ttl option in endpoint '{endpoint}'");
 317            }
 318
 319            try
 320            {
 1321                _mcastTtl = int.Parse(argument, CultureInfo.InvariantCulture);
 1322            }
 0323            catch (FormatException ex)
 324            {
 0325                throw new ParseException($"invalid TTL value '{argument}' in endpoint '{endpoint}'", ex);
 326            }
 327
 1328            if (_mcastTtl < 0)
 329            {
 0330                throw new ParseException($"TTL value '{argument}' out of range in endpoint '{endpoint}'");
 331            }
 332        }
 1333        else if (option == "--interface")
 334        {
 1335            _mcastInterface = argument ??
 1336                throw new ParseException($"no argument provided for --interface option in endpoint '{endpoint}'");
 337        }
 338        else
 339        {
 0340            return false;
 341        }
 342
 1343        return true;
 344    }
 345
 346    protected override Connector createConnector(EndPoint addr, NetworkProxy proxy) =>
 1347        new UdpConnector(instance_, addr, sourceAddr_, _mcastInterface, _mcastTtl, connectionId_);
 348
 349    protected override IPEndpointI createEndpoint(string host, int port, string connectionId)
 350    {
 1351        return new UdpEndpointI(
 1352            instance_,
 1353            host,
 1354            port,
 1355            sourceAddr_,
 1356            _mcastInterface,
 1357            _mcastTtl,
 1358            connectionId,
 1359            _compress);
 360    }
 361
 1362    private string _mcastInterface = "";
 1363    private int _mcastTtl = -1;
 364    private bool _compress;
 365}
 366
 367internal sealed class UdpEndpointFactory : EndpointFactory
 368{
 369    internal UdpEndpointFactory(ProtocolInstance instance) => _instance = instance;
 370
 371    public void initialize()
 372    {
 373    }
 374
 375    public short type() => _instance.type();
 376
 377    public string protocol() => _instance.protocol();
 378
 379    public EndpointI create(List<string> args, bool oaEndpoint)
 380    {
 381        IPEndpointI endpt = new UdpEndpointI(_instance);
 382        endpt.initWithOptions(args, oaEndpoint);
 383        return endpt;
 384    }
 385
 386    public EndpointI read(Ice.InputStream s) => new UdpEndpointI(_instance, s);
 387
 388    public EndpointFactory clone(ProtocolInstance instance) => new UdpEndpointFactory(instance);
 389
 390    private readonly ProtocolInstance _instance;
 391}