| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | #nullable enable |
| | | 4 | | |
| | | 5 | | using Ice.Instrumentation; |
| | | 6 | | |
| | | 7 | | namespace Ice.Internal; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// A middleware that observes dispatches. |
| | | 11 | | /// </summary> |
| | | 12 | | internal sealed class ObserverMiddleware : Object |
| | | 13 | | { |
| | | 14 | | private readonly Object _next; |
| | | 15 | | private readonly CommunicatorObserver _communicatorObserver; |
| | | 16 | | |
| | | 17 | | public ValueTask<OutgoingResponse> dispatchAsync(IncomingRequest request) |
| | | 18 | | { |
| | 1 | 19 | | return _communicatorObserver.getDispatchObserver(request.current, request.size) is DispatchObserver observer ? |
| | 1 | 20 | | performDispatchAsync(observer) : |
| | 1 | 21 | | _next.dispatchAsync(request); |
| | | 22 | | |
| | | 23 | | async ValueTask<OutgoingResponse> performDispatchAsync(DispatchObserver observer) |
| | | 24 | | { |
| | 1 | 25 | | observer.attach(); |
| | | 26 | | try |
| | | 27 | | { |
| | | 28 | | OutgoingResponse response; |
| | | 29 | | try |
| | | 30 | | { |
| | 1 | 31 | | response = await _next.dispatchAsync(request).ConfigureAwait(false); |
| | 1 | 32 | | } |
| | 1 | 33 | | catch (System.Exception ex) |
| | | 34 | | { |
| | | 35 | | // We need to marshal the exception into the response immediately to observe the response size. |
| | 1 | 36 | | response = request.current.createOutgoingResponse(ex); |
| | 1 | 37 | | } |
| | | 38 | | |
| | 1 | 39 | | switch (response.replyStatus) |
| | | 40 | | { |
| | | 41 | | case ReplyStatus.Ok: |
| | | 42 | | // don't do anything |
| | | 43 | | break; |
| | | 44 | | |
| | | 45 | | case ReplyStatus.UserException: |
| | 1 | 46 | | observer.userException(); |
| | 1 | 47 | | break; |
| | | 48 | | |
| | | 49 | | default: |
| | 1 | 50 | | observer.failed(response.exceptionId ?? ""); |
| | | 51 | | break; |
| | | 52 | | } |
| | | 53 | | |
| | 1 | 54 | | if (request.current.requestId != 0) |
| | | 55 | | { |
| | 1 | 56 | | observer.reply(response.size); |
| | | 57 | | } |
| | | 58 | | |
| | 1 | 59 | | return response; |
| | | 60 | | } |
| | | 61 | | finally |
| | | 62 | | { |
| | 1 | 63 | | observer.detach(); |
| | | 64 | | } |
| | 1 | 65 | | } |
| | | 66 | | } |
| | | 67 | | |
| | 1 | 68 | | internal ObserverMiddleware(Object next, CommunicatorObserver communicatorObserver) |
| | | 69 | | { |
| | 1 | 70 | | _next = next; |
| | 1 | 71 | | _communicatorObserver = communicatorObserver; |
| | 1 | 72 | | } |
| | | 73 | | } |