blob: fb11d98fe471a064d75d64951cdaa68f6524083d [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
Jan Tattermusch8cd4ae92016-03-21 18:46:32 -070076std::string GetClientBaseClassName(const ServiceDescriptor* service) {
77 return service->name() + "ClientBase";
78}
79
Jan Tattermusch2d924952015-05-06 10:23:17 -070080std::string GetClientClassName(const ServiceDescriptor* service) {
81 return service->name() + "Client";
82}
83
84std::string GetServerInterfaceName(const ServiceDescriptor* service) {
85 return "I" + service->name();
86}
87
Jan Tattermusch10a002d2016-03-14 15:22:19 -070088std::string GetServerClassName(const ServiceDescriptor* service) {
89 return service->name() + "Base";
90}
91
Jan Tattermusch2d924952015-05-06 10:23:17 -070092std::string GetCSharpMethodType(MethodType method_type) {
93 switch (method_type) {
94 case METHODTYPE_NO_STREAMING:
95 return "MethodType.Unary";
96 case METHODTYPE_CLIENT_STREAMING:
97 return "MethodType.ClientStreaming";
98 case METHODTYPE_SERVER_STREAMING:
99 return "MethodType.ServerStreaming";
100 case METHODTYPE_BIDI_STREAMING:
101 return "MethodType.DuplexStreaming";
102 }
103 GOOGLE_LOG(FATAL)<< "Can't get here.";
104 return "";
105}
106
107std::string GetServiceNameFieldName() {
108 return "__ServiceName";
109}
110
111std::string GetMarshallerFieldName(const Descriptor *message) {
112 return "__Marshaller_" + message->name();
113}
114
115std::string GetMethodFieldName(const MethodDescriptor *method) {
116 return "__Method_" + method->name();
117}
118
Jan Tattermusch8cd4ae92016-03-21 18:46:32 -0700119std::string GetMethodRequestParamMaybe(const MethodDescriptor *method,
120 bool invocation_param=false) {
Jan Tattermusch2d924952015-05-06 10:23:17 -0700121 if (method->client_streaming()) {
122 return "";
123 }
Jan Tattermusch8cd4ae92016-03-21 18:46:32 -0700124 if (invocation_param) {
125 return "request, ";
126 }
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700127 return GetClassName(method->input_type()) + " request, ";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700128}
129
130std::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 Tattermusch2d924952015-05-06 10:23:17 -0700257 out->Print("public interface $name$\n", "name",
258 GetClientInterfaceName(service));
259 out->Print("{\n");
260 out->Indent();
261 for (int i = 0; i < service->method_count(); i++) {
262 const MethodDescriptor *method = service->method(i);
263 MethodType method_type = GetMethodType(method);
264
265 if (method_type == METHODTYPE_NO_STREAMING) {
266 // unary calls have an extra synchronous stub method
267 out->Print(
Jan Tattermusch74529562015-07-23 14:04:51 -0700268 "$response$ $methodname$($request$ request, Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken));\n",
Jan Tattermusch2d924952015-05-06 10:23:17 -0700269 "methodname", method->name(), "request",
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700270 GetClassName(method->input_type()), "response",
271 GetClassName(method->output_type()));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700272
Jan Tattermusche5e57ad2015-08-05 14:54:08 -0700273 // overload taking CallOptions as a param
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700274 out->Print(
Jan Tattermusche5e57ad2015-08-05 14:54:08 -0700275 "$response$ $methodname$($request$ request, CallOptions options);\n",
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700276 "methodname", method->name(), "request",
277 GetClassName(method->input_type()), "response",
278 GetClassName(method->output_type()));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700279 }
280
281 std::string method_name = method->name();
282 if (method_type == METHODTYPE_NO_STREAMING) {
283 method_name += "Async"; // prevent name clash with synchronous method.
284 }
285 out->Print(
Jan Tattermusch74529562015-07-23 14:04:51 -0700286 "$returntype$ $methodname$($request_maybe$Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken));\n",
Jan Tattermusch2d924952015-05-06 10:23:17 -0700287 "methodname", method_name, "request_maybe",
288 GetMethodRequestParamMaybe(method), "returntype",
289 GetMethodReturnTypeClient(method));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700290
Jan Tattermusche5e57ad2015-08-05 14:54:08 -0700291 // overload taking CallOptions as a param
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700292 out->Print(
Jan Tattermusche5e57ad2015-08-05 14:54:08 -0700293 "$returntype$ $methodname$($request_maybe$CallOptions options);\n",
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700294 "methodname", method_name, "request_maybe",
295 GetMethodRequestParamMaybe(method), "returntype",
296 GetMethodReturnTypeClient(method));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700297 }
298 out->Outdent();
299 out->Print("}\n");
300 out->Print("\n");
301}
302
Jan Tattermusch8cd4ae92016-03-21 18:46:32 -0700303void GenerateClientBaseClass(Printer* out, const ServiceDescriptor *service) {
304 out->Print("// abstract client class\n");
305 out->Print("public abstract class $name$ : ClientBase\n", "name",
306 GetClientBaseClassName(service));
307 out->Print("{\n");
308 out->Indent();
309
310 // constructors
311 out->Print(
312 "public $name$(Channel channel) : base(channel)\n",
313 "name", GetClientBaseClassName(service));
314 out->Print("{\n");
315 out->Print("}\n");
316
317 for (int i = 0; i < service->method_count(); i++) {
318 const MethodDescriptor *method = service->method(i);
319 MethodType method_type = GetMethodType(method);
320
321 if (method_type == METHODTYPE_NO_STREAMING) {
322 // unary calls have an extra synchronous stub method
323 out->Print(
324 "public virtual $response$ $methodname$($request$ request, Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken))\n",
325 "methodname", method->name(), "request",
326 GetClassName(method->input_type()), "response",
327 GetClassName(method->output_type()));
328 out->Print("{\n");
329 out->Indent();
330 out->Print("return $methodname$(request, new CallOptions(headers, deadline, cancellationToken));\n",
331 "methodname", method->name());
332 out->Outdent();
333 out->Print("}\n");
334
335 // overload taking CallOptions as a param
336 out->Print(
337 "public virtual $response$ $methodname$($request$ request, CallOptions options)\n",
338 "methodname", method->name(), "request",
339 GetClassName(method->input_type()), "response",
340 GetClassName(method->output_type()));
341 out->Print("{\n");
342 out->Indent();
343 out->Print("throw new NotImplementedException();\n");
344 out->Outdent();
345 out->Print("}\n");
346 }
347
348 std::string method_name = method->name();
349 if (method_type == METHODTYPE_NO_STREAMING) {
350 method_name += "Async"; // prevent name clash with synchronous method.
351 }
352 out->Print(
353 "public virtual $returntype$ $methodname$($request_maybe$Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken))\n",
354 "methodname", method_name, "request_maybe",
355 GetMethodRequestParamMaybe(method), "returntype",
356 GetMethodReturnTypeClient(method));
357 out->Print("{\n");
358 out->Indent();
359
360 out->Print("return $methodname$($request_maybe$new CallOptions(headers, deadline, cancellationToken));\n",
361 "methodname", method_name,
362 "request_maybe", GetMethodRequestParamMaybe(method, true));
363 out->Outdent();
364 out->Print("}\n");
365
366 // overload taking CallOptions as a param
367 out->Print(
368 "public virtual $returntype$ $methodname$($request_maybe$CallOptions options)\n",
369 "methodname", method_name, "request_maybe",
370 GetMethodRequestParamMaybe(method), "returntype",
371 GetMethodReturnTypeClient(method));
372 out->Print("{\n");
373 out->Indent();
374 out->Print("throw new NotImplementedException();\n");
375 out->Outdent();
376 out->Print("}\n");
377 }
378 out->Outdent();
379 out->Print("}\n");
380 out->Print("\n");
381}
382
Jan Tattermusch2d924952015-05-06 10:23:17 -0700383void GenerateServerInterface(Printer* out, const ServiceDescriptor *service) {
384 out->Print("// server-side interface\n");
Jan Tattermusch10a002d2016-03-14 15:22:19 -0700385 out->Print("[System.Obsolete(\"Service implementations should inherit"
386 " from the generated abstract base class instead.\")]\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700387 out->Print("public interface $name$\n", "name",
388 GetServerInterfaceName(service));
389 out->Print("{\n");
390 out->Indent();
391 for (int i = 0; i < service->method_count(); i++) {
392 const MethodDescriptor *method = service->method(i);
Craig Tillerb256faa2015-07-23 11:28:16 -0700393 out->Print(
394 "$returntype$ $methodname$($request$$response_stream_maybe$, "
395 "ServerCallContext context);\n",
396 "methodname", method->name(), "returntype",
397 GetMethodReturnTypeServer(method), "request",
398 GetMethodRequestParamServer(method), "response_stream_maybe",
399 GetMethodResponseStreamMaybe(method));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700400 }
401 out->Outdent();
402 out->Print("}\n");
403 out->Print("\n");
404}
405
Jan Tattermusch10a002d2016-03-14 15:22:19 -0700406void GenerateServerClass(Printer* out, const ServiceDescriptor *service) {
407 out->Print("// server-side abstract class\n");
408 out->Print("public abstract class $name$\n", "name",
409 GetServerClassName(service));
410 out->Print("{\n");
411 out->Indent();
412 for (int i = 0; i < service->method_count(); i++) {
413 const MethodDescriptor *method = service->method(i);
414 out->Print(
415 "public virtual $returntype$ $methodname$($request$$response_stream_maybe$, "
416 "ServerCallContext context)\n",
417 "methodname", method->name(), "returntype",
418 GetMethodReturnTypeServer(method), "request",
419 GetMethodRequestParamServer(method), "response_stream_maybe",
420 GetMethodResponseStreamMaybe(method));
421 out->Print("{\n");
422 out->Indent();
423 out->Print("throw new RpcException("
424 "new Status(StatusCode.Unimplemented, \"\"));\n");
425 out->Outdent();
426 out->Print("}\n\n");
427 }
428 out->Outdent();
429 out->Print("}\n");
430 out->Print("\n");
431}
432
Jan Tattermusch2d924952015-05-06 10:23:17 -0700433void GenerateClientStub(Printer* out, const ServiceDescriptor *service) {
434 out->Print("// client stub\n");
435 out->Print(
Jan Tattermusch8cd4ae92016-03-21 18:46:32 -0700436 "public class $name$ : $baseclass$\n",
437 "name", GetClientClassName(service),
438 "baseclass", GetClientBaseClassName(service));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700439 out->Print("{\n");
440 out->Indent();
441
442 // constructors
443 out->Print(
Jan Tattermuschb5332812015-07-14 19:29:35 -0700444 "public $name$(Channel channel) : base(channel)\n",
Jan Tattermusch2d924952015-05-06 10:23:17 -0700445 "name", GetClientClassName(service));
446 out->Print("{\n");
447 out->Print("}\n");
448
449 for (int i = 0; i < service->method_count(); i++) {
450 const MethodDescriptor *method = service->method(i);
451 MethodType method_type = GetMethodType(method);
452
453 if (method_type == METHODTYPE_NO_STREAMING) {
454 // unary calls have an extra synchronous stub method
455 out->Print(
Jan Tattermusch8cd4ae92016-03-21 18:46:32 -0700456 "public override $response$ $methodname$($request$ request, CallOptions options)\n",
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700457 "methodname", method->name(), "request",
458 GetClassName(method->input_type()), "response",
459 GetClassName(method->output_type()));
460 out->Print("{\n");
461 out->Indent();
Jan Tattermusche5e57ad2015-08-05 14:54:08 -0700462 out->Print("var call = CreateCall($methodfield$, options);\n",
Jan Tattermusch641cb1b2015-08-05 03:51:46 -0700463 "methodfield", GetMethodFieldName(method));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700464 out->Print("return Calls.BlockingUnaryCall(call, request);\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700465 out->Outdent();
466 out->Print("}\n");
467 }
468
469 std::string method_name = method->name();
470 if (method_type == METHODTYPE_NO_STREAMING) {
471 method_name += "Async"; // prevent name clash with synchronous method.
472 }
473 out->Print(
Jan Tattermusch8cd4ae92016-03-21 18:46:32 -0700474 "public override $returntype$ $methodname$($request_maybe$CallOptions options)\n",
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700475 "methodname", method_name, "request_maybe",
476 GetMethodRequestParamMaybe(method), "returntype",
477 GetMethodReturnTypeClient(method));
478 out->Print("{\n");
479 out->Indent();
Jan Tattermusche5e57ad2015-08-05 14:54:08 -0700480 out->Print("var call = CreateCall($methodfield$, options);\n",
Jan Tattermusch641cb1b2015-08-05 03:51:46 -0700481 "methodfield", GetMethodFieldName(method));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700482 switch (GetMethodType(method)) {
483 case METHODTYPE_NO_STREAMING:
484 out->Print("return Calls.AsyncUnaryCall(call, request);\n");
485 break;
486 case METHODTYPE_CLIENT_STREAMING:
487 out->Print("return Calls.AsyncClientStreamingCall(call);\n");
488 break;
489 case METHODTYPE_SERVER_STREAMING:
490 out->Print(
491 "return Calls.AsyncServerStreamingCall(call, request);\n");
492 break;
493 case METHODTYPE_BIDI_STREAMING:
494 out->Print("return Calls.AsyncDuplexStreamingCall(call);\n");
495 break;
496 default:
497 GOOGLE_LOG(FATAL)<< "Can't get here.";
498 }
Jan Tattermusch2d924952015-05-06 10:23:17 -0700499 out->Outdent();
500 out->Print("}\n");
501 }
502 out->Outdent();
503 out->Print("}\n");
504 out->Print("\n");
505}
506
Jan Tattermusch10a002d2016-03-14 15:22:19 -0700507void GenerateBindServiceMethod(Printer* out, const ServiceDescriptor *service,
508 bool use_server_class) {
Jan Tattermusch2d924952015-05-06 10:23:17 -0700509 out->Print(
510 "// creates service definition that can be registered with a server\n");
511 out->Print(
512 "public static ServerServiceDefinition BindService($interface$ serviceImpl)\n",
Jan Tattermusch10a002d2016-03-14 15:22:19 -0700513 "interface", use_server_class ? GetServerClassName(service) :
514 GetServerInterfaceName(service));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700515 out->Print("{\n");
516 out->Indent();
517
518 out->Print(
519 "return ServerServiceDefinition.CreateBuilder($servicenamefield$)\n",
520 "servicenamefield", GetServiceNameFieldName());
521 out->Indent();
522 out->Indent();
523 for (int i = 0; i < service->method_count(); i++) {
524 const MethodDescriptor *method = service->method(i);
525 out->Print(".AddMethod($methodfield$, serviceImpl.$methodname$)",
526 "methodfield", GetMethodFieldName(method), "methodname",
527 method->name());
528 if (i == service->method_count() - 1) {
529 out->Print(".Build();");
530 }
531 out->Print("\n");
532 }
533 out->Outdent();
534 out->Outdent();
535
536 out->Outdent();
537 out->Print("}\n");
538 out->Print("\n");
539}
540
541void GenerateNewStubMethods(Printer* out, const ServiceDescriptor *service) {
Jan Tattermuschb5332812015-07-14 19:29:35 -0700542 out->Print("// creates a new client\n");
543 out->Print("public static $classname$ NewClient(Channel channel)\n",
544 "classname", GetClientClassName(service));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700545 out->Print("{\n");
546 out->Indent();
547 out->Print("return new $classname$(channel);\n", "classname",
548 GetClientClassName(service));
549 out->Outdent();
550 out->Print("}\n");
551 out->Print("\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700552}
553
554void GenerateService(Printer* out, const ServiceDescriptor *service) {
555 out->Print("public static class $classname$\n", "classname",
556 GetServiceClassName(service));
557 out->Print("{\n");
558 out->Indent();
559 out->Print("static readonly string $servicenamefield$ = \"$servicename$\";\n",
560 "servicenamefield", GetServiceNameFieldName(), "servicename",
561 service->full_name());
562 out->Print("\n");
563
564 GenerateMarshallerFields(out, service);
565 for (int i = 0; i < service->method_count(); i++) {
566 GenerateStaticMethodField(out, service->method(i));
567 }
Jan Tattermusche6af5d12015-08-03 10:57:43 -0700568 GenerateServiceDescriptorProperty(out, service);
Jan Tattermusch2d924952015-05-06 10:23:17 -0700569 GenerateClientInterface(out, service);
Jan Tattermusch8cd4ae92016-03-21 18:46:32 -0700570 GenerateClientBaseClass(out, service);
Jan Tattermusch2d924952015-05-06 10:23:17 -0700571 GenerateServerInterface(out, service);
Jan Tattermusch10a002d2016-03-14 15:22:19 -0700572 GenerateServerClass(out, service);
Jan Tattermusch2d924952015-05-06 10:23:17 -0700573 GenerateClientStub(out, service);
Jan Tattermusch10a002d2016-03-14 15:22:19 -0700574 GenerateBindServiceMethod(out, service, false);
575 GenerateBindServiceMethod(out, service, true);
Jan Tattermusch2d924952015-05-06 10:23:17 -0700576 GenerateNewStubMethods(out, service);
577
578 out->Outdent();
579 out->Print("}\n");
580}
581
582} // anonymous namespace
583
584grpc::string GetServices(const FileDescriptor *file) {
585 grpc::string output;
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700586 {
587 // Scope the output stream so it closes and finalizes output to the string.
Jan Tattermusch2d924952015-05-06 10:23:17 -0700588
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700589 StringOutputStream output_stream(&output);
590 Printer out(&output_stream, '$');
591
592 // Don't write out any output if there no services, to avoid empty service
593 // files being generated for proto files that don't declare any.
594 if (file->service_count() == 0) {
595 return output;
596 }
597
598 // Write out a file header.
599 out.Print("// Generated by the protocol buffer compiler. DO NOT EDIT!\n");
600 out.Print("// source: $filename$\n", "filename", file->name());
601 out.Print("#region Designer generated code\n");
602 out.Print("\n");
603 out.Print("using System;\n");
604 out.Print("using System.Threading;\n");
605 out.Print("using System.Threading.Tasks;\n");
606 out.Print("using Grpc.Core;\n");
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700607 out.Print("\n");
608
609 out.Print("namespace $namespace$ {\n", "namespace", GetFileNamespace(file));
610 out.Indent();
611 for (int i = 0; i < file->service_count(); i++) {
612 GenerateService(&out, file->service(i));
613 }
614 out.Outdent();
615 out.Print("}\n");
616 out.Print("#endregion\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700617 }
Jan Tattermusch2d924952015-05-06 10:23:17 -0700618 return output;
619}
620
621} // namespace grpc_csharp_generator