blob: 8353e6afb6b6e64d04a3265d3e616a3934eb956d [file] [log] [blame]
Jan Tattermuscha7fff862015-02-13 11:08:08 -08001#region Copyright notice and license
2
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003// Copyright 2015 gRPC authors.
Craig Tiller190d3602015-02-18 09:23:38 -08004//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005// 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 Tiller190d3602015-02-18 09:23:38 -08008//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009// http://www.apache.org/licenses/LICENSE-2.0
Craig Tiller190d3602015-02-18 09:23:38 -080010//
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011// 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 Tattermuscha7fff862015-02-13 11:08:08 -080016
17#endregion
18
Jan Tattermusch8ce5e8b2015-02-05 10:56:49 -080019using System;
Jan Tattermuschbee93252015-05-12 14:16:33 -070020using Grpc.Core.Utils;
Jan Tattermusch8ce5e8b2015-02-05 10:56:49 -080021
Jan Tattermusch30868622015-02-19 09:22:33 -080022namespace Grpc.Core
Jan Tattermusch8ce5e8b2015-02-05 10:56:49 -080023{
Jan Tattermuscha5272b62015-04-30 11:56:46 -070024 /// <summary>
25 /// Method types supported by gRPC.
26 /// </summary>
Jan Tattermusch8ce5e8b2015-02-05 10:56:49 -080027 public enum MethodType
28 {
Jan Tattermusch66eb18d2015-08-09 20:03:24 -070029 /// <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 Tattermusch8ce5e8b2015-02-05 10:56:49 -080040 }
41
42 /// <summary>
Jan Tattermusche396b8d2015-08-17 15:02:23 -070043 /// 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 Tattermusch66eb18d2015-08-09 20:03:24 -070070 /// A description of a remote method.
Jan Tattermusch8ce5e8b2015-02-05 10:56:49 -080071 /// </summary>
Jan Tattermusch12855fc2015-08-24 16:43:23 -070072 /// <typeparam name="TRequest">Request message type for this method.</typeparam>
73 /// <typeparam name="TResponse">Response message type for this method.</typeparam>
Jan Tattermusche396b8d2015-08-17 15:02:23 -070074 public class Method<TRequest, TResponse> : IMethod
Jan Tattermusch8ce5e8b2015-02-05 10:56:49 -080075 {
76 readonly MethodType type;
Jan Tattermuscha9ddd022015-08-05 03:04:58 -070077 readonly string serviceName;
Jan Tattermusch8ce5e8b2015-02-05 10:56:49 -080078 readonly string name;
Jan Tattermusch15111f52015-02-05 18:15:14 -080079 readonly Marshaller<TRequest> requestMarshaller;
80 readonly Marshaller<TResponse> responseMarshaller;
Jan Tattermuscha9ddd022015-08-05 03:04:58 -070081 readonly string fullName;
Jan Tattermusch8ce5e8b2015-02-05 10:56:49 -080082
Jan Tattermusch66eb18d2015-08-09 20:03:24 -070083 /// <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 Tattermuscha9ddd022015-08-05 03:04:58 -070091 public Method(MethodType type, string serviceName, string name, Marshaller<TRequest> requestMarshaller, Marshaller<TResponse> responseMarshaller)
Jan Tattermusch8ce5e8b2015-02-05 10:56:49 -080092 {
93 this.type = type;
Jan Tattermusch7a3ee6a2016-02-18 10:36:02 -080094 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 Tattermusch66eb18d2015-08-09 20:03:24 -070098 this.fullName = GetFullName(serviceName, name);
Jan Tattermusch8ce5e8b2015-02-05 10:56:49 -080099 }
100
Jan Tattermusch66eb18d2015-08-09 20:03:24 -0700101 /// <summary>
102 /// Gets the type of the method.
103 /// </summary>
Jan Tattermusch8ce5e8b2015-02-05 10:56:49 -0800104 public MethodType Type
105 {
106 get
107 {
108 return this.type;
109 }
110 }
Jan Tattermuscha9ddd022015-08-05 03:04:58 -0700111
Jan Tattermusch66eb18d2015-08-09 20:03:24 -0700112 /// <summary>
113 /// Gets the name of the service to which this method belongs.
114 /// </summary>
Jan Tattermuscha9ddd022015-08-05 03:04:58 -0700115 public string ServiceName
116 {
117 get
118 {
119 return this.serviceName;
120 }
121 }
Jan Tattermusch8ce5e8b2015-02-05 10:56:49 -0800122
Jan Tattermusch66eb18d2015-08-09 20:03:24 -0700123 /// <summary>
124 /// Gets the unqualified name of the method.
125 /// </summary>
Jan Tattermusch8ce5e8b2015-02-05 10:56:49 -0800126 public string Name
127 {
128 get
129 {
130 return this.name;
131 }
132 }
133
Jan Tattermusch66eb18d2015-08-09 20:03:24 -0700134 /// <summary>
135 /// Gets the marshaller used for request messages.
136 /// </summary>
Jan Tattermusch15111f52015-02-05 18:15:14 -0800137 public Marshaller<TRequest> RequestMarshaller
Jan Tattermusch8ce5e8b2015-02-05 10:56:49 -0800138 {
139 get
140 {
141 return this.requestMarshaller;
142 }
143 }
144
Jan Tattermusch66eb18d2015-08-09 20:03:24 -0700145 /// <summary>
146 /// Gets the marshaller used for response messages.
147 /// </summary>
Jan Tattermusch15111f52015-02-05 18:15:14 -0800148 public Marshaller<TResponse> ResponseMarshaller
Jan Tattermusch8ce5e8b2015-02-05 10:56:49 -0800149 {
150 get
151 {
152 return this.responseMarshaller;
153 }
154 }
Jan Tattermusch66eb18d2015-08-09 20:03:24 -0700155
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 Tattermuscha9ddd022015-08-05 03:04:58 -0700160 public string FullName
161 {
162 get
163 {
164 return this.fullName;
165 }
166 }
167
Jan Tattermuschbee93252015-05-12 14:16:33 -0700168 /// <summary>
169 /// Gets full name of the method including the service name.
170 /// </summary>
Jan Tattermusch66eb18d2015-08-09 20:03:24 -0700171 internal static string GetFullName(string serviceName, string methodName)
Jan Tattermuschbee93252015-05-12 14:16:33 -0700172 {
Jan Tattermusch66eb18d2015-08-09 20:03:24 -0700173 return "/" + serviceName + "/" + methodName;
Jan Tattermuschbee93252015-05-12 14:16:33 -0700174 }
Jan Tattermusch8ce5e8b2015-02-05 10:56:49 -0800175 }
176}