< Summary

Information
Class: Ice.Internal.EndpointHostResolver.HelperThread
Assembly: Ice
File(s): /_/csharp/src/Ice/Internal/EndpointHostResolver.cs
Tag: 125_37167941578
Line coverage
81%
Covered lines: 18
Uncovered lines: 4
Coverable lines: 22
Total lines: 238
Line coverage: 81.8%
Branch coverage
50%
Covered branches: 2
Total branches: 4
Branch coverage: 50%
Method coverage
100%
Covered methods: 5
Fully covered methods: 4
Total methods: 5
Method coverage: 100%
Full method coverage: 80%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%22100%
Join()100%11100%
Start(...)100%11100%
Run()0%3242.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{
 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.getThreadPriorityProperty(instance.initializationData().properties, "Ice"));
 18    }
 19
 20    public void resolve(string host, int port, IPEndpointI endpoint, EndpointI_connectors callback)
 21    {
 22        //
 23        // Try to get the addresses without DNS lookup. If this doesn't work, we queue a resolve
 24        // entry and the thread will take care of getting the endpoint addresses.
 25        //
 26        NetworkProxy networkProxy = _instance.networkProxy();
 27        if (networkProxy == null)
 28        {
 29            try
 30            {
 31                List<EndPoint> addrs = Network.getAddresses(host, port, _protocol, _preferIPv6, false);
 32                if (addrs.Count > 0)
 33                {
 34                    callback.connectors(endpoint.connectors(addrs, null));
 35                    return;
 36                }
 37            }
 38            catch (Ice.LocalException ex)
 39            {
 40                callback.exception(ex);
 41                return;
 42            }
 43        }
 44
 45        lock (_mutex)
 46        {
 47            Debug.Assert(!_destroyed);
 48
 49            var entry = new ResolveEntry();
 50            entry.host = host;
 51            entry.port = port;
 52            entry.endpoint = endpoint;
 53            entry.callback = callback;
 54
 55            Ice.Instrumentation.CommunicatorObserver obsv = _instance.initializationData().observer;
 56            if (obsv != null)
 57            {
 58                entry.observer = obsv.getEndpointLookupObserver(endpoint);
 59                entry.observer?.attach();
 60            }
 61
 62            _queue.AddLast(entry);
 63            Monitor.Pulse(_mutex);
 64        }
 65    }
 66
 67    public void destroy()
 68    {
 69        lock (_mutex)
 70        {
 71            Debug.Assert(!_destroyed);
 72            _destroyed = true;
 73            Monitor.Pulse(_mutex);
 74        }
 75    }
 76
 77    public void joinWithThread() => _thread?.Join();
 78
 79    public void run()
 80    {
 81        while (true)
 82        {
 83            ResolveEntry r;
 84            Ice.Instrumentation.ThreadObserver threadObserver;
 85
 86            lock (_mutex)
 87            {
 88                while (!_destroyed && _queue.Count == 0)
 89                {
 90                    Monitor.Wait(_mutex);
 91                }
 92
 93                if (_destroyed)
 94                {
 95                    break;
 96                }
 97
 98                r = _queue.First.Value;
 99                _queue.RemoveFirst();
 100                threadObserver = _observer;
 101            }
 102
 103            threadObserver?.stateChanged(
 104                    Ice.Instrumentation.ThreadState.ThreadStateIdle,
 105                    Ice.Instrumentation.ThreadState.ThreadStateInUseForOther);
 106
 107            try
 108            {
 109                NetworkProxy networkProxy = _instance.networkProxy();
 110                int protocol = _protocol;
 111                if (networkProxy != null)
 112                {
 113                    networkProxy = networkProxy.resolveHost(protocol);
 114                    if (networkProxy != null)
 115                    {
 116                        protocol = networkProxy.getProtocolSupport();
 117                    }
 118                }
 119
 120                List<EndPoint> addrs = Network.getAddresses(r.host, r.port, protocol, _preferIPv6, true);
 121                r.observer?.detach();
 122                r.observer = null;
 123
 124                r.callback.connectors(r.endpoint.connectors(addrs, networkProxy));
 125            }
 126            catch (Ice.LocalException ex)
 127            {
 128                if (r.observer != null)
 129                {
 130                    r.observer.failed(ex.ice_id());
 131                    r.observer.detach();
 132                }
 133                r.callback.exception(ex);
 134            }
 135            finally
 136            {
 137                threadObserver?.stateChanged(
 138                        Ice.Instrumentation.ThreadState.ThreadStateInUseForOther,
 139                        Ice.Instrumentation.ThreadState.ThreadStateIdle);
 140            }
 141        }
 142
 143        foreach (ResolveEntry entry in _queue)
 144        {
 145            var ex = new Ice.CommunicatorDestroyedException();
 146            if (entry.observer != null)
 147            {
 148                entry.observer.failed(ex.ice_id());
 149                entry.observer.detach();
 150            }
 151            entry.callback.exception(ex);
 152        }
 153        _queue.Clear();
 154
 155        _observer?.detach();
 156    }
 157
 158    public void
 159    updateObserver()
 160    {
 161        lock (_mutex)
 162        {
 163            Ice.Instrumentation.CommunicatorObserver obsv = _instance.initializationData().observer;
 164            if (obsv != null)
 165            {
 166                _observer = obsv.getThreadObserver(
 167                    "Communicator",
 168                    _thread.getName(),
 169                    Ice.Instrumentation.ThreadState.ThreadStateIdle,
 170                    _observer);
 171                _observer?.attach();
 172            }
 173        }
 174    }
 175
 176    private class ResolveEntry
 177    {
 178        internal string host;
 179        internal int port;
 180        internal IPEndpointI endpoint;
 181        internal EndpointI_connectors callback;
 182        internal Ice.Instrumentation.Observer observer;
 183    }
 184
 185    private readonly Instance _instance;
 186    private readonly int _protocol;
 187    private readonly bool _preferIPv6;
 188    private bool _destroyed;
 189    private readonly LinkedList<ResolveEntry> _queue = new LinkedList<ResolveEntry>();
 190    private Ice.Instrumentation.ThreadObserver _observer;
 191
 192    private sealed class HelperThread
 193    {
 1194        internal HelperThread(EndpointHostResolver resolver)
 195        {
 1196            _resolver = resolver;
 1197            _name = _resolver._instance.initializationData().properties.getIceProperty("Ice.ProgramName");
 1198            if (_name.Length > 0)
 199            {
 1200                _name += "-";
 201            }
 1202            _name += "Ice.HostResolver";
 1203        }
 204
 1205        public void Join() => _thread.Join();
 206
 207        public void Start(ThreadPriority priority)
 208        {
 1209            _thread = new Thread(new ThreadStart(Run));
 1210            _thread.IsBackground = true;
 1211            _thread.Name = _name;
 1212            _thread.Priority = priority;
 1213            _thread.Start();
 1214        }
 215
 216        public void Run()
 217        {
 218            try
 219            {
 1220                _resolver.run();
 1221            }
 0222            catch (System.Exception ex)
 223            {
 0224                string s = "exception in endpoint host resolver thread " + _name + ":\n" + ex;
 0225                _resolver._instance.initializationData().logger.error(s);
 0226            }
 1227        }
 228
 1229        public string getName() => _name;
 230
 231        private readonly EndpointHostResolver _resolver;
 232        private readonly string _name;
 233        private Thread _thread;
 234    }
 235
 236    private readonly HelperThread _thread;
 237    private readonly object _mutex = new();
 238}