| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using System.Diagnostics; |
| | 4 | |
|
| | 5 | | namespace Ice.Internal; |
| | 6 | |
|
| | 7 | | public class CollocatedRequestHandler : RequestHandler |
| | 8 | | { |
| 1 | 9 | | private static void fillInValue(Ice.OutputStream os, int pos, int value) => os.rewriteInt(value, pos); |
| | 10 | |
|
| 1 | 11 | | internal CollocatedRequestHandler(Reference reference, Ice.ObjectAdapter adapter) |
| | 12 | | { |
| 1 | 13 | | _reference = reference; |
| 1 | 14 | | _executor = _reference.getInstance().initializationData().executor != null; |
| 1 | 15 | | _response = _reference.isTwoway; |
| 1 | 16 | | _adapter = adapter; |
| | 17 | |
|
| 1 | 18 | | _logger = _reference.getInstance().initializationData().logger; // Cached for better performance. |
| 1 | 19 | | _traceLevels = _reference.getInstance().traceLevels(); // Cached for better performance. |
| 1 | 20 | | _requestId = 0; |
| 1 | 21 | | } |
| | 22 | |
|
| 1 | 23 | | public int sendAsyncRequest(ProxyOutgoingAsyncBase outAsync) => outAsync.invokeCollocated(this); |
| | 24 | |
|
| | 25 | | public void asyncRequestCanceled(OutgoingAsyncBase outAsync, Ice.LocalException ex) |
| | 26 | | { |
| 1 | 27 | | lock (_mutex) |
| | 28 | | { |
| 1 | 29 | | if (_sendAsyncRequests.TryGetValue(outAsync, out int requestId)) |
| | 30 | | { |
| 1 | 31 | | if (requestId > 0) |
| | 32 | | { |
| 1 | 33 | | _asyncRequests.Remove(requestId); |
| | 34 | | } |
| 1 | 35 | | _sendAsyncRequests.Remove(outAsync); |
| 1 | 36 | | if (outAsync.exception(ex)) |
| | 37 | | { |
| 1 | 38 | | outAsync.invokeExceptionAsync(); |
| | 39 | | } |
| 1 | 40 | | _adapter.decDirectCount(); // invokeAll won't be called, decrease the direct count. |
| 1 | 41 | | return; |
| | 42 | | } |
| 1 | 43 | | if (outAsync is OutgoingAsync) |
| | 44 | | { |
| 1 | 45 | | var o = (OutgoingAsync)outAsync; |
| | 46 | | Debug.Assert(o != null); |
| 1 | 47 | | foreach (KeyValuePair<int, OutgoingAsyncBase> e in _asyncRequests) |
| | 48 | | { |
| 1 | 49 | | if (e.Value == o) |
| | 50 | | { |
| 1 | 51 | | _asyncRequests.Remove(e.Key); |
| 1 | 52 | | if (outAsync.exception(ex)) |
| | 53 | | { |
| 1 | 54 | | outAsync.invokeExceptionAsync(); |
| | 55 | | } |
| 1 | 56 | | return; |
| | 57 | | } |
| | 58 | | } |
| | 59 | | } |
| 0 | 60 | | } |
| 1 | 61 | | } |
| | 62 | |
|
| 1 | 63 | | public ConnectionI getConnection() => null; |
| | 64 | |
|
| | 65 | | internal int invokeAsyncRequest(OutgoingAsyncBase outAsync, int batchRequestCount, bool synchronous) |
| | 66 | | { |
| | 67 | | // |
| | 68 | | // Increase the direct count to prevent the thread pool from being destroyed before |
| | 69 | | // invokeAll is called. This will also throw if the object adapter has been destroyed. |
| | 70 | | // |
| 1 | 71 | | _adapter.incDirectCount(); |
| | 72 | |
|
| 1 | 73 | | int requestId = 0; |
| | 74 | | try |
| | 75 | | { |
| 1 | 76 | | lock (_mutex) |
| | 77 | | { |
| 1 | 78 | | outAsync.cancelable(this); // This will throw if the request is canceled |
| | 79 | |
|
| 1 | 80 | | if (_response) |
| | 81 | | { |
| 1 | 82 | | requestId = ++_requestId; |
| 1 | 83 | | _asyncRequests.Add(requestId, outAsync); |
| | 84 | | } |
| | 85 | |
|
| 1 | 86 | | _sendAsyncRequests.Add(outAsync, requestId); |
| 1 | 87 | | } |
| | 88 | |
|
| 1 | 89 | | outAsync.attachCollocatedObserver(_adapter, requestId); |
| 1 | 90 | | if (!synchronous || !_response || _reference.getInvocationTimeout() > TimeSpan.Zero) |
| | 91 | | { |
| | 92 | | // Don't invoke from the user thread if async or invocation timeout is set |
| 1 | 93 | | _adapter.getThreadPool().execute( |
| 1 | 94 | | () => |
| 1 | 95 | | { |
| 1 | 96 | | if (sentAsync(outAsync)) |
| 1 | 97 | | { |
| 1 | 98 | | dispatchAll(outAsync.getOs(), requestId, batchRequestCount); |
| 1 | 99 | | } |
| 1 | 100 | | }, |
| 1 | 101 | | null); |
| | 102 | | } |
| 1 | 103 | | else if (_executor) |
| | 104 | | { |
| 1 | 105 | | _adapter.getThreadPool().executeFromThisThread( |
| 1 | 106 | | () => |
| 1 | 107 | | { |
| 1 | 108 | | if (sentAsync(outAsync)) |
| 1 | 109 | | { |
| 1 | 110 | | dispatchAll(outAsync.getOs(), requestId, batchRequestCount); |
| 1 | 111 | | } |
| 1 | 112 | | }, |
| 1 | 113 | | null); |
| | 114 | | } |
| | 115 | | else // Optimization: directly call invokeAll if there's no executor. |
| | 116 | | { |
| 1 | 117 | | if (sentAsync(outAsync)) |
| | 118 | | { |
| 1 | 119 | | dispatchAll(outAsync.getOs(), requestId, batchRequestCount); |
| | 120 | | } |
| | 121 | | } |
| 1 | 122 | | } |
| 0 | 123 | | catch |
| | 124 | | { |
| | 125 | | // Decrement the direct count if any exception is thrown synchronously. |
| 0 | 126 | | _adapter.decDirectCount(); |
| 0 | 127 | | throw; |
| | 128 | | } |
| 1 | 129 | | return OutgoingAsyncBase.AsyncStatusQueued; |
| | 130 | | } |
| | 131 | |
|
| | 132 | | private bool sentAsync(OutgoingAsyncBase outAsync) |
| | 133 | | { |
| 1 | 134 | | lock (_mutex) |
| | 135 | | { |
| 1 | 136 | | if (!_sendAsyncRequests.Remove(outAsync)) |
| | 137 | | { |
| 1 | 138 | | return false; // The request timed-out. |
| | 139 | | } |
| | 140 | |
|
| 1 | 141 | | if (!outAsync.sent()) |
| | 142 | | { |
| 1 | 143 | | return true; |
| | 144 | | } |
| 1 | 145 | | } |
| 1 | 146 | | outAsync.invokeSent(); |
| 1 | 147 | | return true; |
| 1 | 148 | | } |
| | 149 | |
|
| | 150 | | private void dispatchAll(Ice.OutputStream os, int requestId, int requestCount) |
| | 151 | | { |
| 1 | 152 | | if (_traceLevels.protocol >= 1) |
| | 153 | | { |
| 1 | 154 | | fillInValue(os, 10, os.size()); |
| 1 | 155 | | if (requestId > 0) |
| | 156 | | { |
| 1 | 157 | | fillInValue(os, Protocol.headerSize, requestId); |
| | 158 | | } |
| 1 | 159 | | else if (requestCount > 0) |
| | 160 | | { |
| 1 | 161 | | fillInValue(os, Protocol.headerSize, requestCount); |
| | 162 | | } |
| 1 | 163 | | TraceUtil.traceSend(os, _reference.getInstance(), connection: null, _logger, _traceLevels); |
| | 164 | | } |
| | 165 | |
|
| 1 | 166 | | var iss = new Ice.InputStream(_reference.getInstance(), os.getEncoding(), os.getBuffer(), false); |
| | 167 | |
|
| 1 | 168 | | if (requestCount > 0) |
| | 169 | | { |
| 1 | 170 | | iss.pos(Protocol.requestBatchHdr.Length); |
| | 171 | | } |
| | 172 | | else |
| | 173 | | { |
| 1 | 174 | | iss.pos(Protocol.requestHdr.Length); |
| | 175 | | } |
| | 176 | |
|
| 1 | 177 | | int dispatchCount = requestCount > 0 ? requestCount : 1; |
| | 178 | | Debug.Assert(!_response || dispatchCount == 1); |
| | 179 | |
|
| 1 | 180 | | Object dispatcher = _adapter.dispatchPipeline; |
| | 181 | | try |
| | 182 | | { |
| 1 | 183 | | while (dispatchCount > 0) |
| | 184 | | { |
| | 185 | | // Increase the direct count for the dispatch. We increase it again here for |
| | 186 | | // each dispatch. It's important for the direct count to be > 0 until the last |
| | 187 | | // collocated request response is sent to make sure the thread pool isn't |
| | 188 | | // destroyed before. It's decremented when processing the response. |
| | 189 | | try |
| | 190 | | { |
| 1 | 191 | | _adapter.incDirectCount(); |
| 1 | 192 | | } |
| 1 | 193 | | catch (Ice.ObjectAdapterDestroyedException ex) |
| | 194 | | { |
| 1 | 195 | | handleException(ex, requestId, amd: false); |
| 1 | 196 | | break; |
| | 197 | | } |
| | 198 | |
|
| 1 | 199 | | var request = new IncomingRequest(requestId, connection: null, _adapter, iss); |
| | 200 | | // See comment in ConnectionI |
| 1 | 201 | | _ = dispatchAsync(request); |
| 1 | 202 | | --dispatchCount; |
| | 203 | | } |
| 1 | 204 | | } |
| 0 | 205 | | catch (Ice.LocalException ex) |
| | 206 | | { |
| 0 | 207 | | dispatchException(ex, requestId, amd: false); // Fatal invocation exception |
| 0 | 208 | | } |
| | 209 | |
|
| 1 | 210 | | _adapter.decDirectCount(); |
| | 211 | |
|
| | 212 | | async Task dispatchAsync(IncomingRequest request) |
| | 213 | | { |
| 1 | 214 | | bool amd = false; |
| | 215 | |
|
| | 216 | | try |
| | 217 | | { |
| | 218 | | OutgoingResponse response; |
| | 219 | |
|
| | 220 | | try |
| | 221 | | { |
| 1 | 222 | | ValueTask<OutgoingResponse> valueTask = dispatcher.dispatchAsync(request); |
| 1 | 223 | | amd = !valueTask.IsCompleted; |
| 1 | 224 | | response = await valueTask.ConfigureAwait(false); |
| 1 | 225 | | } |
| 1 | 226 | | catch (System.Exception ex) |
| | 227 | | { |
| 1 | 228 | | response = request.current.createOutgoingResponse(ex); |
| 1 | 229 | | } |
| | 230 | |
|
| 1 | 231 | | sendResponse(response, requestId, amd); |
| 1 | 232 | | } |
| 0 | 233 | | catch (Ice.LocalException ex) // TODO: catch all exceptions to avoid UnobservedTaskException |
| | 234 | | { |
| 0 | 235 | | dispatchException(ex, requestId, amd); |
| 0 | 236 | | } |
| 1 | 237 | | } |
| 1 | 238 | | } |
| | 239 | |
|
| | 240 | | private void sendResponse(OutgoingResponse response, int requestId, bool amd) |
| | 241 | | { |
| 1 | 242 | | if (_response) |
| | 243 | | { |
| | 244 | | OutgoingAsyncBase outAsync; |
| 1 | 245 | | OutputStream outputStream = response.outputStream; |
| 1 | 246 | | lock (_mutex) |
| | 247 | | { |
| 1 | 248 | | if (_traceLevels.protocol >= 1) |
| | 249 | | { |
| 1 | 250 | | fillInValue(outputStream, 10, outputStream.size()); |
| | 251 | | } |
| | 252 | |
|
| | 253 | | // Adopt the OutputStream's buffer. |
| 1 | 254 | | var inputStream = new InputStream( |
| 1 | 255 | | _reference.getInstance(), |
| 1 | 256 | | outputStream.getEncoding(), |
| 1 | 257 | | outputStream.getBuffer(), |
| 1 | 258 | | adopt: true); |
| | 259 | |
|
| 1 | 260 | | inputStream.pos(Protocol.replyHdr.Length + 4); |
| | 261 | |
|
| 1 | 262 | | if (_traceLevels.protocol >= 1) |
| | 263 | | { |
| 1 | 264 | | TraceUtil.traceRecv(inputStream, connection: null, _logger, _traceLevels); |
| | 265 | | } |
| | 266 | |
|
| 1 | 267 | | if (_asyncRequests.TryGetValue(requestId, out outAsync)) |
| | 268 | | { |
| 1 | 269 | | outAsync.getIs().swap(inputStream); |
| 1 | 270 | | if (!outAsync.response()) |
| | 271 | | { |
| 1 | 272 | | outAsync = null; |
| | 273 | | } |
| 1 | 274 | | _asyncRequests.Remove(requestId); |
| | 275 | | } |
| 1 | 276 | | } |
| | 277 | |
|
| 1 | 278 | | if (outAsync is not null) |
| | 279 | | { |
| 1 | 280 | | if (amd) |
| | 281 | | { |
| 1 | 282 | | outAsync.invokeResponseAsync(); |
| | 283 | | } |
| | 284 | | else |
| | 285 | | { |
| 1 | 286 | | outAsync.invokeResponse(); |
| | 287 | | } |
| | 288 | | } |
| | 289 | | } |
| 1 | 290 | | _adapter.decDirectCount(); |
| 1 | 291 | | } |
| | 292 | |
|
| | 293 | | private void dispatchException(Ice.LocalException ex, int requestId, bool amd) |
| | 294 | | { |
| 0 | 295 | | handleException(ex, requestId, amd); |
| 0 | 296 | | _adapter.decDirectCount(); |
| 0 | 297 | | } |
| | 298 | |
|
| | 299 | | private void handleException(Ice.Exception ex, int requestId, bool amd) |
| | 300 | | { |
| 1 | 301 | | if (!_response) |
| | 302 | | { |
| 0 | 303 | | return; // Ignore exception for oneway proxies. |
| | 304 | | } |
| | 305 | |
|
| | 306 | | OutgoingAsyncBase outAsync; |
| 1 | 307 | | lock (_mutex) |
| | 308 | | { |
| 1 | 309 | | if (_asyncRequests.TryGetValue(requestId, out outAsync)) |
| | 310 | | { |
| 1 | 311 | | if (!outAsync.exception(ex)) |
| | 312 | | { |
| 0 | 313 | | outAsync = null; |
| | 314 | | } |
| 1 | 315 | | _asyncRequests.Remove(requestId); |
| | 316 | | } |
| 1 | 317 | | } |
| | 318 | |
|
| 1 | 319 | | if (outAsync != null) |
| | 320 | | { |
| | 321 | | // |
| | 322 | | // If called from an AMD dispatch, invoke asynchronously |
| | 323 | | // the completion callback since this might be called from |
| | 324 | | // the user code. |
| | 325 | | // |
| 1 | 326 | | if (amd) |
| | 327 | | { |
| 0 | 328 | | outAsync.invokeExceptionAsync(); |
| | 329 | | } |
| | 330 | | else |
| | 331 | | { |
| 1 | 332 | | outAsync.invokeException(); |
| | 333 | | } |
| | 334 | | } |
| 1 | 335 | | } |
| | 336 | |
|
| | 337 | | private readonly Reference _reference; |
| | 338 | | private readonly bool _executor; |
| | 339 | | private readonly bool _response; |
| | 340 | | private readonly Ice.ObjectAdapter _adapter; |
| | 341 | | private readonly Ice.Logger _logger; |
| | 342 | | private readonly TraceLevels _traceLevels; |
| | 343 | |
|
| | 344 | | private int _requestId; |
| | 345 | |
|
| 1 | 346 | | private readonly Dictionary<OutgoingAsyncBase, int> _sendAsyncRequests = new Dictionary<OutgoingAsyncBase, int>(); |
| 1 | 347 | | private readonly Dictionary<int, OutgoingAsyncBase> _asyncRequests = new Dictionary<int, OutgoingAsyncBase>(); |
| 1 | 348 | | private readonly object _mutex = new(); |
| | 349 | | } |