blob: 08cd41dee7d4883659cb30ce5d43734594917e26 [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 Tattermusch5a4e1e32016-03-25 16:51:55 -0700253 out->Print("[System.Obsolete(\"Client side interfaced will be removed "
254 "in the next release. Use client class directly.\")]\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700255 out->Print("public interface $name$\n", "name",
256 GetClientInterfaceName(service));
257 out->Print("{\n");
258 out->Indent();
259 for (int i = 0; i < service->method_count(); i++) {
260 const MethodDescriptor *method = service->method(i);
261 MethodType method_type = GetMethodType(method);
262
263 if (method_type == METHODTYPE_NO_STREAMING) {
264 // unary calls have an extra synchronous stub method
265 out->Print(
Jan Tattermusch74529562015-07-23 14:04:51 -0700266 "$response$ $methodname$($request$ request, Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken));\n",
Jan Tattermusch2d924952015-05-06 10:23:17 -0700267 "methodname", method->name(), "request",
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700268 GetClassName(method->input_type()), "response",
269 GetClassName(method->output_type()));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700270
Jan Tattermusche5e57ad2015-08-05 14:54:08 -0700271 // overload taking CallOptions as a param
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700272 out->Print(
Jan Tattermusche5e57ad2015-08-05 14:54:08 -0700273 "$response$ $methodname$($request$ request, CallOptions options);\n",
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700274 "methodname", method->name(), "request",
275 GetClassName(method->input_type()), "response",
276 GetClassName(method->output_type()));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700277 }
278
279 std::string method_name = method->name();
280 if (method_type == METHODTYPE_NO_STREAMING) {
281 method_name += "Async"; // prevent name clash with synchronous method.
282 }
283 out->Print(
Jan Tattermusch74529562015-07-23 14:04:51 -0700284 "$returntype$ $methodname$($request_maybe$Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken));\n",
Jan Tattermusch2d924952015-05-06 10:23:17 -0700285 "methodname", method_name, "request_maybe",
286 GetMethodRequestParamMaybe(method), "returntype",
287 GetMethodReturnTypeClient(method));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700288
Jan Tattermusche5e57ad2015-08-05 14:54:08 -0700289 // overload taking CallOptions as a param
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700290 out->Print(
Jan Tattermusche5e57ad2015-08-05 14:54:08 -0700291 "$returntype$ $methodname$($request_maybe$CallOptions options);\n",
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700292 "methodname", method_name, "request_maybe",
293 GetMethodRequestParamMaybe(method), "returntype",
294 GetMethodReturnTypeClient(method));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700295 }
296 out->Outdent();
297 out->Print("}\n");
298 out->Print("\n");
299}
300
301void GenerateServerInterface(Printer* out, const ServiceDescriptor *service) {
302 out->Print("// server-side interface\n");
Jan Tattermusch10a002d2016-03-14 15:22:19 -0700303 out->Print("[System.Obsolete(\"Service implementations should inherit"
304 " from the generated abstract base class instead.\")]\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700305 out->Print("public interface $name$\n", "name",
306 GetServerInterfaceName(service));
307 out->Print("{\n");
308 out->Indent();
309 for (int i = 0; i < service->method_count(); i++) {
310 const MethodDescriptor *method = service->method(i);
Craig Tillerb256faa2015-07-23 11:28:16 -0700311 out->Print(
312 "$returntype$ $methodname$($request$$response_stream_maybe$, "
313 "ServerCallContext context);\n",
314 "methodname", method->name(), "returntype",
315 GetMethodReturnTypeServer(method), "request",
316 GetMethodRequestParamServer(method), "response_stream_maybe",
317 GetMethodResponseStreamMaybe(method));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700318 }
319 out->Outdent();
320 out->Print("}\n");
321 out->Print("\n");
322}
323
Jan Tattermusch10a002d2016-03-14 15:22:19 -0700324void GenerateServerClass(Printer* out, const ServiceDescriptor *service) {
325 out->Print("// server-side abstract class\n");
326 out->Print("public abstract class $name$\n", "name",
327 GetServerClassName(service));
328 out->Print("{\n");
329 out->Indent();
330 for (int i = 0; i < service->method_count(); i++) {
331 const MethodDescriptor *method = service->method(i);
332 out->Print(
333 "public virtual $returntype$ $methodname$($request$$response_stream_maybe$, "
334 "ServerCallContext context)\n",
335 "methodname", method->name(), "returntype",
336 GetMethodReturnTypeServer(method), "request",
337 GetMethodRequestParamServer(method), "response_stream_maybe",
338 GetMethodResponseStreamMaybe(method));
339 out->Print("{\n");
340 out->Indent();
341 out->Print("throw new RpcException("
342 "new Status(StatusCode.Unimplemented, \"\"));\n");
343 out->Outdent();
344 out->Print("}\n\n");
345 }
346 out->Outdent();
347 out->Print("}\n");
348 out->Print("\n");
349}
350
Jan Tattermusch2d924952015-05-06 10:23:17 -0700351void GenerateClientStub(Printer* out, const ServiceDescriptor *service) {
352 out->Print("// client stub\n");
Jan Tattermuschcc8a4b32016-04-25 13:40:00 -0700353 out->Print("#pragma warning disable 0618\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700354 out->Print(
Jan Tattermusch5a4e1e32016-03-25 16:51:55 -0700355 "public class $name$ : ClientBase<$name$>, $interface$\n",
356 "name", GetClientClassName(service),
357 "interface", GetClientInterfaceName(service));
Jan Tattermuschcc8a4b32016-04-25 13:40:00 -0700358 out->Print("#pragma warning restore 0618\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700359 out->Print("{\n");
360 out->Indent();
361
362 // constructors
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700363 out->Print("public $name$(Channel channel) : base(channel)\n",
364 "name", GetClientClassName(service));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700365 out->Print("{\n");
366 out->Print("}\n");
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700367 out->Print("public $name$(CallInvoker callInvoker) : base(callInvoker)\n",
368 "name", GetClientClassName(service));
369 out->Print("{\n");
Jan Tattermuschefb77842016-03-23 07:47:36 -0700370 out->Print("}\n");
Jan Tattermusch2f0a8372016-03-23 09:16:49 -0700371 out->Print("///<summary>Protected parameterless constructor to allow creation"
Jan Tattermuschefb77842016-03-23 07:47:36 -0700372 " of test doubles.</summary>\n");
373 out->Print("protected $name$() : base()\n",
374 "name", GetClientClassName(service));
375 out->Print("{\n");
Jan Tattermusch2f0a8372016-03-23 09:16:49 -0700376 out->Print("}\n");
377 out->Print("///<summary>Protected constructor to allow creation of configured"
378 " clients.</summary>\n");
379 out->Print("protected $name$(ClientBaseConfiguration configuration)"
380 " : base(configuration)\n",
381 "name", GetClientClassName(service));
382 out->Print("{\n");
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700383 out->Print("}\n\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700384
385 for (int i = 0; i < service->method_count(); i++) {
386 const MethodDescriptor *method = service->method(i);
387 MethodType method_type = GetMethodType(method);
388
389 if (method_type == METHODTYPE_NO_STREAMING) {
390 // unary calls have an extra synchronous stub method
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700391 out->Print("public virtual $response$ $methodname$($request$ request, Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken))\n",
392 "methodname", method->name(), "request",
393 GetClassName(method->input_type()), "response",
394 GetClassName(method->output_type()));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700395 out->Print("{\n");
396 out->Indent();
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700397 out->Print("return $methodname$(request, new CallOptions(headers, deadline, cancellationToken));\n",
398 "methodname", method->name());
399 out->Outdent();
400 out->Print("}\n");
401
402 // overload taking CallOptions as a param
403 out->Print("public virtual $response$ $methodname$($request$ request, CallOptions options)\n",
404 "methodname", method->name(), "request",
405 GetClassName(method->input_type()), "response",
406 GetClassName(method->output_type()));
407 out->Print("{\n");
408 out->Indent();
Jan Tattermuschb455bcc2016-03-22 16:08:18 -0700409 out->Print("return CallInvoker.BlockingUnaryCall($methodfield$, null, options, request);\n",
Jan Tattermusch641cb1b2015-08-05 03:51:46 -0700410 "methodfield", GetMethodFieldName(method));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700411 out->Outdent();
412 out->Print("}\n");
413 }
414
415 std::string method_name = method->name();
416 if (method_type == METHODTYPE_NO_STREAMING) {
417 method_name += "Async"; // prevent name clash with synchronous method.
418 }
419 out->Print(
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700420 "public virtual $returntype$ $methodname$($request_maybe$Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken))\n",
421 "methodname", method_name, "request_maybe",
422 GetMethodRequestParamMaybe(method), "returntype",
423 GetMethodReturnTypeClient(method));
424 out->Print("{\n");
425 out->Indent();
426
427 out->Print("return $methodname$($request_maybe$new CallOptions(headers, deadline, cancellationToken));\n",
428 "methodname", method_name,
429 "request_maybe", GetMethodRequestParamMaybe(method, true));
430 out->Outdent();
431 out->Print("}\n");
432
433 // overload taking CallOptions as a param
434 out->Print(
435 "public virtual $returntype$ $methodname$($request_maybe$CallOptions options)\n",
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700436 "methodname", method_name, "request_maybe",
437 GetMethodRequestParamMaybe(method), "returntype",
438 GetMethodReturnTypeClient(method));
439 out->Print("{\n");
440 out->Indent();
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700441 switch (GetMethodType(method)) {
442 case METHODTYPE_NO_STREAMING:
Jan Tattermuschb455bcc2016-03-22 16:08:18 -0700443 out->Print("return CallInvoker.AsyncUnaryCall($methodfield$, null, options, request);\n",
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700444 "methodfield", GetMethodFieldName(method));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700445 break;
446 case METHODTYPE_CLIENT_STREAMING:
Jan Tattermuschb455bcc2016-03-22 16:08:18 -0700447 out->Print("return CallInvoker.AsyncClientStreamingCall($methodfield$, null, options);\n",
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700448 "methodfield", GetMethodFieldName(method));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700449 break;
450 case METHODTYPE_SERVER_STREAMING:
451 out->Print(
Jan Tattermuschb455bcc2016-03-22 16:08:18 -0700452 "return CallInvoker.AsyncServerStreamingCall($methodfield$, null, options, request);\n",
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700453 "methodfield", GetMethodFieldName(method));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700454 break;
455 case METHODTYPE_BIDI_STREAMING:
Jan Tattermuschb455bcc2016-03-22 16:08:18 -0700456 out->Print("return CallInvoker.AsyncDuplexStreamingCall($methodfield$, null, options);\n",
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700457 "methodfield", GetMethodFieldName(method));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700458 break;
459 default:
460 GOOGLE_LOG(FATAL)<< "Can't get here.";
461 }
Jan Tattermusch2d924952015-05-06 10:23:17 -0700462 out->Outdent();
463 out->Print("}\n");
464 }
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700465
466 // override NewInstance method
Jan Tattermusch2f0a8372016-03-23 09:16:49 -0700467 out->Print("protected override $name$ NewInstance(ClientBaseConfiguration configuration)\n",
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700468 "name", GetClientClassName(service));
469 out->Print("{\n");
470 out->Indent();
Jan Tattermusch2f0a8372016-03-23 09:16:49 -0700471 out->Print("return new $name$(configuration);\n",
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700472 "name", GetClientClassName(service));
473 out->Outdent();
474 out->Print("}\n");
475
Jan Tattermusch2d924952015-05-06 10:23:17 -0700476 out->Outdent();
477 out->Print("}\n");
478 out->Print("\n");
479}
480
Jan Tattermusch10a002d2016-03-14 15:22:19 -0700481void GenerateBindServiceMethod(Printer* out, const ServiceDescriptor *service,
482 bool use_server_class) {
Jan Tattermusch2d924952015-05-06 10:23:17 -0700483 out->Print(
484 "// creates service definition that can be registered with a server\n");
Jan Tattermuschcc8a4b32016-04-25 13:40:00 -0700485 out->Print("#pragma warning disable 0618\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700486 out->Print(
487 "public static ServerServiceDefinition BindService($interface$ serviceImpl)\n",
Jan Tattermusch10a002d2016-03-14 15:22:19 -0700488 "interface", use_server_class ? GetServerClassName(service) :
489 GetServerInterfaceName(service));
Jan Tattermuschcc8a4b32016-04-25 13:40:00 -0700490 out->Print("#pragma warning restore 0618\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700491 out->Print("{\n");
492 out->Indent();
493
494 out->Print(
495 "return ServerServiceDefinition.CreateBuilder($servicenamefield$)\n",
496 "servicenamefield", GetServiceNameFieldName());
497 out->Indent();
498 out->Indent();
499 for (int i = 0; i < service->method_count(); i++) {
500 const MethodDescriptor *method = service->method(i);
501 out->Print(".AddMethod($methodfield$, serviceImpl.$methodname$)",
502 "methodfield", GetMethodFieldName(method), "methodname",
503 method->name());
504 if (i == service->method_count() - 1) {
505 out->Print(".Build();");
506 }
507 out->Print("\n");
508 }
509 out->Outdent();
510 out->Outdent();
511
512 out->Outdent();
513 out->Print("}\n");
514 out->Print("\n");
515}
516
517void GenerateNewStubMethods(Printer* out, const ServiceDescriptor *service) {
Jan Tattermuschb5332812015-07-14 19:29:35 -0700518 out->Print("// creates a new client\n");
519 out->Print("public static $classname$ NewClient(Channel channel)\n",
520 "classname", GetClientClassName(service));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700521 out->Print("{\n");
522 out->Indent();
523 out->Print("return new $classname$(channel);\n", "classname",
524 GetClientClassName(service));
525 out->Outdent();
526 out->Print("}\n");
527 out->Print("\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700528}
529
Jan Tattermusch44f7c542016-04-25 14:50:27 -0700530void GenerateService(Printer* out, const ServiceDescriptor *service,
531 bool generate_client, bool generate_server) {
Jan Tattermusch2d924952015-05-06 10:23:17 -0700532 out->Print("public static class $classname$\n", "classname",
533 GetServiceClassName(service));
534 out->Print("{\n");
535 out->Indent();
536 out->Print("static readonly string $servicenamefield$ = \"$servicename$\";\n",
537 "servicenamefield", GetServiceNameFieldName(), "servicename",
538 service->full_name());
539 out->Print("\n");
540
541 GenerateMarshallerFields(out, service);
542 for (int i = 0; i < service->method_count(); i++) {
543 GenerateStaticMethodField(out, service->method(i));
544 }
Jan Tattermusche6af5d12015-08-03 10:57:43 -0700545 GenerateServiceDescriptorProperty(out, service);
Jan Tattermusch44f7c542016-04-25 14:50:27 -0700546
547 if (generate_client) {
548 GenerateClientInterface(out, service);
549 }
550 if (generate_server) {
551 GenerateServerInterface(out, service);
552 GenerateServerClass(out, service);
553 }
554 if (generate_client) {
555 GenerateClientStub(out, service);
556 GenerateNewStubMethods(out, service);
557 }
558 if (generate_server) {
559 GenerateBindServiceMethod(out, service, false);
560 GenerateBindServiceMethod(out, service, true);
561 }
Jan Tattermusch2d924952015-05-06 10:23:17 -0700562
563 out->Outdent();
564 out->Print("}\n");
565}
566
567} // anonymous namespace
568
569grpc::string GetServices(const FileDescriptor *file) {
570 grpc::string output;
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700571 {
572 // Scope the output stream so it closes and finalizes output to the string.
Jan Tattermusch2d924952015-05-06 10:23:17 -0700573
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700574 StringOutputStream output_stream(&output);
575 Printer out(&output_stream, '$');
576
577 // Don't write out any output if there no services, to avoid empty service
578 // files being generated for proto files that don't declare any.
579 if (file->service_count() == 0) {
580 return output;
581 }
582
583 // Write out a file header.
584 out.Print("// Generated by the protocol buffer compiler. DO NOT EDIT!\n");
585 out.Print("// source: $filename$\n", "filename", file->name());
586 out.Print("#region Designer generated code\n");
587 out.Print("\n");
588 out.Print("using System;\n");
589 out.Print("using System.Threading;\n");
590 out.Print("using System.Threading.Tasks;\n");
591 out.Print("using Grpc.Core;\n");
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700592 out.Print("\n");
593
594 out.Print("namespace $namespace$ {\n", "namespace", GetFileNamespace(file));
595 out.Indent();
596 for (int i = 0; i < file->service_count(); i++) {
Jan Tattermusch44f7c542016-04-25 14:50:27 -0700597 GenerateService(&out, file->service(i), true, true);
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700598 }
599 out.Outdent();
600 out.Print("}\n");
601 out.Print("#endregion\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700602 }
Jan Tattermusch2d924952015-05-06 10:23:17 -0700603 return output;
604}
605
606} // namespace grpc_csharp_generator