Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | // Copyright 2015 gRPC authors. |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 4 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 8 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 10 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 16 | |
| 17 | #endregion |
| 18 | |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 19 | using System; |
Jan Tattermusch | bee9325 | 2015-05-12 14:16:33 -0700 | [diff] [blame] | 20 | using Grpc.Core.Utils; |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 21 | |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 22 | namespace Grpc.Core |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 23 | { |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 24 | /// <summary> |
| 25 | /// Method types supported by gRPC. |
| 26 | /// </summary> |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 27 | public enum MethodType |
| 28 | { |
Jan Tattermusch | 66eb18d | 2015-08-09 20:03:24 -0700 | [diff] [blame] | 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 |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | /// <summary> |
Jan Tattermusch | e396b8d | 2015-08-17 15:02:23 -0700 | [diff] [blame] | 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> |
Jan Tattermusch | 66eb18d | 2015-08-09 20:03:24 -0700 | [diff] [blame] | 70 | /// A description of a remote method. |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 71 | /// </summary> |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 72 | /// <typeparam name="TRequest">Request message type for this method.</typeparam> |
| 73 | /// <typeparam name="TResponse">Response message type for this method.</typeparam> |
Jan Tattermusch | e396b8d | 2015-08-17 15:02:23 -0700 | [diff] [blame] | 74 | public class Method<TRequest, TResponse> : IMethod |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 75 | { |
| 76 | readonly MethodType type; |
Jan Tattermusch | a9ddd02 | 2015-08-05 03:04:58 -0700 | [diff] [blame] | 77 | readonly string serviceName; |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 78 | readonly string name; |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 79 | readonly Marshaller<TRequest> requestMarshaller; |
| 80 | readonly Marshaller<TResponse> responseMarshaller; |
Jan Tattermusch | a9ddd02 | 2015-08-05 03:04:58 -0700 | [diff] [blame] | 81 | readonly string fullName; |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 82 | |
Jan Tattermusch | 66eb18d | 2015-08-09 20:03:24 -0700 | [diff] [blame] | 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> |
Jan Tattermusch | a9ddd02 | 2015-08-05 03:04:58 -0700 | [diff] [blame] | 91 | public Method(MethodType type, string serviceName, string name, Marshaller<TRequest> requestMarshaller, Marshaller<TResponse> responseMarshaller) |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 92 | { |
| 93 | this.type = type; |
Jan Tattermusch | 7a3ee6a | 2016-02-18 10:36:02 -0800 | [diff] [blame] | 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"); |
Jan Tattermusch | 66eb18d | 2015-08-09 20:03:24 -0700 | [diff] [blame] | 98 | this.fullName = GetFullName(serviceName, name); |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Jan Tattermusch | 66eb18d | 2015-08-09 20:03:24 -0700 | [diff] [blame] | 101 | /// <summary> |
| 102 | /// Gets the type of the method. |
| 103 | /// </summary> |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 104 | public MethodType Type |
| 105 | { |
| 106 | get |
| 107 | { |
| 108 | return this.type; |
| 109 | } |
| 110 | } |
Jan Tattermusch | a9ddd02 | 2015-08-05 03:04:58 -0700 | [diff] [blame] | 111 | |
Jan Tattermusch | 66eb18d | 2015-08-09 20:03:24 -0700 | [diff] [blame] | 112 | /// <summary> |
| 113 | /// Gets the name of the service to which this method belongs. |
| 114 | /// </summary> |
Jan Tattermusch | a9ddd02 | 2015-08-05 03:04:58 -0700 | [diff] [blame] | 115 | public string ServiceName |
| 116 | { |
| 117 | get |
| 118 | { |
| 119 | return this.serviceName; |
| 120 | } |
| 121 | } |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 122 | |
Jan Tattermusch | 66eb18d | 2015-08-09 20:03:24 -0700 | [diff] [blame] | 123 | /// <summary> |
| 124 | /// Gets the unqualified name of the method. |
| 125 | /// </summary> |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 126 | public string Name |
| 127 | { |
| 128 | get |
| 129 | { |
| 130 | return this.name; |
| 131 | } |
| 132 | } |
| 133 | |
Jan Tattermusch | 66eb18d | 2015-08-09 20:03:24 -0700 | [diff] [blame] | 134 | /// <summary> |
| 135 | /// Gets the marshaller used for request messages. |
| 136 | /// </summary> |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 137 | public Marshaller<TRequest> RequestMarshaller |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 138 | { |
| 139 | get |
| 140 | { |
| 141 | return this.requestMarshaller; |
| 142 | } |
| 143 | } |
| 144 | |
Jan Tattermusch | 66eb18d | 2015-08-09 20:03:24 -0700 | [diff] [blame] | 145 | /// <summary> |
| 146 | /// Gets the marshaller used for response messages. |
| 147 | /// </summary> |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 148 | public Marshaller<TResponse> ResponseMarshaller |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 149 | { |
| 150 | get |
| 151 | { |
| 152 | return this.responseMarshaller; |
| 153 | } |
| 154 | } |
Jan Tattermusch | 66eb18d | 2015-08-09 20:03:24 -0700 | [diff] [blame] | 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> |
Jan Tattermusch | a9ddd02 | 2015-08-05 03:04:58 -0700 | [diff] [blame] | 160 | public string FullName |
| 161 | { |
| 162 | get |
| 163 | { |
| 164 | return this.fullName; |
| 165 | } |
| 166 | } |
| 167 | |
Jan Tattermusch | bee9325 | 2015-05-12 14:16:33 -0700 | [diff] [blame] | 168 | /// <summary> |
| 169 | /// Gets full name of the method including the service name. |
| 170 | /// </summary> |
Jan Tattermusch | 66eb18d | 2015-08-09 20:03:24 -0700 | [diff] [blame] | 171 | internal static string GetFullName(string serviceName, string methodName) |
Jan Tattermusch | bee9325 | 2015-05-12 14:16:33 -0700 | [diff] [blame] | 172 | { |
Jan Tattermusch | 66eb18d | 2015-08-09 20:03:24 -0700 | [diff] [blame] | 173 | return "/" + serviceName + "/" + methodName; |
Jan Tattermusch | bee9325 | 2015-05-12 14:16:33 -0700 | [diff] [blame] | 174 | } |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 175 | } |
| 176 | } |