Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | // Copyright 2015 gRPC authors. |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 4 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 8 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 10 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 16 | |
| 17 | #endregion |
| 18 | |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 19 | using System; |
| 20 | using System.Collections.Generic; |
Jan Tattermusch | dc64fb5 | 2015-08-04 18:45:40 -0700 | [diff] [blame] | 21 | using System.Collections.ObjectModel; |
Mehrdad Afshari | 4df68ae | 2018-02-11 20:11:07 -0800 | [diff] [blame] | 22 | using System.Linq; |
| 23 | using Grpc.Core.Interceptors; |
Jan Tattermusch | 286975f | 2015-03-12 14:04:36 -0700 | [diff] [blame] | 24 | using Grpc.Core.Internal; |
Mehrdad Afshari | 4df68ae | 2018-02-11 20:11:07 -0800 | [diff] [blame] | 25 | using Grpc.Core.Utils; |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 26 | |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 27 | namespace Grpc.Core |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 28 | { |
Jan Tattermusch | 286975f | 2015-03-12 14:04:36 -0700 | [diff] [blame] | 29 | /// <summary> |
| 30 | /// Mapping of method names to server call handlers. |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 31 | /// Normally, the <c>ServerServiceDefinition</c> objects will be created by the <c>BindService</c> factory method |
| 32 | /// that is part of the autogenerated code for a protocol buffers service definition. |
Jan Tattermusch | 286975f | 2015-03-12 14:04:36 -0700 | [diff] [blame] | 33 | /// </summary> |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 34 | public class ServerServiceDefinition |
| 35 | { |
Jan Tattermusch | dc64fb5 | 2015-08-04 18:45:40 -0700 | [diff] [blame] | 36 | readonly ReadOnlyDictionary<string, IServerCallHandler> callHandlers; |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 37 | |
Mehrdad Afshari | cd918d9 | 2018-02-21 10:32:01 -0800 | [diff] [blame] | 38 | internal ServerServiceDefinition(Dictionary<string, IServerCallHandler> callHandlers) |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 39 | { |
Jan Tattermusch | dc64fb5 | 2015-08-04 18:45:40 -0700 | [diff] [blame] | 40 | this.callHandlers = new ReadOnlyDictionary<string, IServerCallHandler>(callHandlers); |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 41 | } |
| 42 | |
Jan Tattermusch | dc64fb5 | 2015-08-04 18:45:40 -0700 | [diff] [blame] | 43 | internal IDictionary<string, IServerCallHandler> CallHandlers |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 44 | { |
| 45 | get |
| 46 | { |
| 47 | return this.callHandlers; |
| 48 | } |
| 49 | } |
| 50 | |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 51 | /// <summary> |
| 52 | /// Creates a new builder object for <c>ServerServiceDefinition</c>. |
| 53 | /// </summary> |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 54 | /// <returns>The builder object.</returns> |
Jan Tattermusch | 781720f | 2016-06-03 17:40:28 -0700 | [diff] [blame] | 55 | public static Builder CreateBuilder() |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 56 | { |
Jan Tattermusch | 781720f | 2016-06-03 17:40:28 -0700 | [diff] [blame] | 57 | return new Builder(); |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 60 | /// <summary> |
| 61 | /// Builder class for <see cref="ServerServiceDefinition"/>. |
| 62 | /// </summary> |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 63 | public class Builder |
| 64 | { |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 65 | readonly Dictionary<string, IServerCallHandler> callHandlers = new Dictionary<string, IServerCallHandler>(); |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 66 | |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 67 | /// <summary> |
| 68 | /// Creates a new instance of builder. |
| 69 | /// </summary> |
Jan Tattermusch | 781720f | 2016-06-03 17:40:28 -0700 | [diff] [blame] | 70 | public Builder() |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 71 | { |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 74 | /// <summary> |
| 75 | /// Adds a definitions for a single request - single response method. |
| 76 | /// </summary> |
| 77 | /// <typeparam name="TRequest">The request message class.</typeparam> |
| 78 | /// <typeparam name="TResponse">The response message class.</typeparam> |
| 79 | /// <param name="method">The method.</param> |
| 80 | /// <param name="handler">The method handler.</param> |
| 81 | /// <returns>This builder instance.</returns> |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 82 | public Builder AddMethod<TRequest, TResponse>( |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 83 | Method<TRequest, TResponse> method, |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 84 | UnaryServerMethod<TRequest, TResponse> handler) |
Jan Tattermusch | bdb1b48 | 2015-05-06 14:46:25 -0700 | [diff] [blame] | 85 | where TRequest : class |
| 86 | where TResponse : class |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 87 | { |
Jan Tattermusch | 66eb18d | 2015-08-09 20:03:24 -0700 | [diff] [blame] | 88 | callHandlers.Add(method.FullName, ServerCalls.UnaryCall(method, handler)); |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 89 | return this; |
| 90 | } |
| 91 | |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 92 | /// <summary> |
| 93 | /// Adds a definitions for a client streaming method. |
| 94 | /// </summary> |
| 95 | /// <typeparam name="TRequest">The request message class.</typeparam> |
| 96 | /// <typeparam name="TResponse">The response message class.</typeparam> |
| 97 | /// <param name="method">The method.</param> |
| 98 | /// <param name="handler">The method handler.</param> |
| 99 | /// <returns>This builder instance.</returns> |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 100 | public Builder AddMethod<TRequest, TResponse>( |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 101 | Method<TRequest, TResponse> method, |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 102 | ClientStreamingServerMethod<TRequest, TResponse> handler) |
Jan Tattermusch | bdb1b48 | 2015-05-06 14:46:25 -0700 | [diff] [blame] | 103 | where TRequest : class |
| 104 | where TResponse : class |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 105 | { |
Jan Tattermusch | 66eb18d | 2015-08-09 20:03:24 -0700 | [diff] [blame] | 106 | callHandlers.Add(method.FullName, ServerCalls.ClientStreamingCall(method, handler)); |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 107 | return this; |
| 108 | } |
| 109 | |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 110 | /// <summary> |
| 111 | /// Adds a definitions for a server streaming method. |
| 112 | /// </summary> |
| 113 | /// <typeparam name="TRequest">The request message class.</typeparam> |
| 114 | /// <typeparam name="TResponse">The response message class.</typeparam> |
| 115 | /// <param name="method">The method.</param> |
| 116 | /// <param name="handler">The method handler.</param> |
| 117 | /// <returns>This builder instance.</returns> |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 118 | public Builder AddMethod<TRequest, TResponse>( |
| 119 | Method<TRequest, TResponse> method, |
| 120 | ServerStreamingServerMethod<TRequest, TResponse> handler) |
Jan Tattermusch | bdb1b48 | 2015-05-06 14:46:25 -0700 | [diff] [blame] | 121 | where TRequest : class |
| 122 | where TResponse : class |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 123 | { |
Jan Tattermusch | 66eb18d | 2015-08-09 20:03:24 -0700 | [diff] [blame] | 124 | callHandlers.Add(method.FullName, ServerCalls.ServerStreamingCall(method, handler)); |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 125 | return this; |
| 126 | } |
| 127 | |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 128 | /// <summary> |
| 129 | /// Adds a definitions for a bidirectional streaming method. |
| 130 | /// </summary> |
| 131 | /// <typeparam name="TRequest">The request message class.</typeparam> |
| 132 | /// <typeparam name="TResponse">The response message class.</typeparam> |
| 133 | /// <param name="method">The method.</param> |
| 134 | /// <param name="handler">The method handler.</param> |
| 135 | /// <returns>This builder instance.</returns> |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 136 | public Builder AddMethod<TRequest, TResponse>( |
| 137 | Method<TRequest, TResponse> method, |
| 138 | DuplexStreamingServerMethod<TRequest, TResponse> handler) |
Jan Tattermusch | bdb1b48 | 2015-05-06 14:46:25 -0700 | [diff] [blame] | 139 | where TRequest : class |
| 140 | where TResponse : class |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 141 | { |
Jan Tattermusch | 66eb18d | 2015-08-09 20:03:24 -0700 | [diff] [blame] | 142 | callHandlers.Add(method.FullName, ServerCalls.DuplexStreamingCall(method, handler)); |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 143 | return this; |
| 144 | } |
| 145 | |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 146 | /// <summary> |
| 147 | /// Creates an immutable <c>ServerServiceDefinition</c> from this builder. |
| 148 | /// </summary> |
| 149 | /// <returns>The <c>ServerServiceDefinition</c> object.</returns> |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 150 | public ServerServiceDefinition Build() |
| 151 | { |
Jan Tattermusch | dc64fb5 | 2015-08-04 18:45:40 -0700 | [diff] [blame] | 152 | return new ServerServiceDefinition(callHandlers); |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 153 | } |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | } |