< Summary

Information
Class: Ice.Internal.EndpointHostResolver
Assembly: Ice
File(s): /_/csharp/src/Ice/Internal/EndpointHostResolver.cs
Tag: 91_21789722663
Line coverage
89%
Covered lines: 104
Uncovered lines: 12
Coverable lines: 116
Total lines: 239
Line coverage: 89.6%
Branch coverage
83%
Covered branches: 35
Total branches: 42
Branch coverage: 83.3%
Method coverage
100%
Covered methods: 11
Total methods: 11
Method coverage: 100%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
resolve(...)87.5%8.13887.5%
destroy()100%11100%
joinWithThread()50%22100%
run()87.5%24.972488.1%
updateObserver()100%44100%
.ctor(...)100%22100%
Join()100%11100%
Start(...)100%11100%
Run()0%2.75242.86%
getName()100%11100%

File(s)

/_/csharp/src/Ice/Internal/EndpointHostResolver.cs

#LineLine coverage
 1// Copyright (c) ZeroC, Inc.
 2
 3using System.Diagnostics;
 4using System.Net;
 5
 6namespace Ice.Internal;
 7
 8public class EndpointHostResolver
 9{
 110    internal EndpointHostResolver(Instance instance)
 11    {
 112        _instance = instance;
 113        _protocol = instance.protocolSupport();
 114        _preferIPv6 = instance.preferIPv6();
 115        _thread = new HelperThread(this);
 116        updateObserver();
 117        _thread.Start(Util.stringToThreadPriority(
 118                instance.initializationData().properties.getIceProperty("Ice.ThreadPriority")));
 119    }
 20
 21    public void resolve(string host, int port, IPEndpointI endpoint, EndpointI_connectors callback)
 22    {
 23        //
 24        // Try to get the addresses without DNS lookup. If this doesn't work, we queue a resolve
 25        // entry and the thread will take care of getting the endpoint addresses.
 26        //
 127        NetworkProxy networkProxy = _instance.networkProxy();
 128        if (networkProxy == null)
 29        {
 30            try
 31            {
 132                List<EndPoint> addrs = Network.getAddresses(host, port, _protocol, _preferIPv6, false);
 133                if (addrs.Count > 0)
 34                {
 135                    callback.connectors(endpoint.connectors(addrs, null));
 136                    return;
 37                }
 138            }
 039            catch (Ice.LocalException ex)
 40            {
 041                callback.exception(ex);
 042                return;
 43            }
 44        }
 45
 146        lock (_mutex)
 47        {
 48            Debug.Assert(!_destroyed);
 49
 150            var entry = new ResolveEntry();
 151            entry.host = host;
 152            entry.port = port;
 153            entry.endpoint = endpoint;
 154            entry.callback = callback;
 55
 156            Ice.Instrumentation.CommunicatorObserver obsv = _instance.initializationData().observer;
 157            if (obsv != null)
 58            {
 159                entry.observer = obsv.getEndpointLookupObserver(endpoint);
 160                entry.observer?.attach();
 61            }
 62
 163            _queue.AddLast(entry);
 164            Monitor.Pulse(_mutex);
 165        }
 166    }
 67
 68    public void destroy()
 69    {
 170        lock (_mutex)
 71        {
 72            Debug.Assert(!_destroyed);
 173            _destroyed = true;
 174            Monitor.Pulse(_mutex);
 175        }
 176    }
 77
 178    public void joinWithThread() => _thread?.Join();
 79
 80    public void run()
 81    {
 82        while (true)
 83        {
 84            ResolveEntry r;
 85            Ice.Instrumentation.ThreadObserver threadObserver;
 86
 187            lock (_mutex)
 88            {
 189                while (!_destroyed && _queue.Count == 0)
 90                {
 191                    Monitor.Wait(_mutex);
 92                }
 93
 194                if (_destroyed)
 95                {
 196                    break;
 97                }
 98
 199                r = _queue.First.Value;
 1100                _queue.RemoveFirst();
 1101                threadObserver = _observer;
 1102            }
 103
 1104            threadObserver?.stateChanged(
 1105                    Ice.Instrumentation.ThreadState.ThreadStateIdle,
 1106                    Ice.Instrumentation.ThreadState.ThreadStateInUseForOther);
 107
 108            try
 109            {
 1110                NetworkProxy networkProxy = _instance.networkProxy();
 1111                int protocol = _protocol;
 1112                if (networkProxy != null)
 113                {
 1114                    networkProxy = networkProxy.resolveHost(protocol);
 1115                    if (networkProxy != null)
 116                    {
 1117                        protocol = networkProxy.getProtocolSupport();
 118                    }
 119                }
 120
 1121                List<EndPoint> addrs = Network.getAddresses(r.host, r.port, protocol, _preferIPv6, true);
 1122                r.observer?.detach();
 1123                r.observer = null;
 124
 1125                r.callback.connectors(r.endpoint.connectors(addrs, networkProxy));
 1126            }
 1127            catch (Ice.LocalException ex)
 128            {
 1129                if (r.observer != null)
 130                {
 1131                    r.observer.failed(ex.ice_id());
 1132                    r.observer.detach();
 133                }
 1134                r.callback.exception(ex);
 1135            }
 136            finally
 137            {
 1138                threadObserver?.stateChanged(
 1139                        Ice.Instrumentation.ThreadState.ThreadStateInUseForOther,
 1140                        Ice.Instrumentation.ThreadState.ThreadStateIdle);
 1141            }
 142        }
 143
 1144        foreach (ResolveEntry entry in _queue)
 145        {
 0146            var ex = new Ice.CommunicatorDestroyedException();
 0147            if (entry.observer != null)
 148            {
 0149                entry.observer.failed(ex.ice_id());
 0150                entry.observer.detach();
 151            }
 0152            entry.callback.exception(ex);
 153        }
 1154        _queue.Clear();
 155
 1156        _observer?.detach();
 1157    }
 158
 159    public void
 160    updateObserver()
 161    {
 1162        lock (_mutex)
 163        {
 1164            Ice.Instrumentation.CommunicatorObserver obsv = _instance.initializationData().observer;
 1165            if (obsv != null)
 166            {
 1167                _observer = obsv.getThreadObserver(
 1168                    "Communicator",
 1169                    _thread.getName(),
 1170                    Ice.Instrumentation.ThreadState.ThreadStateIdle,
 1171                    _observer);
 1172                _observer?.attach();
 173            }
 1174        }
 1175    }
 176
 177    private class ResolveEntry
 178    {
 179        internal string host;
 180        internal int port;
 181        internal IPEndpointI endpoint;
 182        internal EndpointI_connectors callback;
 183        internal Ice.Instrumentation.Observer observer;
 184    }
 185
 186    private readonly Instance _instance;
 187    private readonly int _protocol;
 188    private readonly bool _preferIPv6;
 189    private bool _destroyed;
 1190    private readonly LinkedList<ResolveEntry> _queue = new LinkedList<ResolveEntry>();
 191    private Ice.Instrumentation.ThreadObserver _observer;
 192
 193    private sealed class HelperThread
 194    {
 1195        internal HelperThread(EndpointHostResolver resolver)
 196        {
 1197            _resolver = resolver;
 1198            _name = _resolver._instance.initializationData().properties.getIceProperty("Ice.ProgramName");
 1199            if (_name.Length > 0)
 200            {
 1201                _name += "-";
 202            }
 1203            _name += "Ice.HostResolver";
 1204        }
 205
 1206        public void Join() => _thread.Join();
 207
 208        public void Start(ThreadPriority priority)
 209        {
 1210            _thread = new Thread(new ThreadStart(Run));
 1211            _thread.IsBackground = true;
 1212            _thread.Name = _name;
 1213            _thread.Priority = priority;
 1214            _thread.Start();
 1215        }
 216
 217        public void Run()
 218        {
 219            try
 220            {
 1221                _resolver.run();
 1222            }
 0223            catch (System.Exception ex)
 224            {
 0225                string s = "exception in endpoint host resolver thread " + _name + ":\n" + ex;
 0226                _resolver._instance.initializationData().logger.error(s);
 0227            }
 1228        }
 229
 1230        public string getName() => _name;
 231
 232        private readonly EndpointHostResolver _resolver;
 233        private readonly string _name;
 234        private Thread _thread;
 235    }
 236
 237    private readonly HelperThread _thread;
 1238    private readonly object _mutex = new();
 239}