< Summary

Information
Class: Ice.Internal.UdpEndpointFactory
Assembly: Ice
File(s): /_/csharp/src/Ice/Internal/UdpEndpointI.cs
Tag: 125_37167941578
Line coverage
88%
Covered lines: 8
Uncovered lines: 1
Coverable lines: 9
Total lines: 394
Line coverage: 88.8%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage
85%
Covered methods: 6
Fully covered methods: 6
Total methods: 7
Method coverage: 85.7%
Full method coverage: 85.7%

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%210%

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)
 21        : base(instance, host, port, sourceAddr, connectionId)
 22    {
 23        _mcastInterface = mcastInterface;
 24        _mcastTtl = mttl;
 25        _compress = compress;
 26    }
 27
 28    public UdpEndpointI(ProtocolInstance instance)
 29        : base(instance)
 30    {
 31    }
 32
 33    public UdpEndpointI(ProtocolInstance instance, Ice.InputStream s)
 34        : base(instance, s)
 35    {
 36        if (s.getEncoding().Equals(Ice.Util.Encoding_1_0))
 37        {
 38            s.readByte();
 39            s.readByte();
 40            s.readByte();
 41            s.readByte();
 42        }
 43        _compress = s.readBool();
 44    }
 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
 54        return new UDPEndpointInfo(
 55            _compress,
 56            host_,
 57            port_,
 58            Network.endpointAddressToString(sourceAddr_),
 59            _mcastInterface,
 60            _mcastTtl);
 61    }
 62
 63    //
 64    // Return the timeout for the endpoint in milliseconds. 0 means
 65    // non-blocking, -1 means no timeout.
 66    //
 67    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    //
 74    public override EndpointI timeout(int timeout) => this;
 75
 76    //
 77    // Return true if the endpoints support bzip2 compress, or false
 78    // otherwise.
 79    //
 80    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    {
 89        if (compress == _compress)
 90        {
 91            return this;
 92        }
 93        else
 94        {
 95            return new UdpEndpointI(
 96                instance_,
 97                host_,
 98                port_,
 99                sourceAddr_,
 100                _mcastInterface,
 101                _mcastTtl,
 102                connectionId_,
 103                compress);
 104        }
 105    }
 106
 107    //
 108    // Return true if the endpoint is datagram-based.
 109    //
 110    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    //
 116    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);
 125        return null;
 126    }
 127
 128    public override void initWithOptions(List<string> args, bool oaEndpoint)
 129    {
 130        base.initWithOptions(args, oaEndpoint);
 131
 132        if (_mcastInterface == "*")
 133        {
 134            if (oaEndpoint)
 135            {
 136                _mcastInterface = "";
 137            }
 138            else
 139            {
 140                throw new ParseException($"'--interface *' not valid for proxy endpoint '{this}'");
 141            }
 142        }
 143    }
 144
 145    public UdpEndpointI endpoint(UdpTransceiver transceiver)
 146    {
 147        int port = transceiver.effectivePort();
 148        if (port == port_)
 149        {
 150            return this;
 151        }
 152        else
 153        {
 154            return new UdpEndpointI(
 155                instance_,
 156                host_,
 157                port,
 158                sourceAddr_,
 159                _mcastInterface,
 160                _mcastTtl,
 161                connectionId_,
 162                _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        //
 175        string s = base.options();
 176
 177        if (_mcastInterface.Length != 0)
 178        {
 179            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).
 183            bool addQuote = _mcastInterface.IndexOfAny([' ', ':', '\t', '\n', '\r']) != -1;
 184            if (addQuote)
 185            {
 186                s += "\"";
 187            }
 188            s += _mcastInterface;
 189            if (addQuote)
 190            {
 191                s += "\"";
 192            }
 193        }
 194
 195        if (_mcastTtl != -1)
 196        {
 197            s += " --ttl " + _mcastTtl;
 198        }
 199
 200        if (_compress)
 201        {
 202            s += " -z";
 203        }
 204
 205        return s;
 206    }
 207
 208    //
 209    // Compare endpoints for sorting purposes
 210    //
 211    public override int CompareTo(EndpointI obj)
 212    {
 213        if (!(obj is UdpEndpointI))
 214        {
 215            return type() < obj.type() ? -1 : 1;
 216        }
 217
 218        var p = (UdpEndpointI)obj;
 219        if (this == p)
 220        {
 221            return 0;
 222        }
 223
 224        if (!_compress && p._compress)
 225        {
 226            return -1;
 227        }
 228        else if (!p._compress && _compress)
 229        {
 230            return 1;
 231        }
 232
 233        int rc = string.Compare(_mcastInterface, p._mcastInterface, StringComparison.Ordinal);
 234        if (rc != 0)
 235        {
 236            return rc;
 237        }
 238
 239        if (_mcastTtl < p._mcastTtl)
 240        {
 241            return -1;
 242        }
 243        else if (p._mcastTtl < _mcastTtl)
 244        {
 245            return 1;
 246        }
 247
 248        return base.CompareTo(p);
 249    }
 250
 251    //
 252    // Marshal the endpoint
 253    //
 254    public override void streamWriteImpl(Ice.OutputStream s)
 255    {
 256        base.streamWriteImpl(s);
 257        if (s.getEncoding().Equals(Ice.Util.Encoding_1_0))
 258        {
 259            ProtocolVersion.ice_write(s, Protocol.Protocol_1_0);
 260            EncodingVersion.ice_write(s, Ice.Util.Encoding_1_0);
 261        }
 262        s.writeBool(_compress);
 263    }
 264
 265    public override int GetHashCode() => HashCode.Combine(base.GetHashCode(), _mcastInterface, _mcastTtl, _compress);
 266
 267    public override EndpointI toPublishedEndpoint(string publishedHost) =>
 268        new UdpEndpointI(
 269            instance_,
 270            publishedHost.Length > 0 ? publishedHost : host_,
 271            port_,
 272            sourceAddr: null,
 273            mcastInterface: "",
 274            mttl: -1,
 275            connectionId: "",
 276            _compress);
 277
 278    protected override bool checkOption(string option, string argument, string endpoint)
 279    {
 280        if (base.checkOption(option, argument, endpoint))
 281        {
 282            return true;
 283        }
 284
 285        if (option == "-z")
 286        {
 287            if (argument != null)
 288            {
 289                throw new ParseException(
 290                    $"unexpected argument '{argument}' provided for -z option in endpoint '{endpoint}'");
 291            }
 292
 293            _compress = true;
 294        }
 295        else if (option == "-v" || option == "-e")
 296        {
 297            if (argument == null)
 298            {
 299                throw new ParseException($"no argument provided for {option} option in endpoint '{endpoint}'");
 300            }
 301
 302            try
 303            {
 304                Ice.EncodingVersion v = Protocol.stringToEncodingVersion(argument);
 305                if (v.major != 1 || v.minor != 0)
 306                {
 307                    instance_.logger().warning("deprecated udp endpoint option: " + option);
 308                }
 309            }
 310            catch (ParseException ex)
 311            {
 312                throw new ParseException($"invalid version '{argument}' in endpoint '{endpoint}'", ex);
 313            }
 314        }
 315        else if (option == "--ttl")
 316        {
 317            if (argument == null)
 318            {
 319                throw new ParseException($"no argument provided for --ttl option in endpoint '{endpoint}'");
 320            }
 321
 322            try
 323            {
 324                _mcastTtl = int.Parse(argument, CultureInfo.InvariantCulture);
 325            }
 326            catch (System.Exception ex) when (ex is FormatException or OverflowException)
 327            {
 328                throw new ParseException($"invalid TTL value '{argument}' in endpoint '{endpoint}'", ex);
 329            }
 330
 331            if (_mcastTtl < 0)
 332            {
 333                throw new ParseException($"TTL value '{argument}' out of range in endpoint '{endpoint}'");
 334            }
 335        }
 336        else if (option == "--interface")
 337        {
 338            _mcastInterface = argument ??
 339                throw new ParseException($"no argument provided for --interface option in endpoint '{endpoint}'");
 340        }
 341        else
 342        {
 343            return false;
 344        }
 345
 346        return true;
 347    }
 348
 349    protected override Connector createConnector(EndPoint addr, NetworkProxy proxy) =>
 350        new UdpConnector(instance_, addr, sourceAddr_, _mcastInterface, _mcastTtl, connectionId_);
 351
 352    protected override IPEndpointI createEndpoint(string host, int port, string connectionId)
 353    {
 354        return new UdpEndpointI(
 355            instance_,
 356            host,
 357            port,
 358            sourceAddr_,
 359            _mcastInterface,
 360            _mcastTtl,
 361            connectionId,
 362            _compress);
 363    }
 364
 365    private string _mcastInterface = "";
 366    private int _mcastTtl = -1;
 367    private bool _compress;
 368}
 369
 370internal sealed class UdpEndpointFactory : EndpointFactory
 371{
 1372    internal UdpEndpointFactory(ProtocolInstance instance) => _instance = instance;
 373
 374    public void initialize()
 375    {
 1376    }
 377
 1378    public short type() => _instance.type();
 379
 1380    public string protocol() => _instance.protocol();
 381
 382    public EndpointI create(List<string> args, bool oaEndpoint)
 383    {
 1384        IPEndpointI endpt = new UdpEndpointI(_instance);
 1385        endpt.initWithOptions(args, oaEndpoint);
 1386        return endpt;
 387    }
 388
 1389    public EndpointI read(Ice.InputStream s) => new UdpEndpointI(_instance, s);
 390
 0391    public EndpointFactory clone(ProtocolInstance instance) => new UdpEndpointFactory(instance);
 392
 393    private readonly ProtocolInstance _instance;
 394}