1 #region Copyright notice and license 2 3 // Copyright 2015 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; 20 using Grpc.Core.Utils; 21 22 namespace Grpc.Core 23 { 24 /// <summary> 25 /// Method types supported by gRPC. 26 /// </summary> 27 public enum MethodType 28 { 29 /// <summary>Single request sent from client, single response received from server.</summary> 30 Unary, 31 32 /// <summary>Stream of request sent from client, single response received from server.</summary> 33 ClientStreaming, 34 35 /// <summary>Single request sent from client, stream of responses received from server.</summary> 36 ServerStreaming, 37 38 /// <summary>Both server and client can stream arbitrary number of requests and responses simultaneously.</summary> 39 DuplexStreaming 40 } 41 42 /// <summary> 43 /// A non-generic representation of a remote method. 44 /// </summary> 45 public interface IMethod 46 { 47 /// <summary> 48 /// Gets the type of the method. 49 /// </summary> 50 MethodType Type { get; } 51 52 /// <summary> 53 /// Gets the name of the service to which this method belongs. 54 /// </summary> 55 string ServiceName { get; } 56 57 /// <summary> 58 /// Gets the unqualified name of the method. 59 /// </summary> 60 string Name { get; } 61 62 /// <summary> 63 /// Gets the fully qualified name of the method. On the server side, methods are dispatched 64 /// based on this name. 65 /// </summary> 66 string FullName { get; } 67 } 68 69 /// <summary> 70 /// A description of a remote method. 71 /// </summary> 72 /// <typeparam name="TRequest">Request message type for this method.</typeparam> 73 /// <typeparam name="TResponse">Response message type for this method.</typeparam> 74 public class Method<TRequest, TResponse> : IMethod 75 { 76 readonly MethodType type; 77 readonly string serviceName; 78 readonly string name; 79 readonly Marshaller<TRequest> requestMarshaller; 80 readonly Marshaller<TResponse> responseMarshaller; 81 readonly string fullName; 82 83 /// <summary> 84 /// Initializes a new instance of the <c>Method</c> class. 85 /// </summary> 86 /// <param name="type">Type of method.</param> 87 /// <param name="serviceName">Name of service this method belongs to.</param> 88 /// <param name="name">Unqualified name of the method.</param> 89 /// <param name="requestMarshaller">Marshaller used for request messages.</param> 90 /// <param name="responseMarshaller">Marshaller used for response messages.</param> Method(MethodType type, string serviceName, string name, Marshaller<TRequest> requestMarshaller, Marshaller<TResponse> responseMarshaller)91 public Method(MethodType type, string serviceName, string name, Marshaller<TRequest> requestMarshaller, Marshaller<TResponse> responseMarshaller) 92 { 93 this.type = type; 94 this.serviceName = GrpcPreconditions.CheckNotNull(serviceName, "serviceName"); 95 this.name = GrpcPreconditions.CheckNotNull(name, "name"); 96 this.requestMarshaller = GrpcPreconditions.CheckNotNull(requestMarshaller, "requestMarshaller"); 97 this.responseMarshaller = GrpcPreconditions.CheckNotNull(responseMarshaller, "responseMarshaller"); 98 this.fullName = GetFullName(serviceName, name); 99 } 100 101 /// <summary> 102 /// Gets the type of the method. 103 /// </summary> 104 public MethodType Type 105 { 106 get 107 { 108 return this.type; 109 } 110 } 111 112 /// <summary> 113 /// Gets the name of the service to which this method belongs. 114 /// </summary> 115 public string ServiceName 116 { 117 get 118 { 119 return this.serviceName; 120 } 121 } 122 123 /// <summary> 124 /// Gets the unqualified name of the method. 125 /// </summary> 126 public string Name 127 { 128 get 129 { 130 return this.name; 131 } 132 } 133 134 /// <summary> 135 /// Gets the marshaller used for request messages. 136 /// </summary> 137 public Marshaller<TRequest> RequestMarshaller 138 { 139 get 140 { 141 return this.requestMarshaller; 142 } 143 } 144 145 /// <summary> 146 /// Gets the marshaller used for response messages. 147 /// </summary> 148 public Marshaller<TResponse> ResponseMarshaller 149 { 150 get 151 { 152 return this.responseMarshaller; 153 } 154 } 155 156 /// <summary> 157 /// Gets the fully qualified name of the method. On the server side, methods are dispatched 158 /// based on this name. 159 /// </summary> 160 public string FullName 161 { 162 get 163 { 164 return this.fullName; 165 } 166 } 167 168 /// <summary> 169 /// Gets full name of the method including the service name. 170 /// </summary> GetFullName(string serviceName, string methodName)171 internal static string GetFullName(string serviceName, string methodName) 172 { 173 return "/" + serviceName + "/" + methodName; 174 } 175 } 176 } 177