blob: 13432334231869fa01cdd6ffe38a7029caf58438 [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
126std::string GetMethodReturnTypeClient(const MethodDescriptor *method) {
127 switch (GetMethodType(method)) {
128 case METHODTYPE_NO_STREAMING:
Jan Tattermusch5269d162015-07-21 11:56:42 -0700129 return "AsyncUnaryCall<" + GetClassName(method->output_type()) + ">";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700130 case METHODTYPE_CLIENT_STREAMING:
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700131 return "AsyncClientStreamingCall<" + GetClassName(method->input_type())
132 + ", " + GetClassName(method->output_type()) + ">";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700133 case METHODTYPE_SERVER_STREAMING:
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700134 return "AsyncServerStreamingCall<" + GetClassName(method->output_type())
Jan Tattermusch2d924952015-05-06 10:23:17 -0700135 + ">";
136 case METHODTYPE_BIDI_STREAMING:
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700137 return "AsyncDuplexStreamingCall<" + GetClassName(method->input_type())
138 + ", " + GetClassName(method->output_type()) + ">";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700139 }
140 GOOGLE_LOG(FATAL)<< "Can't get here.";
141 return "";
142}
143
144std::string GetMethodRequestParamServer(const MethodDescriptor *method) {
145 switch (GetMethodType(method)) {
146 case METHODTYPE_NO_STREAMING:
147 case METHODTYPE_SERVER_STREAMING:
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700148 return GetClassName(method->input_type()) + " request";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700149 case METHODTYPE_CLIENT_STREAMING:
150 case METHODTYPE_BIDI_STREAMING:
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700151 return "IAsyncStreamReader<" + GetClassName(method->input_type())
Jan Tattermusch2d924952015-05-06 10:23:17 -0700152 + "> requestStream";
153 }
154 GOOGLE_LOG(FATAL)<< "Can't get here.";
155 return "";
156}
157
158std::string GetMethodReturnTypeServer(const MethodDescriptor *method) {
159 switch (GetMethodType(method)) {
160 case METHODTYPE_NO_STREAMING:
161 case METHODTYPE_CLIENT_STREAMING:
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700162 return "Task<" + GetClassName(method->output_type()) + ">";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700163 case METHODTYPE_SERVER_STREAMING:
164 case METHODTYPE_BIDI_STREAMING:
165 return "Task";
166 }
167 GOOGLE_LOG(FATAL)<< "Can't get here.";
168 return "";
169}
170
171std::string GetMethodResponseStreamMaybe(const MethodDescriptor *method) {
172 switch (GetMethodType(method)) {
173 case METHODTYPE_NO_STREAMING:
174 case METHODTYPE_CLIENT_STREAMING:
175 return "";
176 case METHODTYPE_SERVER_STREAMING:
177 case METHODTYPE_BIDI_STREAMING:
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700178 return ", IServerStreamWriter<" + GetClassName(method->output_type())
Jan Tattermusch2d924952015-05-06 10:23:17 -0700179 + "> responseStream";
180 }
181 GOOGLE_LOG(FATAL)<< "Can't get here.";
182 return "";
183}
184
185// Gets vector of all messages used as input or output types.
186std::vector<const Descriptor*> GetUsedMessages(
187 const ServiceDescriptor *service) {
188 std::set<const Descriptor*> descriptor_set;
189 std::vector<const Descriptor*> result; // vector is to maintain stable ordering
190 for (int i = 0; i < service->method_count(); i++) {
191 const MethodDescriptor *method = service->method(i);
192 if (descriptor_set.find(method->input_type()) == descriptor_set.end()) {
193 descriptor_set.insert(method->input_type());
194 result.push_back(method->input_type());
195 }
196 if (descriptor_set.find(method->output_type()) == descriptor_set.end()) {
197 descriptor_set.insert(method->output_type());
198 result.push_back(method->output_type());
199 }
200 }
201 return result;
202}
203
204void GenerateMarshallerFields(Printer* out, const ServiceDescriptor *service) {
205 std::vector<const Descriptor*> used_messages = GetUsedMessages(service);
206 for (size_t i = 0; i < used_messages.size(); i++) {
207 const Descriptor *message = used_messages[i];
208 out->Print(
Jan Tattermuschad75dd12015-08-03 09:43:07 -0700209 "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 -0700210 "fieldname", GetMarshallerFieldName(message), "type",
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700211 GetClassName(message));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700212 }
213 out->Print("\n");
214}
215
216void GenerateStaticMethodField(Printer* out, const MethodDescriptor *method) {
217 out->Print(
218 "static readonly Method<$request$, $response$> $fieldname$ = new Method<$request$, $response$>(\n",
219 "fieldname", GetMethodFieldName(method), "request",
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700220 GetClassName(method->input_type()), "response",
221 GetClassName(method->output_type()));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700222 out->Indent();
223 out->Indent();
224 out->Print("$methodtype$,\n", "methodtype",
225 GetCSharpMethodType(GetMethodType(method)));
Jan Tattermuscha9ddd022015-08-05 03:04:58 -0700226 out->Print("$servicenamefield$,\n", "servicenamefield",
227 GetServiceNameFieldName());
Jan Tattermusch2d924952015-05-06 10:23:17 -0700228 out->Print("\"$methodname$\",\n", "methodname", method->name());
229 out->Print("$requestmarshaller$,\n", "requestmarshaller",
230 GetMarshallerFieldName(method->input_type()));
231 out->Print("$responsemarshaller$);\n", "responsemarshaller",
232 GetMarshallerFieldName(method->output_type()));
233 out->Print("\n");
234 out->Outdent();
235 out->Outdent();
236}
237
Jan Tattermusche6af5d12015-08-03 10:57:43 -0700238void GenerateServiceDescriptorProperty(Printer* out, const ServiceDescriptor *service) {
yang-gc3ee1d52015-08-28 11:33:52 -0700239 std::ostringstream index;
240 index << service->index();
Jan Tattermusche6af5d12015-08-03 10:57:43 -0700241 out->Print("// service descriptor\n");
242 out->Print("public static global::Google.Protobuf.Reflection.ServiceDescriptor Descriptor\n");
243 out->Print("{\n");
244 out->Print(" get { return $umbrella$.Descriptor.Services[$index$]; }\n",
Jan Tattermuschda717f42016-01-20 13:12:35 -0800245 "umbrella", GetReflectionClassName(service->file()), "index",
yang-gc3ee1d52015-08-28 11:33:52 -0700246 index.str());
Jan Tattermusche6af5d12015-08-03 10:57:43 -0700247 out->Print("}\n");
248 out->Print("\n");
249}
250
Jan Tattermusch2d924952015-05-06 10:23:17 -0700251void GenerateClientInterface(Printer* out, const ServiceDescriptor *service) {
Jan Tattermuschb5332812015-07-14 19:29:35 -0700252 out->Print("// client interface\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700253 out->Print("public interface $name$\n", "name",
254 GetClientInterfaceName(service));
255 out->Print("{\n");
256 out->Indent();
257 for (int i = 0; i < service->method_count(); i++) {
258 const MethodDescriptor *method = service->method(i);
259 MethodType method_type = GetMethodType(method);
260
261 if (method_type == METHODTYPE_NO_STREAMING) {
262 // unary calls have an extra synchronous stub method
263 out->Print(
Jan Tattermusch74529562015-07-23 14:04:51 -0700264 "$response$ $methodname$($request$ request, Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken));\n",
Jan Tattermusch2d924952015-05-06 10:23:17 -0700265 "methodname", method->name(), "request",
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700266 GetClassName(method->input_type()), "response",
267 GetClassName(method->output_type()));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700268
Jan Tattermusche5e57ad2015-08-05 14:54:08 -0700269 // overload taking CallOptions as a param
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700270 out->Print(
Jan Tattermusche5e57ad2015-08-05 14:54:08 -0700271 "$response$ $methodname$($request$ request, CallOptions options);\n",
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700272 "methodname", method->name(), "request",
273 GetClassName(method->input_type()), "response",
274 GetClassName(method->output_type()));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700275 }
276
277 std::string method_name = method->name();
278 if (method_type == METHODTYPE_NO_STREAMING) {
279 method_name += "Async"; // prevent name clash with synchronous method.
280 }
281 out->Print(
Jan Tattermusch74529562015-07-23 14:04:51 -0700282 "$returntype$ $methodname$($request_maybe$Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken));\n",
Jan Tattermusch2d924952015-05-06 10:23:17 -0700283 "methodname", method_name, "request_maybe",
284 GetMethodRequestParamMaybe(method), "returntype",
285 GetMethodReturnTypeClient(method));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700286
Jan Tattermusche5e57ad2015-08-05 14:54:08 -0700287 // overload taking CallOptions as a param
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700288 out->Print(
Jan Tattermusche5e57ad2015-08-05 14:54:08 -0700289 "$returntype$ $methodname$($request_maybe$CallOptions options);\n",
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700290 "methodname", method_name, "request_maybe",
291 GetMethodRequestParamMaybe(method), "returntype",
292 GetMethodReturnTypeClient(method));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700293 }
294 out->Outdent();
295 out->Print("}\n");
296 out->Print("\n");
297}
298
299void GenerateServerInterface(Printer* out, const ServiceDescriptor *service) {
300 out->Print("// server-side interface\n");
Jan Tattermusch10a002d2016-03-14 15:22:19 -0700301 out->Print("[System.Obsolete(\"Service implementations should inherit"
302 " from the generated abstract base class instead.\")]\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700303 out->Print("public interface $name$\n", "name",
304 GetServerInterfaceName(service));
305 out->Print("{\n");
306 out->Indent();
307 for (int i = 0; i < service->method_count(); i++) {
308 const MethodDescriptor *method = service->method(i);
Craig Tillerb256faa2015-07-23 11:28:16 -0700309 out->Print(
310 "$returntype$ $methodname$($request$$response_stream_maybe$, "
311 "ServerCallContext context);\n",
312 "methodname", method->name(), "returntype",
313 GetMethodReturnTypeServer(method), "request",
314 GetMethodRequestParamServer(method), "response_stream_maybe",
315 GetMethodResponseStreamMaybe(method));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700316 }
317 out->Outdent();
318 out->Print("}\n");
319 out->Print("\n");
320}
321
Jan Tattermusch10a002d2016-03-14 15:22:19 -0700322void GenerateServerClass(Printer* out, const ServiceDescriptor *service) {
323 out->Print("// server-side abstract class\n");
324 out->Print("public abstract class $name$\n", "name",
325 GetServerClassName(service));
326 out->Print("{\n");
327 out->Indent();
328 for (int i = 0; i < service->method_count(); i++) {
329 const MethodDescriptor *method = service->method(i);
330 out->Print(
331 "public virtual $returntype$ $methodname$($request$$response_stream_maybe$, "
332 "ServerCallContext context)\n",
333 "methodname", method->name(), "returntype",
334 GetMethodReturnTypeServer(method), "request",
335 GetMethodRequestParamServer(method), "response_stream_maybe",
336 GetMethodResponseStreamMaybe(method));
337 out->Print("{\n");
338 out->Indent();
339 out->Print("throw new RpcException("
340 "new Status(StatusCode.Unimplemented, \"\"));\n");
341 out->Outdent();
342 out->Print("}\n\n");
343 }
344 out->Outdent();
345 out->Print("}\n");
346 out->Print("\n");
347}
348
Jan Tattermusch2d924952015-05-06 10:23:17 -0700349void GenerateClientStub(Printer* out, const ServiceDescriptor *service) {
350 out->Print("// client stub\n");
351 out->Print(
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700352 "public class $name$ : ClientBase<$name$>\n",
353 "name", GetClientClassName(service));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700354 out->Print("{\n");
355 out->Indent();
356
357 // constructors
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700358 out->Print("public $name$(Channel channel) : base(channel)\n",
359 "name", GetClientClassName(service));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700360 out->Print("{\n");
361 out->Print("}\n");
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700362 out->Print("public $name$(CallInvoker callInvoker) : base(callInvoker)\n",
363 "name", GetClientClassName(service));
364 out->Print("{\n");
Jan Tattermuschefb77842016-03-23 07:47:36 -0700365 out->Print("}\n");
366 out->Print("///<summary>Parameterless constructor to allow creation"
367 " of test doubles.</summary>\n");
368 out->Print("protected $name$() : base()\n",
369 "name", GetClientClassName(service));
370 out->Print("{\n");
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700371 out->Print("}\n\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700372
373 for (int i = 0; i < service->method_count(); i++) {
374 const MethodDescriptor *method = service->method(i);
375 MethodType method_type = GetMethodType(method);
376
377 if (method_type == METHODTYPE_NO_STREAMING) {
378 // unary calls have an extra synchronous stub method
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700379 out->Print("public virtual $response$ $methodname$($request$ request, Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken))\n",
380 "methodname", method->name(), "request",
381 GetClassName(method->input_type()), "response",
382 GetClassName(method->output_type()));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700383 out->Print("{\n");
384 out->Indent();
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700385 out->Print("return $methodname$(request, new CallOptions(headers, deadline, cancellationToken));\n",
386 "methodname", method->name());
387 out->Outdent();
388 out->Print("}\n");
389
390 // overload taking CallOptions as a param
391 out->Print("public virtual $response$ $methodname$($request$ request, CallOptions options)\n",
392 "methodname", method->name(), "request",
393 GetClassName(method->input_type()), "response",
394 GetClassName(method->output_type()));
395 out->Print("{\n");
396 out->Indent();
Jan Tattermuschb455bcc2016-03-22 16:08:18 -0700397 out->Print("return CallInvoker.BlockingUnaryCall($methodfield$, null, options, request);\n",
Jan Tattermusch641cb1b2015-08-05 03:51:46 -0700398 "methodfield", GetMethodFieldName(method));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700399 out->Outdent();
400 out->Print("}\n");
401 }
402
403 std::string method_name = method->name();
404 if (method_type == METHODTYPE_NO_STREAMING) {
405 method_name += "Async"; // prevent name clash with synchronous method.
406 }
407 out->Print(
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700408 "public virtual $returntype$ $methodname$($request_maybe$Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken))\n",
409 "methodname", method_name, "request_maybe",
410 GetMethodRequestParamMaybe(method), "returntype",
411 GetMethodReturnTypeClient(method));
412 out->Print("{\n");
413 out->Indent();
414
415 out->Print("return $methodname$($request_maybe$new CallOptions(headers, deadline, cancellationToken));\n",
416 "methodname", method_name,
417 "request_maybe", GetMethodRequestParamMaybe(method, true));
418 out->Outdent();
419 out->Print("}\n");
420
421 // overload taking CallOptions as a param
422 out->Print(
423 "public virtual $returntype$ $methodname$($request_maybe$CallOptions options)\n",
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700424 "methodname", method_name, "request_maybe",
425 GetMethodRequestParamMaybe(method), "returntype",
426 GetMethodReturnTypeClient(method));
427 out->Print("{\n");
428 out->Indent();
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700429 switch (GetMethodType(method)) {
430 case METHODTYPE_NO_STREAMING:
Jan Tattermuschb455bcc2016-03-22 16:08:18 -0700431 out->Print("return CallInvoker.AsyncUnaryCall($methodfield$, null, options, request);\n",
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700432 "methodfield", GetMethodFieldName(method));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700433 break;
434 case METHODTYPE_CLIENT_STREAMING:
Jan Tattermuschb455bcc2016-03-22 16:08:18 -0700435 out->Print("return CallInvoker.AsyncClientStreamingCall($methodfield$, null, options);\n",
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700436 "methodfield", GetMethodFieldName(method));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700437 break;
438 case METHODTYPE_SERVER_STREAMING:
439 out->Print(
Jan Tattermuschb455bcc2016-03-22 16:08:18 -0700440 "return CallInvoker.AsyncServerStreamingCall($methodfield$, null, options, request);\n",
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700441 "methodfield", GetMethodFieldName(method));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700442 break;
443 case METHODTYPE_BIDI_STREAMING:
Jan Tattermuschb455bcc2016-03-22 16:08:18 -0700444 out->Print("return CallInvoker.AsyncDuplexStreamingCall($methodfield$, null, options);\n",
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700445 "methodfield", GetMethodFieldName(method));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700446 break;
447 default:
448 GOOGLE_LOG(FATAL)<< "Can't get here.";
449 }
Jan Tattermusch2d924952015-05-06 10:23:17 -0700450 out->Outdent();
451 out->Print("}\n");
452 }
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700453
454 // override NewInstance method
455 out->Print("protected override $name$ NewInstance(CallInvoker callInvoker)\n",
456 "name", GetClientClassName(service));
457 out->Print("{\n");
458 out->Indent();
459 out->Print("return new $name$(callInvoker);\n",
460 "name", GetClientClassName(service));
461 out->Outdent();
462 out->Print("}\n");
463
Jan Tattermusch2d924952015-05-06 10:23:17 -0700464 out->Outdent();
465 out->Print("}\n");
466 out->Print("\n");
467}
468
Jan Tattermusch10a002d2016-03-14 15:22:19 -0700469void GenerateBindServiceMethod(Printer* out, const ServiceDescriptor *service,
470 bool use_server_class) {
Jan Tattermusch2d924952015-05-06 10:23:17 -0700471 out->Print(
472 "// creates service definition that can be registered with a server\n");
473 out->Print(
474 "public static ServerServiceDefinition BindService($interface$ serviceImpl)\n",
Jan Tattermusch10a002d2016-03-14 15:22:19 -0700475 "interface", use_server_class ? GetServerClassName(service) :
476 GetServerInterfaceName(service));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700477 out->Print("{\n");
478 out->Indent();
479
480 out->Print(
481 "return ServerServiceDefinition.CreateBuilder($servicenamefield$)\n",
482 "servicenamefield", GetServiceNameFieldName());
483 out->Indent();
484 out->Indent();
485 for (int i = 0; i < service->method_count(); i++) {
486 const MethodDescriptor *method = service->method(i);
487 out->Print(".AddMethod($methodfield$, serviceImpl.$methodname$)",
488 "methodfield", GetMethodFieldName(method), "methodname",
489 method->name());
490 if (i == service->method_count() - 1) {
491 out->Print(".Build();");
492 }
493 out->Print("\n");
494 }
495 out->Outdent();
496 out->Outdent();
497
498 out->Outdent();
499 out->Print("}\n");
500 out->Print("\n");
501}
502
503void GenerateNewStubMethods(Printer* out, const ServiceDescriptor *service) {
Jan Tattermuschb5332812015-07-14 19:29:35 -0700504 out->Print("// creates a new client\n");
505 out->Print("public static $classname$ NewClient(Channel channel)\n",
506 "classname", GetClientClassName(service));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700507 out->Print("{\n");
508 out->Indent();
509 out->Print("return new $classname$(channel);\n", "classname",
510 GetClientClassName(service));
511 out->Outdent();
512 out->Print("}\n");
513 out->Print("\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700514}
515
516void GenerateService(Printer* out, const ServiceDescriptor *service) {
517 out->Print("public static class $classname$\n", "classname",
518 GetServiceClassName(service));
519 out->Print("{\n");
520 out->Indent();
521 out->Print("static readonly string $servicenamefield$ = \"$servicename$\";\n",
522 "servicenamefield", GetServiceNameFieldName(), "servicename",
523 service->full_name());
524 out->Print("\n");
525
526 GenerateMarshallerFields(out, service);
527 for (int i = 0; i < service->method_count(); i++) {
528 GenerateStaticMethodField(out, service->method(i));
529 }
Jan Tattermusche6af5d12015-08-03 10:57:43 -0700530 GenerateServiceDescriptorProperty(out, service);
Jan Tattermusch2d924952015-05-06 10:23:17 -0700531 GenerateClientInterface(out, service);
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700532 //GenerateClientBaseClass(out, service);
Jan Tattermusch2d924952015-05-06 10:23:17 -0700533 GenerateServerInterface(out, service);
Jan Tattermusch10a002d2016-03-14 15:22:19 -0700534 GenerateServerClass(out, service);
Jan Tattermusch2d924952015-05-06 10:23:17 -0700535 GenerateClientStub(out, service);
Jan Tattermusch10a002d2016-03-14 15:22:19 -0700536 GenerateBindServiceMethod(out, service, false);
537 GenerateBindServiceMethod(out, service, true);
Jan Tattermusch2d924952015-05-06 10:23:17 -0700538 GenerateNewStubMethods(out, service);
539
540 out->Outdent();
541 out->Print("}\n");
542}
543
544} // anonymous namespace
545
546grpc::string GetServices(const FileDescriptor *file) {
547 grpc::string output;
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700548 {
549 // Scope the output stream so it closes and finalizes output to the string.
Jan Tattermusch2d924952015-05-06 10:23:17 -0700550
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700551 StringOutputStream output_stream(&output);
552 Printer out(&output_stream, '$');
553
554 // Don't write out any output if there no services, to avoid empty service
555 // files being generated for proto files that don't declare any.
556 if (file->service_count() == 0) {
557 return output;
558 }
559
560 // Write out a file header.
561 out.Print("// Generated by the protocol buffer compiler. DO NOT EDIT!\n");
562 out.Print("// source: $filename$\n", "filename", file->name());
563 out.Print("#region Designer generated code\n");
564 out.Print("\n");
565 out.Print("using System;\n");
566 out.Print("using System.Threading;\n");
567 out.Print("using System.Threading.Tasks;\n");
568 out.Print("using Grpc.Core;\n");
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700569 out.Print("\n");
570
571 out.Print("namespace $namespace$ {\n", "namespace", GetFileNamespace(file));
572 out.Indent();
573 for (int i = 0; i < file->service_count(); i++) {
574 GenerateService(&out, file->service(i));
575 }
576 out.Outdent();
577 out.Print("}\n");
578 out.Print("#endregion\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700579 }
Jan Tattermusch2d924952015-05-06 10:23:17 -0700580 return output;
581}
582
583} // namespace grpc_csharp_generator