blob: 19f0e3c57f6c4043f8c94de7261ee8b2529d553a [file] [log] [blame]
Jan Tattermuscha7fff862015-02-13 11:08:08 -08001#region Copyright notice and license
2
Jan Tattermuschaf77b3d2015-02-13 11:22:21 -08003// Copyright 2015, Google Inc.
Jan Tattermuscha7fff862015-02-13 11:08:08 -08004// All rights reserved.
Craig Tiller190d3602015-02-18 09:23:38 -08005//
Jan Tattermuscha7fff862015-02-13 11:08:08 -08006// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met:
Craig Tiller190d3602015-02-18 09:23:38 -08009//
Jan Tattermuscha7fff862015-02-13 11:08:08 -080010// * 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 Tiller190d3602015-02-18 09:23:38 -080019//
Jan Tattermuscha7fff862015-02-13 11:08:08 -080020// 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 Tattermusch8ce5e8b2015-02-05 10:56:49 -080034using System;
Jan Tattermusch7ca61792015-05-18 14:28:22 -070035using System.Collections.Generic;
Jan Tattermuscha29d0f32015-03-04 17:54:56 -080036using System.Linq;
Jan Tattermusch998eb9b2015-07-20 22:12:53 -070037using System.Threading;
Jan Tattermuscha5272b62015-04-30 11:56:46 -070038using System.Threading.Tasks;
Jan Tattermusch30868622015-02-19 09:22:33 -080039using Grpc.Core.Internal;
Jan Tattermusch05261612015-07-24 00:28:16 -070040using Grpc.Core.Logging;
Jan Tattermuscha29d0f32015-03-04 17:54:56 -080041using Grpc.Core.Utils;
Jan Tattermusch8ce5e8b2015-02-05 10:56:49 -080042
Jan Tattermusch286975f2015-03-12 14:04:36 -070043namespace Grpc.Core.Internal
Jan Tattermusch8ce5e8b2015-02-05 10:56:49 -080044{
45 internal interface IServerCallHandler
46 {
Jan Tattermusch5bbd8182015-07-20 20:48:40 -070047 Task HandleCall(ServerRpcNew newRpc, GrpcEnvironment environment);
Jan Tattermusch8ce5e8b2015-02-05 10:56:49 -080048 }
49
Jan Tattermuscha5272b62015-04-30 11:56:46 -070050 internal class UnaryServerCallHandler<TRequest, TResponse> : IServerCallHandler
Jan Tattermuschbdb1b482015-05-06 14:46:25 -070051 where TRequest : class
52 where TResponse : class
Jan Tattermusch8ce5e8b2015-02-05 10:56:49 -080053 {
Jan Tattermusch05261612015-07-24 00:28:16 -070054 static readonly ILogger Logger = GrpcEnvironment.Logger.ForType<UnaryServerCallHandler<TRequest, TResponse>>();
55
Jan Tattermusch8ce5e8b2015-02-05 10:56:49 -080056 readonly Method<TRequest, TResponse> method;
Jan Tattermuscha5272b62015-04-30 11:56:46 -070057 readonly UnaryServerMethod<TRequest, TResponse> handler;
Jan Tattermusch8ce5e8b2015-02-05 10:56:49 -080058
Jan Tattermuscha5272b62015-04-30 11:56:46 -070059 public UnaryServerCallHandler(Method<TRequest, TResponse> method, UnaryServerMethod<TRequest, TResponse> handler)
Jan Tattermusch8ce5e8b2015-02-05 10:56:49 -080060 {
61 this.method = method;
62 this.handler = handler;
63 }
64
Jan Tattermusch5bbd8182015-07-20 20:48:40 -070065 public async Task HandleCall(ServerRpcNew newRpc, GrpcEnvironment environment)
Jan Tattermusch8ce5e8b2015-02-05 10:56:49 -080066 {
Jan Tattermuscha29d0f32015-03-04 17:54:56 -080067 var asyncCall = new AsyncCallServer<TRequest, TResponse>(
Jan Tattermusch15111f52015-02-05 18:15:14 -080068 method.ResponseMarshaller.Serializer,
Jan Tattermusch04eb89c2015-06-12 13:03:05 -070069 method.RequestMarshaller.Deserializer,
70 environment);
Jan Tattermusch8ce5e8b2015-02-05 10:56:49 -080071
Jan Tattermusch5bbd8182015-07-20 20:48:40 -070072 asyncCall.Initialize(newRpc.Call);
Jan Tattermuscha5272b62015-04-30 11:56:46 -070073 var finishedTask = asyncCall.ServerSideCallAsync();
74 var requestStream = new ServerRequestStream<TRequest, TResponse>(asyncCall);
75 var responseStream = new ServerResponseStream<TRequest, TResponse>(asyncCall);
Craig Tiller190d3602015-02-18 09:23:38 -080076
Jan Tattermusch998eb9b2015-07-20 22:12:53 -070077 Status status;
Jan Tattermusch062c3292015-07-23 20:28:42 -070078 var context = HandlerUtils.NewContext(newRpc, asyncCall.Peer, asyncCall.CancellationToken);
Jan Tattermusche5c44602015-05-01 11:12:34 -070079 try
80 {
Jan Tattermusch7ca61792015-05-18 14:28:22 -070081 Preconditions.CheckArgument(await requestStream.MoveNext());
82 var request = requestStream.Current;
Jan Tattermusche5c44602015-05-01 11:12:34 -070083 // TODO(jtattermusch): we need to read the full stream so that native callhandle gets deallocated.
Jan Tattermusch7ca61792015-05-18 14:28:22 -070084 Preconditions.CheckArgument(!await requestStream.MoveNext());
Jan Tattermusch25bb2ef2015-07-21 12:15:53 -070085 var result = await handler(request, context);
Jan Tattermusch998eb9b2015-07-20 22:12:53 -070086 status = context.Status;
Jan Tattermusch43dc50b2015-05-18 15:27:19 -070087 await responseStream.WriteAsync(result);
Jan Tattermusche5c44602015-05-01 11:12:34 -070088 }
89 catch (Exception e)
90 {
Jan Tattermusch05261612015-07-24 00:28:16 -070091 Logger.Error(e, "Exception occured in handler.");
Jan Tattermusche5c44602015-05-01 11:12:34 -070092 status = HandlerUtils.StatusFromException(e);
93 }
Jan Tattermusch8c2dd9d2015-05-04 09:20:43 -070094 try
95 {
Jan Tattermuscha0bb0652015-07-20 22:34:19 -070096 await responseStream.WriteStatusAsync(status, context.ResponseTrailers);
Jan Tattermusch8c2dd9d2015-05-04 09:20:43 -070097 }
98 catch (OperationCanceledException)
99 {
100 // Call has been already cancelled.
101 }
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700102 await finishedTask;
Jan Tattermusch8ce5e8b2015-02-05 10:56:49 -0800103 }
104 }
105
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700106 internal class ServerStreamingServerCallHandler<TRequest, TResponse> : IServerCallHandler
Jan Tattermuschbdb1b482015-05-06 14:46:25 -0700107 where TRequest : class
108 where TResponse : class
Jan Tattermusch8ce5e8b2015-02-05 10:56:49 -0800109 {
Jan Tattermusch05261612015-07-24 00:28:16 -0700110 static readonly ILogger Logger = GrpcEnvironment.Logger.ForType<ServerStreamingServerCallHandler<TRequest, TResponse>>();
111
Jan Tattermusch8ce5e8b2015-02-05 10:56:49 -0800112 readonly Method<TRequest, TResponse> method;
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700113 readonly ServerStreamingServerMethod<TRequest, TResponse> handler;
Jan Tattermusch8ce5e8b2015-02-05 10:56:49 -0800114
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700115 public ServerStreamingServerCallHandler(Method<TRequest, TResponse> method, ServerStreamingServerMethod<TRequest, TResponse> handler)
Jan Tattermusch8ce5e8b2015-02-05 10:56:49 -0800116 {
117 this.method = method;
118 this.handler = handler;
119 }
120
Jan Tattermusch5bbd8182015-07-20 20:48:40 -0700121 public async Task HandleCall(ServerRpcNew newRpc, GrpcEnvironment environment)
Jan Tattermusch8ce5e8b2015-02-05 10:56:49 -0800122 {
Jan Tattermuscha29d0f32015-03-04 17:54:56 -0800123 var asyncCall = new AsyncCallServer<TRequest, TResponse>(
Jan Tattermusch15111f52015-02-05 18:15:14 -0800124 method.ResponseMarshaller.Serializer,
Jan Tattermusch04eb89c2015-06-12 13:03:05 -0700125 method.RequestMarshaller.Deserializer,
126 environment);
Jan Tattermusch8ce5e8b2015-02-05 10:56:49 -0800127
Jan Tattermusch5bbd8182015-07-20 20:48:40 -0700128 asyncCall.Initialize(newRpc.Call);
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700129 var finishedTask = asyncCall.ServerSideCallAsync();
130 var requestStream = new ServerRequestStream<TRequest, TResponse>(asyncCall);
131 var responseStream = new ServerResponseStream<TRequest, TResponse>(asyncCall);
Jan Tattermusch8ce5e8b2015-02-05 10:56:49 -0800132
Jan Tattermusch998eb9b2015-07-20 22:12:53 -0700133 Status status;
Jan Tattermusch062c3292015-07-23 20:28:42 -0700134 var context = HandlerUtils.NewContext(newRpc, asyncCall.Peer, asyncCall.CancellationToken);
Jan Tattermusche5c44602015-05-01 11:12:34 -0700135 try
136 {
Jan Tattermusch7ca61792015-05-18 14:28:22 -0700137 Preconditions.CheckArgument(await requestStream.MoveNext());
138 var request = requestStream.Current;
Jan Tattermusche5c44602015-05-01 11:12:34 -0700139 // TODO(jtattermusch): we need to read the full stream so that native callhandle gets deallocated.
Jan Tattermusch7ca61792015-05-18 14:28:22 -0700140 Preconditions.CheckArgument(!await requestStream.MoveNext());
Jan Tattermusch25bb2ef2015-07-21 12:15:53 -0700141 await handler(request, responseStream, context);
Jan Tattermusch998eb9b2015-07-20 22:12:53 -0700142 status = context.Status;
Jan Tattermusche5c44602015-05-01 11:12:34 -0700143 }
144 catch (Exception e)
145 {
Jan Tattermusch05261612015-07-24 00:28:16 -0700146 Logger.Error(e, "Exception occured in handler.");
Jan Tattermusche5c44602015-05-01 11:12:34 -0700147 status = HandlerUtils.StatusFromException(e);
148 }
Jan Tattermusch8c2dd9d2015-05-04 09:20:43 -0700149
150 try
151 {
Jan Tattermuscha0bb0652015-07-20 22:34:19 -0700152 await responseStream.WriteStatusAsync(status, context.ResponseTrailers);
Jan Tattermusch8c2dd9d2015-05-04 09:20:43 -0700153 }
154 catch (OperationCanceledException)
155 {
156 // Call has been already cancelled.
157 }
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700158 await finishedTask;
159 }
160 }
161
162 internal class ClientStreamingServerCallHandler<TRequest, TResponse> : IServerCallHandler
Jan Tattermuschbdb1b482015-05-06 14:46:25 -0700163 where TRequest : class
164 where TResponse : class
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700165 {
Jan Tattermusch05261612015-07-24 00:28:16 -0700166 static readonly ILogger Logger = GrpcEnvironment.Logger.ForType<ClientStreamingServerCallHandler<TRequest, TResponse>>();
167
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700168 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 Tattermusch5bbd8182015-07-20 20:48:40 -0700177 public async Task HandleCall(ServerRpcNew newRpc, GrpcEnvironment environment)
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700178 {
179 var asyncCall = new AsyncCallServer<TRequest, TResponse>(
180 method.ResponseMarshaller.Serializer,
Jan Tattermusch04eb89c2015-06-12 13:03:05 -0700181 method.RequestMarshaller.Deserializer,
182 environment);
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700183
Jan Tattermusch5bbd8182015-07-20 20:48:40 -0700184 asyncCall.Initialize(newRpc.Call);
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700185 var finishedTask = asyncCall.ServerSideCallAsync();
186 var requestStream = new ServerRequestStream<TRequest, TResponse>(asyncCall);
187 var responseStream = new ServerResponseStream<TRequest, TResponse>(asyncCall);
Jan Tattermuscha0bb0652015-07-20 22:34:19 -0700188
Jan Tattermusch998eb9b2015-07-20 22:12:53 -0700189 Status status;
Jan Tattermusch062c3292015-07-23 20:28:42 -0700190 var context = HandlerUtils.NewContext(newRpc, asyncCall.Peer, asyncCall.CancellationToken);
Jan Tattermusche5c44602015-05-01 11:12:34 -0700191 try
192 {
Jan Tattermusch25bb2ef2015-07-21 12:15:53 -0700193 var result = await handler(requestStream, context);
Jan Tattermusch998eb9b2015-07-20 22:12:53 -0700194 status = context.Status;
Jan Tattermusch8c2dd9d2015-05-04 09:20:43 -0700195 try
196 {
Jan Tattermusch43dc50b2015-05-18 15:27:19 -0700197 await responseStream.WriteAsync(result);
Jan Tattermusch8c2dd9d2015-05-04 09:20:43 -0700198 }
199 catch (OperationCanceledException)
200 {
201 status = Status.DefaultCancelled;
202 }
203 }
Jan Tattermusche5c44602015-05-01 11:12:34 -0700204 catch (Exception e)
205 {
Jan Tattermusch05261612015-07-24 00:28:16 -0700206 Logger.Error(e, "Exception occured in handler.");
Jan Tattermusche5c44602015-05-01 11:12:34 -0700207 status = HandlerUtils.StatusFromException(e);
208 }
Jan Tattermusch8c2dd9d2015-05-04 09:20:43 -0700209
210 try
211 {
Jan Tattermuscha0bb0652015-07-20 22:34:19 -0700212 await responseStream.WriteStatusAsync(status, context.ResponseTrailers);
Jan Tattermusch8c2dd9d2015-05-04 09:20:43 -0700213 }
214 catch (OperationCanceledException)
215 {
216 // Call has been already cancelled.
217 }
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700218 await finishedTask;
219 }
220 }
221
222 internal class DuplexStreamingServerCallHandler<TRequest, TResponse> : IServerCallHandler
Jan Tattermuschbdb1b482015-05-06 14:46:25 -0700223 where TRequest : class
224 where TResponse : class
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700225 {
Jan Tattermusch05261612015-07-24 00:28:16 -0700226 static readonly ILogger Logger = GrpcEnvironment.Logger.ForType<DuplexStreamingServerCallHandler<TRequest, TResponse>>();
227
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700228 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 Tattermusch5bbd8182015-07-20 20:48:40 -0700237 public async Task HandleCall(ServerRpcNew newRpc, GrpcEnvironment environment)
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700238 {
239 var asyncCall = new AsyncCallServer<TRequest, TResponse>(
240 method.ResponseMarshaller.Serializer,
Jan Tattermusch04eb89c2015-06-12 13:03:05 -0700241 method.RequestMarshaller.Deserializer,
242 environment);
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700243
Jan Tattermusch5bbd8182015-07-20 20:48:40 -0700244 asyncCall.Initialize(newRpc.Call);
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700245 var finishedTask = asyncCall.ServerSideCallAsync();
246 var requestStream = new ServerRequestStream<TRequest, TResponse>(asyncCall);
247 var responseStream = new ServerResponseStream<TRequest, TResponse>(asyncCall);
248
Jan Tattermusch998eb9b2015-07-20 22:12:53 -0700249 Status status;
Jan Tattermusch062c3292015-07-23 20:28:42 -0700250 var context = HandlerUtils.NewContext(newRpc, asyncCall.Peer, asyncCall.CancellationToken);
Jan Tattermusche5c44602015-05-01 11:12:34 -0700251 try
252 {
Jan Tattermusch25bb2ef2015-07-21 12:15:53 -0700253 await handler(requestStream, responseStream, context);
Jan Tattermusch998eb9b2015-07-20 22:12:53 -0700254 status = context.Status;
Jan Tattermusche5c44602015-05-01 11:12:34 -0700255 }
256 catch (Exception e)
257 {
Jan Tattermusch05261612015-07-24 00:28:16 -0700258 Logger.Error(e, "Exception occured in handler.");
Jan Tattermusche5c44602015-05-01 11:12:34 -0700259 status = HandlerUtils.StatusFromException(e);
260 }
Jan Tattermusch8c2dd9d2015-05-04 09:20:43 -0700261 try
262 {
Jan Tattermuscha0bb0652015-07-20 22:34:19 -0700263 await responseStream.WriteStatusAsync(status, context.ResponseTrailers);
Jan Tattermusch8c2dd9d2015-05-04 09:20:43 -0700264 }
265 catch (OperationCanceledException)
266 {
267 // Call has been already cancelled.
268 }
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700269 await finishedTask;
Jan Tattermusch8ce5e8b2015-02-05 10:56:49 -0800270 }
271 }
272
273 internal class NoSuchMethodCallHandler : IServerCallHandler
274 {
Jan Tattermusch5bbd8182015-07-20 20:48:40 -0700275 public static readonly NoSuchMethodCallHandler Instance = new NoSuchMethodCallHandler();
276
277 public async Task HandleCall(ServerRpcNew newRpc, GrpcEnvironment environment)
Jan Tattermusch8ce5e8b2015-02-05 10:56:49 -0800278 {
279 // We don't care about the payload type here.
Jan Tattermuscha29d0f32015-03-04 17:54:56 -0800280 var asyncCall = new AsyncCallServer<byte[], byte[]>(
Jan Tattermusch04eb89c2015-06-12 13:03:05 -0700281 (payload) => payload, (payload) => payload, environment);
Jan Tattermusche5c44602015-05-01 11:12:34 -0700282
Jan Tattermusch5bbd8182015-07-20 20:48:40 -0700283 asyncCall.Initialize(newRpc.Call);
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700284 var finishedTask = asyncCall.ServerSideCallAsync();
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700285 var responseStream = new ServerResponseStream<byte[], byte[]>(asyncCall);
Jan Tattermusche5c44602015-05-01 11:12:34 -0700286
Jan Tattermuscha0bb0652015-07-20 22:34:19 -0700287 await responseStream.WriteStatusAsync(new Status(StatusCode.Unimplemented, "No such method."), Metadata.Empty);
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700288 await finishedTask;
Jan Tattermusch37afb9a2015-02-18 11:20:04 -0800289 }
Jan Tattermusch37afb9a2015-02-18 11:20:04 -0800290 }
Jan Tattermusche5c44602015-05-01 11:12:34 -0700291
292 internal static class HandlerUtils
293 {
294 public static Status StatusFromException(Exception e)
295 {
Jan Tattermusch8271f5d2015-07-20 22:48:15 -0700296 var rpcException = e as RpcException;
297 if (rpcException != null)
298 {
299 // use the status thrown by handler.
300 return rpcException.Status;
301 }
302
Jan Tattermusche5c44602015-05-01 11:12:34 -0700303 // TODO(jtattermusch): what is the right status code here?
304 return new Status(StatusCode.Unknown, "Exception was thrown by handler.");
305 }
Jan Tattermusch998eb9b2015-07-20 22:12:53 -0700306
Jan Tattermusch062c3292015-07-23 20:28:42 -0700307 public static ServerCallContext NewContext(ServerRpcNew newRpc, string peer, CancellationToken cancellationToken)
Jan Tattermusch998eb9b2015-07-20 22:12:53 -0700308 {
Jan Tattermusch7a3ac622015-07-23 18:40:48 -0700309 DateTime realtimeDeadline = newRpc.Deadline.ToClockType(GPRClockType.Realtime).ToDateTime();
310
Jan Tattermusch998eb9b2015-07-20 22:12:53 -0700311 return new ServerCallContext(
Jan Tattermusch062c3292015-07-23 20:28:42 -0700312 newRpc.Method, newRpc.Host, peer, realtimeDeadline,
Jan Tattermusch0846b682015-07-23 17:02:12 -0700313 newRpc.RequestMetadata, cancellationToken);
Jan Tattermusch998eb9b2015-07-20 22:12:53 -0700314 }
Jan Tattermusche5c44602015-05-01 11:12:34 -0700315 }
Jan Tattermusch8ce5e8b2015-02-05 10:56:49 -0800316}