blob: f5a0876cf98c815089b61d337889b92d672d2d31 [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 Tattermusch59c20ed2016-04-28 09:12:13 -070068// This function is a massaged version of
69// https://github.com/google/protobuf/blob/master/src/google/protobuf/compiler/csharp/csharp_doc_comment.cc
70// Currently, we cannot easily reuse the functionality as
71// google/protobuf/compiler/csharp/csharp_doc_comment.h is not a public header.
72// TODO(jtattermusch): reuse the functionality from google/protobuf.
73void GenerateDocCommentBodyImpl(grpc::protobuf::io::Printer* printer, grpc::protobuf::SourceLocation location) {
74 grpc::string comments = location.leading_comments.empty() ?
75 location.trailing_comments : location.leading_comments;
76 if (comments.empty()) {
77 return;
78 }
79 // XML escaping... no need for apostrophes etc as the whole text is going to be a child
80 // node of a summary element, not part of an attribute.
81 comments = grpc_generator::StringReplace(comments, "&", "&amp;", true);
82 comments = grpc_generator::StringReplace(comments, "<", "&lt;", true);
83
84 std::vector<grpc::string> lines;
85 grpc_generator::Split(comments, '\n', &lines);
86 // TODO: We really should work out which part to put in the summary and which to put in the remarks...
87 // but that needs to be part of a bigger effort to understand the markdown better anyway.
88 printer->Print("/// <summary>\n");
89 bool last_was_empty = false;
90 // We squash multiple blank lines down to one, and remove any trailing blank lines. We need
91 // to preserve the blank lines themselves, as this is relevant in the markdown.
92 // Note that we can't remove leading or trailing whitespace as *that's* relevant in markdown too.
93 // (We don't skip "just whitespace" lines, either.)
94 for (std::vector<grpc::string>::iterator it = lines.begin(); it != lines.end(); ++it) {
95 grpc::string line = *it;
96 if (line.empty()) {
97 last_was_empty = true;
98 } else {
99 if (last_was_empty) {
100 printer->Print("///\n");
101 }
102 last_was_empty = false;
103 printer->Print("/// $line$\n", "line", *it);
104 }
105 }
106 printer->Print("/// </summary>\n");
107}
108
109template <typename DescriptorType>
110void GenerateDocCommentBody(
111 grpc::protobuf::io::Printer* printer, const DescriptorType* descriptor) {
112 grpc::protobuf::SourceLocation location;
113 if (descriptor->GetSourceLocation(&location)) {
114 GenerateDocCommentBodyImpl(printer, location);
115 }
116}
117
Jan Tattermusch2d924952015-05-06 10:23:17 -0700118std::string GetServiceClassName(const ServiceDescriptor* service) {
119 return service->name();
120}
121
Jan Tattermusch2d924952015-05-06 10:23:17 -0700122std::string GetClientClassName(const ServiceDescriptor* service) {
123 return service->name() + "Client";
124}
125
Jan Tattermusch10a002d2016-03-14 15:22:19 -0700126std::string GetServerClassName(const ServiceDescriptor* service) {
127 return service->name() + "Base";
128}
129
Jan Tattermusch2d924952015-05-06 10:23:17 -0700130std::string GetCSharpMethodType(MethodType method_type) {
131 switch (method_type) {
132 case METHODTYPE_NO_STREAMING:
133 return "MethodType.Unary";
134 case METHODTYPE_CLIENT_STREAMING:
135 return "MethodType.ClientStreaming";
136 case METHODTYPE_SERVER_STREAMING:
137 return "MethodType.ServerStreaming";
138 case METHODTYPE_BIDI_STREAMING:
139 return "MethodType.DuplexStreaming";
140 }
141 GOOGLE_LOG(FATAL)<< "Can't get here.";
142 return "";
143}
144
145std::string GetServiceNameFieldName() {
146 return "__ServiceName";
147}
148
149std::string GetMarshallerFieldName(const Descriptor *message) {
150 return "__Marshaller_" + message->name();
151}
152
153std::string GetMethodFieldName(const MethodDescriptor *method) {
154 return "__Method_" + method->name();
155}
156
Jan Tattermusch8cd4ae92016-03-21 18:46:32 -0700157std::string GetMethodRequestParamMaybe(const MethodDescriptor *method,
158 bool invocation_param=false) {
Jan Tattermusch2d924952015-05-06 10:23:17 -0700159 if (method->client_streaming()) {
160 return "";
161 }
Jan Tattermusch8cd4ae92016-03-21 18:46:32 -0700162 if (invocation_param) {
163 return "request, ";
164 }
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700165 return GetClassName(method->input_type()) + " request, ";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700166}
167
Jan Tattermusch4e0f73c2016-04-25 16:11:03 -0700168std::string GetAccessLevel(bool internal_access) {
169 return internal_access ? "internal" : "public";
170}
171
Jan Tattermusch2d924952015-05-06 10:23:17 -0700172std::string GetMethodReturnTypeClient(const MethodDescriptor *method) {
173 switch (GetMethodType(method)) {
174 case METHODTYPE_NO_STREAMING:
Jan Tattermusch5269d162015-07-21 11:56:42 -0700175 return "AsyncUnaryCall<" + GetClassName(method->output_type()) + ">";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700176 case METHODTYPE_CLIENT_STREAMING:
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700177 return "AsyncClientStreamingCall<" + GetClassName(method->input_type())
178 + ", " + GetClassName(method->output_type()) + ">";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700179 case METHODTYPE_SERVER_STREAMING:
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700180 return "AsyncServerStreamingCall<" + GetClassName(method->output_type())
Jan Tattermusch2d924952015-05-06 10:23:17 -0700181 + ">";
182 case METHODTYPE_BIDI_STREAMING:
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700183 return "AsyncDuplexStreamingCall<" + GetClassName(method->input_type())
184 + ", " + GetClassName(method->output_type()) + ">";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700185 }
186 GOOGLE_LOG(FATAL)<< "Can't get here.";
187 return "";
188}
189
190std::string GetMethodRequestParamServer(const MethodDescriptor *method) {
191 switch (GetMethodType(method)) {
192 case METHODTYPE_NO_STREAMING:
193 case METHODTYPE_SERVER_STREAMING:
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700194 return GetClassName(method->input_type()) + " request";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700195 case METHODTYPE_CLIENT_STREAMING:
196 case METHODTYPE_BIDI_STREAMING:
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700197 return "IAsyncStreamReader<" + GetClassName(method->input_type())
Jan Tattermusch2d924952015-05-06 10:23:17 -0700198 + "> requestStream";
199 }
200 GOOGLE_LOG(FATAL)<< "Can't get here.";
201 return "";
202}
203
204std::string GetMethodReturnTypeServer(const MethodDescriptor *method) {
205 switch (GetMethodType(method)) {
206 case METHODTYPE_NO_STREAMING:
207 case METHODTYPE_CLIENT_STREAMING:
Kirill Katsnelson0ddf46f2016-04-19 18:49:31 -0700208 return "global::System.Threading.Tasks.Task<" + GetClassName(method->output_type()) + ">";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700209 case METHODTYPE_SERVER_STREAMING:
210 case METHODTYPE_BIDI_STREAMING:
Kirill Katsnelson0ddf46f2016-04-19 18:49:31 -0700211 return "global::System.Threading.Tasks.Task";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700212 }
213 GOOGLE_LOG(FATAL)<< "Can't get here.";
214 return "";
215}
216
217std::string GetMethodResponseStreamMaybe(const MethodDescriptor *method) {
218 switch (GetMethodType(method)) {
219 case METHODTYPE_NO_STREAMING:
220 case METHODTYPE_CLIENT_STREAMING:
221 return "";
222 case METHODTYPE_SERVER_STREAMING:
223 case METHODTYPE_BIDI_STREAMING:
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700224 return ", IServerStreamWriter<" + GetClassName(method->output_type())
Jan Tattermusch2d924952015-05-06 10:23:17 -0700225 + "> responseStream";
226 }
227 GOOGLE_LOG(FATAL)<< "Can't get here.";
228 return "";
229}
230
231// Gets vector of all messages used as input or output types.
232std::vector<const Descriptor*> GetUsedMessages(
233 const ServiceDescriptor *service) {
234 std::set<const Descriptor*> descriptor_set;
235 std::vector<const Descriptor*> result; // vector is to maintain stable ordering
236 for (int i = 0; i < service->method_count(); i++) {
237 const MethodDescriptor *method = service->method(i);
238 if (descriptor_set.find(method->input_type()) == descriptor_set.end()) {
239 descriptor_set.insert(method->input_type());
240 result.push_back(method->input_type());
241 }
242 if (descriptor_set.find(method->output_type()) == descriptor_set.end()) {
243 descriptor_set.insert(method->output_type());
244 result.push_back(method->output_type());
245 }
246 }
247 return result;
248}
249
250void GenerateMarshallerFields(Printer* out, const ServiceDescriptor *service) {
251 std::vector<const Descriptor*> used_messages = GetUsedMessages(service);
252 for (size_t i = 0; i < used_messages.size(); i++) {
253 const Descriptor *message = used_messages[i];
254 out->Print(
Jan Tattermuschad75dd12015-08-03 09:43:07 -0700255 "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 -0700256 "fieldname", GetMarshallerFieldName(message), "type",
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700257 GetClassName(message));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700258 }
259 out->Print("\n");
260}
261
262void GenerateStaticMethodField(Printer* out, const MethodDescriptor *method) {
263 out->Print(
264 "static readonly Method<$request$, $response$> $fieldname$ = new Method<$request$, $response$>(\n",
265 "fieldname", GetMethodFieldName(method), "request",
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700266 GetClassName(method->input_type()), "response",
267 GetClassName(method->output_type()));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700268 out->Indent();
269 out->Indent();
270 out->Print("$methodtype$,\n", "methodtype",
271 GetCSharpMethodType(GetMethodType(method)));
Jan Tattermuscha9ddd022015-08-05 03:04:58 -0700272 out->Print("$servicenamefield$,\n", "servicenamefield",
273 GetServiceNameFieldName());
Jan Tattermusch2d924952015-05-06 10:23:17 -0700274 out->Print("\"$methodname$\",\n", "methodname", method->name());
275 out->Print("$requestmarshaller$,\n", "requestmarshaller",
276 GetMarshallerFieldName(method->input_type()));
277 out->Print("$responsemarshaller$);\n", "responsemarshaller",
278 GetMarshallerFieldName(method->output_type()));
279 out->Print("\n");
280 out->Outdent();
281 out->Outdent();
282}
283
Jan Tattermusche6af5d12015-08-03 10:57:43 -0700284void GenerateServiceDescriptorProperty(Printer* out, const ServiceDescriptor *service) {
yang-gc3ee1d52015-08-28 11:33:52 -0700285 std::ostringstream index;
286 index << service->index();
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700287 out->Print("/// <summary>Service descriptor</summary>\n");
Jan Tattermusche6af5d12015-08-03 10:57:43 -0700288 out->Print("public static global::Google.Protobuf.Reflection.ServiceDescriptor Descriptor\n");
289 out->Print("{\n");
290 out->Print(" get { return $umbrella$.Descriptor.Services[$index$]; }\n",
Jan Tattermuschda717f42016-01-20 13:12:35 -0800291 "umbrella", GetReflectionClassName(service->file()), "index",
yang-gc3ee1d52015-08-28 11:33:52 -0700292 index.str());
Jan Tattermusche6af5d12015-08-03 10:57:43 -0700293 out->Print("}\n");
294 out->Print("\n");
295}
296
Jan Tattermusch10a002d2016-03-14 15:22:19 -0700297void GenerateServerClass(Printer* out, const ServiceDescriptor *service) {
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700298 out->Print("/// <summary>Base class for server-side implementations of $servicename$</summary>\n",
299 "servicename", GetServiceClassName(service));
Jan Tattermusch10a002d2016-03-14 15:22:19 -0700300 out->Print("public abstract class $name$\n", "name",
301 GetServerClassName(service));
302 out->Print("{\n");
303 out->Indent();
304 for (int i = 0; i < service->method_count(); i++) {
305 const MethodDescriptor *method = service->method(i);
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700306 GenerateDocCommentBody(out, method);
Jan Tattermusch10a002d2016-03-14 15:22:19 -0700307 out->Print(
308 "public virtual $returntype$ $methodname$($request$$response_stream_maybe$, "
309 "ServerCallContext context)\n",
310 "methodname", method->name(), "returntype",
311 GetMethodReturnTypeServer(method), "request",
312 GetMethodRequestParamServer(method), "response_stream_maybe",
313 GetMethodResponseStreamMaybe(method));
314 out->Print("{\n");
315 out->Indent();
316 out->Print("throw new RpcException("
317 "new Status(StatusCode.Unimplemented, \"\"));\n");
318 out->Outdent();
319 out->Print("}\n\n");
320 }
321 out->Outdent();
322 out->Print("}\n");
323 out->Print("\n");
324}
325
Jan Tattermusch2d924952015-05-06 10:23:17 -0700326void GenerateClientStub(Printer* out, const ServiceDescriptor *service) {
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700327 out->Print("/// <summary>Client for $servicename$</summary>\n",
328 "servicename", GetServiceClassName(service));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700329 out->Print(
Jan Tattermuschfcc8d972016-06-06 09:44:02 -0700330 "public class $name$ : ClientBase<$name$>\n",
331 "name", GetClientClassName(service));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700332 out->Print("{\n");
333 out->Indent();
334
335 // constructors
Jan Tattermuschbfcd6b62016-06-22 16:52:46 -0700336 out->Print("/// <summary>Creates a new client for $servicename$</summary>\n"
337 "/// <param name=\"channel\">The channel to use to make remote calls.</param>\n",
338 "servicename", GetServiceClassName(service));
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700339 out->Print("public $name$(Channel channel) : base(channel)\n",
340 "name", GetClientClassName(service));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700341 out->Print("{\n");
342 out->Print("}\n");
Jan Tattermuschbfcd6b62016-06-22 16:52:46 -0700343 out->Print("/// <summary>Creates a new client for $servicename$ that uses a custom <c>CallInvoker</c>.</summary>\n"
344 "/// <param name=\"callInvoker\">The callInvoker to use to make remote calls.</param>\n",
345 "servicename", GetServiceClassName(service));
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700346 out->Print("public $name$(CallInvoker callInvoker) : base(callInvoker)\n",
347 "name", GetClientClassName(service));
348 out->Print("{\n");
Jan Tattermuschefb77842016-03-23 07:47:36 -0700349 out->Print("}\n");
Jan Tattermuschbfcd6b62016-06-22 16:52:46 -0700350 out->Print("/// <summary>Protected parameterless constructor to allow creation"
Jan Tattermuschefb77842016-03-23 07:47:36 -0700351 " of test doubles.</summary>\n");
352 out->Print("protected $name$() : base()\n",
353 "name", GetClientClassName(service));
354 out->Print("{\n");
Jan Tattermusch2f0a8372016-03-23 09:16:49 -0700355 out->Print("}\n");
Jan Tattermuschbfcd6b62016-06-22 16:52:46 -0700356 out->Print("/// <summary>Protected constructor to allow creation of configured clients.</summary>\n"
357 "/// <param name=\"configuration\">The client configuration.</param>\n");
Jan Tattermusch2f0a8372016-03-23 09:16:49 -0700358 out->Print("protected $name$(ClientBaseConfiguration configuration)"
359 " : base(configuration)\n",
360 "name", GetClientClassName(service));
361 out->Print("{\n");
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700362 out->Print("}\n\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700363
364 for (int i = 0; i < service->method_count(); i++) {
365 const MethodDescriptor *method = service->method(i);
366 MethodType method_type = GetMethodType(method);
367
368 if (method_type == METHODTYPE_NO_STREAMING) {
369 // unary calls have an extra synchronous stub method
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700370 GenerateDocCommentBody(out, method);
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700371 out->Print("public virtual $response$ $methodname$($request$ request, Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken))\n",
372 "methodname", method->name(), "request",
373 GetClassName(method->input_type()), "response",
374 GetClassName(method->output_type()));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700375 out->Print("{\n");
376 out->Indent();
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700377 out->Print("return $methodname$(request, new CallOptions(headers, deadline, cancellationToken));\n",
378 "methodname", method->name());
379 out->Outdent();
380 out->Print("}\n");
381
382 // overload taking CallOptions as a param
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700383 GenerateDocCommentBody(out, method);
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700384 out->Print("public virtual $response$ $methodname$($request$ request, CallOptions options)\n",
385 "methodname", method->name(), "request",
386 GetClassName(method->input_type()), "response",
387 GetClassName(method->output_type()));
388 out->Print("{\n");
389 out->Indent();
Jan Tattermuschb455bcc2016-03-22 16:08:18 -0700390 out->Print("return CallInvoker.BlockingUnaryCall($methodfield$, null, options, request);\n",
Jan Tattermusch641cb1b2015-08-05 03:51:46 -0700391 "methodfield", GetMethodFieldName(method));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700392 out->Outdent();
393 out->Print("}\n");
394 }
395
396 std::string method_name = method->name();
397 if (method_type == METHODTYPE_NO_STREAMING) {
398 method_name += "Async"; // prevent name clash with synchronous method.
399 }
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700400 GenerateDocCommentBody(out, method);
Jan Tattermusch2d924952015-05-06 10:23:17 -0700401 out->Print(
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700402 "public virtual $returntype$ $methodname$($request_maybe$Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken))\n",
403 "methodname", method_name, "request_maybe",
404 GetMethodRequestParamMaybe(method), "returntype",
405 GetMethodReturnTypeClient(method));
406 out->Print("{\n");
407 out->Indent();
408
409 out->Print("return $methodname$($request_maybe$new CallOptions(headers, deadline, cancellationToken));\n",
410 "methodname", method_name,
411 "request_maybe", GetMethodRequestParamMaybe(method, true));
412 out->Outdent();
413 out->Print("}\n");
414
415 // overload taking CallOptions as a param
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700416 GenerateDocCommentBody(out, method);
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700417 out->Print(
418 "public virtual $returntype$ $methodname$($request_maybe$CallOptions options)\n",
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700419 "methodname", method_name, "request_maybe",
420 GetMethodRequestParamMaybe(method), "returntype",
421 GetMethodReturnTypeClient(method));
422 out->Print("{\n");
423 out->Indent();
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700424 switch (GetMethodType(method)) {
425 case METHODTYPE_NO_STREAMING:
Jan Tattermuschb455bcc2016-03-22 16:08:18 -0700426 out->Print("return CallInvoker.AsyncUnaryCall($methodfield$, null, options, request);\n",
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700427 "methodfield", GetMethodFieldName(method));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700428 break;
429 case METHODTYPE_CLIENT_STREAMING:
Jan Tattermuschb455bcc2016-03-22 16:08:18 -0700430 out->Print("return CallInvoker.AsyncClientStreamingCall($methodfield$, null, options);\n",
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700431 "methodfield", GetMethodFieldName(method));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700432 break;
433 case METHODTYPE_SERVER_STREAMING:
434 out->Print(
Jan Tattermuschb455bcc2016-03-22 16:08:18 -0700435 "return CallInvoker.AsyncServerStreamingCall($methodfield$, null, options, request);\n",
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700436 "methodfield", GetMethodFieldName(method));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700437 break;
438 case METHODTYPE_BIDI_STREAMING:
Jan Tattermuschb455bcc2016-03-22 16:08:18 -0700439 out->Print("return CallInvoker.AsyncDuplexStreamingCall($methodfield$, null, options);\n",
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700440 "methodfield", GetMethodFieldName(method));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700441 break;
442 default:
443 GOOGLE_LOG(FATAL)<< "Can't get here.";
444 }
Jan Tattermusch2d924952015-05-06 10:23:17 -0700445 out->Outdent();
446 out->Print("}\n");
447 }
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700448
449 // override NewInstance method
Jan Tattermusch2f0a8372016-03-23 09:16:49 -0700450 out->Print("protected override $name$ NewInstance(ClientBaseConfiguration configuration)\n",
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700451 "name", GetClientClassName(service));
452 out->Print("{\n");
453 out->Indent();
Jan Tattermusch2f0a8372016-03-23 09:16:49 -0700454 out->Print("return new $name$(configuration);\n",
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700455 "name", GetClientClassName(service));
456 out->Outdent();
457 out->Print("}\n");
458
Jan Tattermusch2d924952015-05-06 10:23:17 -0700459 out->Outdent();
460 out->Print("}\n");
461 out->Print("\n");
462}
463
Jan Tattermuschfcc8d972016-06-06 09:44:02 -0700464void GenerateBindServiceMethod(Printer* out, const ServiceDescriptor *service) {
Jan Tattermusch2d924952015-05-06 10:23:17 -0700465 out->Print(
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700466 "/// <summary>Creates service definition that can be registered with a server</summary>\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700467 out->Print(
Jan Tattermuschfcc8d972016-06-06 09:44:02 -0700468 "public static ServerServiceDefinition BindService($implclass$ serviceImpl)\n",
469 "implclass", GetServerClassName(service));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700470 out->Print("{\n");
471 out->Indent();
472
Jan Tattermusch562cd052016-06-06 08:47:17 -0700473 out->Print("return ServerServiceDefinition.CreateBuilder()\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700474 out->Indent();
475 out->Indent();
476 for (int i = 0; i < service->method_count(); i++) {
477 const MethodDescriptor *method = service->method(i);
478 out->Print(".AddMethod($methodfield$, serviceImpl.$methodname$)",
479 "methodfield", GetMethodFieldName(method), "methodname",
480 method->name());
481 if (i == service->method_count() - 1) {
482 out->Print(".Build();");
483 }
484 out->Print("\n");
485 }
486 out->Outdent();
487 out->Outdent();
488
489 out->Outdent();
490 out->Print("}\n");
491 out->Print("\n");
492}
493
Jan Tattermusch44f7c542016-04-25 14:50:27 -0700494void GenerateService(Printer* out, const ServiceDescriptor *service,
Jan Tattermusch4e0f73c2016-04-25 16:11:03 -0700495 bool generate_client, bool generate_server,
496 bool internal_access) {
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700497 GenerateDocCommentBody(out, service);
Jan Tattermusch4e0f73c2016-04-25 16:11:03 -0700498 out->Print("$access_level$ static class $classname$\n", "access_level",
499 GetAccessLevel(internal_access), "classname",
Jan Tattermusch2d924952015-05-06 10:23:17 -0700500 GetServiceClassName(service));
501 out->Print("{\n");
502 out->Indent();
503 out->Print("static readonly string $servicenamefield$ = \"$servicename$\";\n",
504 "servicenamefield", GetServiceNameFieldName(), "servicename",
505 service->full_name());
506 out->Print("\n");
507
508 GenerateMarshallerFields(out, service);
509 for (int i = 0; i < service->method_count(); i++) {
510 GenerateStaticMethodField(out, service->method(i));
511 }
Jan Tattermusche6af5d12015-08-03 10:57:43 -0700512 GenerateServiceDescriptorProperty(out, service);
Jan Tattermusch44f7c542016-04-25 14:50:27 -0700513
Jan Tattermusch44f7c542016-04-25 14:50:27 -0700514 if (generate_server) {
Jan Tattermusch44f7c542016-04-25 14:50:27 -0700515 GenerateServerClass(out, service);
516 }
517 if (generate_client) {
518 GenerateClientStub(out, service);
Jan Tattermusch44f7c542016-04-25 14:50:27 -0700519 }
520 if (generate_server) {
Jan Tattermuschfcc8d972016-06-06 09:44:02 -0700521 GenerateBindServiceMethod(out, service);
Jan Tattermusch44f7c542016-04-25 14:50:27 -0700522 }
Jan Tattermusch2d924952015-05-06 10:23:17 -0700523
524 out->Outdent();
525 out->Print("}\n");
526}
527
528} // anonymous namespace
529
Jan Tattermusch5f8872f2016-04-25 15:57:10 -0700530grpc::string GetServices(const FileDescriptor *file, bool generate_client,
Jan Tattermusch4e0f73c2016-04-25 16:11:03 -0700531 bool generate_server, bool internal_access) {
Jan Tattermusch2d924952015-05-06 10:23:17 -0700532 grpc::string output;
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700533 {
534 // Scope the output stream so it closes and finalizes output to the string.
Jan Tattermusch2d924952015-05-06 10:23:17 -0700535
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700536 StringOutputStream output_stream(&output);
537 Printer out(&output_stream, '$');
538
539 // Don't write out any output if there no services, to avoid empty service
540 // files being generated for proto files that don't declare any.
541 if (file->service_count() == 0) {
542 return output;
543 }
544
545 // Write out a file header.
546 out.Print("// Generated by the protocol buffer compiler. DO NOT EDIT!\n");
547 out.Print("// source: $filename$\n", "filename", file->name());
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700548
549 // use C++ style as there are no file-level XML comments in .NET
murgatroid99210f3c42016-05-20 13:24:59 -0700550 grpc::string leading_comments = GetCsharpComments(file, true);
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700551 if (!leading_comments.empty()) {
552 out.Print("// Original file comments:\n");
553 out.Print(leading_comments.c_str());
554 }
555
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700556 out.Print("#region Designer generated code\n");
557 out.Print("\n");
558 out.Print("using System;\n");
559 out.Print("using System.Threading;\n");
560 out.Print("using System.Threading.Tasks;\n");
561 out.Print("using Grpc.Core;\n");
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700562 out.Print("\n");
563
564 out.Print("namespace $namespace$ {\n", "namespace", GetFileNamespace(file));
565 out.Indent();
566 for (int i = 0; i < file->service_count(); i++) {
Jan Tattermusch4e0f73c2016-04-25 16:11:03 -0700567 GenerateService(&out, file->service(i), generate_client, generate_server,
568 internal_access);
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700569 }
570 out.Outdent();
571 out.Print("}\n");
572 out.Print("#endregion\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700573 }
Jan Tattermusch2d924952015-05-06 10:23:17 -0700574 return output;
575}
576
577} // namespace grpc_csharp_generator