Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 1 | /* |
| 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> |
| 36 | #include <vector> |
| 37 | |
| 38 | #include "src/compiler/config.h" |
Jan Tattermusch | b5897bf | 2015-05-07 15:45:37 -0700 | [diff] [blame] | 39 | #include "src/compiler/csharp_generator_helpers.h" |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 40 | #include "src/compiler/csharp_generator.h" |
| 41 | |
| 42 | using grpc::protobuf::FileDescriptor; |
| 43 | using grpc::protobuf::Descriptor; |
| 44 | using grpc::protobuf::ServiceDescriptor; |
| 45 | using grpc::protobuf::MethodDescriptor; |
| 46 | using grpc::protobuf::io::Printer; |
| 47 | using grpc::protobuf::io::StringOutputStream; |
Jan Tattermusch | b5897bf | 2015-05-07 15:45:37 -0700 | [diff] [blame] | 48 | using grpc_generator::MethodType; |
| 49 | using grpc_generator::GetMethodType; |
| 50 | using grpc_generator::METHODTYPE_NO_STREAMING; |
| 51 | using grpc_generator::METHODTYPE_CLIENT_STREAMING; |
| 52 | using grpc_generator::METHODTYPE_SERVER_STREAMING; |
| 53 | using grpc_generator::METHODTYPE_BIDI_STREAMING; |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 54 | using grpc_generator::StringReplace; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 55 | using std::map; |
| 56 | using std::vector; |
| 57 | |
| 58 | namespace grpc_csharp_generator { |
| 59 | namespace { |
| 60 | |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 61 | // TODO(jtattermusch): make GetFileNamespace part of libprotoc public API. |
| 62 | // NOTE: Implementation needs to match exactly to GetFileNamespace |
| 63 | // defined in csharp_helpers.h in protoc csharp plugin. |
| 64 | // We cannot reference it directly because google3 protobufs |
| 65 | // don't have a csharp protoc plugin. |
| 66 | std::string GetFileNamespace(const FileDescriptor* file) { |
| 67 | if (file->options().has_csharp_namespace()) { |
| 68 | return file->options().csharp_namespace(); |
| 69 | } |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 70 | return file->package(); |
| 71 | } |
| 72 | |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 73 | std::string ToCSharpName(const std::string& name, const FileDescriptor* file) { |
| 74 | std::string result = GetFileNamespace(file); |
| 75 | if (result != "") { |
| 76 | result += '.'; |
| 77 | } |
| 78 | std::string classname; |
| 79 | if (file->package().empty()) { |
| 80 | classname = name; |
| 81 | } else { |
| 82 | // Strip the proto package from full_name since we've replaced it with |
| 83 | // the C# namespace. |
| 84 | classname = name.substr(file->package().size() + 1); |
| 85 | } |
| 86 | result += StringReplace(classname, ".", ".Types.", false); |
| 87 | return "global::" + result; |
| 88 | } |
| 89 | |
| 90 | // TODO(jtattermusch): make GetClassName part of libprotoc public API. |
| 91 | // NOTE: Implementation needs to match exactly to GetClassName |
| 92 | // defined in csharp_helpers.h in protoc csharp plugin. |
| 93 | // We cannot reference it directly because google3 protobufs |
| 94 | // don't have a csharp protoc plugin. |
| 95 | std::string GetClassName(const Descriptor* message) { |
| 96 | return ToCSharpName(message->full_name(), message->file()); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | std::string GetServiceClassName(const ServiceDescriptor* service) { |
| 100 | return service->name(); |
| 101 | } |
| 102 | |
| 103 | std::string GetClientInterfaceName(const ServiceDescriptor* service) { |
| 104 | return "I" + service->name() + "Client"; |
| 105 | } |
| 106 | |
| 107 | std::string GetClientClassName(const ServiceDescriptor* service) { |
| 108 | return service->name() + "Client"; |
| 109 | } |
| 110 | |
| 111 | std::string GetServerInterfaceName(const ServiceDescriptor* service) { |
| 112 | return "I" + service->name(); |
| 113 | } |
| 114 | |
| 115 | std::string GetCSharpMethodType(MethodType method_type) { |
| 116 | switch (method_type) { |
| 117 | case METHODTYPE_NO_STREAMING: |
| 118 | return "MethodType.Unary"; |
| 119 | case METHODTYPE_CLIENT_STREAMING: |
| 120 | return "MethodType.ClientStreaming"; |
| 121 | case METHODTYPE_SERVER_STREAMING: |
| 122 | return "MethodType.ServerStreaming"; |
| 123 | case METHODTYPE_BIDI_STREAMING: |
| 124 | return "MethodType.DuplexStreaming"; |
| 125 | } |
| 126 | GOOGLE_LOG(FATAL)<< "Can't get here."; |
| 127 | return ""; |
| 128 | } |
| 129 | |
| 130 | std::string GetServiceNameFieldName() { |
| 131 | return "__ServiceName"; |
| 132 | } |
| 133 | |
| 134 | std::string GetMarshallerFieldName(const Descriptor *message) { |
| 135 | return "__Marshaller_" + message->name(); |
| 136 | } |
| 137 | |
| 138 | std::string GetMethodFieldName(const MethodDescriptor *method) { |
| 139 | return "__Method_" + method->name(); |
| 140 | } |
| 141 | |
| 142 | std::string GetMethodRequestParamMaybe(const MethodDescriptor *method) { |
| 143 | if (method->client_streaming()) { |
| 144 | return ""; |
| 145 | } |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 146 | return GetClassName(method->input_type()) + " request, "; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | std::string GetMethodReturnTypeClient(const MethodDescriptor *method) { |
| 150 | switch (GetMethodType(method)) { |
| 151 | case METHODTYPE_NO_STREAMING: |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 152 | return "Task<" + GetClassName(method->output_type()) + ">"; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 153 | case METHODTYPE_CLIENT_STREAMING: |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 154 | return "AsyncClientStreamingCall<" + GetClassName(method->input_type()) |
| 155 | + ", " + GetClassName(method->output_type()) + ">"; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 156 | case METHODTYPE_SERVER_STREAMING: |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 157 | return "AsyncServerStreamingCall<" + GetClassName(method->output_type()) |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 158 | + ">"; |
| 159 | case METHODTYPE_BIDI_STREAMING: |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 160 | return "AsyncDuplexStreamingCall<" + GetClassName(method->input_type()) |
| 161 | + ", " + GetClassName(method->output_type()) + ">"; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 162 | } |
| 163 | GOOGLE_LOG(FATAL)<< "Can't get here."; |
| 164 | return ""; |
| 165 | } |
| 166 | |
| 167 | std::string GetMethodRequestParamServer(const MethodDescriptor *method) { |
| 168 | switch (GetMethodType(method)) { |
| 169 | case METHODTYPE_NO_STREAMING: |
| 170 | case METHODTYPE_SERVER_STREAMING: |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 171 | return GetClassName(method->input_type()) + " request"; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 172 | case METHODTYPE_CLIENT_STREAMING: |
| 173 | case METHODTYPE_BIDI_STREAMING: |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 174 | return "IAsyncStreamReader<" + GetClassName(method->input_type()) |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 175 | + "> requestStream"; |
| 176 | } |
| 177 | GOOGLE_LOG(FATAL)<< "Can't get here."; |
| 178 | return ""; |
| 179 | } |
| 180 | |
| 181 | std::string GetMethodReturnTypeServer(const MethodDescriptor *method) { |
| 182 | switch (GetMethodType(method)) { |
| 183 | case METHODTYPE_NO_STREAMING: |
| 184 | case METHODTYPE_CLIENT_STREAMING: |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 185 | return "Task<" + GetClassName(method->output_type()) + ">"; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 186 | case METHODTYPE_SERVER_STREAMING: |
| 187 | case METHODTYPE_BIDI_STREAMING: |
| 188 | return "Task"; |
| 189 | } |
| 190 | GOOGLE_LOG(FATAL)<< "Can't get here."; |
| 191 | return ""; |
| 192 | } |
| 193 | |
| 194 | std::string GetMethodResponseStreamMaybe(const MethodDescriptor *method) { |
| 195 | switch (GetMethodType(method)) { |
| 196 | case METHODTYPE_NO_STREAMING: |
| 197 | case METHODTYPE_CLIENT_STREAMING: |
| 198 | return ""; |
| 199 | case METHODTYPE_SERVER_STREAMING: |
| 200 | case METHODTYPE_BIDI_STREAMING: |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 201 | return ", IServerStreamWriter<" + GetClassName(method->output_type()) |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 202 | + "> responseStream"; |
| 203 | } |
| 204 | GOOGLE_LOG(FATAL)<< "Can't get here."; |
| 205 | return ""; |
| 206 | } |
| 207 | |
| 208 | // Gets vector of all messages used as input or output types. |
| 209 | std::vector<const Descriptor*> GetUsedMessages( |
| 210 | const ServiceDescriptor *service) { |
| 211 | std::set<const Descriptor*> descriptor_set; |
| 212 | std::vector<const Descriptor*> result; // vector is to maintain stable ordering |
| 213 | for (int i = 0; i < service->method_count(); i++) { |
| 214 | const MethodDescriptor *method = service->method(i); |
| 215 | if (descriptor_set.find(method->input_type()) == descriptor_set.end()) { |
| 216 | descriptor_set.insert(method->input_type()); |
| 217 | result.push_back(method->input_type()); |
| 218 | } |
| 219 | if (descriptor_set.find(method->output_type()) == descriptor_set.end()) { |
| 220 | descriptor_set.insert(method->output_type()); |
| 221 | result.push_back(method->output_type()); |
| 222 | } |
| 223 | } |
| 224 | return result; |
| 225 | } |
| 226 | |
| 227 | void GenerateMarshallerFields(Printer* out, const ServiceDescriptor *service) { |
| 228 | std::vector<const Descriptor*> used_messages = GetUsedMessages(service); |
| 229 | for (size_t i = 0; i < used_messages.size(); i++) { |
| 230 | const Descriptor *message = used_messages[i]; |
| 231 | out->Print( |
| 232 | "static readonly Marshaller<$type$> $fieldname$ = Marshallers.Create((arg) => arg.ToByteArray(), $type$.ParseFrom);\n", |
| 233 | "fieldname", GetMarshallerFieldName(message), "type", |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 234 | GetClassName(message)); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 235 | } |
| 236 | out->Print("\n"); |
| 237 | } |
| 238 | |
| 239 | void GenerateStaticMethodField(Printer* out, const MethodDescriptor *method) { |
| 240 | out->Print( |
| 241 | "static readonly Method<$request$, $response$> $fieldname$ = new Method<$request$, $response$>(\n", |
| 242 | "fieldname", GetMethodFieldName(method), "request", |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 243 | GetClassName(method->input_type()), "response", |
| 244 | GetClassName(method->output_type())); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 245 | out->Indent(); |
| 246 | out->Indent(); |
| 247 | out->Print("$methodtype$,\n", "methodtype", |
| 248 | GetCSharpMethodType(GetMethodType(method))); |
| 249 | out->Print("\"$methodname$\",\n", "methodname", method->name()); |
| 250 | out->Print("$requestmarshaller$,\n", "requestmarshaller", |
| 251 | GetMarshallerFieldName(method->input_type())); |
| 252 | out->Print("$responsemarshaller$);\n", "responsemarshaller", |
| 253 | GetMarshallerFieldName(method->output_type())); |
| 254 | out->Print("\n"); |
| 255 | out->Outdent(); |
| 256 | out->Outdent(); |
| 257 | } |
| 258 | |
| 259 | void GenerateClientInterface(Printer* out, const ServiceDescriptor *service) { |
Jan Tattermusch | b533281 | 2015-07-14 19:29:35 -0700 | [diff] [blame] | 260 | out->Print("// client interface\n"); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 261 | out->Print("public interface $name$\n", "name", |
| 262 | GetClientInterfaceName(service)); |
| 263 | out->Print("{\n"); |
| 264 | out->Indent(); |
| 265 | for (int i = 0; i < service->method_count(); i++) { |
| 266 | const MethodDescriptor *method = service->method(i); |
| 267 | MethodType method_type = GetMethodType(method); |
| 268 | |
| 269 | if (method_type == METHODTYPE_NO_STREAMING) { |
| 270 | // unary calls have an extra synchronous stub method |
| 271 | out->Print( |
Jan Tattermusch | fd953a5 | 2015-07-14 21:41:29 -0700 | [diff] [blame] | 272 | "$response$ $methodname$($request$ request, Metadata headers = null, CancellationToken cancellationToken = default(CancellationToken));\n", |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 273 | "methodname", method->name(), "request", |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 274 | GetClassName(method->input_type()), "response", |
| 275 | GetClassName(method->output_type())); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | std::string method_name = method->name(); |
| 279 | if (method_type == METHODTYPE_NO_STREAMING) { |
| 280 | method_name += "Async"; // prevent name clash with synchronous method. |
| 281 | } |
| 282 | out->Print( |
Jan Tattermusch | fd953a5 | 2015-07-14 21:41:29 -0700 | [diff] [blame] | 283 | "$returntype$ $methodname$($request_maybe$Metadata headers = null, CancellationToken cancellationToken = default(CancellationToken));\n", |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 284 | "methodname", method_name, "request_maybe", |
| 285 | GetMethodRequestParamMaybe(method), "returntype", |
| 286 | GetMethodReturnTypeClient(method)); |
| 287 | } |
| 288 | out->Outdent(); |
| 289 | out->Print("}\n"); |
| 290 | out->Print("\n"); |
| 291 | } |
| 292 | |
| 293 | void GenerateServerInterface(Printer* out, const ServiceDescriptor *service) { |
| 294 | out->Print("// server-side interface\n"); |
| 295 | out->Print("public interface $name$\n", "name", |
| 296 | GetServerInterfaceName(service)); |
| 297 | out->Print("{\n"); |
| 298 | out->Indent(); |
| 299 | for (int i = 0; i < service->method_count(); i++) { |
| 300 | const MethodDescriptor *method = service->method(i); |
| 301 | out->Print("$returntype$ $methodname$(ServerCallContext context, $request$$response_stream_maybe$);\n", |
| 302 | "methodname", method->name(), "returntype", |
| 303 | GetMethodReturnTypeServer(method), "request", |
| 304 | GetMethodRequestParamServer(method), "response_stream_maybe", |
| 305 | GetMethodResponseStreamMaybe(method)); |
| 306 | } |
| 307 | out->Outdent(); |
| 308 | out->Print("}\n"); |
| 309 | out->Print("\n"); |
| 310 | } |
| 311 | |
| 312 | void GenerateClientStub(Printer* out, const ServiceDescriptor *service) { |
| 313 | out->Print("// client stub\n"); |
| 314 | out->Print( |
Jan Tattermusch | b533281 | 2015-07-14 19:29:35 -0700 | [diff] [blame] | 315 | "public class $name$ : ClientBase, $interface$\n", |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 316 | "name", GetClientClassName(service), "interface", |
| 317 | GetClientInterfaceName(service)); |
| 318 | out->Print("{\n"); |
| 319 | out->Indent(); |
| 320 | |
| 321 | // constructors |
| 322 | out->Print( |
Jan Tattermusch | b533281 | 2015-07-14 19:29:35 -0700 | [diff] [blame] | 323 | "public $name$(Channel channel) : base(channel)\n", |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 324 | "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 Tattermusch | fd953a5 | 2015-07-14 21:41:29 -0700 | [diff] [blame] | 335 | "public $response$ $methodname$($request$ request, Metadata headers = null, CancellationToken cancellationToken = default(CancellationToken))\n", |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 336 | "methodname", method->name(), "request", |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 337 | GetClassName(method->input_type()), "response", |
| 338 | GetClassName(method->output_type())); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 339 | out->Print("{\n"); |
| 340 | out->Indent(); |
Jan Tattermusch | fd953a5 | 2015-07-14 21:41:29 -0700 | [diff] [blame] | 341 | out->Print("var call = CreateCall($servicenamefield$, $methodfield$, headers);\n", |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 342 | "servicenamefield", GetServiceNameFieldName(), "methodfield", |
| 343 | GetMethodFieldName(method)); |
Jan Tattermusch | fd953a5 | 2015-07-14 21:41:29 -0700 | [diff] [blame] | 344 | out->Print("return Calls.BlockingUnaryCall(call, request, cancellationToken);\n"); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 345 | out->Outdent(); |
| 346 | out->Print("}\n"); |
| 347 | } |
| 348 | |
| 349 | std::string method_name = method->name(); |
| 350 | if (method_type == METHODTYPE_NO_STREAMING) { |
| 351 | method_name += "Async"; // prevent name clash with synchronous method. |
| 352 | } |
| 353 | out->Print( |
Jan Tattermusch | fd953a5 | 2015-07-14 21:41:29 -0700 | [diff] [blame] | 354 | "public $returntype$ $methodname$($request_maybe$Metadata headers = null, CancellationToken cancellationToken = default(CancellationToken))\n", |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 355 | "methodname", method_name, "request_maybe", |
| 356 | GetMethodRequestParamMaybe(method), "returntype", |
| 357 | GetMethodReturnTypeClient(method)); |
| 358 | out->Print("{\n"); |
| 359 | out->Indent(); |
Jan Tattermusch | fd953a5 | 2015-07-14 21:41:29 -0700 | [diff] [blame] | 360 | out->Print("var call = CreateCall($servicenamefield$, $methodfield$, headers);\n", |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 361 | "servicenamefield", GetServiceNameFieldName(), "methodfield", |
| 362 | GetMethodFieldName(method)); |
| 363 | switch (GetMethodType(method)) { |
| 364 | case METHODTYPE_NO_STREAMING: |
Jan Tattermusch | fd953a5 | 2015-07-14 21:41:29 -0700 | [diff] [blame] | 365 | out->Print("return Calls.AsyncUnaryCall(call, request, cancellationToken);\n"); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 366 | break; |
| 367 | case METHODTYPE_CLIENT_STREAMING: |
Jan Tattermusch | fd953a5 | 2015-07-14 21:41:29 -0700 | [diff] [blame] | 368 | out->Print("return Calls.AsyncClientStreamingCall(call, cancellationToken);\n"); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 369 | break; |
| 370 | case METHODTYPE_SERVER_STREAMING: |
| 371 | out->Print( |
Jan Tattermusch | fd953a5 | 2015-07-14 21:41:29 -0700 | [diff] [blame] | 372 | "return Calls.AsyncServerStreamingCall(call, request, cancellationToken);\n"); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 373 | break; |
| 374 | case METHODTYPE_BIDI_STREAMING: |
Jan Tattermusch | fd953a5 | 2015-07-14 21:41:29 -0700 | [diff] [blame] | 375 | out->Print("return Calls.AsyncDuplexStreamingCall(call, cancellationToken);\n"); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 376 | break; |
| 377 | default: |
| 378 | GOOGLE_LOG(FATAL)<< "Can't get here."; |
| 379 | } |
| 380 | out->Outdent(); |
| 381 | out->Print("}\n"); |
| 382 | } |
| 383 | out->Outdent(); |
| 384 | out->Print("}\n"); |
| 385 | out->Print("\n"); |
| 386 | } |
| 387 | |
| 388 | void GenerateBindServiceMethod(Printer* out, const ServiceDescriptor *service) { |
| 389 | out->Print( |
| 390 | "// creates service definition that can be registered with a server\n"); |
| 391 | out->Print( |
| 392 | "public static ServerServiceDefinition BindService($interface$ serviceImpl)\n", |
| 393 | "interface", GetServerInterfaceName(service)); |
| 394 | out->Print("{\n"); |
| 395 | out->Indent(); |
| 396 | |
| 397 | out->Print( |
| 398 | "return ServerServiceDefinition.CreateBuilder($servicenamefield$)\n", |
| 399 | "servicenamefield", GetServiceNameFieldName()); |
| 400 | out->Indent(); |
| 401 | out->Indent(); |
| 402 | for (int i = 0; i < service->method_count(); i++) { |
| 403 | const MethodDescriptor *method = service->method(i); |
| 404 | out->Print(".AddMethod($methodfield$, serviceImpl.$methodname$)", |
| 405 | "methodfield", GetMethodFieldName(method), "methodname", |
| 406 | method->name()); |
| 407 | if (i == service->method_count() - 1) { |
| 408 | out->Print(".Build();"); |
| 409 | } |
| 410 | out->Print("\n"); |
| 411 | } |
| 412 | out->Outdent(); |
| 413 | out->Outdent(); |
| 414 | |
| 415 | out->Outdent(); |
| 416 | out->Print("}\n"); |
| 417 | out->Print("\n"); |
| 418 | } |
| 419 | |
| 420 | void GenerateNewStubMethods(Printer* out, const ServiceDescriptor *service) { |
Jan Tattermusch | b533281 | 2015-07-14 19:29:35 -0700 | [diff] [blame] | 421 | out->Print("// creates a new client\n"); |
| 422 | out->Print("public static $classname$ NewClient(Channel channel)\n", |
| 423 | "classname", GetClientClassName(service)); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 424 | out->Print("{\n"); |
| 425 | out->Indent(); |
| 426 | out->Print("return new $classname$(channel);\n", "classname", |
| 427 | GetClientClassName(service)); |
| 428 | out->Outdent(); |
| 429 | out->Print("}\n"); |
| 430 | out->Print("\n"); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | void GenerateService(Printer* out, const ServiceDescriptor *service) { |
| 434 | out->Print("public static class $classname$\n", "classname", |
| 435 | GetServiceClassName(service)); |
| 436 | out->Print("{\n"); |
| 437 | out->Indent(); |
| 438 | out->Print("static readonly string $servicenamefield$ = \"$servicename$\";\n", |
| 439 | "servicenamefield", GetServiceNameFieldName(), "servicename", |
| 440 | service->full_name()); |
| 441 | out->Print("\n"); |
| 442 | |
| 443 | GenerateMarshallerFields(out, service); |
| 444 | for (int i = 0; i < service->method_count(); i++) { |
| 445 | GenerateStaticMethodField(out, service->method(i)); |
| 446 | } |
| 447 | GenerateClientInterface(out, service); |
| 448 | GenerateServerInterface(out, service); |
| 449 | GenerateClientStub(out, service); |
| 450 | GenerateBindServiceMethod(out, service); |
| 451 | GenerateNewStubMethods(out, service); |
| 452 | |
| 453 | out->Outdent(); |
| 454 | out->Print("}\n"); |
| 455 | } |
| 456 | |
| 457 | } // anonymous namespace |
| 458 | |
| 459 | grpc::string GetServices(const FileDescriptor *file) { |
| 460 | grpc::string output; |
Jan Tattermusch | 5dcebd9 | 2015-05-27 15:30:59 -0700 | [diff] [blame] | 461 | { |
| 462 | // Scope the output stream so it closes and finalizes output to the string. |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 463 | |
Jan Tattermusch | 5dcebd9 | 2015-05-27 15:30:59 -0700 | [diff] [blame] | 464 | StringOutputStream output_stream(&output); |
| 465 | Printer out(&output_stream, '$'); |
| 466 | |
| 467 | // Don't write out any output if there no services, to avoid empty service |
| 468 | // files being generated for proto files that don't declare any. |
| 469 | if (file->service_count() == 0) { |
| 470 | return output; |
| 471 | } |
| 472 | |
| 473 | // Write out a file header. |
| 474 | out.Print("// Generated by the protocol buffer compiler. DO NOT EDIT!\n"); |
| 475 | out.Print("// source: $filename$\n", "filename", file->name()); |
| 476 | out.Print("#region Designer generated code\n"); |
| 477 | out.Print("\n"); |
| 478 | out.Print("using System;\n"); |
| 479 | out.Print("using System.Threading;\n"); |
| 480 | out.Print("using System.Threading.Tasks;\n"); |
| 481 | out.Print("using Grpc.Core;\n"); |
| 482 | // TODO(jtattermusch): add using for protobuf message classes |
| 483 | out.Print("\n"); |
| 484 | |
| 485 | out.Print("namespace $namespace$ {\n", "namespace", GetFileNamespace(file)); |
| 486 | out.Indent(); |
| 487 | for (int i = 0; i < file->service_count(); i++) { |
| 488 | GenerateService(&out, file->service(i)); |
| 489 | } |
| 490 | out.Outdent(); |
| 491 | out.Print("}\n"); |
| 492 | out.Print("#endregion\n"); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 493 | } |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 494 | return output; |
| 495 | } |
| 496 | |
| 497 | } // namespace grpc_csharp_generator |