ServerServiceDefinition.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 Grpc.Core.Internal;
  20. namespace Grpc.Core
  21. {
  22. /// <summary>
  23. /// Mapping of method names to server call handlers.
  24. /// Normally, the <c>ServerServiceDefinition</c> objects will be created by the <c>BindService</c> factory method
  25. /// that is part of the autogenerated code for a protocol buffers service definition.
  26. /// </summary>
  27. public class ServerServiceDefinition
  28. {
  29. readonly ReadOnlyDictionary<string, IServerCallHandler> callHandlers;
  30. private ServerServiceDefinition(Dictionary<string, IServerCallHandler> callHandlers)
  31. {
  32. this.callHandlers = new ReadOnlyDictionary<string, IServerCallHandler>(callHandlers);
  33. }
  34. internal IDictionary<string, IServerCallHandler> CallHandlers
  35. {
  36. get
  37. {
  38. return this.callHandlers;
  39. }
  40. }
  41. /// <summary>
  42. /// Creates a new builder object for <c>ServerServiceDefinition</c>.
  43. /// </summary>
  44. /// <returns>The builder object.</returns>
  45. public static Builder CreateBuilder()
  46. {
  47. return new Builder();
  48. }
  49. /// <summary>
  50. /// Builder class for <see cref="ServerServiceDefinition"/>.
  51. /// </summary>
  52. public class Builder
  53. {
  54. readonly Dictionary<string, IServerCallHandler> callHandlers = new Dictionary<string, IServerCallHandler>();
  55. /// <summary>
  56. /// Creates a new instance of builder.
  57. /// </summary>
  58. public Builder()
  59. {
  60. }
  61. /// <summary>
  62. /// Adds a definitions for a single request - single response method.
  63. /// </summary>
  64. /// <typeparam name="TRequest">The request message class.</typeparam>
  65. /// <typeparam name="TResponse">The response message class.</typeparam>
  66. /// <param name="method">The method.</param>
  67. /// <param name="handler">The method handler.</param>
  68. /// <returns>This builder instance.</returns>
  69. public Builder AddMethod<TRequest, TResponse>(
  70. Method<TRequest, TResponse> method,
  71. UnaryServerMethod<TRequest, TResponse> handler)
  72. where TRequest : class
  73. where TResponse : class
  74. {
  75. callHandlers.Add(method.FullName, ServerCalls.UnaryCall(method, handler));
  76. return this;
  77. }
  78. /// <summary>
  79. /// Adds a definitions for a client streaming method.
  80. /// </summary>
  81. /// <typeparam name="TRequest">The request message class.</typeparam>
  82. /// <typeparam name="TResponse">The response message class.</typeparam>
  83. /// <param name="method">The method.</param>
  84. /// <param name="handler">The method handler.</param>
  85. /// <returns>This builder instance.</returns>
  86. public Builder AddMethod<TRequest, TResponse>(
  87. Method<TRequest, TResponse> method,
  88. ClientStreamingServerMethod<TRequest, TResponse> handler)
  89. where TRequest : class
  90. where TResponse : class
  91. {
  92. callHandlers.Add(method.FullName, ServerCalls.ClientStreamingCall(method, handler));
  93. return this;
  94. }
  95. /// <summary>
  96. /// Adds a definitions for a server streaming method.
  97. /// </summary>
  98. /// <typeparam name="TRequest">The request message class.</typeparam>
  99. /// <typeparam name="TResponse">The response message class.</typeparam>
  100. /// <param name="method">The method.</param>
  101. /// <param name="handler">The method handler.</param>
  102. /// <returns>This builder instance.</returns>
  103. public Builder AddMethod<TRequest, TResponse>(
  104. Method<TRequest, TResponse> method,
  105. ServerStreamingServerMethod<TRequest, TResponse> handler)
  106. where TRequest : class
  107. where TResponse : class
  108. {
  109. callHandlers.Add(method.FullName, ServerCalls.ServerStreamingCall(method, handler));
  110. return this;
  111. }
  112. /// <summary>
  113. /// Adds a definitions for a bidirectional streaming method.
  114. /// </summary>
  115. /// <typeparam name="TRequest">The request message class.</typeparam>
  116. /// <typeparam name="TResponse">The response message class.</typeparam>
  117. /// <param name="method">The method.</param>
  118. /// <param name="handler">The method handler.</param>
  119. /// <returns>This builder instance.</returns>
  120. public Builder AddMethod<TRequest, TResponse>(
  121. Method<TRequest, TResponse> method,
  122. DuplexStreamingServerMethod<TRequest, TResponse> handler)
  123. where TRequest : class
  124. where TResponse : class
  125. {
  126. callHandlers.Add(method.FullName, ServerCalls.DuplexStreamingCall(method, handler));
  127. return this;
  128. }
  129. /// <summary>
  130. /// Creates an immutable <c>ServerServiceDefinition</c> from this builder.
  131. /// </summary>
  132. /// <returns>The <c>ServerServiceDefinition</c> object.</returns>
  133. public ServerServiceDefinition Build()
  134. {
  135. return new ServerServiceDefinition(callHandlers);
  136. }
  137. }
  138. }
  139. }