| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using System.Diagnostics; |
| | 4 | | using System.Net; |
| | 5 | |
|
| | 6 | | namespace Ice.Internal; |
| | 7 | |
|
| | 8 | | public class EndpointHostResolver |
| | 9 | | { |
| 1 | 10 | | internal EndpointHostResolver(Instance instance) |
| | 11 | | { |
| 1 | 12 | | _instance = instance; |
| 1 | 13 | | _protocol = instance.protocolSupport(); |
| 1 | 14 | | _preferIPv6 = instance.preferIPv6(); |
| 1 | 15 | | _thread = new HelperThread(this); |
| 1 | 16 | | updateObserver(); |
| 1 | 17 | | _thread.Start(Util.stringToThreadPriority( |
| 1 | 18 | | instance.initializationData().properties.getIceProperty("Ice.ThreadPriority"))); |
| 1 | 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 | | // |
| 1 | 27 | | NetworkProxy networkProxy = _instance.networkProxy(); |
| 1 | 28 | | if (networkProxy == null) |
| | 29 | | { |
| | 30 | | try |
| | 31 | | { |
| 1 | 32 | | List<EndPoint> addrs = Network.getAddresses(host, port, _protocol, _preferIPv6, false); |
| 1 | 33 | | if (addrs.Count > 0) |
| | 34 | | { |
| 1 | 35 | | callback.connectors(endpoint.connectors(addrs, null)); |
| 1 | 36 | | return; |
| | 37 | | } |
| 1 | 38 | | } |
| 0 | 39 | | catch (Ice.LocalException ex) |
| | 40 | | { |
| 0 | 41 | | callback.exception(ex); |
| 0 | 42 | | return; |
| | 43 | | } |
| | 44 | | } |
| | 45 | |
|
| 1 | 46 | | lock (_mutex) |
| | 47 | | { |
| | 48 | | Debug.Assert(!_destroyed); |
| | 49 | |
|
| 1 | 50 | | var entry = new ResolveEntry(); |
| 1 | 51 | | entry.host = host; |
| 1 | 52 | | entry.port = port; |
| 1 | 53 | | entry.endpoint = endpoint; |
| 1 | 54 | | entry.callback = callback; |
| | 55 | |
|
| 1 | 56 | | Ice.Instrumentation.CommunicatorObserver obsv = _instance.initializationData().observer; |
| 1 | 57 | | if (obsv != null) |
| | 58 | | { |
| 1 | 59 | | entry.observer = obsv.getEndpointLookupObserver(endpoint); |
| 1 | 60 | | entry.observer?.attach(); |
| | 61 | | } |
| | 62 | |
|
| 1 | 63 | | _queue.AddLast(entry); |
| 1 | 64 | | Monitor.Pulse(_mutex); |
| 1 | 65 | | } |
| 1 | 66 | | } |
| | 67 | |
|
| | 68 | | public void destroy() |
| | 69 | | { |
| 1 | 70 | | lock (_mutex) |
| | 71 | | { |
| | 72 | | Debug.Assert(!_destroyed); |
| 1 | 73 | | _destroyed = true; |
| 1 | 74 | | Monitor.Pulse(_mutex); |
| 1 | 75 | | } |
| 1 | 76 | | } |
| | 77 | |
|
| 1 | 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 | |
|
| 1 | 87 | | lock (_mutex) |
| | 88 | | { |
| 1 | 89 | | while (!_destroyed && _queue.Count == 0) |
| | 90 | | { |
| 1 | 91 | | Monitor.Wait(_mutex); |
| | 92 | | } |
| | 93 | |
|
| 1 | 94 | | if (_destroyed) |
| | 95 | | { |
| 1 | 96 | | break; |
| | 97 | | } |
| | 98 | |
|
| 1 | 99 | | r = _queue.First.Value; |
| 1 | 100 | | _queue.RemoveFirst(); |
| 1 | 101 | | threadObserver = _observer; |
| 1 | 102 | | } |
| | 103 | |
|
| 1 | 104 | | threadObserver?.stateChanged( |
| 1 | 105 | | Ice.Instrumentation.ThreadState.ThreadStateIdle, |
| 1 | 106 | | Ice.Instrumentation.ThreadState.ThreadStateInUseForOther); |
| | 107 | |
|
| | 108 | | try |
| | 109 | | { |
| 1 | 110 | | NetworkProxy networkProxy = _instance.networkProxy(); |
| 1 | 111 | | int protocol = _protocol; |
| 1 | 112 | | if (networkProxy != null) |
| | 113 | | { |
| 1 | 114 | | networkProxy = networkProxy.resolveHost(protocol); |
| 1 | 115 | | if (networkProxy != null) |
| | 116 | | { |
| 1 | 117 | | protocol = networkProxy.getProtocolSupport(); |
| | 118 | | } |
| | 119 | | } |
| | 120 | |
|
| 1 | 121 | | List<EndPoint> addrs = Network.getAddresses(r.host, r.port, protocol, _preferIPv6, true); |
| 1 | 122 | | if (r.observer != null) |
| | 123 | | { |
| 1 | 124 | | r.observer.detach(); |
| 1 | 125 | | r.observer = null; |
| | 126 | | } |
| | 127 | |
|
| 1 | 128 | | r.callback.connectors(r.endpoint.connectors(addrs, networkProxy)); |
| 1 | 129 | | } |
| 1 | 130 | | catch (Ice.LocalException ex) |
| | 131 | | { |
| 1 | 132 | | if (r.observer != null) |
| | 133 | | { |
| 1 | 134 | | r.observer.failed(ex.ice_id()); |
| 1 | 135 | | r.observer.detach(); |
| | 136 | | } |
| 1 | 137 | | r.callback.exception(ex); |
| 1 | 138 | | } |
| | 139 | | finally |
| | 140 | | { |
| 1 | 141 | | threadObserver?.stateChanged( |
| 1 | 142 | | Ice.Instrumentation.ThreadState.ThreadStateInUseForOther, |
| 1 | 143 | | Ice.Instrumentation.ThreadState.ThreadStateIdle); |
| 1 | 144 | | } |
| | 145 | | } |
| | 146 | |
|
| 1 | 147 | | foreach (ResolveEntry entry in _queue) |
| | 148 | | { |
| 0 | 149 | | var ex = new Ice.CommunicatorDestroyedException(); |
| 0 | 150 | | if (entry.observer != null) |
| | 151 | | { |
| 0 | 152 | | entry.observer.failed(ex.ice_id()); |
| 0 | 153 | | entry.observer.detach(); |
| | 154 | | } |
| 0 | 155 | | entry.callback.exception(ex); |
| | 156 | | } |
| 1 | 157 | | _queue.Clear(); |
| | 158 | |
|
| 1 | 159 | | _observer?.detach(); |
| 1 | 160 | | } |
| | 161 | |
|
| | 162 | | public void |
| | 163 | | updateObserver() |
| | 164 | | { |
| 1 | 165 | | lock (_mutex) |
| | 166 | | { |
| 1 | 167 | | Ice.Instrumentation.CommunicatorObserver obsv = _instance.initializationData().observer; |
| 1 | 168 | | if (obsv != null) |
| | 169 | | { |
| 1 | 170 | | _observer = obsv.getThreadObserver( |
| 1 | 171 | | "Communicator", |
| 1 | 172 | | _thread.getName(), |
| 1 | 173 | | Ice.Instrumentation.ThreadState.ThreadStateIdle, |
| 1 | 174 | | _observer); |
| 1 | 175 | | _observer?.attach(); |
| | 176 | | } |
| 1 | 177 | | } |
| 1 | 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; |
| 1 | 193 | | private readonly LinkedList<ResolveEntry> _queue = new LinkedList<ResolveEntry>(); |
| | 194 | | private Ice.Instrumentation.ThreadObserver _observer; |
| | 195 | |
|
| | 196 | | private sealed class HelperThread |
| | 197 | | { |
| 1 | 198 | | internal HelperThread(EndpointHostResolver resolver) |
| | 199 | | { |
| 1 | 200 | | _resolver = resolver; |
| 1 | 201 | | _name = _resolver._instance.initializationData().properties.getIceProperty("Ice.ProgramName"); |
| 1 | 202 | | if (_name.Length > 0) |
| | 203 | | { |
| 1 | 204 | | _name += "-"; |
| | 205 | | } |
| 1 | 206 | | _name += "Ice.HostResolver"; |
| 1 | 207 | | } |
| | 208 | |
|
| 1 | 209 | | public void Join() => _thread.Join(); |
| | 210 | |
|
| | 211 | | public void Start(ThreadPriority priority) |
| | 212 | | { |
| 1 | 213 | | _thread = new Thread(new ThreadStart(Run)); |
| 1 | 214 | | _thread.IsBackground = true; |
| 1 | 215 | | _thread.Name = _name; |
| 1 | 216 | | _thread.Priority = priority; |
| 1 | 217 | | _thread.Start(); |
| 1 | 218 | | } |
| | 219 | |
|
| | 220 | | public void Run() |
| | 221 | | { |
| | 222 | | try |
| | 223 | | { |
| 1 | 224 | | _resolver.run(); |
| 1 | 225 | | } |
| 0 | 226 | | catch (System.Exception ex) |
| | 227 | | { |
| 0 | 228 | | string s = "exception in endpoint host resolver thread " + _name + ":\n" + ex; |
| 0 | 229 | | _resolver._instance.initializationData().logger.error(s); |
| 0 | 230 | | } |
| 1 | 231 | | } |
| | 232 | |
|
| 1 | 233 | | 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; |
| 1 | 241 | | private readonly object _mutex = new(); |
| | 242 | | } |