OLD | NEW |
(Empty) | |
| 1 #region Copyright notice and license |
| 2 |
| 3 // Copyright 2015, Google Inc. |
| 4 // All rights reserved. |
| 5 // |
| 6 // Redistribution and use in source and binary forms, with or without |
| 7 // modification, are permitted provided that the following conditions are |
| 8 // met: |
| 9 // |
| 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. |
| 19 // |
| 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 |
| 34 using System; |
| 35 using System.Collections.Generic; |
| 36 using System.Collections.ObjectModel; |
| 37 using Grpc.Core.Internal; |
| 38 |
| 39 namespace Grpc.Core |
| 40 { |
| 41 /// <summary> |
| 42 /// Mapping of method names to server call handlers. |
| 43 /// Normally, the <c>ServerServiceDefinition</c> objects will be created by
the <c>BindService</c> factory method |
| 44 /// that is part of the autogenerated code for a protocol buffers service de
finition. |
| 45 /// </summary> |
| 46 public class ServerServiceDefinition |
| 47 { |
| 48 readonly ReadOnlyDictionary<string, IServerCallHandler> callHandlers; |
| 49 |
| 50 private ServerServiceDefinition(Dictionary<string, IServerCallHandler> c
allHandlers) |
| 51 { |
| 52 this.callHandlers = new ReadOnlyDictionary<string, IServerCallHandle
r>(callHandlers); |
| 53 } |
| 54 |
| 55 internal IDictionary<string, IServerCallHandler> CallHandlers |
| 56 { |
| 57 get |
| 58 { |
| 59 return this.callHandlers; |
| 60 } |
| 61 } |
| 62 |
| 63 /// <summary> |
| 64 /// Creates a new builder object for <c>ServerServiceDefinition</c>. |
| 65 /// </summary> |
| 66 /// <param name="serviceName">The service name.</param> |
| 67 /// <returns>The builder object.</returns> |
| 68 public static Builder CreateBuilder(string serviceName) |
| 69 { |
| 70 return new Builder(serviceName); |
| 71 } |
| 72 |
| 73 /// <summary> |
| 74 /// Builder class for <see cref="ServerServiceDefinition"/>. |
| 75 /// </summary> |
| 76 public class Builder |
| 77 { |
| 78 readonly string serviceName; |
| 79 readonly Dictionary<string, IServerCallHandler> callHandlers = new D
ictionary<string, IServerCallHandler>(); |
| 80 |
| 81 /// <summary> |
| 82 /// Creates a new instance of builder. |
| 83 /// </summary> |
| 84 /// <param name="serviceName">The service name.</param> |
| 85 public Builder(string serviceName) |
| 86 { |
| 87 this.serviceName = serviceName; |
| 88 } |
| 89 |
| 90 /// <summary> |
| 91 /// Adds a definitions for a single request - single response method
. |
| 92 /// </summary> |
| 93 /// <typeparam name="TRequest">The request message class.</typeparam
> |
| 94 /// <typeparam name="TResponse">The response message class.</typepar
am> |
| 95 /// <param name="method">The method.</param> |
| 96 /// <param name="handler">The method handler.</param> |
| 97 /// <returns>This builder instance.</returns> |
| 98 public Builder AddMethod<TRequest, TResponse>( |
| 99 Method<TRequest, TResponse> method, |
| 100 UnaryServerMethod<TRequest, TResponse> handler) |
| 101 where TRequest : class |
| 102 where TResponse : class |
| 103 { |
| 104 callHandlers.Add(method.FullName, ServerCalls.UnaryCall(method,
handler)); |
| 105 return this; |
| 106 } |
| 107 |
| 108 /// <summary> |
| 109 /// Adds a definitions for a client streaming method. |
| 110 /// </summary> |
| 111 /// <typeparam name="TRequest">The request message class.</typeparam
> |
| 112 /// <typeparam name="TResponse">The response message class.</typepar
am> |
| 113 /// <param name="method">The method.</param> |
| 114 /// <param name="handler">The method handler.</param> |
| 115 /// <returns>This builder instance.</returns> |
| 116 public Builder AddMethod<TRequest, TResponse>( |
| 117 Method<TRequest, TResponse> method, |
| 118 ClientStreamingServerMethod<TRequest, TResponse> handler) |
| 119 where TRequest : class |
| 120 where TResponse : class |
| 121 { |
| 122 callHandlers.Add(method.FullName, ServerCalls.ClientStreamingCal
l(method, handler)); |
| 123 return this; |
| 124 } |
| 125 |
| 126 /// <summary> |
| 127 /// Adds a definitions for a server streaming method. |
| 128 /// </summary> |
| 129 /// <typeparam name="TRequest">The request message class.</typeparam
> |
| 130 /// <typeparam name="TResponse">The response message class.</typepar
am> |
| 131 /// <param name="method">The method.</param> |
| 132 /// <param name="handler">The method handler.</param> |
| 133 /// <returns>This builder instance.</returns> |
| 134 public Builder AddMethod<TRequest, TResponse>( |
| 135 Method<TRequest, TResponse> method, |
| 136 ServerStreamingServerMethod<TRequest, TResponse> handler) |
| 137 where TRequest : class |
| 138 where TResponse : class |
| 139 { |
| 140 callHandlers.Add(method.FullName, ServerCalls.ServerStreamingCal
l(method, handler)); |
| 141 return this; |
| 142 } |
| 143 |
| 144 /// <summary> |
| 145 /// Adds a definitions for a bidirectional streaming method. |
| 146 /// </summary> |
| 147 /// <typeparam name="TRequest">The request message class.</typeparam
> |
| 148 /// <typeparam name="TResponse">The response message class.</typepar
am> |
| 149 /// <param name="method">The method.</param> |
| 150 /// <param name="handler">The method handler.</param> |
| 151 /// <returns>This builder instance.</returns> |
| 152 public Builder AddMethod<TRequest, TResponse>( |
| 153 Method<TRequest, TResponse> method, |
| 154 DuplexStreamingServerMethod<TRequest, TResponse> handler) |
| 155 where TRequest : class |
| 156 where TResponse : class |
| 157 { |
| 158 callHandlers.Add(method.FullName, ServerCalls.DuplexStreamingCal
l(method, handler)); |
| 159 return this; |
| 160 } |
| 161 |
| 162 /// <summary> |
| 163 /// Creates an immutable <c>ServerServiceDefinition</c> from this bu
ilder. |
| 164 /// </summary> |
| 165 /// <returns>The <c>ServerServiceDefinition</c> object.</returns> |
| 166 public ServerServiceDefinition Build() |
| 167 { |
| 168 return new ServerServiceDefinition(callHandlers); |
| 169 } |
| 170 } |
| 171 } |
| 172 } |
OLD | NEW |