blob: 0d1404341d27cc869bdb6b54a2009088d24983cc [file] [log] [blame]
Jan Tattermusch2d924952015-05-06 10:23:17 -07001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Jan Tattermusch2d924952015-05-06 10:23:17 -07004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#include <cctype>
35#include <map>
yang-gc3ee1d52015-08-28 11:33:52 -070036#include <sstream>
Jan Tattermusch2d924952015-05-06 10:23:17 -070037#include <vector>
38
Jan Tattermusch741e64c2015-08-03 08:08:56 -070039#include "src/compiler/csharp_generator.h"
Jan Tattermusch2d924952015-05-06 10:23:17 -070040#include "src/compiler/config.h"
Jan Tattermuschb5897bf2015-05-07 15:45:37 -070041#include "src/compiler/csharp_generator_helpers.h"
Jan Tattermusch2d924952015-05-06 10:23:17 -070042#include "src/compiler/csharp_generator.h"
43
Jan Tattermusch741e64c2015-08-03 08:08:56 -070044
45using google::protobuf::compiler::csharp::GetFileNamespace;
46using google::protobuf::compiler::csharp::GetClassName;
Jan Tattermuschda717f42016-01-20 13:12:35 -080047using google::protobuf::compiler::csharp::GetReflectionClassName;
Jan Tattermusch2d924952015-05-06 10:23:17 -070048using grpc::protobuf::FileDescriptor;
49using grpc::protobuf::Descriptor;
50using grpc::protobuf::ServiceDescriptor;
51using grpc::protobuf::MethodDescriptor;
52using grpc::protobuf::io::Printer;
53using grpc::protobuf::io::StringOutputStream;
Jan Tattermuschb5897bf2015-05-07 15:45:37 -070054using grpc_generator::MethodType;
55using grpc_generator::GetMethodType;
56using grpc_generator::METHODTYPE_NO_STREAMING;
57using grpc_generator::METHODTYPE_CLIENT_STREAMING;
58using grpc_generator::METHODTYPE_SERVER_STREAMING;
59using grpc_generator::METHODTYPE_BIDI_STREAMING;
Jan Tattermusch41f9f332015-05-20 08:52:00 -070060using grpc_generator::StringReplace;
Jan Tattermusch2d924952015-05-06 10:23:17 -070061using std::map;
62using std::vector;
63
Jan Tattermusch741e64c2015-08-03 08:08:56 -070064
Jan Tattermusch2d924952015-05-06 10:23:17 -070065namespace grpc_csharp_generator {
66namespace {
67
Jan Tattermusch2d924952015-05-06 10:23:17 -070068std::string GetServiceClassName(const ServiceDescriptor* service) {
69 return service->name();
70}
71
72std::string GetClientInterfaceName(const ServiceDescriptor* service) {
73 return "I" + service->name() + "Client";
74}
75
76std::string GetClientClassName(const ServiceDescriptor* service) {
77 return service->name() + "Client";
78}
79
80std::string GetServerInterfaceName(const ServiceDescriptor* service) {
81 return "I" + service->name();
82}
83
Jan Tattermusch10a002d2016-03-14 15:22:19 -070084std::string GetServerClassName(const ServiceDescriptor* service) {
85 return service->name() + "Base";
86}
87
Jan Tattermusch2d924952015-05-06 10:23:17 -070088std::string GetCSharpMethodType(MethodType method_type) {
89 switch (method_type) {
90 case METHODTYPE_NO_STREAMING:
91 return "MethodType.Unary";
92 case METHODTYPE_CLIENT_STREAMING:
93 return "MethodType.ClientStreaming";
94 case METHODTYPE_SERVER_STREAMING:
95 return "MethodType.ServerStreaming";
96 case METHODTYPE_BIDI_STREAMING:
97 return "MethodType.DuplexStreaming";
98 }
99 GOOGLE_LOG(FATAL)<< "Can't get here.";
100 return "";
101}
102
103std::string GetServiceNameFieldName() {
104 return "__ServiceName";
105}
106
107std::string GetMarshallerFieldName(const Descriptor *message) {
108 return "__Marshaller_" + message->name();
109}
110
111std::string GetMethodFieldName(const MethodDescriptor *method) {
112 return "__Method_" + method->name();
113}
114
Jan Tattermusch8cd4ae92016-03-21 18:46:32 -0700115std::string GetMethodRequestParamMaybe(const MethodDescriptor *method,
116 bool invocation_param=false) {
Jan Tattermusch2d924952015-05-06 10:23:17 -0700117 if (method->client_streaming()) {
118 return "";
119 }
Jan Tattermusch8cd4ae92016-03-21 18:46:32 -0700120 if (invocation_param) {
121 return "request, ";
122 }
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700123 return GetClassName(method->input_type()) + " request, ";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700124}
125
Jan Tattermusch4e0f73c2016-04-25 16:11:03 -0700126std::string GetAccessLevel(bool internal_access) {
127 return internal_access ? "internal" : "public";
128}
129
Jan Tattermusch2d924952015-05-06 10:23:17 -0700130std::string GetMethodReturnTypeClient(const MethodDescriptor *method) {
131 switch (GetMethodType(method)) {
132 case METHODTYPE_NO_STREAMING:
Jan Tattermusch5269d162015-07-21 11:56:42 -0700133 return "AsyncUnaryCall<" + GetClassName(method->output_type()) + ">";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700134 case METHODTYPE_CLIENT_STREAMING:
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700135 return "AsyncClientStreamingCall<" + GetClassName(method->input_type())
136 + ", " + GetClassName(method->output_type()) + ">";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700137 case METHODTYPE_SERVER_STREAMING:
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700138 return "AsyncServerStreamingCall<" + GetClassName(method->output_type())
Jan Tattermusch2d924952015-05-06 10:23:17 -0700139 + ">";
140 case METHODTYPE_BIDI_STREAMING:
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700141 return "AsyncDuplexStreamingCall<" + GetClassName(method->input_type())
142 + ", " + GetClassName(method->output_type()) + ">";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700143 }
144 GOOGLE_LOG(FATAL)<< "Can't get here.";
145 return "";
146}
147
148std::string GetMethodRequestParamServer(const MethodDescriptor *method) {
149 switch (GetMethodType(method)) {
150 case METHODTYPE_NO_STREAMING:
151 case METHODTYPE_SERVER_STREAMING:
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700152 return GetClassName(method->input_type()) + " request";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700153 case METHODTYPE_CLIENT_STREAMING:
154 case METHODTYPE_BIDI_STREAMING:
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700155 return "IAsyncStreamReader<" + GetClassName(method->input_type())
Jan Tattermusch2d924952015-05-06 10:23:17 -0700156 + "> requestStream";
157 }
158 GOOGLE_LOG(FATAL)<< "Can't get here.";
159 return "";
160}
161
162std::string GetMethodReturnTypeServer(const MethodDescriptor *method) {
163 switch (GetMethodType(method)) {
164 case METHODTYPE_NO_STREAMING:
165 case METHODTYPE_CLIENT_STREAMING:
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700166 return "Task<" + GetClassName(method->output_type()) + ">";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700167 case METHODTYPE_SERVER_STREAMING:
168 case METHODTYPE_BIDI_STREAMING:
169 return "Task";
170 }
171 GOOGLE_LOG(FATAL)<< "Can't get here.";
172 return "";
173}
174
175std::string GetMethodResponseStreamMaybe(const MethodDescriptor *method) {
176 switch (GetMethodType(method)) {
177 case METHODTYPE_NO_STREAMING:
178 case METHODTYPE_CLIENT_STREAMING:
179 return "";
180 case METHODTYPE_SERVER_STREAMING:
181 case METHODTYPE_BIDI_STREAMING:
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700182 return ", IServerStreamWriter<" + GetClassName(method->output_type())
Jan Tattermusch2d924952015-05-06 10:23:17 -0700183 + "> responseStream";
184 }
185 GOOGLE_LOG(FATAL)<< "Can't get here.";
186 return "";
187}
188
189// Gets vector of all messages used as input or output types.
190std::vector<const Descriptor*> GetUsedMessages(
191 const ServiceDescriptor *service) {
192 std::set<const Descriptor*> descriptor_set;
193 std::vector<const Descriptor*> result; // vector is to maintain stable ordering
194 for (int i = 0; i < service->method_count(); i++) {
195 const MethodDescriptor *method = service->method(i);
196 if (descriptor_set.find(method->input_type()) == descriptor_set.end()) {
197 descriptor_set.insert(method->input_type());
198 result.push_back(method->input_type());
199 }
200 if (descriptor_set.find(method->output_type()) == descriptor_set.end()) {
201 descriptor_set.insert(method->output_type());
202 result.push_back(method->output_type());
203 }
204 }
205 return result;
206}
207
208void GenerateMarshallerFields(Printer* out, const ServiceDescriptor *service) {
209 std::vector<const Descriptor*> used_messages = GetUsedMessages(service);
210 for (size_t i = 0; i < used_messages.size(); i++) {
211 const Descriptor *message = used_messages[i];
212 out->Print(
Jan Tattermuschad75dd12015-08-03 09:43:07 -0700213 "static readonly Marshaller<$type$> $fieldname$ = Marshallers.Create((arg) => global::Google.Protobuf.MessageExtensions.ToByteArray(arg), $type$.Parser.ParseFrom);\n",
Jan Tattermusch2d924952015-05-06 10:23:17 -0700214 "fieldname", GetMarshallerFieldName(message), "type",
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700215 GetClassName(message));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700216 }
217 out->Print("\n");
218}
219
220void GenerateStaticMethodField(Printer* out, const MethodDescriptor *method) {
221 out->Print(
222 "static readonly Method<$request$, $response$> $fieldname$ = new Method<$request$, $response$>(\n",
223 "fieldname", GetMethodFieldName(method), "request",
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700224 GetClassName(method->input_type()), "response",
225 GetClassName(method->output_type()));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700226 out->Indent();
227 out->Indent();
228 out->Print("$methodtype$,\n", "methodtype",
229 GetCSharpMethodType(GetMethodType(method)));
Jan Tattermuscha9ddd022015-08-05 03:04:58 -0700230 out->Print("$servicenamefield$,\n", "servicenamefield",
231 GetServiceNameFieldName());
Jan Tattermusch2d924952015-05-06 10:23:17 -0700232 out->Print("\"$methodname$\",\n", "methodname", method->name());
233 out->Print("$requestmarshaller$,\n", "requestmarshaller",
234 GetMarshallerFieldName(method->input_type()));
235 out->Print("$responsemarshaller$);\n", "responsemarshaller",
236 GetMarshallerFieldName(method->output_type()));
237 out->Print("\n");
238 out->Outdent();
239 out->Outdent();
240}
241
Jan Tattermusche6af5d12015-08-03 10:57:43 -0700242void GenerateServiceDescriptorProperty(Printer* out, const ServiceDescriptor *service) {
yang-gc3ee1d52015-08-28 11:33:52 -0700243 std::ostringstream index;
244 index << service->index();
Jan Tattermusche6af5d12015-08-03 10:57:43 -0700245 out->Print("// service descriptor\n");
246 out->Print("public static global::Google.Protobuf.Reflection.ServiceDescriptor Descriptor\n");
247 out->Print("{\n");
248 out->Print(" get { return $umbrella$.Descriptor.Services[$index$]; }\n",
Jan Tattermuschda717f42016-01-20 13:12:35 -0800249 "umbrella", GetReflectionClassName(service->file()), "index",
yang-gc3ee1d52015-08-28 11:33:52 -0700250 index.str());
Jan Tattermusche6af5d12015-08-03 10:57:43 -0700251 out->Print("}\n");
252 out->Print("\n");
253}
254
Jan Tattermusch2d924952015-05-06 10:23:17 -0700255void GenerateClientInterface(Printer* out, const ServiceDescriptor *service) {
Jan Tattermuschb5332812015-07-14 19:29:35 -0700256 out->Print("// client interface\n");
Jan Tattermusch5a4e1e32016-03-25 16:51:55 -0700257 out->Print("[System.Obsolete(\"Client side interfaced will be removed "
258 "in the next release. Use client class directly.\")]\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700259 out->Print("public interface $name$\n", "name",
260 GetClientInterfaceName(service));
261 out->Print("{\n");
262 out->Indent();
263 for (int i = 0; i < service->method_count(); i++) {
264 const MethodDescriptor *method = service->method(i);
265 MethodType method_type = GetMethodType(method);
266
267 if (method_type == METHODTYPE_NO_STREAMING) {
268 // unary calls have an extra synchronous stub method
269 out->Print(
Jan Tattermusch74529562015-07-23 14:04:51 -0700270 "$response$ $methodname$($request$ request, Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken));\n",
Jan Tattermusch2d924952015-05-06 10:23:17 -0700271 "methodname", method->name(), "request",
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700272 GetClassName(method->input_type()), "response",
273 GetClassName(method->output_type()));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700274
Jan Tattermusche5e57ad2015-08-05 14:54:08 -0700275 // overload taking CallOptions as a param
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700276 out->Print(
Jan Tattermusche5e57ad2015-08-05 14:54:08 -0700277 "$response$ $methodname$($request$ request, CallOptions options);\n",
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700278 "methodname", method->name(), "request",
279 GetClassName(method->input_type()), "response",
280 GetClassName(method->output_type()));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700281 }
282
283 std::string method_name = method->name();
284 if (method_type == METHODTYPE_NO_STREAMING) {
285 method_name += "Async"; // prevent name clash with synchronous method.
286 }
287 out->Print(
Jan Tattermusch74529562015-07-23 14:04:51 -0700288 "$returntype$ $methodname$($request_maybe$Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken));\n",
Jan Tattermusch2d924952015-05-06 10:23:17 -0700289 "methodname", method_name, "request_maybe",
290 GetMethodRequestParamMaybe(method), "returntype",
291 GetMethodReturnTypeClient(method));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700292
Jan Tattermusche5e57ad2015-08-05 14:54:08 -0700293 // overload taking CallOptions as a param
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700294 out->Print(
Jan Tattermusche5e57ad2015-08-05 14:54:08 -0700295 "$returntype$ $methodname$($request_maybe$CallOptions options);\n",
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700296 "methodname", method_name, "request_maybe",
297 GetMethodRequestParamMaybe(method), "returntype",
298 GetMethodReturnTypeClient(method));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700299 }
300 out->Outdent();
301 out->Print("}\n");
302 out->Print("\n");
303}
304
305void GenerateServerInterface(Printer* out, const ServiceDescriptor *service) {
306 out->Print("// server-side interface\n");
Jan Tattermusch10a002d2016-03-14 15:22:19 -0700307 out->Print("[System.Obsolete(\"Service implementations should inherit"
308 " from the generated abstract base class instead.\")]\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700309 out->Print("public interface $name$\n", "name",
310 GetServerInterfaceName(service));
311 out->Print("{\n");
312 out->Indent();
313 for (int i = 0; i < service->method_count(); i++) {
314 const MethodDescriptor *method = service->method(i);
Craig Tillerb256faa2015-07-23 11:28:16 -0700315 out->Print(
316 "$returntype$ $methodname$($request$$response_stream_maybe$, "
317 "ServerCallContext context);\n",
318 "methodname", method->name(), "returntype",
319 GetMethodReturnTypeServer(method), "request",
320 GetMethodRequestParamServer(method), "response_stream_maybe",
321 GetMethodResponseStreamMaybe(method));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700322 }
323 out->Outdent();
324 out->Print("}\n");
325 out->Print("\n");
326}
327
Jan Tattermusch10a002d2016-03-14 15:22:19 -0700328void GenerateServerClass(Printer* out, const ServiceDescriptor *service) {
329 out->Print("// server-side abstract class\n");
330 out->Print("public abstract class $name$\n", "name",
331 GetServerClassName(service));
332 out->Print("{\n");
333 out->Indent();
334 for (int i = 0; i < service->method_count(); i++) {
335 const MethodDescriptor *method = service->method(i);
336 out->Print(
337 "public virtual $returntype$ $methodname$($request$$response_stream_maybe$, "
338 "ServerCallContext context)\n",
339 "methodname", method->name(), "returntype",
340 GetMethodReturnTypeServer(method), "request",
341 GetMethodRequestParamServer(method), "response_stream_maybe",
342 GetMethodResponseStreamMaybe(method));
343 out->Print("{\n");
344 out->Indent();
345 out->Print("throw new RpcException("
346 "new Status(StatusCode.Unimplemented, \"\"));\n");
347 out->Outdent();
348 out->Print("}\n\n");
349 }
350 out->Outdent();
351 out->Print("}\n");
352 out->Print("\n");
353}
354
Jan Tattermusch2d924952015-05-06 10:23:17 -0700355void GenerateClientStub(Printer* out, const ServiceDescriptor *service) {
356 out->Print("// client stub\n");
Jan Tattermuschcc8a4b32016-04-25 13:40:00 -0700357 out->Print("#pragma warning disable 0618\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700358 out->Print(
Jan Tattermusch5a4e1e32016-03-25 16:51:55 -0700359 "public class $name$ : ClientBase<$name$>, $interface$\n",
360 "name", GetClientClassName(service),
361 "interface", GetClientInterfaceName(service));
Jan Tattermuschcc8a4b32016-04-25 13:40:00 -0700362 out->Print("#pragma warning restore 0618\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700363 out->Print("{\n");
364 out->Indent();
365
366 // constructors
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700367 out->Print("public $name$(Channel channel) : base(channel)\n",
368 "name", GetClientClassName(service));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700369 out->Print("{\n");
370 out->Print("}\n");
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700371 out->Print("public $name$(CallInvoker callInvoker) : base(callInvoker)\n",
372 "name", GetClientClassName(service));
373 out->Print("{\n");
Jan Tattermuschefb77842016-03-23 07:47:36 -0700374 out->Print("}\n");
Jan Tattermusch2f0a8372016-03-23 09:16:49 -0700375 out->Print("///<summary>Protected parameterless constructor to allow creation"
Jan Tattermuschefb77842016-03-23 07:47:36 -0700376 " of test doubles.</summary>\n");
377 out->Print("protected $name$() : base()\n",
378 "name", GetClientClassName(service));
379 out->Print("{\n");
Jan Tattermusch2f0a8372016-03-23 09:16:49 -0700380 out->Print("}\n");
381 out->Print("///<summary>Protected constructor to allow creation of configured"
382 " clients.</summary>\n");
383 out->Print("protected $name$(ClientBaseConfiguration configuration)"
384 " : base(configuration)\n",
385 "name", GetClientClassName(service));
386 out->Print("{\n");
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700387 out->Print("}\n\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700388
389 for (int i = 0; i < service->method_count(); i++) {
390 const MethodDescriptor *method = service->method(i);
391 MethodType method_type = GetMethodType(method);
392
393 if (method_type == METHODTYPE_NO_STREAMING) {
394 // unary calls have an extra synchronous stub method
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700395 out->Print("public virtual $response$ $methodname$($request$ request, Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken))\n",
396 "methodname", method->name(), "request",
397 GetClassName(method->input_type()), "response",
398 GetClassName(method->output_type()));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700399 out->Print("{\n");
400 out->Indent();
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700401 out->Print("return $methodname$(request, new CallOptions(headers, deadline, cancellationToken));\n",
402 "methodname", method->name());
403 out->Outdent();
404 out->Print("}\n");
405
406 // overload taking CallOptions as a param
407 out->Print("public virtual $response$ $methodname$($request$ request, CallOptions options)\n",
408 "methodname", method->name(), "request",
409 GetClassName(method->input_type()), "response",
410 GetClassName(method->output_type()));
411 out->Print("{\n");
412 out->Indent();
Jan Tattermuschb455bcc2016-03-22 16:08:18 -0700413 out->Print("return CallInvoker.BlockingUnaryCall($methodfield$, null, options, request);\n",
Jan Tattermusch641cb1b2015-08-05 03:51:46 -0700414 "methodfield", GetMethodFieldName(method));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700415 out->Outdent();
416 out->Print("}\n");
417 }
418
419 std::string method_name = method->name();
420 if (method_type == METHODTYPE_NO_STREAMING) {
421 method_name += "Async"; // prevent name clash with synchronous method.
422 }
423 out->Print(
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700424 "public virtual $returntype$ $methodname$($request_maybe$Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken))\n",
425 "methodname", method_name, "request_maybe",
426 GetMethodRequestParamMaybe(method), "returntype",
427 GetMethodReturnTypeClient(method));
428 out->Print("{\n");
429 out->Indent();
430
431 out->Print("return $methodname$($request_maybe$new CallOptions(headers, deadline, cancellationToken));\n",
432 "methodname", method_name,
433 "request_maybe", GetMethodRequestParamMaybe(method, true));
434 out->Outdent();
435 out->Print("}\n");
436
437 // overload taking CallOptions as a param
438 out->Print(
439 "public virtual $returntype$ $methodname$($request_maybe$CallOptions options)\n",
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700440 "methodname", method_name, "request_maybe",
441 GetMethodRequestParamMaybe(method), "returntype",
442 GetMethodReturnTypeClient(method));
443 out->Print("{\n");
444 out->Indent();
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700445 switch (GetMethodType(method)) {
446 case METHODTYPE_NO_STREAMING:
Jan Tattermuschb455bcc2016-03-22 16:08:18 -0700447 out->Print("return CallInvoker.AsyncUnaryCall($methodfield$, null, options, request);\n",
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700448 "methodfield", GetMethodFieldName(method));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700449 break;
450 case METHODTYPE_CLIENT_STREAMING:
Jan Tattermuschb455bcc2016-03-22 16:08:18 -0700451 out->Print("return CallInvoker.AsyncClientStreamingCall($methodfield$, null, options);\n",
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700452 "methodfield", GetMethodFieldName(method));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700453 break;
454 case METHODTYPE_SERVER_STREAMING:
455 out->Print(
Jan Tattermuschb455bcc2016-03-22 16:08:18 -0700456 "return CallInvoker.AsyncServerStreamingCall($methodfield$, null, options, request);\n",
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700457 "methodfield", GetMethodFieldName(method));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700458 break;
459 case METHODTYPE_BIDI_STREAMING:
Jan Tattermuschb455bcc2016-03-22 16:08:18 -0700460 out->Print("return CallInvoker.AsyncDuplexStreamingCall($methodfield$, null, options);\n",
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700461 "methodfield", GetMethodFieldName(method));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700462 break;
463 default:
464 GOOGLE_LOG(FATAL)<< "Can't get here.";
465 }
Jan Tattermusch2d924952015-05-06 10:23:17 -0700466 out->Outdent();
467 out->Print("}\n");
468 }
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700469
470 // override NewInstance method
Jan Tattermusch2f0a8372016-03-23 09:16:49 -0700471 out->Print("protected override $name$ NewInstance(ClientBaseConfiguration configuration)\n",
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700472 "name", GetClientClassName(service));
473 out->Print("{\n");
474 out->Indent();
Jan Tattermusch2f0a8372016-03-23 09:16:49 -0700475 out->Print("return new $name$(configuration);\n",
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700476 "name", GetClientClassName(service));
477 out->Outdent();
478 out->Print("}\n");
479
Jan Tattermusch2d924952015-05-06 10:23:17 -0700480 out->Outdent();
481 out->Print("}\n");
482 out->Print("\n");
483}
484
Jan Tattermusch10a002d2016-03-14 15:22:19 -0700485void GenerateBindServiceMethod(Printer* out, const ServiceDescriptor *service,
486 bool use_server_class) {
Jan Tattermusch2d924952015-05-06 10:23:17 -0700487 out->Print(
488 "// creates service definition that can be registered with a server\n");
Jan Tattermuschcc8a4b32016-04-25 13:40:00 -0700489 out->Print("#pragma warning disable 0618\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700490 out->Print(
491 "public static ServerServiceDefinition BindService($interface$ serviceImpl)\n",
Jan Tattermusch10a002d2016-03-14 15:22:19 -0700492 "interface", use_server_class ? GetServerClassName(service) :
493 GetServerInterfaceName(service));
Jan Tattermuschcc8a4b32016-04-25 13:40:00 -0700494 out->Print("#pragma warning restore 0618\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700495 out->Print("{\n");
496 out->Indent();
497
498 out->Print(
499 "return ServerServiceDefinition.CreateBuilder($servicenamefield$)\n",
500 "servicenamefield", GetServiceNameFieldName());
501 out->Indent();
502 out->Indent();
503 for (int i = 0; i < service->method_count(); i++) {
504 const MethodDescriptor *method = service->method(i);
505 out->Print(".AddMethod($methodfield$, serviceImpl.$methodname$)",
506 "methodfield", GetMethodFieldName(method), "methodname",
507 method->name());
508 if (i == service->method_count() - 1) {
509 out->Print(".Build();");
510 }
511 out->Print("\n");
512 }
513 out->Outdent();
514 out->Outdent();
515
516 out->Outdent();
517 out->Print("}\n");
518 out->Print("\n");
519}
520
521void GenerateNewStubMethods(Printer* out, const ServiceDescriptor *service) {
Jan Tattermuschb5332812015-07-14 19:29:35 -0700522 out->Print("// creates a new client\n");
523 out->Print("public static $classname$ NewClient(Channel channel)\n",
524 "classname", GetClientClassName(service));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700525 out->Print("{\n");
526 out->Indent();
527 out->Print("return new $classname$(channel);\n", "classname",
528 GetClientClassName(service));
529 out->Outdent();
530 out->Print("}\n");
531 out->Print("\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700532}
533
Jan Tattermusch44f7c542016-04-25 14:50:27 -0700534void GenerateService(Printer* out, const ServiceDescriptor *service,
Jan Tattermusch4e0f73c2016-04-25 16:11:03 -0700535 bool generate_client, bool generate_server,
536 bool internal_access) {
537 out->Print("$access_level$ static class $classname$\n", "access_level",
538 GetAccessLevel(internal_access), "classname",
Jan Tattermusch2d924952015-05-06 10:23:17 -0700539 GetServiceClassName(service));
540 out->Print("{\n");
541 out->Indent();
542 out->Print("static readonly string $servicenamefield$ = \"$servicename$\";\n",
543 "servicenamefield", GetServiceNameFieldName(), "servicename",
544 service->full_name());
545 out->Print("\n");
546
547 GenerateMarshallerFields(out, service);
548 for (int i = 0; i < service->method_count(); i++) {
549 GenerateStaticMethodField(out, service->method(i));
550 }
Jan Tattermusche6af5d12015-08-03 10:57:43 -0700551 GenerateServiceDescriptorProperty(out, service);
Jan Tattermusch44f7c542016-04-25 14:50:27 -0700552
553 if (generate_client) {
554 GenerateClientInterface(out, service);
555 }
556 if (generate_server) {
557 GenerateServerInterface(out, service);
558 GenerateServerClass(out, service);
559 }
560 if (generate_client) {
561 GenerateClientStub(out, service);
562 GenerateNewStubMethods(out, service);
563 }
564 if (generate_server) {
565 GenerateBindServiceMethod(out, service, false);
566 GenerateBindServiceMethod(out, service, true);
567 }
Jan Tattermusch2d924952015-05-06 10:23:17 -0700568
569 out->Outdent();
570 out->Print("}\n");
571}
572
573} // anonymous namespace
574
Jan Tattermusch5f8872f2016-04-25 15:57:10 -0700575grpc::string GetServices(const FileDescriptor *file, bool generate_client,
Jan Tattermusch4e0f73c2016-04-25 16:11:03 -0700576 bool generate_server, bool internal_access) {
Jan Tattermusch2d924952015-05-06 10:23:17 -0700577 grpc::string output;
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700578 {
579 // Scope the output stream so it closes and finalizes output to the string.
Jan Tattermusch2d924952015-05-06 10:23:17 -0700580
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700581 StringOutputStream output_stream(&output);
582 Printer out(&output_stream, '$');
583
584 // Don't write out any output if there no services, to avoid empty service
585 // files being generated for proto files that don't declare any.
586 if (file->service_count() == 0) {
587 return output;
588 }
589
590 // Write out a file header.
591 out.Print("// Generated by the protocol buffer compiler. DO NOT EDIT!\n");
592 out.Print("// source: $filename$\n", "filename", file->name());
593 out.Print("#region Designer generated code\n");
594 out.Print("\n");
595 out.Print("using System;\n");
596 out.Print("using System.Threading;\n");
597 out.Print("using System.Threading.Tasks;\n");
598 out.Print("using Grpc.Core;\n");
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700599 out.Print("\n");
600
601 out.Print("namespace $namespace$ {\n", "namespace", GetFileNamespace(file));
602 out.Indent();
603 for (int i = 0; i < file->service_count(); i++) {
Jan Tattermusch4e0f73c2016-04-25 16:11:03 -0700604 GenerateService(&out, file->service(i), generate_client, generate_server,
605 internal_access);
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700606 }
607 out.Outdent();
608 out.Print("}\n");
609 out.Print("#endregion\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700610 }
Jan Tattermusch2d924952015-05-06 10:23:17 -0700611 return output;
612}
613
614} // namespace grpc_csharp_generator