Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | |
Jan Tattermusch | af77b3d | 2015-02-13 11:22:21 -0800 | [diff] [blame] | 3 | // Copyright 2015, Google Inc. |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 4 | // All rights reserved. |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 5 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 6 | // Redistribution and use in source and binary forms, with or without |
| 7 | // modification, are permitted provided that the following conditions are |
| 8 | // met: |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 9 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 10 | // * Redistributions of source code must retain the above copyright |
| 11 | // notice, this list of conditions and the following disclaimer. |
| 12 | // * Redistributions in binary form must reproduce the above |
| 13 | // copyright notice, this list of conditions and the following disclaimer |
| 14 | // in the documentation and/or other materials provided with the |
| 15 | // distribution. |
| 16 | // * Neither the name of Google Inc. nor the names of its |
| 17 | // contributors may be used to endorse or promote products derived from |
| 18 | // this software without specific prior written permission. |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 19 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | |
| 32 | #endregion |
| 33 | |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 34 | using System; |
Jan Tattermusch | 7ca6179 | 2015-05-18 14:28:22 -0700 | [diff] [blame] | 35 | using System.Collections.Generic; |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 36 | using System.Linq; |
Jan Tattermusch | 998eb9b | 2015-07-20 22:12:53 -0700 | [diff] [blame] | 37 | using System.Threading; |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 38 | using System.Threading.Tasks; |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 39 | using Grpc.Core.Internal; |
Jan Tattermusch | 0526161 | 2015-07-24 00:28:16 -0700 | [diff] [blame] | 40 | using Grpc.Core.Logging; |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 41 | using Grpc.Core.Utils; |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 42 | |
Jan Tattermusch | 286975f | 2015-03-12 14:04:36 -0700 | [diff] [blame] | 43 | namespace Grpc.Core.Internal |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 44 | { |
| 45 | internal interface IServerCallHandler |
| 46 | { |
Jan Tattermusch | 5bbd818 | 2015-07-20 20:48:40 -0700 | [diff] [blame] | 47 | Task HandleCall(ServerRpcNew newRpc, GrpcEnvironment environment); |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 50 | internal class UnaryServerCallHandler<TRequest, TResponse> : IServerCallHandler |
Jan Tattermusch | bdb1b48 | 2015-05-06 14:46:25 -0700 | [diff] [blame] | 51 | where TRequest : class |
| 52 | where TResponse : class |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 53 | { |
Jan Tattermusch | 0526161 | 2015-07-24 00:28:16 -0700 | [diff] [blame] | 54 | static readonly ILogger Logger = GrpcEnvironment.Logger.ForType<UnaryServerCallHandler<TRequest, TResponse>>(); |
| 55 | |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 56 | readonly Method<TRequest, TResponse> method; |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 57 | readonly UnaryServerMethod<TRequest, TResponse> handler; |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 58 | |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 59 | public UnaryServerCallHandler(Method<TRequest, TResponse> method, UnaryServerMethod<TRequest, TResponse> handler) |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 60 | { |
| 61 | this.method = method; |
| 62 | this.handler = handler; |
| 63 | } |
| 64 | |
Jan Tattermusch | 5bbd818 | 2015-07-20 20:48:40 -0700 | [diff] [blame] | 65 | public async Task HandleCall(ServerRpcNew newRpc, GrpcEnvironment environment) |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 66 | { |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 67 | var asyncCall = new AsyncCallServer<TRequest, TResponse>( |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 68 | method.ResponseMarshaller.Serializer, |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 69 | method.RequestMarshaller.Deserializer, |
| 70 | environment); |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 71 | |
Jan Tattermusch | 5bbd818 | 2015-07-20 20:48:40 -0700 | [diff] [blame] | 72 | asyncCall.Initialize(newRpc.Call); |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 73 | var finishedTask = asyncCall.ServerSideCallAsync(); |
| 74 | var requestStream = new ServerRequestStream<TRequest, TResponse>(asyncCall); |
| 75 | var responseStream = new ServerResponseStream<TRequest, TResponse>(asyncCall); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 76 | |
Jan Tattermusch | 998eb9b | 2015-07-20 22:12:53 -0700 | [diff] [blame] | 77 | Status status; |
Jan Tattermusch | bff90ac | 2015-08-06 21:30:26 -0700 | [diff] [blame] | 78 | var context = HandlerUtils.NewContext(newRpc, asyncCall.Peer, responseStream, asyncCall.CancellationToken); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 79 | try |
| 80 | { |
Jan Tattermusch | 7ca6179 | 2015-05-18 14:28:22 -0700 | [diff] [blame] | 81 | Preconditions.CheckArgument(await requestStream.MoveNext()); |
| 82 | var request = requestStream.Current; |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 83 | // TODO(jtattermusch): we need to read the full stream so that native callhandle gets deallocated. |
Jan Tattermusch | 7ca6179 | 2015-05-18 14:28:22 -0700 | [diff] [blame] | 84 | Preconditions.CheckArgument(!await requestStream.MoveNext()); |
Jan Tattermusch | 25bb2ef | 2015-07-21 12:15:53 -0700 | [diff] [blame] | 85 | var result = await handler(request, context); |
Jan Tattermusch | 998eb9b | 2015-07-20 22:12:53 -0700 | [diff] [blame] | 86 | status = context.Status; |
Jan Tattermusch | 43dc50b | 2015-05-18 15:27:19 -0700 | [diff] [blame] | 87 | await responseStream.WriteAsync(result); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 88 | } |
| 89 | catch (Exception e) |
| 90 | { |
Jan Tattermusch | 0526161 | 2015-07-24 00:28:16 -0700 | [diff] [blame] | 91 | Logger.Error(e, "Exception occured in handler."); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 92 | status = HandlerUtils.StatusFromException(e); |
| 93 | } |
Jan Tattermusch | 8c2dd9d | 2015-05-04 09:20:43 -0700 | [diff] [blame] | 94 | try |
| 95 | { |
Jan Tattermusch | a0bb065 | 2015-07-20 22:34:19 -0700 | [diff] [blame] | 96 | await responseStream.WriteStatusAsync(status, context.ResponseTrailers); |
Jan Tattermusch | 8c2dd9d | 2015-05-04 09:20:43 -0700 | [diff] [blame] | 97 | } |
| 98 | catch (OperationCanceledException) |
| 99 | { |
| 100 | // Call has been already cancelled. |
| 101 | } |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 102 | await finishedTask; |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 106 | internal class ServerStreamingServerCallHandler<TRequest, TResponse> : IServerCallHandler |
Jan Tattermusch | bdb1b48 | 2015-05-06 14:46:25 -0700 | [diff] [blame] | 107 | where TRequest : class |
| 108 | where TResponse : class |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 109 | { |
Jan Tattermusch | 0526161 | 2015-07-24 00:28:16 -0700 | [diff] [blame] | 110 | static readonly ILogger Logger = GrpcEnvironment.Logger.ForType<ServerStreamingServerCallHandler<TRequest, TResponse>>(); |
| 111 | |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 112 | readonly Method<TRequest, TResponse> method; |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 113 | readonly ServerStreamingServerMethod<TRequest, TResponse> handler; |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 114 | |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 115 | public ServerStreamingServerCallHandler(Method<TRequest, TResponse> method, ServerStreamingServerMethod<TRequest, TResponse> handler) |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 116 | { |
| 117 | this.method = method; |
| 118 | this.handler = handler; |
| 119 | } |
| 120 | |
Jan Tattermusch | 5bbd818 | 2015-07-20 20:48:40 -0700 | [diff] [blame] | 121 | public async Task HandleCall(ServerRpcNew newRpc, GrpcEnvironment environment) |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 122 | { |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 123 | var asyncCall = new AsyncCallServer<TRequest, TResponse>( |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 124 | method.ResponseMarshaller.Serializer, |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 125 | method.RequestMarshaller.Deserializer, |
| 126 | environment); |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 127 | |
Jan Tattermusch | 5bbd818 | 2015-07-20 20:48:40 -0700 | [diff] [blame] | 128 | asyncCall.Initialize(newRpc.Call); |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 129 | var finishedTask = asyncCall.ServerSideCallAsync(); |
| 130 | var requestStream = new ServerRequestStream<TRequest, TResponse>(asyncCall); |
| 131 | var responseStream = new ServerResponseStream<TRequest, TResponse>(asyncCall); |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 132 | |
Jan Tattermusch | 998eb9b | 2015-07-20 22:12:53 -0700 | [diff] [blame] | 133 | Status status; |
Jan Tattermusch | bff90ac | 2015-08-06 21:30:26 -0700 | [diff] [blame] | 134 | var context = HandlerUtils.NewContext(newRpc, asyncCall.Peer, responseStream, asyncCall.CancellationToken); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 135 | try |
| 136 | { |
Jan Tattermusch | 7ca6179 | 2015-05-18 14:28:22 -0700 | [diff] [blame] | 137 | Preconditions.CheckArgument(await requestStream.MoveNext()); |
| 138 | var request = requestStream.Current; |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 139 | // TODO(jtattermusch): we need to read the full stream so that native callhandle gets deallocated. |
Jan Tattermusch | 7ca6179 | 2015-05-18 14:28:22 -0700 | [diff] [blame] | 140 | Preconditions.CheckArgument(!await requestStream.MoveNext()); |
Jan Tattermusch | 25bb2ef | 2015-07-21 12:15:53 -0700 | [diff] [blame] | 141 | await handler(request, responseStream, context); |
Jan Tattermusch | 998eb9b | 2015-07-20 22:12:53 -0700 | [diff] [blame] | 142 | status = context.Status; |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 143 | } |
| 144 | catch (Exception e) |
| 145 | { |
Jan Tattermusch | 0526161 | 2015-07-24 00:28:16 -0700 | [diff] [blame] | 146 | Logger.Error(e, "Exception occured in handler."); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 147 | status = HandlerUtils.StatusFromException(e); |
| 148 | } |
Jan Tattermusch | 8c2dd9d | 2015-05-04 09:20:43 -0700 | [diff] [blame] | 149 | |
| 150 | try |
| 151 | { |
Jan Tattermusch | a0bb065 | 2015-07-20 22:34:19 -0700 | [diff] [blame] | 152 | await responseStream.WriteStatusAsync(status, context.ResponseTrailers); |
Jan Tattermusch | 8c2dd9d | 2015-05-04 09:20:43 -0700 | [diff] [blame] | 153 | } |
| 154 | catch (OperationCanceledException) |
| 155 | { |
| 156 | // Call has been already cancelled. |
| 157 | } |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 158 | await finishedTask; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | internal class ClientStreamingServerCallHandler<TRequest, TResponse> : IServerCallHandler |
Jan Tattermusch | bdb1b48 | 2015-05-06 14:46:25 -0700 | [diff] [blame] | 163 | where TRequest : class |
| 164 | where TResponse : class |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 165 | { |
Jan Tattermusch | 0526161 | 2015-07-24 00:28:16 -0700 | [diff] [blame] | 166 | static readonly ILogger Logger = GrpcEnvironment.Logger.ForType<ClientStreamingServerCallHandler<TRequest, TResponse>>(); |
| 167 | |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 168 | readonly Method<TRequest, TResponse> method; |
| 169 | readonly ClientStreamingServerMethod<TRequest, TResponse> handler; |
| 170 | |
| 171 | public ClientStreamingServerCallHandler(Method<TRequest, TResponse> method, ClientStreamingServerMethod<TRequest, TResponse> handler) |
| 172 | { |
| 173 | this.method = method; |
| 174 | this.handler = handler; |
| 175 | } |
| 176 | |
Jan Tattermusch | 5bbd818 | 2015-07-20 20:48:40 -0700 | [diff] [blame] | 177 | public async Task HandleCall(ServerRpcNew newRpc, GrpcEnvironment environment) |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 178 | { |
| 179 | var asyncCall = new AsyncCallServer<TRequest, TResponse>( |
| 180 | method.ResponseMarshaller.Serializer, |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 181 | method.RequestMarshaller.Deserializer, |
| 182 | environment); |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 183 | |
Jan Tattermusch | 5bbd818 | 2015-07-20 20:48:40 -0700 | [diff] [blame] | 184 | asyncCall.Initialize(newRpc.Call); |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 185 | var finishedTask = asyncCall.ServerSideCallAsync(); |
| 186 | var requestStream = new ServerRequestStream<TRequest, TResponse>(asyncCall); |
| 187 | var responseStream = new ServerResponseStream<TRequest, TResponse>(asyncCall); |
Jan Tattermusch | a0bb065 | 2015-07-20 22:34:19 -0700 | [diff] [blame] | 188 | |
Jan Tattermusch | 998eb9b | 2015-07-20 22:12:53 -0700 | [diff] [blame] | 189 | Status status; |
Jan Tattermusch | bff90ac | 2015-08-06 21:30:26 -0700 | [diff] [blame] | 190 | var context = HandlerUtils.NewContext(newRpc, asyncCall.Peer, responseStream, asyncCall.CancellationToken); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 191 | try |
| 192 | { |
Jan Tattermusch | 25bb2ef | 2015-07-21 12:15:53 -0700 | [diff] [blame] | 193 | var result = await handler(requestStream, context); |
Jan Tattermusch | 998eb9b | 2015-07-20 22:12:53 -0700 | [diff] [blame] | 194 | status = context.Status; |
Jan Tattermusch | 8c2dd9d | 2015-05-04 09:20:43 -0700 | [diff] [blame] | 195 | try |
| 196 | { |
Jan Tattermusch | 43dc50b | 2015-05-18 15:27:19 -0700 | [diff] [blame] | 197 | await responseStream.WriteAsync(result); |
Jan Tattermusch | 8c2dd9d | 2015-05-04 09:20:43 -0700 | [diff] [blame] | 198 | } |
| 199 | catch (OperationCanceledException) |
| 200 | { |
| 201 | status = Status.DefaultCancelled; |
| 202 | } |
| 203 | } |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 204 | catch (Exception e) |
| 205 | { |
Jan Tattermusch | 0526161 | 2015-07-24 00:28:16 -0700 | [diff] [blame] | 206 | Logger.Error(e, "Exception occured in handler."); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 207 | status = HandlerUtils.StatusFromException(e); |
| 208 | } |
Jan Tattermusch | 8c2dd9d | 2015-05-04 09:20:43 -0700 | [diff] [blame] | 209 | |
| 210 | try |
| 211 | { |
Jan Tattermusch | a0bb065 | 2015-07-20 22:34:19 -0700 | [diff] [blame] | 212 | await responseStream.WriteStatusAsync(status, context.ResponseTrailers); |
Jan Tattermusch | 8c2dd9d | 2015-05-04 09:20:43 -0700 | [diff] [blame] | 213 | } |
| 214 | catch (OperationCanceledException) |
| 215 | { |
| 216 | // Call has been already cancelled. |
| 217 | } |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 218 | await finishedTask; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | internal class DuplexStreamingServerCallHandler<TRequest, TResponse> : IServerCallHandler |
Jan Tattermusch | bdb1b48 | 2015-05-06 14:46:25 -0700 | [diff] [blame] | 223 | where TRequest : class |
| 224 | where TResponse : class |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 225 | { |
Jan Tattermusch | 0526161 | 2015-07-24 00:28:16 -0700 | [diff] [blame] | 226 | static readonly ILogger Logger = GrpcEnvironment.Logger.ForType<DuplexStreamingServerCallHandler<TRequest, TResponse>>(); |
| 227 | |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 228 | readonly Method<TRequest, TResponse> method; |
| 229 | readonly DuplexStreamingServerMethod<TRequest, TResponse> handler; |
| 230 | |
| 231 | public DuplexStreamingServerCallHandler(Method<TRequest, TResponse> method, DuplexStreamingServerMethod<TRequest, TResponse> handler) |
| 232 | { |
| 233 | this.method = method; |
| 234 | this.handler = handler; |
| 235 | } |
| 236 | |
Jan Tattermusch | 5bbd818 | 2015-07-20 20:48:40 -0700 | [diff] [blame] | 237 | public async Task HandleCall(ServerRpcNew newRpc, GrpcEnvironment environment) |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 238 | { |
| 239 | var asyncCall = new AsyncCallServer<TRequest, TResponse>( |
| 240 | method.ResponseMarshaller.Serializer, |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 241 | method.RequestMarshaller.Deserializer, |
| 242 | environment); |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 243 | |
Jan Tattermusch | 5bbd818 | 2015-07-20 20:48:40 -0700 | [diff] [blame] | 244 | asyncCall.Initialize(newRpc.Call); |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 245 | var finishedTask = asyncCall.ServerSideCallAsync(); |
| 246 | var requestStream = new ServerRequestStream<TRequest, TResponse>(asyncCall); |
| 247 | var responseStream = new ServerResponseStream<TRequest, TResponse>(asyncCall); |
| 248 | |
Jan Tattermusch | 998eb9b | 2015-07-20 22:12:53 -0700 | [diff] [blame] | 249 | Status status; |
Jan Tattermusch | bff90ac | 2015-08-06 21:30:26 -0700 | [diff] [blame] | 250 | var context = HandlerUtils.NewContext(newRpc, asyncCall.Peer, responseStream, asyncCall.CancellationToken); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 251 | try |
| 252 | { |
Jan Tattermusch | 25bb2ef | 2015-07-21 12:15:53 -0700 | [diff] [blame] | 253 | await handler(requestStream, responseStream, context); |
Jan Tattermusch | 998eb9b | 2015-07-20 22:12:53 -0700 | [diff] [blame] | 254 | status = context.Status; |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 255 | } |
| 256 | catch (Exception e) |
| 257 | { |
Jan Tattermusch | 0526161 | 2015-07-24 00:28:16 -0700 | [diff] [blame] | 258 | Logger.Error(e, "Exception occured in handler."); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 259 | status = HandlerUtils.StatusFromException(e); |
| 260 | } |
Jan Tattermusch | 8c2dd9d | 2015-05-04 09:20:43 -0700 | [diff] [blame] | 261 | try |
| 262 | { |
Jan Tattermusch | a0bb065 | 2015-07-20 22:34:19 -0700 | [diff] [blame] | 263 | await responseStream.WriteStatusAsync(status, context.ResponseTrailers); |
Jan Tattermusch | 8c2dd9d | 2015-05-04 09:20:43 -0700 | [diff] [blame] | 264 | } |
| 265 | catch (OperationCanceledException) |
| 266 | { |
| 267 | // Call has been already cancelled. |
| 268 | } |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 269 | await finishedTask; |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | |
| 273 | internal class NoSuchMethodCallHandler : IServerCallHandler |
| 274 | { |
Jan Tattermusch | 5bbd818 | 2015-07-20 20:48:40 -0700 | [diff] [blame] | 275 | public static readonly NoSuchMethodCallHandler Instance = new NoSuchMethodCallHandler(); |
| 276 | |
| 277 | public async Task HandleCall(ServerRpcNew newRpc, GrpcEnvironment environment) |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 278 | { |
| 279 | // We don't care about the payload type here. |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 280 | var asyncCall = new AsyncCallServer<byte[], byte[]>( |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 281 | (payload) => payload, (payload) => payload, environment); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 282 | |
Jan Tattermusch | 5bbd818 | 2015-07-20 20:48:40 -0700 | [diff] [blame] | 283 | asyncCall.Initialize(newRpc.Call); |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 284 | var finishedTask = asyncCall.ServerSideCallAsync(); |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 285 | var responseStream = new ServerResponseStream<byte[], byte[]>(asyncCall); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 286 | |
Jan Tattermusch | a0bb065 | 2015-07-20 22:34:19 -0700 | [diff] [blame] | 287 | await responseStream.WriteStatusAsync(new Status(StatusCode.Unimplemented, "No such method."), Metadata.Empty); |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 288 | await finishedTask; |
Jan Tattermusch | 37afb9a | 2015-02-18 11:20:04 -0800 | [diff] [blame] | 289 | } |
Jan Tattermusch | 37afb9a | 2015-02-18 11:20:04 -0800 | [diff] [blame] | 290 | } |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 291 | |
| 292 | internal static class HandlerUtils |
| 293 | { |
| 294 | public static Status StatusFromException(Exception e) |
| 295 | { |
Jan Tattermusch | 8271f5d | 2015-07-20 22:48:15 -0700 | [diff] [blame] | 296 | var rpcException = e as RpcException; |
| 297 | if (rpcException != null) |
| 298 | { |
| 299 | // use the status thrown by handler. |
| 300 | return rpcException.Status; |
| 301 | } |
| 302 | |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 303 | // TODO(jtattermusch): what is the right status code here? |
| 304 | return new Status(StatusCode.Unknown, "Exception was thrown by handler."); |
| 305 | } |
Jan Tattermusch | 998eb9b | 2015-07-20 22:12:53 -0700 | [diff] [blame] | 306 | |
Jan Tattermusch | 8368b2e | 2015-08-07 01:18:37 -0700 | [diff] [blame^] | 307 | public static ServerCallContext NewContext<TRequest, TResponse>(ServerRpcNew newRpc, string peer, ServerResponseStream<TRequest, TResponse> serverResponseStream, CancellationToken cancellationToken) |
| 308 | where TRequest : class |
| 309 | where TResponse : class |
Jan Tattermusch | 998eb9b | 2015-07-20 22:12:53 -0700 | [diff] [blame] | 310 | { |
Jan Tattermusch | 7a3ac62 | 2015-07-23 18:40:48 -0700 | [diff] [blame] | 311 | DateTime realtimeDeadline = newRpc.Deadline.ToClockType(GPRClockType.Realtime).ToDateTime(); |
| 312 | |
Jan Tattermusch | 998eb9b | 2015-07-20 22:12:53 -0700 | [diff] [blame] | 313 | return new ServerCallContext( |
Jan Tattermusch | 062c329 | 2015-07-23 20:28:42 -0700 | [diff] [blame] | 314 | newRpc.Method, newRpc.Host, peer, realtimeDeadline, |
Jan Tattermusch | 8368b2e | 2015-08-07 01:18:37 -0700 | [diff] [blame^] | 315 | newRpc.RequestMetadata, cancellationToken, serverResponseStream.WriteResponseHeadersAsync, serverResponseStream); |
Jan Tattermusch | 998eb9b | 2015-07-20 22:12:53 -0700 | [diff] [blame] | 316 | } |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 317 | } |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 318 | } |