ServerServiceDefinition.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #region Copyright notice and license
  2. // Copyright 2015, Google Inc.
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #endregion
  31. using System;
  32. using System.Collections.Generic;
  33. using System.Collections.ObjectModel;
  34. using Grpc.Core.Internal;
  35. namespace Grpc.Core
  36. {
  37. /// <summary>
  38. /// Mapping of method names to server call handlers.
  39. /// Normally, the <c>ServerServiceDefinition</c> objects will be created by the <c>BindService</c> factory method
  40. /// that is part of the autogenerated code for a protocol buffers service definition.
  41. /// </summary>
  42. public class ServerServiceDefinition
  43. {
  44. readonly ReadOnlyDictionary<string, IServerCallHandler> callHandlers;
  45. private ServerServiceDefinition(Dictionary<string, IServerCallHandler> callHandlers)
  46. {
  47. this.callHandlers = new ReadOnlyDictionary<string, IServerCallHandler>(callHandlers);
  48. }
  49. internal IDictionary<string, IServerCallHandler> CallHandlers
  50. {
  51. get
  52. {
  53. return this.callHandlers;
  54. }
  55. }
  56. /// <summary>
  57. /// Creates a new builder object for <c>ServerServiceDefinition</c>.
  58. /// </summary>
  59. /// <returns>The builder object.</returns>
  60. public static Builder CreateBuilder()
  61. {
  62. return new Builder();
  63. }
  64. /// <summary>
  65. /// Builder class for <see cref="ServerServiceDefinition"/>.
  66. /// </summary>
  67. public class Builder
  68. {
  69. readonly Dictionary<string, IServerCallHandler> callHandlers = new Dictionary<string, IServerCallHandler>();
  70. /// <summary>
  71. /// Creates a new instance of builder.
  72. /// </summary>
  73. public Builder()
  74. {
  75. }
  76. /// <summary>
  77. /// Adds a definitions for a single request - single response method.
  78. /// </summary>
  79. /// <typeparam name="TRequest">The request message class.</typeparam>
  80. /// <typeparam name="TResponse">The response message class.</typeparam>
  81. /// <param name="method">The method.</param>
  82. /// <param name="handler">The method handler.</param>
  83. /// <returns>This builder instance.</returns>
  84. public Builder AddMethod<TRequest, TResponse>(
  85. Method<TRequest, TResponse> method,
  86. UnaryServerMethod<TRequest, TResponse> handler)
  87. where TRequest : class
  88. where TResponse : class
  89. {
  90. callHandlers.Add(method.FullName, ServerCalls.UnaryCall(method, handler));
  91. return this;
  92. }
  93. /// <summary>
  94. /// Adds a definitions for a client streaming method.
  95. /// </summary>
  96. /// <typeparam name="TRequest">The request message class.</typeparam>
  97. /// <typeparam name="TResponse">The response message class.</typeparam>
  98. /// <param name="method">The method.</param>
  99. /// <param name="handler">The method handler.</param>
  100. /// <returns>This builder instance.</returns>
  101. public Builder AddMethod<TRequest, TResponse>(
  102. Method<TRequest, TResponse> method,
  103. ClientStreamingServerMethod<TRequest, TResponse> handler)
  104. where TRequest : class
  105. where TResponse : class
  106. {
  107. callHandlers.Add(method.FullName, ServerCalls.ClientStreamingCall(method, handler));
  108. return this;
  109. }
  110. /// <summary>
  111. /// Adds a definitions for a server streaming method.
  112. /// </summary>
  113. /// <typeparam name="TRequest">The request message class.</typeparam>
  114. /// <typeparam name="TResponse">The response message class.</typeparam>
  115. /// <param name="method">The method.</param>
  116. /// <param name="handler">The method handler.</param>
  117. /// <returns>This builder instance.</returns>
  118. public Builder AddMethod<TRequest, TResponse>(
  119. Method<TRequest, TResponse> method,
  120. ServerStreamingServerMethod<TRequest, TResponse> handler)
  121. where TRequest : class
  122. where TResponse : class
  123. {
  124. callHandlers.Add(method.FullName, ServerCalls.ServerStreamingCall(method, handler));
  125. return this;
  126. }
  127. /// <summary>
  128. /// Adds a definitions for a bidirectional streaming method.
  129. /// </summary>
  130. /// <typeparam name="TRequest">The request message class.</typeparam>
  131. /// <typeparam name="TResponse">The response message class.</typeparam>
  132. /// <param name="method">The method.</param>
  133. /// <param name="handler">The method handler.</param>
  134. /// <returns>This builder instance.</returns>
  135. public Builder AddMethod<TRequest, TResponse>(
  136. Method<TRequest, TResponse> method,
  137. DuplexStreamingServerMethod<TRequest, TResponse> handler)
  138. where TRequest : class
  139. where TResponse : class
  140. {
  141. callHandlers.Add(method.FullName, ServerCalls.DuplexStreamingCall(method, handler));
  142. return this;
  143. }
  144. /// <summary>
  145. /// Creates an immutable <c>ServerServiceDefinition</c> from this builder.
  146. /// </summary>
  147. /// <returns>The <c>ServerServiceDefinition</c> object.</returns>
  148. public ServerServiceDefinition Build()
  149. {
  150. return new ServerServiceDefinition(callHandlers);
  151. }
  152. }
  153. }
  154. }