< Summary

Information
Class: Ice.Internal.EndpointHostResolver.HelperThread
Assembly: Ice
File(s): /home/runner/work/ice/ice/csharp/src/Ice/Internal/EndpointHostResolver.cs
Tag: 71_18251537082
Line coverage
81%
Covered lines: 18
Uncovered lines: 4
Coverable lines: 22
Total lines: 242
Line coverage: 81.8%
Branch coverage
50%
Covered branches: 2
Total branches: 4
Branch coverage: 50%
Method coverage
100%
Covered methods: 5
Total methods: 5
Method coverage: 100%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%22100%
Join()100%11100%
Start(...)100%11100%
Run()0%2.75242.86%
getName()100%11100%

File(s)

/home/runner/work/ice/ice/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{
 10    internal EndpointHostResolver(Instance instance)
 11    {
 12        _instance = instance;
 13        _protocol = instance.protocolSupport();
 14        _preferIPv6 = instance.preferIPv6();
 15        _thread = new HelperThread(this);
 16        updateObserver();
 17        _thread.Start(Util.stringToThreadPriority(
 18                instance.initializationData().properties.getIceProperty("Ice.ThreadPriority")));
 19    }
 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        //
 27        NetworkProxy networkProxy = _instance.networkProxy();
 28        if (networkProxy == null)
 29        {
 30            try
 31            {
 32                List<EndPoint> addrs = Network.getAddresses(host, port, _protocol, _preferIPv6, false);
 33                if (addrs.Count > 0)
 34                {
 35                    callback.connectors(endpoint.connectors(addrs, null));
 36                    return;
 37                }
 38            }
 39            catch (Ice.LocalException ex)
 40            {
 41                callback.exception(ex);
 42                return;
 43            }
 44        }
 45
 46        lock (_mutex)
 47        {
 48            Debug.Assert(!_destroyed);
 49
 50            var entry = new ResolveEntry();
 51            entry.host = host;
 52            entry.port = port;
 53            entry.endpoint = endpoint;
 54            entry.callback = callback;
 55
 56            Ice.Instrumentation.CommunicatorObserver obsv = _instance.initializationData().observer;
 57            if (obsv != null)
 58            {
 59                entry.observer = obsv.getEndpointLookupObserver(endpoint);
 60                entry.observer?.attach();
 61            }
 62
 63            _queue.AddLast(entry);
 64            Monitor.Pulse(_mutex);
 65        }
 66    }
 67
 68    public void destroy()
 69    {
 70        lock (_mutex)
 71        {
 72            Debug.Assert(!_destroyed);
 73            _destroyed = true;
 74            Monitor.Pulse(_mutex);
 75        }
 76    }
 77
 78    public void joinWithThread() => _thread?.Join();
 79
 80    public void run()
 81    {
 82        while (true)
 83        {
 84            ResolveEntry r;
 85            Ice.Instrumentation.ThreadObserver threadObserver;
 86
 87            lock (_mutex)
 88            {
 89                while (!_destroyed && _queue.Count == 0)
 90                {
 91                    Monitor.Wait(_mutex);
 92                }
 93
 94                if (_destroyed)
 95                {
 96                    break;
 97                }
 98
 99                r = _queue.First.Value;
 100                _queue.RemoveFirst();
 101                threadObserver = _observer;
 102            }
 103
 104            threadObserver?.stateChanged(
 105                    Ice.Instrumentation.ThreadState.ThreadStateIdle,
 106                    Ice.Instrumentation.ThreadState.ThreadStateInUseForOther);
 107
 108            try
 109            {
 110                NetworkProxy networkProxy = _instance.networkProxy();
 111                int protocol = _protocol;
 112                if (networkProxy != null)
 113                {
 114                    networkProxy = networkProxy.resolveHost(protocol);
 115                    if (networkProxy != null)
 116                    {
 117                        protocol = networkProxy.getProtocolSupport();
 118                    }
 119                }
 120
 121                List<EndPoint> addrs = Network.getAddresses(r.host, r.port, protocol, _preferIPv6, true);
 122                if (r.observer != null)
 123                {
 124                    r.observer.detach();
 125                    r.observer = null;
 126                }
 127
 128                r.callback.connectors(r.endpoint.connectors(addrs, networkProxy));
 129            }
 130            catch (Ice.LocalException ex)
 131            {
 132                if (r.observer != null)
 133                {
 134                    r.observer.failed(ex.ice_id());
 135                    r.observer.detach();
 136                }
 137                r.callback.exception(ex);
 138            }
 139            finally
 140            {
 141                threadObserver?.stateChanged(
 142                        Ice.Instrumentation.ThreadState.ThreadStateInUseForOther,
 143                        Ice.Instrumentation.ThreadState.ThreadStateIdle);
 144            }
 145        }
 146
 147        foreach (ResolveEntry entry in _queue)
 148        {
 149            var ex = new Ice.CommunicatorDestroyedException();
 150            if (entry.observer != null)
 151            {
 152                entry.observer.failed(ex.ice_id());
 153                entry.observer.detach();
 154            }
 155            entry.callback.exception(ex);
 156        }
 157        _queue.Clear();
 158
 159        _observer?.detach();
 160    }
 161
 162    public void
 163    updateObserver()
 164    {
 165        lock (_mutex)
 166        {
 167            Ice.Instrumentation.CommunicatorObserver obsv = _instance.initializationData().observer;
 168            if (obsv != null)
 169            {
 170                _observer = obsv.getThreadObserver(
 171                    "Communicator",
 172                    _thread.getName(),
 173                    Ice.Instrumentation.ThreadState.ThreadStateIdle,
 174                    _observer);
 175                _observer?.attach();
 176            }
 177        }
 178    }
 179
 180    private class ResolveEntry
 181    {
 182        internal string host;
 183        internal int port;
 184        internal IPEndpointI endpoint;
 185        internal EndpointI_connectors callback;
 186        internal Ice.Instrumentation.Observer observer;
 187    }
 188
 189    private readonly Instance _instance;
 190    private readonly int _protocol;
 191    private readonly bool _preferIPv6;
 192    private bool _destroyed;
 193    private readonly LinkedList<ResolveEntry> _queue = new LinkedList<ResolveEntry>();
 194    private Ice.Instrumentation.ThreadObserver _observer;
 195
 196    private sealed class HelperThread
 197    {
 1198        internal HelperThread(EndpointHostResolver resolver)
 199        {
 1200            _resolver = resolver;
 1201            _name = _resolver._instance.initializationData().properties.getIceProperty("Ice.ProgramName");
 1202            if (_name.Length > 0)
 203            {
 1204                _name += "-";
 205            }
 1206            _name += "Ice.HostResolver";
 1207        }
 208
 1209        public void Join() => _thread.Join();
 210
 211        public void Start(ThreadPriority priority)
 212        {
 1213            _thread = new Thread(new ThreadStart(Run));
 1214            _thread.IsBackground = true;
 1215            _thread.Name = _name;
 1216            _thread.Priority = priority;
 1217            _thread.Start();
 1218        }
 219
 220        public void Run()
 221        {
 222            try
 223            {
 1224                _resolver.run();
 1225            }
 0226            catch (System.Exception ex)
 227            {
 0228                string s = "exception in endpoint host resolver thread " + _name + ":\n" + ex;
 0229                _resolver._instance.initializationData().logger.error(s);
 0230            }
 1231        }
 232
 1233        public string getName() => _name;
 234
 235        private readonly EndpointHostResolver _resolver;
 236        private readonly string _name;
 237        private Thread _thread;
 238    }
 239
 240    private readonly HelperThread _thread;
 241    private readonly object _mutex = new();
 242}