blob: fc8feaf0fcc9bd36a1e6b22fb881a86360e9cb59 [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 Tattermusch055c2dd2016-03-22 14:43:56 -0700336 out->Print("public $name$(Channel channel) : base(channel)\n",
337 "name", GetClientClassName(service));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700338 out->Print("{\n");
339 out->Print("}\n");
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700340 out->Print("public $name$(CallInvoker callInvoker) : base(callInvoker)\n",
341 "name", GetClientClassName(service));
342 out->Print("{\n");
Jan Tattermuschefb77842016-03-23 07:47:36 -0700343 out->Print("}\n");
Jan Tattermusch2f0a8372016-03-23 09:16:49 -0700344 out->Print("///<summary>Protected parameterless constructor to allow creation"
Jan Tattermuschefb77842016-03-23 07:47:36 -0700345 " of test doubles.</summary>\n");
346 out->Print("protected $name$() : base()\n",
347 "name", GetClientClassName(service));
348 out->Print("{\n");
Jan Tattermusch2f0a8372016-03-23 09:16:49 -0700349 out->Print("}\n");
350 out->Print("///<summary>Protected constructor to allow creation of configured"
351 " clients.</summary>\n");
352 out->Print("protected $name$(ClientBaseConfiguration configuration)"
353 " : base(configuration)\n",
354 "name", GetClientClassName(service));
355 out->Print("{\n");
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700356 out->Print("}\n\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700357
358 for (int i = 0; i < service->method_count(); i++) {
359 const MethodDescriptor *method = service->method(i);
360 MethodType method_type = GetMethodType(method);
361
362 if (method_type == METHODTYPE_NO_STREAMING) {
363 // unary calls have an extra synchronous stub method
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700364 GenerateDocCommentBody(out, method);
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700365 out->Print("public virtual $response$ $methodname$($request$ request, Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken))\n",
366 "methodname", method->name(), "request",
367 GetClassName(method->input_type()), "response",
368 GetClassName(method->output_type()));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700369 out->Print("{\n");
370 out->Indent();
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700371 out->Print("return $methodname$(request, new CallOptions(headers, deadline, cancellationToken));\n",
372 "methodname", method->name());
373 out->Outdent();
374 out->Print("}\n");
375
376 // overload taking CallOptions as a param
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700377 GenerateDocCommentBody(out, method);
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700378 out->Print("public virtual $response$ $methodname$($request$ request, CallOptions options)\n",
379 "methodname", method->name(), "request",
380 GetClassName(method->input_type()), "response",
381 GetClassName(method->output_type()));
382 out->Print("{\n");
383 out->Indent();
Jan Tattermuschb455bcc2016-03-22 16:08:18 -0700384 out->Print("return CallInvoker.BlockingUnaryCall($methodfield$, null, options, request);\n",
Jan Tattermusch641cb1b2015-08-05 03:51:46 -0700385 "methodfield", GetMethodFieldName(method));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700386 out->Outdent();
387 out->Print("}\n");
388 }
389
390 std::string method_name = method->name();
391 if (method_type == METHODTYPE_NO_STREAMING) {
392 method_name += "Async"; // prevent name clash with synchronous method.
393 }
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700394 GenerateDocCommentBody(out, method);
Jan Tattermusch2d924952015-05-06 10:23:17 -0700395 out->Print(
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700396 "public virtual $returntype$ $methodname$($request_maybe$Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken))\n",
397 "methodname", method_name, "request_maybe",
398 GetMethodRequestParamMaybe(method), "returntype",
399 GetMethodReturnTypeClient(method));
400 out->Print("{\n");
401 out->Indent();
402
403 out->Print("return $methodname$($request_maybe$new CallOptions(headers, deadline, cancellationToken));\n",
404 "methodname", method_name,
405 "request_maybe", GetMethodRequestParamMaybe(method, true));
406 out->Outdent();
407 out->Print("}\n");
408
409 // overload taking CallOptions as a param
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700410 GenerateDocCommentBody(out, method);
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700411 out->Print(
412 "public virtual $returntype$ $methodname$($request_maybe$CallOptions options)\n",
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700413 "methodname", method_name, "request_maybe",
414 GetMethodRequestParamMaybe(method), "returntype",
415 GetMethodReturnTypeClient(method));
416 out->Print("{\n");
417 out->Indent();
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700418 switch (GetMethodType(method)) {
419 case METHODTYPE_NO_STREAMING:
Jan Tattermuschb455bcc2016-03-22 16:08:18 -0700420 out->Print("return CallInvoker.AsyncUnaryCall($methodfield$, null, options, request);\n",
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700421 "methodfield", GetMethodFieldName(method));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700422 break;
423 case METHODTYPE_CLIENT_STREAMING:
Jan Tattermuschb455bcc2016-03-22 16:08:18 -0700424 out->Print("return CallInvoker.AsyncClientStreamingCall($methodfield$, null, options);\n",
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700425 "methodfield", GetMethodFieldName(method));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700426 break;
427 case METHODTYPE_SERVER_STREAMING:
428 out->Print(
Jan Tattermuschb455bcc2016-03-22 16:08:18 -0700429 "return CallInvoker.AsyncServerStreamingCall($methodfield$, null, options, request);\n",
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700430 "methodfield", GetMethodFieldName(method));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700431 break;
432 case METHODTYPE_BIDI_STREAMING:
Jan Tattermuschb455bcc2016-03-22 16:08:18 -0700433 out->Print("return CallInvoker.AsyncDuplexStreamingCall($methodfield$, null, options);\n",
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700434 "methodfield", GetMethodFieldName(method));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700435 break;
436 default:
437 GOOGLE_LOG(FATAL)<< "Can't get here.";
438 }
Jan Tattermusch2d924952015-05-06 10:23:17 -0700439 out->Outdent();
440 out->Print("}\n");
441 }
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700442
443 // override NewInstance method
Jan Tattermusch2f0a8372016-03-23 09:16:49 -0700444 out->Print("protected override $name$ NewInstance(ClientBaseConfiguration configuration)\n",
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700445 "name", GetClientClassName(service));
446 out->Print("{\n");
447 out->Indent();
Jan Tattermusch2f0a8372016-03-23 09:16:49 -0700448 out->Print("return new $name$(configuration);\n",
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700449 "name", GetClientClassName(service));
450 out->Outdent();
451 out->Print("}\n");
452
Jan Tattermusch2d924952015-05-06 10:23:17 -0700453 out->Outdent();
454 out->Print("}\n");
455 out->Print("\n");
456}
457
Jan Tattermuschfcc8d972016-06-06 09:44:02 -0700458void GenerateBindServiceMethod(Printer* out, const ServiceDescriptor *service) {
Jan Tattermusch2d924952015-05-06 10:23:17 -0700459 out->Print(
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700460 "/// <summary>Creates service definition that can be registered with a server</summary>\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700461 out->Print(
Jan Tattermuschfcc8d972016-06-06 09:44:02 -0700462 "public static ServerServiceDefinition BindService($implclass$ serviceImpl)\n",
463 "implclass", GetServerClassName(service));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700464 out->Print("{\n");
465 out->Indent();
466
Jan Tattermusch562cd052016-06-06 08:47:17 -0700467 out->Print("return ServerServiceDefinition.CreateBuilder()\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700468 out->Indent();
469 out->Indent();
470 for (int i = 0; i < service->method_count(); i++) {
471 const MethodDescriptor *method = service->method(i);
472 out->Print(".AddMethod($methodfield$, serviceImpl.$methodname$)",
473 "methodfield", GetMethodFieldName(method), "methodname",
474 method->name());
475 if (i == service->method_count() - 1) {
476 out->Print(".Build();");
477 }
478 out->Print("\n");
479 }
480 out->Outdent();
481 out->Outdent();
482
483 out->Outdent();
484 out->Print("}\n");
485 out->Print("\n");
486}
487
488void GenerateNewStubMethods(Printer* out, const ServiceDescriptor *service) {
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700489 out->Print("/// <summary>Creates a new client for $servicename$</summary>\n",
490 "servicename", GetServiceClassName(service));
Jan Tattermuschb5332812015-07-14 19:29:35 -0700491 out->Print("public static $classname$ NewClient(Channel channel)\n",
492 "classname", GetClientClassName(service));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700493 out->Print("{\n");
494 out->Indent();
495 out->Print("return new $classname$(channel);\n", "classname",
496 GetClientClassName(service));
497 out->Outdent();
498 out->Print("}\n");
499 out->Print("\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700500}
501
Jan Tattermusch44f7c542016-04-25 14:50:27 -0700502void GenerateService(Printer* out, const ServiceDescriptor *service,
Jan Tattermusch4e0f73c2016-04-25 16:11:03 -0700503 bool generate_client, bool generate_server,
504 bool internal_access) {
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700505 GenerateDocCommentBody(out, service);
Jan Tattermusch4e0f73c2016-04-25 16:11:03 -0700506 out->Print("$access_level$ static class $classname$\n", "access_level",
507 GetAccessLevel(internal_access), "classname",
Jan Tattermusch2d924952015-05-06 10:23:17 -0700508 GetServiceClassName(service));
509 out->Print("{\n");
510 out->Indent();
511 out->Print("static readonly string $servicenamefield$ = \"$servicename$\";\n",
512 "servicenamefield", GetServiceNameFieldName(), "servicename",
513 service->full_name());
514 out->Print("\n");
515
516 GenerateMarshallerFields(out, service);
517 for (int i = 0; i < service->method_count(); i++) {
518 GenerateStaticMethodField(out, service->method(i));
519 }
Jan Tattermusche6af5d12015-08-03 10:57:43 -0700520 GenerateServiceDescriptorProperty(out, service);
Jan Tattermusch44f7c542016-04-25 14:50:27 -0700521
Jan Tattermusch44f7c542016-04-25 14:50:27 -0700522 if (generate_server) {
Jan Tattermusch44f7c542016-04-25 14:50:27 -0700523 GenerateServerClass(out, service);
524 }
525 if (generate_client) {
526 GenerateClientStub(out, service);
527 GenerateNewStubMethods(out, service);
528 }
529 if (generate_server) {
Jan Tattermuschfcc8d972016-06-06 09:44:02 -0700530 GenerateBindServiceMethod(out, service);
Jan Tattermusch44f7c542016-04-25 14:50:27 -0700531 }
Jan Tattermusch2d924952015-05-06 10:23:17 -0700532
533 out->Outdent();
534 out->Print("}\n");
535}
536
537} // anonymous namespace
538
Jan Tattermusch5f8872f2016-04-25 15:57:10 -0700539grpc::string GetServices(const FileDescriptor *file, bool generate_client,
Jan Tattermusch4e0f73c2016-04-25 16:11:03 -0700540 bool generate_server, bool internal_access) {
Jan Tattermusch2d924952015-05-06 10:23:17 -0700541 grpc::string output;
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700542 {
543 // Scope the output stream so it closes and finalizes output to the string.
Jan Tattermusch2d924952015-05-06 10:23:17 -0700544
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700545 StringOutputStream output_stream(&output);
546 Printer out(&output_stream, '$');
547
548 // Don't write out any output if there no services, to avoid empty service
549 // files being generated for proto files that don't declare any.
550 if (file->service_count() == 0) {
551 return output;
552 }
553
554 // Write out a file header.
555 out.Print("// Generated by the protocol buffer compiler. DO NOT EDIT!\n");
556 out.Print("// source: $filename$\n", "filename", file->name());
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700557
558 // use C++ style as there are no file-level XML comments in .NET
murgatroid99210f3c42016-05-20 13:24:59 -0700559 grpc::string leading_comments = GetCsharpComments(file, true);
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700560 if (!leading_comments.empty()) {
561 out.Print("// Original file comments:\n");
562 out.Print(leading_comments.c_str());
563 }
564
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700565 out.Print("#region Designer generated code\n");
566 out.Print("\n");
567 out.Print("using System;\n");
568 out.Print("using System.Threading;\n");
569 out.Print("using System.Threading.Tasks;\n");
570 out.Print("using Grpc.Core;\n");
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700571 out.Print("\n");
572
573 out.Print("namespace $namespace$ {\n", "namespace", GetFileNamespace(file));
574 out.Indent();
575 for (int i = 0; i < file->service_count(); i++) {
Jan Tattermusch4e0f73c2016-04-25 16:11:03 -0700576 GenerateService(&out, file->service(i), generate_client, generate_server,
577 internal_access);
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700578 }
579 out.Outdent();
580 out.Print("}\n");
581 out.Print("#endregion\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700582 }
Jan Tattermusch2d924952015-05-06 10:23:17 -0700583 return output;
584}
585
586} // namespace grpc_csharp_generator