ServerServiceDefinition.cs 6.2 KB

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