1 #region Copyright notice and license 2 3 // Copyright 2019 The gRPC Authors 4 // 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 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 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. 16 17 #endregion 18 19 using System.Collections.Generic; 20 using System.Collections.ObjectModel; 21 using Grpc.Core.Internal; 22 23 namespace Grpc.Core.Internal 24 { 25 internal static class ServerServiceDefinitionExtensions 26 { 27 /// <summary> 28 /// Maps methods from <c>ServerServiceDefinition</c> to server call handlers. 29 /// </summary> GetCallHandlers(this ServerServiceDefinition serviceDefinition)30 internal static ReadOnlyDictionary<string, IServerCallHandler> GetCallHandlers(this ServerServiceDefinition serviceDefinition) 31 { 32 var binder = new DefaultServiceBinder(); 33 serviceDefinition.BindService(binder); 34 return binder.GetCallHandlers(); 35 } 36 37 /// <summary> 38 /// Helper for converting <c>ServerServiceDefinition</c> to server call handlers. 39 /// </summary> 40 private class DefaultServiceBinder : ServiceBinderBase 41 { 42 readonly Dictionary<string, IServerCallHandler> callHandlers = new Dictionary<string, IServerCallHandler>(); 43 GetCallHandlers()44 internal ReadOnlyDictionary<string, IServerCallHandler> GetCallHandlers() 45 { 46 return new ReadOnlyDictionary<string, IServerCallHandler>(this.callHandlers); 47 } 48 AddMethod( Method<TRequest, TResponse> method, UnaryServerMethod<TRequest, TResponse> handler)49 public override void AddMethod<TRequest, TResponse>( 50 Method<TRequest, TResponse> method, 51 UnaryServerMethod<TRequest, TResponse> handler) 52 { 53 callHandlers.Add(method.FullName, ServerCalls.UnaryCall(method, handler)); 54 } 55 AddMethod( Method<TRequest, TResponse> method, ClientStreamingServerMethod<TRequest, TResponse> handler)56 public override void AddMethod<TRequest, TResponse>( 57 Method<TRequest, TResponse> method, 58 ClientStreamingServerMethod<TRequest, TResponse> handler) 59 { 60 callHandlers.Add(method.FullName, ServerCalls.ClientStreamingCall(method, handler)); 61 } 62 AddMethod( Method<TRequest, TResponse> method, ServerStreamingServerMethod<TRequest, TResponse> handler)63 public override void AddMethod<TRequest, TResponse>( 64 Method<TRequest, TResponse> method, 65 ServerStreamingServerMethod<TRequest, TResponse> handler) 66 { 67 callHandlers.Add(method.FullName, ServerCalls.ServerStreamingCall(method, handler)); 68 } 69 AddMethod( Method<TRequest, TResponse> method, DuplexStreamingServerMethod<TRequest, TResponse> handler)70 public override void AddMethod<TRequest, TResponse>( 71 Method<TRequest, TResponse> method, 72 DuplexStreamingServerMethod<TRequest, TResponse> handler) 73 { 74 callHandlers.Add(method.FullName, ServerCalls.DuplexStreamingCall(method, handler)); 75 } 76 } 77 } 78 } 79