|
| 1 | +using System.Threading; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using Grpc.Core; |
| 4 | +using Grpc.Core.Interceptors; |
| 5 | +using JDBC.NET.Data.Exceptions; |
| 6 | + |
| 7 | +namespace JDBC.NET.Data; |
| 8 | + |
| 9 | +internal sealed class JdbcCallInterceptor : Interceptor |
| 10 | +{ |
| 11 | + public override TResponse BlockingUnaryCall<TRequest, TResponse>(TRequest request, ClientInterceptorContext<TRequest, TResponse> context, BlockingUnaryCallContinuation<TRequest, TResponse> continuation) |
| 12 | + { |
| 13 | + try |
| 14 | + { |
| 15 | + return base.BlockingUnaryCall(request, context, continuation); |
| 16 | + } |
| 17 | + catch (RpcException e) |
| 18 | + { |
| 19 | + throw new JdbcException(e); |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + public override AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>(TRequest request, ClientInterceptorContext<TRequest, TResponse> context, AsyncUnaryCallContinuation<TRequest, TResponse> continuation) |
| 24 | + { |
| 25 | + AsyncUnaryCall<TResponse> asyncCall = base.AsyncUnaryCall(request, context, continuation); |
| 26 | + |
| 27 | + return new AsyncUnaryCall<TResponse>( |
| 28 | + Wrap(asyncCall.ResponseAsync), |
| 29 | + Wrap(asyncCall.ResponseHeadersAsync), |
| 30 | + asyncCall.GetStatus, |
| 31 | + asyncCall.GetTrailers, |
| 32 | + asyncCall.Dispose |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + public override AsyncClientStreamingCall<TRequest, TResponse> AsyncClientStreamingCall<TRequest, TResponse>(ClientInterceptorContext<TRequest, TResponse> context, AsyncClientStreamingCallContinuation<TRequest, TResponse> continuation) |
| 37 | + { |
| 38 | + AsyncClientStreamingCall<TRequest, TResponse> asyncCall = base.AsyncClientStreamingCall(context, continuation); |
| 39 | + |
| 40 | + return new AsyncClientStreamingCall<TRequest, TResponse>( |
| 41 | + Wrap(asyncCall.RequestStream), |
| 42 | + Wrap(asyncCall.ResponseAsync), |
| 43 | + Wrap(asyncCall.ResponseHeadersAsync), |
| 44 | + asyncCall.GetStatus, |
| 45 | + asyncCall.GetTrailers, |
| 46 | + asyncCall.Dispose |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + public override AsyncDuplexStreamingCall<TRequest, TResponse> AsyncDuplexStreamingCall<TRequest, TResponse>(ClientInterceptorContext<TRequest, TResponse> context, AsyncDuplexStreamingCallContinuation<TRequest, TResponse> continuation) |
| 51 | + { |
| 52 | + AsyncDuplexStreamingCall<TRequest, TResponse> asyncCall = base.AsyncDuplexStreamingCall(context, continuation); |
| 53 | + |
| 54 | + return new AsyncDuplexStreamingCall<TRequest, TResponse>( |
| 55 | + Wrap(asyncCall.RequestStream), |
| 56 | + Wrap(asyncCall.ResponseStream), |
| 57 | + Wrap(asyncCall.ResponseHeadersAsync), |
| 58 | + asyncCall.GetStatus, |
| 59 | + asyncCall.GetTrailers, |
| 60 | + asyncCall.Dispose |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + public override AsyncServerStreamingCall<TResponse> AsyncServerStreamingCall<TRequest, TResponse>(TRequest request, ClientInterceptorContext<TRequest, TResponse> context, AsyncServerStreamingCallContinuation<TRequest, TResponse> continuation) |
| 65 | + { |
| 66 | + AsyncServerStreamingCall<TResponse> asyncCall = base.AsyncServerStreamingCall(request, context, continuation); |
| 67 | + |
| 68 | + return new AsyncServerStreamingCall<TResponse>( |
| 69 | + Wrap(asyncCall.ResponseStream), |
| 70 | + Wrap(asyncCall.ResponseHeadersAsync), |
| 71 | + asyncCall.GetStatus, |
| 72 | + asyncCall.GetTrailers, |
| 73 | + asyncCall.Dispose |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + private static async Task<T> Wrap<T>(Task<T> task) |
| 78 | + { |
| 79 | + if (task.Status != TaskStatus.WaitingForActivation) |
| 80 | + return await task; |
| 81 | + |
| 82 | + try |
| 83 | + { |
| 84 | + return await task; |
| 85 | + } |
| 86 | + catch (RpcException e) |
| 87 | + { |
| 88 | + throw new JdbcException(e); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private static IClientStreamWriter<T> Wrap<T>(IClientStreamWriter<T> writer) |
| 93 | + { |
| 94 | + if (writer == null) |
| 95 | + return null; |
| 96 | + |
| 97 | + return new JdbcClientStreamWriter<T>(writer); |
| 98 | + } |
| 99 | + |
| 100 | + private static IAsyncStreamReader<T> Wrap<T>(IAsyncStreamReader<T> reader) |
| 101 | + { |
| 102 | + if (reader == null) |
| 103 | + return null; |
| 104 | + |
| 105 | + return new JdbcAsyncStreamReader<T>(reader); |
| 106 | + } |
| 107 | + |
| 108 | + private readonly struct JdbcClientStreamWriter<T> : IClientStreamWriter<T> |
| 109 | + { |
| 110 | + public WriteOptions WriteOptions |
| 111 | + { |
| 112 | + get => _writer.WriteOptions; |
| 113 | + set => _writer.WriteOptions = value; |
| 114 | + } |
| 115 | + |
| 116 | + private readonly IClientStreamWriter<T> _writer; |
| 117 | + |
| 118 | + public JdbcClientStreamWriter(IClientStreamWriter<T> writer) |
| 119 | + { |
| 120 | + _writer = writer; |
| 121 | + } |
| 122 | + |
| 123 | + public async Task WriteAsync(T message) |
| 124 | + { |
| 125 | + try |
| 126 | + { |
| 127 | + await _writer.WriteAsync(message); |
| 128 | + } |
| 129 | + catch (RpcException e) |
| 130 | + { |
| 131 | + throw new JdbcException(e); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + public async Task CompleteAsync() |
| 136 | + { |
| 137 | + try |
| 138 | + { |
| 139 | + await _writer.CompleteAsync(); |
| 140 | + } |
| 141 | + catch (RpcException e) |
| 142 | + { |
| 143 | + throw new JdbcException(e); |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + private readonly struct JdbcAsyncStreamReader<T> : IAsyncStreamReader<T> |
| 149 | + { |
| 150 | + public T Current => _reader.Current; |
| 151 | + |
| 152 | + private readonly IAsyncStreamReader<T> _reader; |
| 153 | + |
| 154 | + public JdbcAsyncStreamReader(IAsyncStreamReader<T> reader) |
| 155 | + { |
| 156 | + _reader = reader; |
| 157 | + } |
| 158 | + |
| 159 | + public async Task<bool> MoveNext(CancellationToken cancellationToken) |
| 160 | + { |
| 161 | + try |
| 162 | + { |
| 163 | + return await _reader.MoveNext(cancellationToken); |
| 164 | + } |
| 165 | + catch (RpcException e) |
| 166 | + { |
| 167 | + throw new JdbcException(e); |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments