< Summary

Information
Class: Ice.Internal.UdpEndpointI
Assembly: Ice
File(s): /_/csharp/src/Ice/Internal/UdpEndpointI.cs
Tag: 125_37167941578
Line coverage
77%
Covered lines: 113
Uncovered lines: 33
Coverable lines: 146
Total lines: 394
Line coverage: 77.3%
Branch coverage
58%
Covered branches: 41
Total branches: 70
Branch coverage: 58.5%
Method coverage
90%
Covered methods: 19
Fully covered methods: 11
Total methods: 21
Method coverage: 90.4%
Full method coverage: 52.3%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
.ctor(...)100%11100%
.ctor(...)50%2260%
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%101092.86%
CompareTo(...)60%382064.71%
streamWriteImpl(...)50%2266.67%
GetHashCode()100%11100%
toPublishedEndpoint(...)100%22100%
checkOption(...)42.31%1112650%
createConnector(...)100%11100%
createEndpoint(...)100%11100%

File(s)

/_/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            s += " --interface ";
 180            // Quote the interface if it contains a character that would be re-parsed as a separator: whitespace, on
 181            // which the endpoint parser splits options (e.g. a Windows adapter name like "Ethernet 2"), or ':', on
 182            // which the proxy-string parser splits endpoints (e.g. an IPv6 interface address).
 1183            bool addQuote = _mcastInterface.IndexOfAny([' ', ':', '\t', '\n', '\r']) != -1;
 1184            if (addQuote)
 185            {
 1186                s += "\"";
 187            }
 1188            s += _mcastInterface;
 1189            if (addQuote)
 190            {
 1191                s += "\"";
 192            }
 193        }
 194
 1195        if (_mcastTtl != -1)
 196        {
 0197            s += " --ttl " + _mcastTtl;
 198        }
 199
 1200        if (_compress)
 201        {
 1202            s += " -z";
 203        }
 204
 1205        return s;
 206    }
 207
 208    //
 209    // Compare endpoints for sorting purposes
 210    //
 211    public override int CompareTo(EndpointI obj)
 212    {
 1213        if (!(obj is UdpEndpointI))
 214        {
 0215            return type() < obj.type() ? -1 : 1;
 216        }
 217
 1218        var p = (UdpEndpointI)obj;
 1219        if (this == p)
 220        {
 1221            return 0;
 222        }
 223
 1224        if (!_compress && p._compress)
 225        {
 0226            return -1;
 227        }
 1228        else if (!p._compress && _compress)
 229        {
 0230            return 1;
 231        }
 232
 1233        int rc = string.Compare(_mcastInterface, p._mcastInterface, StringComparison.Ordinal);
 1234        if (rc != 0)
 235        {
 0236            return rc;
 237        }
 238
 1239        if (_mcastTtl < p._mcastTtl)
 240        {
 0241            return -1;
 242        }
 1243        else if (p._mcastTtl < _mcastTtl)
 244        {
 0245            return 1;
 246        }
 247
 1248        return base.CompareTo(p);
 249    }
 250
 251    //
 252    // Marshal the endpoint
 253    //
 254    public override void streamWriteImpl(Ice.OutputStream s)
 255    {
 1256        base.streamWriteImpl(s);
 1257        if (s.getEncoding().Equals(Ice.Util.Encoding_1_0))
 258        {
 0259            ProtocolVersion.ice_write(s, Protocol.Protocol_1_0);
 0260            EncodingVersion.ice_write(s, Ice.Util.Encoding_1_0);
 261        }
 1262        s.writeBool(_compress);
 1263    }
 264
 1265    public override int GetHashCode() => HashCode.Combine(base.GetHashCode(), _mcastInterface, _mcastTtl, _compress);
 266
 267    public override EndpointI toPublishedEndpoint(string publishedHost) =>
 1268        new UdpEndpointI(
 1269            instance_,
 1270            publishedHost.Length > 0 ? publishedHost : host_,
 1271            port_,
 1272            sourceAddr: null,
 1273            mcastInterface: "",
 1274            mttl: -1,
 1275            connectionId: "",
 1276            _compress);
 277
 278    protected override bool checkOption(string option, string argument, string endpoint)
 279    {
 1280        if (base.checkOption(option, argument, endpoint))
 281        {
 1282            return true;
 283        }
 284
 1285        if (option == "-z")
 286        {
 0287            if (argument != null)
 288            {
 0289                throw new ParseException(
 0290                    $"unexpected argument '{argument}' provided for -z option in endpoint '{endpoint}'");
 291            }
 292
 0293            _compress = true;
 294        }
 1295        else if (option == "-v" || option == "-e")
 296        {
 0297            if (argument == null)
 298            {
 0299                throw new ParseException($"no argument provided for {option} option in endpoint '{endpoint}'");
 300            }
 301
 302            try
 303            {
 0304                Ice.EncodingVersion v = Protocol.stringToEncodingVersion(argument);
 0305                if (v.major != 1 || v.minor != 0)
 306                {
 0307                    instance_.logger().warning("deprecated udp endpoint option: " + option);
 308                }
 0309            }
 0310            catch (ParseException ex)
 311            {
 0312                throw new ParseException($"invalid version '{argument}' in endpoint '{endpoint}'", ex);
 313            }
 314        }
 1315        else if (option == "--ttl")
 316        {
 1317            if (argument == null)
 318            {
 0319                throw new ParseException($"no argument provided for --ttl option in endpoint '{endpoint}'");
 320            }
 321
 322            try
 323            {
 1324                _mcastTtl = int.Parse(argument, CultureInfo.InvariantCulture);
 1325            }
 1326            catch (System.Exception ex) when (ex is FormatException or OverflowException)
 327            {
 1328                throw new ParseException($"invalid TTL value '{argument}' in endpoint '{endpoint}'", ex);
 329            }
 330
 1331            if (_mcastTtl < 0)
 332            {
 0333                throw new ParseException($"TTL value '{argument}' out of range in endpoint '{endpoint}'");
 334            }
 335        }
 1336        else if (option == "--interface")
 337        {
 1338            _mcastInterface = argument ??
 1339                throw new ParseException($"no argument provided for --interface option in endpoint '{endpoint}'");
 340        }
 341        else
 342        {
 0343            return false;
 344        }
 345
 1346        return true;
 347    }
 348
 349    protected override Connector createConnector(EndPoint addr, NetworkProxy proxy) =>
 1350        new UdpConnector(instance_, addr, sourceAddr_, _mcastInterface, _mcastTtl, connectionId_);
 351
 352    protected override IPEndpointI createEndpoint(string host, int port, string connectionId)
 353    {
 1354        return new UdpEndpointI(
 1355            instance_,
 1356            host,
 1357            port,
 1358            sourceAddr_,
 1359            _mcastInterface,
 1360            _mcastTtl,
 1361            connectionId,
 1362            _compress);
 363    }
 364
 1365    private string _mcastInterface = "";
 1366    private int _mcastTtl = -1;
 367    private bool _compress;
 368}
 369
 370internal sealed class UdpEndpointFactory : EndpointFactory
 371{
 372    internal UdpEndpointFactory(ProtocolInstance instance) => _instance = instance;
 373
 374    public void initialize()
 375    {
 376    }
 377
 378    public short type() => _instance.type();
 379
 380    public string protocol() => _instance.protocol();
 381
 382    public EndpointI create(List<string> args, bool oaEndpoint)
 383    {
 384        IPEndpointI endpt = new UdpEndpointI(_instance);
 385        endpt.initWithOptions(args, oaEndpoint);
 386        return endpt;
 387    }
 388
 389    public EndpointI read(Ice.InputStream s) => new UdpEndpointI(_instance, s);
 390
 391    public EndpointFactory clone(ProtocolInstance instance) => new UdpEndpointFactory(instance);
 392
 393    private readonly ProtocolInstance _instance;
 394}