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; |
Jan Tattermusch | 286975f | 2015-03-12 14:04:36 -0700 | [diff] [blame] | 22 | using Grpc.Core.Internal; |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 23 | |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 24 | namespace Grpc.Core |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 25 | { |
Jan Tattermusch | 286975f | 2015-03-12 14:04:36 -0700 | [diff] [blame] | 26 | /// <summary> |
| 27 | /// Mapping of method names to server call handlers. |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 28 | /// Normally, the <c>ServerServiceDefinition</c> objects will be created by the <c>BindService</c> factory method |
| 29 | /// 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] | 30 | /// </summary> |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 31 | public class ServerServiceDefinition |
| 32 | { |
Jan Tattermusch | dc64fb5 | 2015-08-04 18:45:40 -0700 | [diff] [blame] | 33 | readonly ReadOnlyDictionary<string, IServerCallHandler> callHandlers; |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 34 | |
Jan Tattermusch | dc64fb5 | 2015-08-04 18:45:40 -0700 | [diff] [blame] | 35 | private ServerServiceDefinition(Dictionary<string, IServerCallHandler> callHandlers) |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 36 | { |
Jan Tattermusch | dc64fb5 | 2015-08-04 18:45:40 -0700 | [diff] [blame] | 37 | this.callHandlers = new ReadOnlyDictionary<string, IServerCallHandler>(callHandlers); |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Jan Tattermusch | dc64fb5 | 2015-08-04 18:45:40 -0700 | [diff] [blame] | 40 | internal IDictionary<string, IServerCallHandler> CallHandlers |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 41 | { |
| 42 | get |
| 43 | { |
| 44 | return this.callHandlers; |
| 45 | } |
| 46 | } |
| 47 | |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 48 | /// <summary> |
| 49 | /// Creates a new builder object for <c>ServerServiceDefinition</c>. |
| 50 | /// </summary> |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 51 | /// <returns>The builder object.</returns> |
Jan Tattermusch | 781720f | 2016-06-03 17:40:28 -0700 | [diff] [blame] | 52 | public static Builder CreateBuilder() |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 53 | { |
Jan Tattermusch | 781720f | 2016-06-03 17:40:28 -0700 | [diff] [blame] | 54 | return new Builder(); |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 55 | } |
| 56 | |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 57 | /// <summary> |
| 58 | /// Builder class for <see cref="ServerServiceDefinition"/>. |
| 59 | /// </summary> |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 60 | public class Builder |
| 61 | { |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 62 | readonly Dictionary<string, IServerCallHandler> callHandlers = new Dictionary<string, IServerCallHandler>(); |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 63 | |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 64 | /// <summary> |
| 65 | /// Creates a new instance of builder. |
| 66 | /// </summary> |
Jan Tattermusch | 781720f | 2016-06-03 17:40:28 -0700 | [diff] [blame] | 67 | public Builder() |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 68 | { |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 71 | /// <summary> |
| 72 | /// Adds a definitions for a single request - single response method. |
| 73 | /// </summary> |
| 74 | /// <typeparam name="TRequest">The request message class.</typeparam> |
| 75 | /// <typeparam name="TResponse">The response message class.</typeparam> |
| 76 | /// <param name="method">The method.</param> |
| 77 | /// <param name="handler">The method handler.</param> |
| 78 | /// <returns>This builder instance.</returns> |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 79 | public Builder AddMethod<TRequest, TResponse>( |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 80 | Method<TRequest, TResponse> method, |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 81 | UnaryServerMethod<TRequest, TResponse> handler) |
Jan Tattermusch | bdb1b48 | 2015-05-06 14:46:25 -0700 | [diff] [blame] | 82 | where TRequest : class |
| 83 | where TResponse : class |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 84 | { |
Jan Tattermusch | 66eb18d | 2015-08-09 20:03:24 -0700 | [diff] [blame] | 85 | callHandlers.Add(method.FullName, ServerCalls.UnaryCall(method, handler)); |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 86 | return this; |
| 87 | } |
| 88 | |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 89 | /// <summary> |
| 90 | /// Adds a definitions for a client streaming method. |
| 91 | /// </summary> |
| 92 | /// <typeparam name="TRequest">The request message class.</typeparam> |
| 93 | /// <typeparam name="TResponse">The response message class.</typeparam> |
| 94 | /// <param name="method">The method.</param> |
| 95 | /// <param name="handler">The method handler.</param> |
| 96 | /// <returns>This builder instance.</returns> |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 97 | public Builder AddMethod<TRequest, TResponse>( |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 98 | Method<TRequest, TResponse> method, |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 99 | ClientStreamingServerMethod<TRequest, TResponse> handler) |
Jan Tattermusch | bdb1b48 | 2015-05-06 14:46:25 -0700 | [diff] [blame] | 100 | where TRequest : class |
| 101 | where TResponse : class |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 102 | { |
Jan Tattermusch | 66eb18d | 2015-08-09 20:03:24 -0700 | [diff] [blame] | 103 | callHandlers.Add(method.FullName, ServerCalls.ClientStreamingCall(method, handler)); |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 104 | return this; |
| 105 | } |
| 106 | |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 107 | /// <summary> |
| 108 | /// Adds a definitions for a server streaming method. |
| 109 | /// </summary> |
| 110 | /// <typeparam name="TRequest">The request message class.</typeparam> |
| 111 | /// <typeparam name="TResponse">The response message class.</typeparam> |
| 112 | /// <param name="method">The method.</param> |
| 113 | /// <param name="handler">The method handler.</param> |
| 114 | /// <returns>This builder instance.</returns> |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 115 | public Builder AddMethod<TRequest, TResponse>( |
| 116 | Method<TRequest, TResponse> method, |
| 117 | ServerStreamingServerMethod<TRequest, TResponse> handler) |
Jan Tattermusch | bdb1b48 | 2015-05-06 14:46:25 -0700 | [diff] [blame] | 118 | where TRequest : class |
| 119 | where TResponse : class |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 120 | { |
Jan Tattermusch | 66eb18d | 2015-08-09 20:03:24 -0700 | [diff] [blame] | 121 | callHandlers.Add(method.FullName, ServerCalls.ServerStreamingCall(method, handler)); |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 122 | return this; |
| 123 | } |
| 124 | |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 125 | /// <summary> |
| 126 | /// Adds a definitions for a bidirectional streaming method. |
| 127 | /// </summary> |
| 128 | /// <typeparam name="TRequest">The request message class.</typeparam> |
| 129 | /// <typeparam name="TResponse">The response message class.</typeparam> |
| 130 | /// <param name="method">The method.</param> |
| 131 | /// <param name="handler">The method handler.</param> |
| 132 | /// <returns>This builder instance.</returns> |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 133 | public Builder AddMethod<TRequest, TResponse>( |
| 134 | Method<TRequest, TResponse> method, |
| 135 | DuplexStreamingServerMethod<TRequest, TResponse> handler) |
Jan Tattermusch | bdb1b48 | 2015-05-06 14:46:25 -0700 | [diff] [blame] | 136 | where TRequest : class |
| 137 | where TResponse : class |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 138 | { |
Jan Tattermusch | 66eb18d | 2015-08-09 20:03:24 -0700 | [diff] [blame] | 139 | callHandlers.Add(method.FullName, ServerCalls.DuplexStreamingCall(method, handler)); |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 140 | return this; |
| 141 | } |
| 142 | |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 143 | /// <summary> |
| 144 | /// Creates an immutable <c>ServerServiceDefinition</c> from this builder. |
| 145 | /// </summary> |
| 146 | /// <returns>The <c>ServerServiceDefinition</c> object.</returns> |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 147 | public ServerServiceDefinition Build() |
| 148 | { |
Jan Tattermusch | dc64fb5 | 2015-08-04 18:45:40 -0700 | [diff] [blame] | 149 | return new ServerServiceDefinition(callHandlers); |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 150 | } |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | } |