blob: 591e5ae3d42d81a5a7da8d18027256bb7130d20c [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
39#include "src/compiler/config.h"
40#include "src/compiler/csharp_generator.h"
Masood Malekghassemiac592452016-07-01 11:58:04 -070041#include "src/compiler/csharp_generator.h"
42#include "src/compiler/csharp_generator_helpers.h"
Jan Tattermusch741e64c2015-08-03 08:08:56 -070043
44using google::protobuf::compiler::csharp::GetFileNamespace;
45using google::protobuf::compiler::csharp::GetClassName;
Jan Tattermuschda717f42016-01-20 13:12:35 -080046using google::protobuf::compiler::csharp::GetReflectionClassName;
Jan Tattermusch2d924952015-05-06 10:23:17 -070047using grpc::protobuf::FileDescriptor;
48using grpc::protobuf::Descriptor;
49using grpc::protobuf::ServiceDescriptor;
50using grpc::protobuf::MethodDescriptor;
51using grpc::protobuf::io::Printer;
52using grpc::protobuf::io::StringOutputStream;
Jan Tattermuschb5897bf2015-05-07 15:45:37 -070053using grpc_generator::MethodType;
54using grpc_generator::GetMethodType;
55using grpc_generator::METHODTYPE_NO_STREAMING;
56using grpc_generator::METHODTYPE_CLIENT_STREAMING;
57using grpc_generator::METHODTYPE_SERVER_STREAMING;
58using grpc_generator::METHODTYPE_BIDI_STREAMING;
Jan Tattermusch41f9f332015-05-20 08:52:00 -070059using grpc_generator::StringReplace;
Jan Tattermusch2d924952015-05-06 10:23:17 -070060using std::map;
61using std::vector;
62
63namespace grpc_csharp_generator {
64namespace {
65
Jan Tattermusch59c20ed2016-04-28 09:12:13 -070066// This function is a massaged version of
67// https://github.com/google/protobuf/blob/master/src/google/protobuf/compiler/csharp/csharp_doc_comment.cc
68// Currently, we cannot easily reuse the functionality as
69// google/protobuf/compiler/csharp/csharp_doc_comment.h is not a public header.
70// TODO(jtattermusch): reuse the functionality from google/protobuf.
Masood Malekghassemiac592452016-07-01 11:58:04 -070071void GenerateDocCommentBodyImpl(grpc::protobuf::io::Printer *printer,
72 grpc::protobuf::SourceLocation location) {
73 grpc::string comments = location.leading_comments.empty()
74 ? location.trailing_comments
75 : location.leading_comments;
Jan Tattermusch59c20ed2016-04-28 09:12:13 -070076 if (comments.empty()) {
77 return;
78 }
Masood Malekghassemiac592452016-07-01 11:58:04 -070079 // XML escaping... no need for apostrophes etc as the whole text is going to
80 // be a child
Jan Tattermusch59c20ed2016-04-28 09:12:13 -070081 // node of a summary element, not part of an attribute.
82 comments = grpc_generator::StringReplace(comments, "&", "&amp;", true);
83 comments = grpc_generator::StringReplace(comments, "<", "&lt;", true);
84
85 std::vector<grpc::string> lines;
86 grpc_generator::Split(comments, '\n', &lines);
Masood Malekghassemiac592452016-07-01 11:58:04 -070087 // TODO: We really should work out which part to put in the summary and which
88 // to put in the remarks...
89 // but that needs to be part of a bigger effort to understand the markdown
90 // better anyway.
Jan Tattermusch59c20ed2016-04-28 09:12:13 -070091 printer->Print("/// <summary>\n");
92 bool last_was_empty = false;
Masood Malekghassemiac592452016-07-01 11:58:04 -070093 // We squash multiple blank lines down to one, and remove any trailing blank
94 // lines. We need
95 // to preserve the blank lines themselves, as this is relevant in the
96 // markdown.
97 // Note that we can't remove leading or trailing whitespace as *that's*
98 // relevant in markdown too.
Jan Tattermusch59c20ed2016-04-28 09:12:13 -070099 // (We don't skip "just whitespace" lines, either.)
Masood Malekghassemiac592452016-07-01 11:58:04 -0700100 for (std::vector<grpc::string>::iterator it = lines.begin();
101 it != lines.end(); ++it) {
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700102 grpc::string line = *it;
103 if (line.empty()) {
104 last_was_empty = true;
105 } else {
106 if (last_was_empty) {
Masood Malekghassemiac592452016-07-01 11:58:04 -0700107 printer->Print("///\n");
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700108 }
109 last_was_empty = false;
110 printer->Print("/// $line$\n", "line", *it);
111 }
112 }
113 printer->Print("/// </summary>\n");
114}
115
116template <typename DescriptorType>
Masood Malekghassemiac592452016-07-01 11:58:04 -0700117void GenerateDocCommentBody(grpc::protobuf::io::Printer *printer,
118 const DescriptorType *descriptor) {
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700119 grpc::protobuf::SourceLocation location;
120 if (descriptor->GetSourceLocation(&location)) {
121 GenerateDocCommentBodyImpl(printer, location);
122 }
123}
124
Masood Malekghassemiac592452016-07-01 11:58:04 -0700125std::string GetServiceClassName(const ServiceDescriptor *service) {
Jan Tattermusch2d924952015-05-06 10:23:17 -0700126 return service->name();
127}
128
Masood Malekghassemiac592452016-07-01 11:58:04 -0700129std::string GetClientClassName(const ServiceDescriptor *service) {
Jan Tattermusch2d924952015-05-06 10:23:17 -0700130 return service->name() + "Client";
131}
132
Masood Malekghassemiac592452016-07-01 11:58:04 -0700133std::string GetServerClassName(const ServiceDescriptor *service) {
Jan Tattermusch10a002d2016-03-14 15:22:19 -0700134 return service->name() + "Base";
135}
136
Jan Tattermusch2d924952015-05-06 10:23:17 -0700137std::string GetCSharpMethodType(MethodType method_type) {
138 switch (method_type) {
139 case METHODTYPE_NO_STREAMING:
140 return "MethodType.Unary";
141 case METHODTYPE_CLIENT_STREAMING:
142 return "MethodType.ClientStreaming";
143 case METHODTYPE_SERVER_STREAMING:
144 return "MethodType.ServerStreaming";
145 case METHODTYPE_BIDI_STREAMING:
146 return "MethodType.DuplexStreaming";
147 }
Masood Malekghassemiac592452016-07-01 11:58:04 -0700148 GOOGLE_LOG(FATAL) << "Can't get here.";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700149 return "";
150}
151
Masood Malekghassemiac592452016-07-01 11:58:04 -0700152std::string GetServiceNameFieldName() { return "__ServiceName"; }
Jan Tattermusch2d924952015-05-06 10:23:17 -0700153
154std::string GetMarshallerFieldName(const Descriptor *message) {
155 return "__Marshaller_" + message->name();
156}
157
158std::string GetMethodFieldName(const MethodDescriptor *method) {
159 return "__Method_" + method->name();
160}
161
Jan Tattermusch8cd4ae92016-03-21 18:46:32 -0700162std::string GetMethodRequestParamMaybe(const MethodDescriptor *method,
Masood Malekghassemiac592452016-07-01 11:58:04 -0700163 bool invocation_param = false) {
Jan Tattermusch2d924952015-05-06 10:23:17 -0700164 if (method->client_streaming()) {
165 return "";
166 }
Jan Tattermusch8cd4ae92016-03-21 18:46:32 -0700167 if (invocation_param) {
168 return "request, ";
169 }
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700170 return GetClassName(method->input_type()) + " request, ";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700171}
172
Jan Tattermusch4e0f73c2016-04-25 16:11:03 -0700173std::string GetAccessLevel(bool internal_access) {
174 return internal_access ? "internal" : "public";
175}
176
Jan Tattermusch2d924952015-05-06 10:23:17 -0700177std::string GetMethodReturnTypeClient(const MethodDescriptor *method) {
178 switch (GetMethodType(method)) {
179 case METHODTYPE_NO_STREAMING:
Jan Tattermusch5269d162015-07-21 11:56:42 -0700180 return "AsyncUnaryCall<" + GetClassName(method->output_type()) + ">";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700181 case METHODTYPE_CLIENT_STREAMING:
Masood Malekghassemiac592452016-07-01 11:58:04 -0700182 return "AsyncClientStreamingCall<" + GetClassName(method->input_type()) +
183 ", " + GetClassName(method->output_type()) + ">";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700184 case METHODTYPE_SERVER_STREAMING:
Masood Malekghassemiac592452016-07-01 11:58:04 -0700185 return "AsyncServerStreamingCall<" + GetClassName(method->output_type()) +
186 ">";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700187 case METHODTYPE_BIDI_STREAMING:
Masood Malekghassemiac592452016-07-01 11:58:04 -0700188 return "AsyncDuplexStreamingCall<" + GetClassName(method->input_type()) +
189 ", " + GetClassName(method->output_type()) + ">";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700190 }
Masood Malekghassemiac592452016-07-01 11:58:04 -0700191 GOOGLE_LOG(FATAL) << "Can't get here.";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700192 return "";
193}
194
195std::string GetMethodRequestParamServer(const MethodDescriptor *method) {
196 switch (GetMethodType(method)) {
197 case METHODTYPE_NO_STREAMING:
198 case METHODTYPE_SERVER_STREAMING:
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700199 return GetClassName(method->input_type()) + " request";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700200 case METHODTYPE_CLIENT_STREAMING:
201 case METHODTYPE_BIDI_STREAMING:
Masood Malekghassemiac592452016-07-01 11:58:04 -0700202 return "IAsyncStreamReader<" + GetClassName(method->input_type()) +
203 "> requestStream";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700204 }
Masood Malekghassemiac592452016-07-01 11:58:04 -0700205 GOOGLE_LOG(FATAL) << "Can't get here.";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700206 return "";
207}
208
209std::string GetMethodReturnTypeServer(const MethodDescriptor *method) {
210 switch (GetMethodType(method)) {
211 case METHODTYPE_NO_STREAMING:
212 case METHODTYPE_CLIENT_STREAMING:
Masood Malekghassemiac592452016-07-01 11:58:04 -0700213 return "global::System.Threading.Tasks.Task<" +
214 GetClassName(method->output_type()) + ">";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700215 case METHODTYPE_SERVER_STREAMING:
216 case METHODTYPE_BIDI_STREAMING:
Kirill Katsnelson0ddf46f2016-04-19 18:49:31 -0700217 return "global::System.Threading.Tasks.Task";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700218 }
Masood Malekghassemiac592452016-07-01 11:58:04 -0700219 GOOGLE_LOG(FATAL) << "Can't get here.";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700220 return "";
221}
222
223std::string GetMethodResponseStreamMaybe(const MethodDescriptor *method) {
224 switch (GetMethodType(method)) {
225 case METHODTYPE_NO_STREAMING:
226 case METHODTYPE_CLIENT_STREAMING:
227 return "";
228 case METHODTYPE_SERVER_STREAMING:
229 case METHODTYPE_BIDI_STREAMING:
Masood Malekghassemiac592452016-07-01 11:58:04 -0700230 return ", IServerStreamWriter<" + GetClassName(method->output_type()) +
231 "> responseStream";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700232 }
Masood Malekghassemiac592452016-07-01 11:58:04 -0700233 GOOGLE_LOG(FATAL) << "Can't get here.";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700234 return "";
235}
236
237// Gets vector of all messages used as input or output types.
Masood Malekghassemiac592452016-07-01 11:58:04 -0700238std::vector<const Descriptor *> GetUsedMessages(
Jan Tattermusch2d924952015-05-06 10:23:17 -0700239 const ServiceDescriptor *service) {
Masood Malekghassemiac592452016-07-01 11:58:04 -0700240 std::set<const Descriptor *> descriptor_set;
241 std::vector<const Descriptor *>
242 result; // vector is to maintain stable ordering
Jan Tattermusch2d924952015-05-06 10:23:17 -0700243 for (int i = 0; i < service->method_count(); i++) {
244 const MethodDescriptor *method = service->method(i);
245 if (descriptor_set.find(method->input_type()) == descriptor_set.end()) {
246 descriptor_set.insert(method->input_type());
247 result.push_back(method->input_type());
248 }
249 if (descriptor_set.find(method->output_type()) == descriptor_set.end()) {
250 descriptor_set.insert(method->output_type());
251 result.push_back(method->output_type());
252 }
253 }
254 return result;
255}
256
Masood Malekghassemiac592452016-07-01 11:58:04 -0700257void GenerateMarshallerFields(Printer *out, const ServiceDescriptor *service) {
258 std::vector<const Descriptor *> used_messages = GetUsedMessages(service);
Jan Tattermusch2d924952015-05-06 10:23:17 -0700259 for (size_t i = 0; i < used_messages.size(); i++) {
260 const Descriptor *message = used_messages[i];
261 out->Print(
Masood Malekghassemiac592452016-07-01 11:58:04 -0700262 "static readonly Marshaller<$type$> $fieldname$ = "
263 "Marshallers.Create((arg) => "
264 "global::Google.Protobuf.MessageExtensions.ToByteArray(arg), "
265 "$type$.Parser.ParseFrom);\n",
Jan Tattermusch2d924952015-05-06 10:23:17 -0700266 "fieldname", GetMarshallerFieldName(message), "type",
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700267 GetClassName(message));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700268 }
269 out->Print("\n");
270}
271
Masood Malekghassemiac592452016-07-01 11:58:04 -0700272void GenerateStaticMethodField(Printer *out, const MethodDescriptor *method) {
Jan Tattermusch2d924952015-05-06 10:23:17 -0700273 out->Print(
Masood Malekghassemiac592452016-07-01 11:58:04 -0700274 "static readonly Method<$request$, $response$> $fieldname$ = new "
275 "Method<$request$, $response$>(\n",
Jan Tattermusch2d924952015-05-06 10:23:17 -0700276 "fieldname", GetMethodFieldName(method), "request",
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700277 GetClassName(method->input_type()), "response",
278 GetClassName(method->output_type()));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700279 out->Indent();
280 out->Indent();
281 out->Print("$methodtype$,\n", "methodtype",
282 GetCSharpMethodType(GetMethodType(method)));
Jan Tattermuscha9ddd022015-08-05 03:04:58 -0700283 out->Print("$servicenamefield$,\n", "servicenamefield",
Masood Malekghassemiac592452016-07-01 11:58:04 -0700284 GetServiceNameFieldName());
Jan Tattermusch2d924952015-05-06 10:23:17 -0700285 out->Print("\"$methodname$\",\n", "methodname", method->name());
286 out->Print("$requestmarshaller$,\n", "requestmarshaller",
287 GetMarshallerFieldName(method->input_type()));
288 out->Print("$responsemarshaller$);\n", "responsemarshaller",
289 GetMarshallerFieldName(method->output_type()));
290 out->Print("\n");
291 out->Outdent();
292 out->Outdent();
293}
294
Masood Malekghassemiac592452016-07-01 11:58:04 -0700295void GenerateServiceDescriptorProperty(Printer *out,
296 const ServiceDescriptor *service) {
yang-gc3ee1d52015-08-28 11:33:52 -0700297 std::ostringstream index;
298 index << service->index();
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700299 out->Print("/// <summary>Service descriptor</summary>\n");
Masood Malekghassemiac592452016-07-01 11:58:04 -0700300 out->Print(
301 "public static global::Google.Protobuf.Reflection.ServiceDescriptor "
302 "Descriptor\n");
Jan Tattermusche6af5d12015-08-03 10:57:43 -0700303 out->Print("{\n");
304 out->Print(" get { return $umbrella$.Descriptor.Services[$index$]; }\n",
Jan Tattermuschda717f42016-01-20 13:12:35 -0800305 "umbrella", GetReflectionClassName(service->file()), "index",
yang-gc3ee1d52015-08-28 11:33:52 -0700306 index.str());
Jan Tattermusche6af5d12015-08-03 10:57:43 -0700307 out->Print("}\n");
308 out->Print("\n");
309}
310
Masood Malekghassemiac592452016-07-01 11:58:04 -0700311void GenerateServerClass(Printer *out, const ServiceDescriptor *service) {
312 out->Print(
313 "/// <summary>Base class for server-side implementations of "
314 "$servicename$</summary>\n",
315 "servicename", GetServiceClassName(service));
Jan Tattermusch10a002d2016-03-14 15:22:19 -0700316 out->Print("public abstract class $name$\n", "name",
317 GetServerClassName(service));
318 out->Print("{\n");
319 out->Indent();
320 for (int i = 0; i < service->method_count(); i++) {
321 const MethodDescriptor *method = service->method(i);
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700322 GenerateDocCommentBody(out, method);
Jan Tattermusch10a002d2016-03-14 15:22:19 -0700323 out->Print(
Masood Malekghassemiac592452016-07-01 11:58:04 -0700324 "public virtual $returntype$ "
325 "$methodname$($request$$response_stream_maybe$, "
Jan Tattermusch10a002d2016-03-14 15:22:19 -0700326 "ServerCallContext context)\n",
327 "methodname", method->name(), "returntype",
328 GetMethodReturnTypeServer(method), "request",
329 GetMethodRequestParamServer(method), "response_stream_maybe",
330 GetMethodResponseStreamMaybe(method));
331 out->Print("{\n");
332 out->Indent();
Masood Malekghassemiac592452016-07-01 11:58:04 -0700333 out->Print(
334 "throw new RpcException("
335 "new Status(StatusCode.Unimplemented, \"\"));\n");
Jan Tattermusch10a002d2016-03-14 15:22:19 -0700336 out->Outdent();
337 out->Print("}\n\n");
338 }
339 out->Outdent();
340 out->Print("}\n");
341 out->Print("\n");
342}
343
Masood Malekghassemiac592452016-07-01 11:58:04 -0700344void GenerateClientStub(Printer *out, const ServiceDescriptor *service) {
345 out->Print("/// <summary>Client for $servicename$</summary>\n", "servicename",
346 GetServiceClassName(service));
347 out->Print("public class $name$ : ClientBase<$name$>\n", "name",
348 GetClientClassName(service));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700349 out->Print("{\n");
350 out->Indent();
351
352 // constructors
Masood Malekghassemiac592452016-07-01 11:58:04 -0700353 out->Print(
354 "/// <summary>Creates a new client for $servicename$</summary>\n"
355 "/// <param name=\"channel\">The channel to use to make remote "
356 "calls.</param>\n",
357 "servicename", GetServiceClassName(service));
358 out->Print("public $name$(Channel channel) : base(channel)\n", "name",
359 GetClientClassName(service));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700360 out->Print("{\n");
361 out->Print("}\n");
Masood Malekghassemiac592452016-07-01 11:58:04 -0700362 out->Print(
363 "/// <summary>Creates a new client for $servicename$ that uses a custom "
364 "<c>CallInvoker</c>.</summary>\n"
365 "/// <param name=\"callInvoker\">The callInvoker to use to make remote "
366 "calls.</param>\n",
367 "servicename", GetServiceClassName(service));
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700368 out->Print("public $name$(CallInvoker callInvoker) : base(callInvoker)\n",
369 "name", GetClientClassName(service));
370 out->Print("{\n");
Jan Tattermuschefb77842016-03-23 07:47:36 -0700371 out->Print("}\n");
Masood Malekghassemiac592452016-07-01 11:58:04 -0700372 out->Print(
373 "/// <summary>Protected parameterless constructor to allow creation"
374 " of test doubles.</summary>\n");
375 out->Print("protected $name$() : base()\n", "name",
376 GetClientClassName(service));
Jan Tattermuschefb77842016-03-23 07:47:36 -0700377 out->Print("{\n");
Jan Tattermusch2f0a8372016-03-23 09:16:49 -0700378 out->Print("}\n");
Masood Malekghassemiac592452016-07-01 11:58:04 -0700379 out->Print(
380 "/// <summary>Protected constructor to allow creation of configured "
381 "clients.</summary>\n"
382 "/// <param name=\"configuration\">The client configuration.</param>\n");
383 out->Print(
384 "protected $name$(ClientBaseConfiguration configuration)"
385 " : base(configuration)\n",
386 "name", GetClientClassName(service));
Jan Tattermusch2f0a8372016-03-23 09:16:49 -0700387 out->Print("{\n");
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700388 out->Print("}\n\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700389
390 for (int i = 0; i < service->method_count(); i++) {
391 const MethodDescriptor *method = service->method(i);
392 MethodType method_type = GetMethodType(method);
393
394 if (method_type == METHODTYPE_NO_STREAMING) {
395 // unary calls have an extra synchronous stub method
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700396 GenerateDocCommentBody(out, method);
Masood Malekghassemiac592452016-07-01 11:58:04 -0700397 out->Print(
398 "public virtual $response$ $methodname$($request$ request, Metadata "
399 "headers = null, DateTime? deadline = null, CancellationToken "
400 "cancellationToken = default(CancellationToken))\n",
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700401 "methodname", method->name(), "request",
402 GetClassName(method->input_type()), "response",
403 GetClassName(method->output_type()));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700404 out->Print("{\n");
405 out->Indent();
Masood Malekghassemiac592452016-07-01 11:58:04 -0700406 out->Print(
407 "return $methodname$(request, new CallOptions(headers, deadline, "
408 "cancellationToken));\n",
409 "methodname", method->name());
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700410 out->Outdent();
411 out->Print("}\n");
412
413 // overload taking CallOptions as a param
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700414 GenerateDocCommentBody(out, method);
Masood Malekghassemiac592452016-07-01 11:58:04 -0700415 out->Print(
416 "public virtual $response$ $methodname$($request$ request, "
417 "CallOptions options)\n",
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700418 "methodname", method->name(), "request",
419 GetClassName(method->input_type()), "response",
420 GetClassName(method->output_type()));
421 out->Print("{\n");
422 out->Indent();
Masood Malekghassemiac592452016-07-01 11:58:04 -0700423 out->Print(
424 "return CallInvoker.BlockingUnaryCall($methodfield$, null, options, "
425 "request);\n",
426 "methodfield", GetMethodFieldName(method));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700427 out->Outdent();
428 out->Print("}\n");
429 }
430
431 std::string method_name = method->name();
432 if (method_type == METHODTYPE_NO_STREAMING) {
433 method_name += "Async"; // prevent name clash with synchronous method.
434 }
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700435 GenerateDocCommentBody(out, method);
Jan Tattermusch2d924952015-05-06 10:23:17 -0700436 out->Print(
Masood Malekghassemiac592452016-07-01 11:58:04 -0700437 "public virtual $returntype$ $methodname$($request_maybe$Metadata "
438 "headers = null, DateTime? deadline = null, CancellationToken "
439 "cancellationToken = default(CancellationToken))\n",
440 "methodname", method_name, "request_maybe",
441 GetMethodRequestParamMaybe(method), "returntype",
442 GetMethodReturnTypeClient(method));
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700443 out->Print("{\n");
444 out->Indent();
445
Masood Malekghassemiac592452016-07-01 11:58:04 -0700446 out->Print(
447 "return $methodname$($request_maybe$new CallOptions(headers, deadline, "
448 "cancellationToken));\n",
449 "methodname", method_name, "request_maybe",
450 GetMethodRequestParamMaybe(method, true));
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700451 out->Outdent();
452 out->Print("}\n");
453
454 // overload taking CallOptions as a param
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700455 GenerateDocCommentBody(out, method);
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700456 out->Print(
Masood Malekghassemiac592452016-07-01 11:58:04 -0700457 "public virtual $returntype$ $methodname$($request_maybe$CallOptions "
458 "options)\n",
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700459 "methodname", method_name, "request_maybe",
460 GetMethodRequestParamMaybe(method), "returntype",
461 GetMethodReturnTypeClient(method));
462 out->Print("{\n");
463 out->Indent();
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700464 switch (GetMethodType(method)) {
465 case METHODTYPE_NO_STREAMING:
Masood Malekghassemiac592452016-07-01 11:58:04 -0700466 out->Print(
467 "return CallInvoker.AsyncUnaryCall($methodfield$, null, options, "
468 "request);\n",
469 "methodfield", GetMethodFieldName(method));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700470 break;
471 case METHODTYPE_CLIENT_STREAMING:
Masood Malekghassemiac592452016-07-01 11:58:04 -0700472 out->Print(
473 "return CallInvoker.AsyncClientStreamingCall($methodfield$, null, "
474 "options);\n",
475 "methodfield", GetMethodFieldName(method));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700476 break;
477 case METHODTYPE_SERVER_STREAMING:
478 out->Print(
Masood Malekghassemiac592452016-07-01 11:58:04 -0700479 "return CallInvoker.AsyncServerStreamingCall($methodfield$, null, "
480 "options, request);\n",
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700481 "methodfield", GetMethodFieldName(method));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700482 break;
483 case METHODTYPE_BIDI_STREAMING:
Masood Malekghassemiac592452016-07-01 11:58:04 -0700484 out->Print(
485 "return CallInvoker.AsyncDuplexStreamingCall($methodfield$, null, "
486 "options);\n",
487 "methodfield", GetMethodFieldName(method));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700488 break;
489 default:
Masood Malekghassemiac592452016-07-01 11:58:04 -0700490 GOOGLE_LOG(FATAL) << "Can't get here.";
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700491 }
Jan Tattermusch2d924952015-05-06 10:23:17 -0700492 out->Outdent();
493 out->Print("}\n");
494 }
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700495
496 // override NewInstance method
Masood Malekghassemiac592452016-07-01 11:58:04 -0700497 out->Print(
498 "protected override $name$ NewInstance(ClientBaseConfiguration "
499 "configuration)\n",
500 "name", GetClientClassName(service));
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700501 out->Print("{\n");
502 out->Indent();
Masood Malekghassemiac592452016-07-01 11:58:04 -0700503 out->Print("return new $name$(configuration);\n", "name",
504 GetClientClassName(service));
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700505 out->Outdent();
506 out->Print("}\n");
507
Jan Tattermusch2d924952015-05-06 10:23:17 -0700508 out->Outdent();
509 out->Print("}\n");
510 out->Print("\n");
511}
512
Masood Malekghassemiac592452016-07-01 11:58:04 -0700513void GenerateBindServiceMethod(Printer *out, const ServiceDescriptor *service) {
Jan Tattermusch2d924952015-05-06 10:23:17 -0700514 out->Print(
Masood Malekghassemiac592452016-07-01 11:58:04 -0700515 "/// <summary>Creates service definition that can be registered with a "
516 "server</summary>\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700517 out->Print(
Masood Malekghassemiac592452016-07-01 11:58:04 -0700518 "public static ServerServiceDefinition BindService($implclass$ "
519 "serviceImpl)\n",
Jan Tattermuschfcc8d972016-06-06 09:44:02 -0700520 "implclass", GetServerClassName(service));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700521 out->Print("{\n");
522 out->Indent();
523
Jan Tattermusch562cd052016-06-06 08:47:17 -0700524 out->Print("return ServerServiceDefinition.CreateBuilder()\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700525 out->Indent();
526 out->Indent();
527 for (int i = 0; i < service->method_count(); i++) {
528 const MethodDescriptor *method = service->method(i);
529 out->Print(".AddMethod($methodfield$, serviceImpl.$methodname$)",
530 "methodfield", GetMethodFieldName(method), "methodname",
531 method->name());
532 if (i == service->method_count() - 1) {
533 out->Print(".Build();");
534 }
535 out->Print("\n");
536 }
537 out->Outdent();
538 out->Outdent();
539
540 out->Outdent();
541 out->Print("}\n");
542 out->Print("\n");
543}
544
Masood Malekghassemiac592452016-07-01 11:58:04 -0700545void GenerateService(Printer *out, const ServiceDescriptor *service,
Jan Tattermusch4e0f73c2016-04-25 16:11:03 -0700546 bool generate_client, bool generate_server,
547 bool internal_access) {
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700548 GenerateDocCommentBody(out, service);
Jan Tattermusch4e0f73c2016-04-25 16:11:03 -0700549 out->Print("$access_level$ static class $classname$\n", "access_level",
550 GetAccessLevel(internal_access), "classname",
Jan Tattermusch2d924952015-05-06 10:23:17 -0700551 GetServiceClassName(service));
552 out->Print("{\n");
553 out->Indent();
554 out->Print("static readonly string $servicenamefield$ = \"$servicename$\";\n",
555 "servicenamefield", GetServiceNameFieldName(), "servicename",
556 service->full_name());
557 out->Print("\n");
558
559 GenerateMarshallerFields(out, service);
560 for (int i = 0; i < service->method_count(); i++) {
561 GenerateStaticMethodField(out, service->method(i));
562 }
Jan Tattermusche6af5d12015-08-03 10:57:43 -0700563 GenerateServiceDescriptorProperty(out, service);
Jan Tattermusch44f7c542016-04-25 14:50:27 -0700564
Jan Tattermusch44f7c542016-04-25 14:50:27 -0700565 if (generate_server) {
Jan Tattermusch44f7c542016-04-25 14:50:27 -0700566 GenerateServerClass(out, service);
567 }
568 if (generate_client) {
569 GenerateClientStub(out, service);
Jan Tattermusch44f7c542016-04-25 14:50:27 -0700570 }
571 if (generate_server) {
Jan Tattermuschfcc8d972016-06-06 09:44:02 -0700572 GenerateBindServiceMethod(out, service);
Jan Tattermusch44f7c542016-04-25 14:50:27 -0700573 }
Jan Tattermusch2d924952015-05-06 10:23:17 -0700574
575 out->Outdent();
576 out->Print("}\n");
577}
578
579} // anonymous namespace
580
Jan Tattermusch5f8872f2016-04-25 15:57:10 -0700581grpc::string GetServices(const FileDescriptor *file, bool generate_client,
Jan Tattermusch4e0f73c2016-04-25 16:11:03 -0700582 bool generate_server, bool internal_access) {
Jan Tattermusch2d924952015-05-06 10:23:17 -0700583 grpc::string output;
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700584 {
585 // Scope the output stream so it closes and finalizes output to the string.
Jan Tattermusch2d924952015-05-06 10:23:17 -0700586
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700587 StringOutputStream output_stream(&output);
588 Printer out(&output_stream, '$');
589
590 // Don't write out any output if there no services, to avoid empty service
591 // files being generated for proto files that don't declare any.
592 if (file->service_count() == 0) {
593 return output;
594 }
595
596 // Write out a file header.
597 out.Print("// Generated by the protocol buffer compiler. DO NOT EDIT!\n");
598 out.Print("// source: $filename$\n", "filename", file->name());
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700599
600 // use C++ style as there are no file-level XML comments in .NET
murgatroid99210f3c42016-05-20 13:24:59 -0700601 grpc::string leading_comments = GetCsharpComments(file, true);
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700602 if (!leading_comments.empty()) {
603 out.Print("// Original file comments:\n");
604 out.Print(leading_comments.c_str());
605 }
606
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700607 out.Print("#region Designer generated code\n");
608 out.Print("\n");
609 out.Print("using System;\n");
610 out.Print("using System.Threading;\n");
611 out.Print("using System.Threading.Tasks;\n");
612 out.Print("using Grpc.Core;\n");
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700613 out.Print("\n");
614
615 out.Print("namespace $namespace$ {\n", "namespace", GetFileNamespace(file));
616 out.Indent();
617 for (int i = 0; i < file->service_count(); i++) {
Jan Tattermusch4e0f73c2016-04-25 16:11:03 -0700618 GenerateService(&out, file->service(i), generate_client, generate_server,
619 internal_access);
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700620 }
621 out.Outdent();
622 out.Print("}\n");
623 out.Print("#endregion\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700624 }
Jan Tattermusch2d924952015-05-06 10:23:17 -0700625 return output;
626}
627
628} // namespace grpc_csharp_generator