blob: 7b497df7f4775edafbac825b104633615737e66f [file] [log] [blame]
Jan Tattermusch2d924952015-05-06 10:23:17 -07001/*
2 *
3 * Copyright 2015, Google Inc.
4 * 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;
47using google::protobuf::compiler::csharp::GetUmbrellaClassName;
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
84std::string GetCSharpMethodType(MethodType method_type) {
85 switch (method_type) {
86 case METHODTYPE_NO_STREAMING:
87 return "MethodType.Unary";
88 case METHODTYPE_CLIENT_STREAMING:
89 return "MethodType.ClientStreaming";
90 case METHODTYPE_SERVER_STREAMING:
91 return "MethodType.ServerStreaming";
92 case METHODTYPE_BIDI_STREAMING:
93 return "MethodType.DuplexStreaming";
94 }
95 GOOGLE_LOG(FATAL)<< "Can't get here.";
96 return "";
97}
98
99std::string GetServiceNameFieldName() {
100 return "__ServiceName";
101}
102
103std::string GetMarshallerFieldName(const Descriptor *message) {
104 return "__Marshaller_" + message->name();
105}
106
107std::string GetMethodFieldName(const MethodDescriptor *method) {
108 return "__Method_" + method->name();
109}
110
111std::string GetMethodRequestParamMaybe(const MethodDescriptor *method) {
112 if (method->client_streaming()) {
113 return "";
114 }
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700115 return GetClassName(method->input_type()) + " request, ";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700116}
117
118std::string GetMethodReturnTypeClient(const MethodDescriptor *method) {
119 switch (GetMethodType(method)) {
120 case METHODTYPE_NO_STREAMING:
Jan Tattermusch5269d162015-07-21 11:56:42 -0700121 return "AsyncUnaryCall<" + GetClassName(method->output_type()) + ">";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700122 case METHODTYPE_CLIENT_STREAMING:
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700123 return "AsyncClientStreamingCall<" + GetClassName(method->input_type())
124 + ", " + GetClassName(method->output_type()) + ">";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700125 case METHODTYPE_SERVER_STREAMING:
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700126 return "AsyncServerStreamingCall<" + GetClassName(method->output_type())
Jan Tattermusch2d924952015-05-06 10:23:17 -0700127 + ">";
128 case METHODTYPE_BIDI_STREAMING:
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700129 return "AsyncDuplexStreamingCall<" + GetClassName(method->input_type())
130 + ", " + GetClassName(method->output_type()) + ">";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700131 }
132 GOOGLE_LOG(FATAL)<< "Can't get here.";
133 return "";
134}
135
136std::string GetMethodRequestParamServer(const MethodDescriptor *method) {
137 switch (GetMethodType(method)) {
138 case METHODTYPE_NO_STREAMING:
139 case METHODTYPE_SERVER_STREAMING:
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700140 return GetClassName(method->input_type()) + " request";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700141 case METHODTYPE_CLIENT_STREAMING:
142 case METHODTYPE_BIDI_STREAMING:
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700143 return "IAsyncStreamReader<" + GetClassName(method->input_type())
Jan Tattermusch2d924952015-05-06 10:23:17 -0700144 + "> requestStream";
145 }
146 GOOGLE_LOG(FATAL)<< "Can't get here.";
147 return "";
148}
149
150std::string GetMethodReturnTypeServer(const MethodDescriptor *method) {
151 switch (GetMethodType(method)) {
152 case METHODTYPE_NO_STREAMING:
153 case METHODTYPE_CLIENT_STREAMING:
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700154 return "Task<" + GetClassName(method->output_type()) + ">";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700155 case METHODTYPE_SERVER_STREAMING:
156 case METHODTYPE_BIDI_STREAMING:
157 return "Task";
158 }
159 GOOGLE_LOG(FATAL)<< "Can't get here.";
160 return "";
161}
162
163std::string GetMethodResponseStreamMaybe(const MethodDescriptor *method) {
164 switch (GetMethodType(method)) {
165 case METHODTYPE_NO_STREAMING:
166 case METHODTYPE_CLIENT_STREAMING:
167 return "";
168 case METHODTYPE_SERVER_STREAMING:
169 case METHODTYPE_BIDI_STREAMING:
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700170 return ", IServerStreamWriter<" + GetClassName(method->output_type())
Jan Tattermusch2d924952015-05-06 10:23:17 -0700171 + "> responseStream";
172 }
173 GOOGLE_LOG(FATAL)<< "Can't get here.";
174 return "";
175}
176
177// Gets vector of all messages used as input or output types.
178std::vector<const Descriptor*> GetUsedMessages(
179 const ServiceDescriptor *service) {
180 std::set<const Descriptor*> descriptor_set;
181 std::vector<const Descriptor*> result; // vector is to maintain stable ordering
182 for (int i = 0; i < service->method_count(); i++) {
183 const MethodDescriptor *method = service->method(i);
184 if (descriptor_set.find(method->input_type()) == descriptor_set.end()) {
185 descriptor_set.insert(method->input_type());
186 result.push_back(method->input_type());
187 }
188 if (descriptor_set.find(method->output_type()) == descriptor_set.end()) {
189 descriptor_set.insert(method->output_type());
190 result.push_back(method->output_type());
191 }
192 }
193 return result;
194}
195
196void GenerateMarshallerFields(Printer* out, const ServiceDescriptor *service) {
197 std::vector<const Descriptor*> used_messages = GetUsedMessages(service);
198 for (size_t i = 0; i < used_messages.size(); i++) {
199 const Descriptor *message = used_messages[i];
200 out->Print(
Jan Tattermuschad75dd12015-08-03 09:43:07 -0700201 "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 -0700202 "fieldname", GetMarshallerFieldName(message), "type",
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700203 GetClassName(message));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700204 }
205 out->Print("\n");
206}
207
208void GenerateStaticMethodField(Printer* out, const MethodDescriptor *method) {
209 out->Print(
210 "static readonly Method<$request$, $response$> $fieldname$ = new Method<$request$, $response$>(\n",
211 "fieldname", GetMethodFieldName(method), "request",
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700212 GetClassName(method->input_type()), "response",
213 GetClassName(method->output_type()));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700214 out->Indent();
215 out->Indent();
216 out->Print("$methodtype$,\n", "methodtype",
217 GetCSharpMethodType(GetMethodType(method)));
Jan Tattermuscha9ddd022015-08-05 03:04:58 -0700218 out->Print("$servicenamefield$,\n", "servicenamefield",
219 GetServiceNameFieldName());
Jan Tattermusch2d924952015-05-06 10:23:17 -0700220 out->Print("\"$methodname$\",\n", "methodname", method->name());
221 out->Print("$requestmarshaller$,\n", "requestmarshaller",
222 GetMarshallerFieldName(method->input_type()));
223 out->Print("$responsemarshaller$);\n", "responsemarshaller",
224 GetMarshallerFieldName(method->output_type()));
225 out->Print("\n");
226 out->Outdent();
227 out->Outdent();
228}
229
Jan Tattermusche6af5d12015-08-03 10:57:43 -0700230void GenerateServiceDescriptorProperty(Printer* out, const ServiceDescriptor *service) {
yang-gc3ee1d52015-08-28 11:33:52 -0700231 std::ostringstream index;
232 index << service->index();
Jan Tattermusche6af5d12015-08-03 10:57:43 -0700233 out->Print("// service descriptor\n");
234 out->Print("public static global::Google.Protobuf.Reflection.ServiceDescriptor Descriptor\n");
235 out->Print("{\n");
236 out->Print(" get { return $umbrella$.Descriptor.Services[$index$]; }\n",
yang-gc3ee1d52015-08-28 11:33:52 -0700237 "umbrella", GetUmbrellaClassName(service->file()), "index",
238 index.str());
Jan Tattermusche6af5d12015-08-03 10:57:43 -0700239 out->Print("}\n");
240 out->Print("\n");
241}
242
Jan Tattermusch2d924952015-05-06 10:23:17 -0700243void GenerateClientInterface(Printer* out, const ServiceDescriptor *service) {
Jan Tattermuschb5332812015-07-14 19:29:35 -0700244 out->Print("// client interface\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700245 out->Print("public interface $name$\n", "name",
246 GetClientInterfaceName(service));
247 out->Print("{\n");
248 out->Indent();
249 for (int i = 0; i < service->method_count(); i++) {
250 const MethodDescriptor *method = service->method(i);
251 MethodType method_type = GetMethodType(method);
252
253 if (method_type == METHODTYPE_NO_STREAMING) {
254 // unary calls have an extra synchronous stub method
255 out->Print(
Jan Tattermusch74529562015-07-23 14:04:51 -0700256 "$response$ $methodname$($request$ request, Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken));\n",
Jan Tattermusch2d924952015-05-06 10:23:17 -0700257 "methodname", method->name(), "request",
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700258 GetClassName(method->input_type()), "response",
259 GetClassName(method->output_type()));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700260
Jan Tattermusche5e57ad2015-08-05 14:54:08 -0700261 // overload taking CallOptions as a param
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700262 out->Print(
Jan Tattermusche5e57ad2015-08-05 14:54:08 -0700263 "$response$ $methodname$($request$ request, CallOptions options);\n",
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700264 "methodname", method->name(), "request",
265 GetClassName(method->input_type()), "response",
266 GetClassName(method->output_type()));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700267 }
268
269 std::string method_name = method->name();
270 if (method_type == METHODTYPE_NO_STREAMING) {
271 method_name += "Async"; // prevent name clash with synchronous method.
272 }
273 out->Print(
Jan Tattermusch74529562015-07-23 14:04:51 -0700274 "$returntype$ $methodname$($request_maybe$Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken));\n",
Jan Tattermusch2d924952015-05-06 10:23:17 -0700275 "methodname", method_name, "request_maybe",
276 GetMethodRequestParamMaybe(method), "returntype",
277 GetMethodReturnTypeClient(method));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700278
Jan Tattermusche5e57ad2015-08-05 14:54:08 -0700279 // overload taking CallOptions as a param
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700280 out->Print(
Jan Tattermusche5e57ad2015-08-05 14:54:08 -0700281 "$returntype$ $methodname$($request_maybe$CallOptions options);\n",
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700282 "methodname", method_name, "request_maybe",
283 GetMethodRequestParamMaybe(method), "returntype",
284 GetMethodReturnTypeClient(method));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700285 }
286 out->Outdent();
287 out->Print("}\n");
288 out->Print("\n");
289}
290
291void GenerateServerInterface(Printer* out, const ServiceDescriptor *service) {
292 out->Print("// server-side interface\n");
293 out->Print("public interface $name$\n", "name",
294 GetServerInterfaceName(service));
295 out->Print("{\n");
296 out->Indent();
297 for (int i = 0; i < service->method_count(); i++) {
298 const MethodDescriptor *method = service->method(i);
Craig Tillerb256faa2015-07-23 11:28:16 -0700299 out->Print(
300 "$returntype$ $methodname$($request$$response_stream_maybe$, "
301 "ServerCallContext context);\n",
302 "methodname", method->name(), "returntype",
303 GetMethodReturnTypeServer(method), "request",
304 GetMethodRequestParamServer(method), "response_stream_maybe",
305 GetMethodResponseStreamMaybe(method));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700306 }
307 out->Outdent();
308 out->Print("}\n");
309 out->Print("\n");
310}
311
312void GenerateClientStub(Printer* out, const ServiceDescriptor *service) {
313 out->Print("// client stub\n");
314 out->Print(
Jan Tattermuschb5332812015-07-14 19:29:35 -0700315 "public class $name$ : ClientBase, $interface$\n",
Jan Tattermusch2d924952015-05-06 10:23:17 -0700316 "name", GetClientClassName(service), "interface",
317 GetClientInterfaceName(service));
318 out->Print("{\n");
319 out->Indent();
320
321 // constructors
322 out->Print(
Jan Tattermuschb5332812015-07-14 19:29:35 -0700323 "public $name$(Channel channel) : base(channel)\n",
Jan Tattermusch2d924952015-05-06 10:23:17 -0700324 "name", GetClientClassName(service));
325 out->Print("{\n");
326 out->Print("}\n");
327
328 for (int i = 0; i < service->method_count(); i++) {
329 const MethodDescriptor *method = service->method(i);
330 MethodType method_type = GetMethodType(method);
331
332 if (method_type == METHODTYPE_NO_STREAMING) {
333 // unary calls have an extra synchronous stub method
334 out->Print(
Jan Tattermusch74529562015-07-23 14:04:51 -0700335 "public $response$ $methodname$($request$ request, Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken))\n",
Jan Tattermusch2d924952015-05-06 10:23:17 -0700336 "methodname", method->name(), "request",
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700337 GetClassName(method->input_type()), "response",
338 GetClassName(method->output_type()));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700339 out->Print("{\n");
340 out->Indent();
Jan Tattermusche5e57ad2015-08-05 14:54:08 -0700341 out->Print("var call = CreateCall($methodfield$, new CallOptions(headers, deadline, cancellationToken));\n",
Jan Tattermusch641cb1b2015-08-05 03:51:46 -0700342 "methodfield", GetMethodFieldName(method));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700343 out->Print("return Calls.BlockingUnaryCall(call, request);\n");
344 out->Outdent();
345 out->Print("}\n");
346
Jan Tattermusche5e57ad2015-08-05 14:54:08 -0700347 // overload taking CallOptions as a param
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700348 out->Print(
Jan Tattermusche5e57ad2015-08-05 14:54:08 -0700349 "public $response$ $methodname$($request$ request, CallOptions options)\n",
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700350 "methodname", method->name(), "request",
351 GetClassName(method->input_type()), "response",
352 GetClassName(method->output_type()));
353 out->Print("{\n");
354 out->Indent();
Jan Tattermusche5e57ad2015-08-05 14:54:08 -0700355 out->Print("var call = CreateCall($methodfield$, options);\n",
Jan Tattermusch641cb1b2015-08-05 03:51:46 -0700356 "methodfield", GetMethodFieldName(method));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700357 out->Print("return Calls.BlockingUnaryCall(call, request);\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700358 out->Outdent();
359 out->Print("}\n");
360 }
361
362 std::string method_name = method->name();
363 if (method_type == METHODTYPE_NO_STREAMING) {
364 method_name += "Async"; // prevent name clash with synchronous method.
365 }
366 out->Print(
Jan Tattermusch74529562015-07-23 14:04:51 -0700367 "public $returntype$ $methodname$($request_maybe$Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken))\n",
Jan Tattermusch2d924952015-05-06 10:23:17 -0700368 "methodname", method_name, "request_maybe",
369 GetMethodRequestParamMaybe(method), "returntype",
370 GetMethodReturnTypeClient(method));
371 out->Print("{\n");
372 out->Indent();
Jan Tattermusche5e57ad2015-08-05 14:54:08 -0700373 out->Print("var call = CreateCall($methodfield$, new CallOptions(headers, deadline, cancellationToken));\n",
Jan Tattermusch641cb1b2015-08-05 03:51:46 -0700374 "methodfield", GetMethodFieldName(method));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700375 switch (GetMethodType(method)) {
376 case METHODTYPE_NO_STREAMING:
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700377 out->Print("return Calls.AsyncUnaryCall(call, request);\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700378 break;
379 case METHODTYPE_CLIENT_STREAMING:
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700380 out->Print("return Calls.AsyncClientStreamingCall(call);\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700381 break;
382 case METHODTYPE_SERVER_STREAMING:
383 out->Print(
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700384 "return Calls.AsyncServerStreamingCall(call, request);\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700385 break;
386 case METHODTYPE_BIDI_STREAMING:
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700387 out->Print("return Calls.AsyncDuplexStreamingCall(call);\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700388 break;
389 default:
390 GOOGLE_LOG(FATAL)<< "Can't get here.";
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700391 }
392 out->Outdent();
393 out->Print("}\n");
394
Jan Tattermusche5e57ad2015-08-05 14:54:08 -0700395 // overload taking CallOptions as a param
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700396 out->Print(
Jan Tattermusche5e57ad2015-08-05 14:54:08 -0700397 "public $returntype$ $methodname$($request_maybe$CallOptions options)\n",
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700398 "methodname", method_name, "request_maybe",
399 GetMethodRequestParamMaybe(method), "returntype",
400 GetMethodReturnTypeClient(method));
401 out->Print("{\n");
402 out->Indent();
Jan Tattermusche5e57ad2015-08-05 14:54:08 -0700403 out->Print("var call = CreateCall($methodfield$, options);\n",
Jan Tattermusch641cb1b2015-08-05 03:51:46 -0700404 "methodfield", GetMethodFieldName(method));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700405 switch (GetMethodType(method)) {
406 case METHODTYPE_NO_STREAMING:
407 out->Print("return Calls.AsyncUnaryCall(call, request);\n");
408 break;
409 case METHODTYPE_CLIENT_STREAMING:
410 out->Print("return Calls.AsyncClientStreamingCall(call);\n");
411 break;
412 case METHODTYPE_SERVER_STREAMING:
413 out->Print(
414 "return Calls.AsyncServerStreamingCall(call, request);\n");
415 break;
416 case METHODTYPE_BIDI_STREAMING:
417 out->Print("return Calls.AsyncDuplexStreamingCall(call);\n");
418 break;
419 default:
420 GOOGLE_LOG(FATAL)<< "Can't get here.";
421 }
Jan Tattermusch2d924952015-05-06 10:23:17 -0700422 out->Outdent();
423 out->Print("}\n");
424 }
425 out->Outdent();
426 out->Print("}\n");
427 out->Print("\n");
428}
429
430void GenerateBindServiceMethod(Printer* out, const ServiceDescriptor *service) {
431 out->Print(
432 "// creates service definition that can be registered with a server\n");
433 out->Print(
434 "public static ServerServiceDefinition BindService($interface$ serviceImpl)\n",
435 "interface", GetServerInterfaceName(service));
436 out->Print("{\n");
437 out->Indent();
438
439 out->Print(
440 "return ServerServiceDefinition.CreateBuilder($servicenamefield$)\n",
441 "servicenamefield", GetServiceNameFieldName());
442 out->Indent();
443 out->Indent();
444 for (int i = 0; i < service->method_count(); i++) {
445 const MethodDescriptor *method = service->method(i);
446 out->Print(".AddMethod($methodfield$, serviceImpl.$methodname$)",
447 "methodfield", GetMethodFieldName(method), "methodname",
448 method->name());
449 if (i == service->method_count() - 1) {
450 out->Print(".Build();");
451 }
452 out->Print("\n");
453 }
454 out->Outdent();
455 out->Outdent();
456
457 out->Outdent();
458 out->Print("}\n");
459 out->Print("\n");
460}
461
462void GenerateNewStubMethods(Printer* out, const ServiceDescriptor *service) {
Jan Tattermuschb5332812015-07-14 19:29:35 -0700463 out->Print("// creates a new client\n");
464 out->Print("public static $classname$ NewClient(Channel channel)\n",
465 "classname", GetClientClassName(service));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700466 out->Print("{\n");
467 out->Indent();
468 out->Print("return new $classname$(channel);\n", "classname",
469 GetClientClassName(service));
470 out->Outdent();
471 out->Print("}\n");
472 out->Print("\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700473}
474
475void GenerateService(Printer* out, const ServiceDescriptor *service) {
476 out->Print("public static class $classname$\n", "classname",
477 GetServiceClassName(service));
478 out->Print("{\n");
479 out->Indent();
480 out->Print("static readonly string $servicenamefield$ = \"$servicename$\";\n",
481 "servicenamefield", GetServiceNameFieldName(), "servicename",
482 service->full_name());
483 out->Print("\n");
484
485 GenerateMarshallerFields(out, service);
486 for (int i = 0; i < service->method_count(); i++) {
487 GenerateStaticMethodField(out, service->method(i));
488 }
Jan Tattermusche6af5d12015-08-03 10:57:43 -0700489 GenerateServiceDescriptorProperty(out, service);
Jan Tattermusch2d924952015-05-06 10:23:17 -0700490 GenerateClientInterface(out, service);
491 GenerateServerInterface(out, service);
492 GenerateClientStub(out, service);
493 GenerateBindServiceMethod(out, service);
494 GenerateNewStubMethods(out, service);
495
496 out->Outdent();
497 out->Print("}\n");
498}
499
500} // anonymous namespace
501
502grpc::string GetServices(const FileDescriptor *file) {
503 grpc::string output;
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700504 {
505 // Scope the output stream so it closes and finalizes output to the string.
Jan Tattermusch2d924952015-05-06 10:23:17 -0700506
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700507 StringOutputStream output_stream(&output);
508 Printer out(&output_stream, '$');
509
510 // Don't write out any output if there no services, to avoid empty service
511 // files being generated for proto files that don't declare any.
512 if (file->service_count() == 0) {
513 return output;
514 }
515
516 // Write out a file header.
517 out.Print("// Generated by the protocol buffer compiler. DO NOT EDIT!\n");
518 out.Print("// source: $filename$\n", "filename", file->name());
519 out.Print("#region Designer generated code\n");
520 out.Print("\n");
521 out.Print("using System;\n");
522 out.Print("using System.Threading;\n");
523 out.Print("using System.Threading.Tasks;\n");
524 out.Print("using Grpc.Core;\n");
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700525 out.Print("\n");
526
527 out.Print("namespace $namespace$ {\n", "namespace", GetFileNamespace(file));
528 out.Indent();
529 for (int i = 0; i < file->service_count(); i++) {
530 GenerateService(&out, file->service(i));
531 }
532 out.Outdent();
533 out.Print("}\n");
534 out.Print("#endregion\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700535 }
Jan Tattermusch2d924952015-05-06 10:23:17 -0700536 return output;
537}
538
539} // namespace grpc_csharp_generator