Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 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-g | c3ee1d5 | 2015-08-28 11:33:52 -0700 | [diff] [blame] | 36 | #include <sstream> |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 37 | #include <vector> |
| 38 | |
Jan Tattermusch | 741e64c | 2015-08-03 08:08:56 -0700 | [diff] [blame] | 39 | #include "src/compiler/csharp_generator.h" |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 40 | #include "src/compiler/config.h" |
Jan Tattermusch | b5897bf | 2015-05-07 15:45:37 -0700 | [diff] [blame] | 41 | #include "src/compiler/csharp_generator_helpers.h" |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 42 | #include "src/compiler/csharp_generator.h" |
| 43 | |
Jan Tattermusch | 741e64c | 2015-08-03 08:08:56 -0700 | [diff] [blame] | 44 | |
| 45 | using google::protobuf::compiler::csharp::GetFileNamespace; |
| 46 | using google::protobuf::compiler::csharp::GetClassName; |
Jan Tattermusch | da717f4 | 2016-01-20 13:12:35 -0800 | [diff] [blame] | 47 | using google::protobuf::compiler::csharp::GetReflectionClassName; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 48 | using grpc::protobuf::FileDescriptor; |
| 49 | using grpc::protobuf::Descriptor; |
| 50 | using grpc::protobuf::ServiceDescriptor; |
| 51 | using grpc::protobuf::MethodDescriptor; |
| 52 | using grpc::protobuf::io::Printer; |
| 53 | using grpc::protobuf::io::StringOutputStream; |
Jan Tattermusch | b5897bf | 2015-05-07 15:45:37 -0700 | [diff] [blame] | 54 | using grpc_generator::MethodType; |
| 55 | using grpc_generator::GetMethodType; |
| 56 | using grpc_generator::METHODTYPE_NO_STREAMING; |
| 57 | using grpc_generator::METHODTYPE_CLIENT_STREAMING; |
| 58 | using grpc_generator::METHODTYPE_SERVER_STREAMING; |
| 59 | using grpc_generator::METHODTYPE_BIDI_STREAMING; |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 60 | using grpc_generator::StringReplace; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 61 | using std::map; |
| 62 | using std::vector; |
| 63 | |
Jan Tattermusch | 741e64c | 2015-08-03 08:08:56 -0700 | [diff] [blame] | 64 | |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 65 | namespace grpc_csharp_generator { |
| 66 | namespace { |
| 67 | |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 68 | std::string GetServiceClassName(const ServiceDescriptor* service) { |
| 69 | return service->name(); |
| 70 | } |
| 71 | |
| 72 | std::string GetClientInterfaceName(const ServiceDescriptor* service) { |
| 73 | return "I" + service->name() + "Client"; |
| 74 | } |
| 75 | |
| 76 | std::string GetClientClassName(const ServiceDescriptor* service) { |
| 77 | return service->name() + "Client"; |
| 78 | } |
| 79 | |
| 80 | std::string GetServerInterfaceName(const ServiceDescriptor* service) { |
| 81 | return "I" + service->name(); |
| 82 | } |
| 83 | |
Jan Tattermusch | 10a002d | 2016-03-14 15:22:19 -0700 | [diff] [blame] | 84 | std::string GetServerClassName(const ServiceDescriptor* service) { |
| 85 | return service->name() + "Base"; |
| 86 | } |
| 87 | |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 88 | std::string GetCSharpMethodType(MethodType method_type) { |
| 89 | switch (method_type) { |
| 90 | case METHODTYPE_NO_STREAMING: |
| 91 | return "MethodType.Unary"; |
| 92 | case METHODTYPE_CLIENT_STREAMING: |
| 93 | return "MethodType.ClientStreaming"; |
| 94 | case METHODTYPE_SERVER_STREAMING: |
| 95 | return "MethodType.ServerStreaming"; |
| 96 | case METHODTYPE_BIDI_STREAMING: |
| 97 | return "MethodType.DuplexStreaming"; |
| 98 | } |
| 99 | GOOGLE_LOG(FATAL)<< "Can't get here."; |
| 100 | return ""; |
| 101 | } |
| 102 | |
| 103 | std::string GetServiceNameFieldName() { |
| 104 | return "__ServiceName"; |
| 105 | } |
| 106 | |
| 107 | std::string GetMarshallerFieldName(const Descriptor *message) { |
| 108 | return "__Marshaller_" + message->name(); |
| 109 | } |
| 110 | |
| 111 | std::string GetMethodFieldName(const MethodDescriptor *method) { |
| 112 | return "__Method_" + method->name(); |
| 113 | } |
| 114 | |
Jan Tattermusch | 8cd4ae9 | 2016-03-21 18:46:32 -0700 | [diff] [blame] | 115 | std::string GetMethodRequestParamMaybe(const MethodDescriptor *method, |
| 116 | bool invocation_param=false) { |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 117 | if (method->client_streaming()) { |
| 118 | return ""; |
| 119 | } |
Jan Tattermusch | 8cd4ae9 | 2016-03-21 18:46:32 -0700 | [diff] [blame] | 120 | if (invocation_param) { |
| 121 | return "request, "; |
| 122 | } |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 123 | return GetClassName(method->input_type()) + " request, "; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | std::string GetMethodReturnTypeClient(const MethodDescriptor *method) { |
| 127 | switch (GetMethodType(method)) { |
| 128 | case METHODTYPE_NO_STREAMING: |
Jan Tattermusch | 5269d16 | 2015-07-21 11:56:42 -0700 | [diff] [blame] | 129 | return "AsyncUnaryCall<" + GetClassName(method->output_type()) + ">"; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 130 | case METHODTYPE_CLIENT_STREAMING: |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 131 | return "AsyncClientStreamingCall<" + GetClassName(method->input_type()) |
| 132 | + ", " + GetClassName(method->output_type()) + ">"; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 133 | case METHODTYPE_SERVER_STREAMING: |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 134 | return "AsyncServerStreamingCall<" + GetClassName(method->output_type()) |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 135 | + ">"; |
| 136 | case METHODTYPE_BIDI_STREAMING: |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 137 | return "AsyncDuplexStreamingCall<" + GetClassName(method->input_type()) |
| 138 | + ", " + GetClassName(method->output_type()) + ">"; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 139 | } |
| 140 | GOOGLE_LOG(FATAL)<< "Can't get here."; |
| 141 | return ""; |
| 142 | } |
| 143 | |
| 144 | std::string GetMethodRequestParamServer(const MethodDescriptor *method) { |
| 145 | switch (GetMethodType(method)) { |
| 146 | case METHODTYPE_NO_STREAMING: |
| 147 | case METHODTYPE_SERVER_STREAMING: |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 148 | return GetClassName(method->input_type()) + " request"; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 149 | case METHODTYPE_CLIENT_STREAMING: |
| 150 | case METHODTYPE_BIDI_STREAMING: |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 151 | return "IAsyncStreamReader<" + GetClassName(method->input_type()) |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 152 | + "> requestStream"; |
| 153 | } |
| 154 | GOOGLE_LOG(FATAL)<< "Can't get here."; |
| 155 | return ""; |
| 156 | } |
| 157 | |
| 158 | std::string GetMethodReturnTypeServer(const MethodDescriptor *method) { |
| 159 | switch (GetMethodType(method)) { |
| 160 | case METHODTYPE_NO_STREAMING: |
| 161 | case METHODTYPE_CLIENT_STREAMING: |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 162 | return "Task<" + GetClassName(method->output_type()) + ">"; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 163 | case METHODTYPE_SERVER_STREAMING: |
| 164 | case METHODTYPE_BIDI_STREAMING: |
| 165 | return "Task"; |
| 166 | } |
| 167 | GOOGLE_LOG(FATAL)<< "Can't get here."; |
| 168 | return ""; |
| 169 | } |
| 170 | |
| 171 | std::string GetMethodResponseStreamMaybe(const MethodDescriptor *method) { |
| 172 | switch (GetMethodType(method)) { |
| 173 | case METHODTYPE_NO_STREAMING: |
| 174 | case METHODTYPE_CLIENT_STREAMING: |
| 175 | return ""; |
| 176 | case METHODTYPE_SERVER_STREAMING: |
| 177 | case METHODTYPE_BIDI_STREAMING: |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 178 | return ", IServerStreamWriter<" + GetClassName(method->output_type()) |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 179 | + "> responseStream"; |
| 180 | } |
| 181 | GOOGLE_LOG(FATAL)<< "Can't get here."; |
| 182 | return ""; |
| 183 | } |
| 184 | |
| 185 | // Gets vector of all messages used as input or output types. |
| 186 | std::vector<const Descriptor*> GetUsedMessages( |
| 187 | const ServiceDescriptor *service) { |
| 188 | std::set<const Descriptor*> descriptor_set; |
| 189 | std::vector<const Descriptor*> result; // vector is to maintain stable ordering |
| 190 | for (int i = 0; i < service->method_count(); i++) { |
| 191 | const MethodDescriptor *method = service->method(i); |
| 192 | if (descriptor_set.find(method->input_type()) == descriptor_set.end()) { |
| 193 | descriptor_set.insert(method->input_type()); |
| 194 | result.push_back(method->input_type()); |
| 195 | } |
| 196 | if (descriptor_set.find(method->output_type()) == descriptor_set.end()) { |
| 197 | descriptor_set.insert(method->output_type()); |
| 198 | result.push_back(method->output_type()); |
| 199 | } |
| 200 | } |
| 201 | return result; |
| 202 | } |
| 203 | |
| 204 | void GenerateMarshallerFields(Printer* out, const ServiceDescriptor *service) { |
| 205 | std::vector<const Descriptor*> used_messages = GetUsedMessages(service); |
| 206 | for (size_t i = 0; i < used_messages.size(); i++) { |
| 207 | const Descriptor *message = used_messages[i]; |
| 208 | out->Print( |
Jan Tattermusch | ad75dd1 | 2015-08-03 09:43:07 -0700 | [diff] [blame] | 209 | "static readonly Marshaller<$type$> $fieldname$ = Marshallers.Create((arg) => global::Google.Protobuf.MessageExtensions.ToByteArray(arg), $type$.Parser.ParseFrom);\n", |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 210 | "fieldname", GetMarshallerFieldName(message), "type", |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 211 | GetClassName(message)); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 212 | } |
| 213 | out->Print("\n"); |
| 214 | } |
| 215 | |
| 216 | void GenerateStaticMethodField(Printer* out, const MethodDescriptor *method) { |
| 217 | out->Print( |
| 218 | "static readonly Method<$request$, $response$> $fieldname$ = new Method<$request$, $response$>(\n", |
| 219 | "fieldname", GetMethodFieldName(method), "request", |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 220 | GetClassName(method->input_type()), "response", |
| 221 | GetClassName(method->output_type())); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 222 | out->Indent(); |
| 223 | out->Indent(); |
| 224 | out->Print("$methodtype$,\n", "methodtype", |
| 225 | GetCSharpMethodType(GetMethodType(method))); |
Jan Tattermusch | a9ddd02 | 2015-08-05 03:04:58 -0700 | [diff] [blame] | 226 | out->Print("$servicenamefield$,\n", "servicenamefield", |
| 227 | GetServiceNameFieldName()); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 228 | out->Print("\"$methodname$\",\n", "methodname", method->name()); |
| 229 | out->Print("$requestmarshaller$,\n", "requestmarshaller", |
| 230 | GetMarshallerFieldName(method->input_type())); |
| 231 | out->Print("$responsemarshaller$);\n", "responsemarshaller", |
| 232 | GetMarshallerFieldName(method->output_type())); |
| 233 | out->Print("\n"); |
| 234 | out->Outdent(); |
| 235 | out->Outdent(); |
| 236 | } |
| 237 | |
Jan Tattermusch | e6af5d1 | 2015-08-03 10:57:43 -0700 | [diff] [blame] | 238 | void GenerateServiceDescriptorProperty(Printer* out, const ServiceDescriptor *service) { |
yang-g | c3ee1d5 | 2015-08-28 11:33:52 -0700 | [diff] [blame] | 239 | std::ostringstream index; |
| 240 | index << service->index(); |
Jan Tattermusch | e6af5d1 | 2015-08-03 10:57:43 -0700 | [diff] [blame] | 241 | out->Print("// service descriptor\n"); |
| 242 | out->Print("public static global::Google.Protobuf.Reflection.ServiceDescriptor Descriptor\n"); |
| 243 | out->Print("{\n"); |
| 244 | out->Print(" get { return $umbrella$.Descriptor.Services[$index$]; }\n", |
Jan Tattermusch | da717f4 | 2016-01-20 13:12:35 -0800 | [diff] [blame] | 245 | "umbrella", GetReflectionClassName(service->file()), "index", |
yang-g | c3ee1d5 | 2015-08-28 11:33:52 -0700 | [diff] [blame] | 246 | index.str()); |
Jan Tattermusch | e6af5d1 | 2015-08-03 10:57:43 -0700 | [diff] [blame] | 247 | out->Print("}\n"); |
| 248 | out->Print("\n"); |
| 249 | } |
| 250 | |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 251 | void GenerateClientInterface(Printer* out, const ServiceDescriptor *service) { |
Jan Tattermusch | b533281 | 2015-07-14 19:29:35 -0700 | [diff] [blame] | 252 | out->Print("// client interface\n"); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 253 | out->Print("public interface $name$\n", "name", |
| 254 | GetClientInterfaceName(service)); |
| 255 | out->Print("{\n"); |
| 256 | out->Indent(); |
| 257 | for (int i = 0; i < service->method_count(); i++) { |
| 258 | const MethodDescriptor *method = service->method(i); |
| 259 | MethodType method_type = GetMethodType(method); |
| 260 | |
| 261 | if (method_type == METHODTYPE_NO_STREAMING) { |
| 262 | // unary calls have an extra synchronous stub method |
| 263 | out->Print( |
Jan Tattermusch | 7452956 | 2015-07-23 14:04:51 -0700 | [diff] [blame] | 264 | "$response$ $methodname$($request$ request, Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken));\n", |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 265 | "methodname", method->name(), "request", |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 266 | GetClassName(method->input_type()), "response", |
| 267 | GetClassName(method->output_type())); |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 268 | |
Jan Tattermusch | e5e57ad | 2015-08-05 14:54:08 -0700 | [diff] [blame] | 269 | // overload taking CallOptions as a param |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 270 | out->Print( |
Jan Tattermusch | e5e57ad | 2015-08-05 14:54:08 -0700 | [diff] [blame] | 271 | "$response$ $methodname$($request$ request, CallOptions options);\n", |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 272 | "methodname", method->name(), "request", |
| 273 | GetClassName(method->input_type()), "response", |
| 274 | GetClassName(method->output_type())); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | std::string method_name = method->name(); |
| 278 | if (method_type == METHODTYPE_NO_STREAMING) { |
| 279 | method_name += "Async"; // prevent name clash with synchronous method. |
| 280 | } |
| 281 | out->Print( |
Jan Tattermusch | 7452956 | 2015-07-23 14:04:51 -0700 | [diff] [blame] | 282 | "$returntype$ $methodname$($request_maybe$Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken));\n", |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 283 | "methodname", method_name, "request_maybe", |
| 284 | GetMethodRequestParamMaybe(method), "returntype", |
| 285 | GetMethodReturnTypeClient(method)); |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 286 | |
Jan Tattermusch | e5e57ad | 2015-08-05 14:54:08 -0700 | [diff] [blame] | 287 | // overload taking CallOptions as a param |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 288 | out->Print( |
Jan Tattermusch | e5e57ad | 2015-08-05 14:54:08 -0700 | [diff] [blame] | 289 | "$returntype$ $methodname$($request_maybe$CallOptions options);\n", |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 290 | "methodname", method_name, "request_maybe", |
| 291 | GetMethodRequestParamMaybe(method), "returntype", |
| 292 | GetMethodReturnTypeClient(method)); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 293 | } |
| 294 | out->Outdent(); |
| 295 | out->Print("}\n"); |
| 296 | out->Print("\n"); |
| 297 | } |
| 298 | |
| 299 | void GenerateServerInterface(Printer* out, const ServiceDescriptor *service) { |
| 300 | out->Print("// server-side interface\n"); |
Jan Tattermusch | 10a002d | 2016-03-14 15:22:19 -0700 | [diff] [blame] | 301 | out->Print("[System.Obsolete(\"Service implementations should inherit" |
| 302 | " from the generated abstract base class instead.\")]\n"); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 303 | out->Print("public interface $name$\n", "name", |
| 304 | GetServerInterfaceName(service)); |
| 305 | out->Print("{\n"); |
| 306 | out->Indent(); |
| 307 | for (int i = 0; i < service->method_count(); i++) { |
| 308 | const MethodDescriptor *method = service->method(i); |
Craig Tiller | b256faa | 2015-07-23 11:28:16 -0700 | [diff] [blame] | 309 | out->Print( |
| 310 | "$returntype$ $methodname$($request$$response_stream_maybe$, " |
| 311 | "ServerCallContext context);\n", |
| 312 | "methodname", method->name(), "returntype", |
| 313 | GetMethodReturnTypeServer(method), "request", |
| 314 | GetMethodRequestParamServer(method), "response_stream_maybe", |
| 315 | GetMethodResponseStreamMaybe(method)); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 316 | } |
| 317 | out->Outdent(); |
| 318 | out->Print("}\n"); |
| 319 | out->Print("\n"); |
| 320 | } |
| 321 | |
Jan Tattermusch | 10a002d | 2016-03-14 15:22:19 -0700 | [diff] [blame] | 322 | void GenerateServerClass(Printer* out, const ServiceDescriptor *service) { |
| 323 | out->Print("// server-side abstract class\n"); |
| 324 | out->Print("public abstract class $name$\n", "name", |
| 325 | GetServerClassName(service)); |
| 326 | out->Print("{\n"); |
| 327 | out->Indent(); |
| 328 | for (int i = 0; i < service->method_count(); i++) { |
| 329 | const MethodDescriptor *method = service->method(i); |
| 330 | out->Print( |
| 331 | "public virtual $returntype$ $methodname$($request$$response_stream_maybe$, " |
| 332 | "ServerCallContext context)\n", |
| 333 | "methodname", method->name(), "returntype", |
| 334 | GetMethodReturnTypeServer(method), "request", |
| 335 | GetMethodRequestParamServer(method), "response_stream_maybe", |
| 336 | GetMethodResponseStreamMaybe(method)); |
| 337 | out->Print("{\n"); |
| 338 | out->Indent(); |
| 339 | out->Print("throw new RpcException(" |
| 340 | "new Status(StatusCode.Unimplemented, \"\"));\n"); |
| 341 | out->Outdent(); |
| 342 | out->Print("}\n\n"); |
| 343 | } |
| 344 | out->Outdent(); |
| 345 | out->Print("}\n"); |
| 346 | out->Print("\n"); |
| 347 | } |
| 348 | |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 349 | void GenerateClientStub(Printer* out, const ServiceDescriptor *service) { |
| 350 | out->Print("// client stub\n"); |
| 351 | out->Print( |
Jan Tattermusch | 055c2dd | 2016-03-22 14:43:56 -0700 | [diff] [blame^] | 352 | "public class $name$ : ClientBase<$name$>\n", |
| 353 | "name", GetClientClassName(service)); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 354 | out->Print("{\n"); |
| 355 | out->Indent(); |
| 356 | |
| 357 | // constructors |
Jan Tattermusch | 055c2dd | 2016-03-22 14:43:56 -0700 | [diff] [blame^] | 358 | out->Print("public $name$(Channel channel) : base(channel)\n", |
| 359 | "name", GetClientClassName(service)); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 360 | out->Print("{\n"); |
| 361 | out->Print("}\n"); |
Jan Tattermusch | 055c2dd | 2016-03-22 14:43:56 -0700 | [diff] [blame^] | 362 | out->Print("public $name$(CallInvoker callInvoker) : base(callInvoker)\n", |
| 363 | "name", GetClientClassName(service)); |
| 364 | out->Print("{\n"); |
| 365 | out->Print("}\n\n"); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 366 | |
| 367 | for (int i = 0; i < service->method_count(); i++) { |
| 368 | const MethodDescriptor *method = service->method(i); |
| 369 | MethodType method_type = GetMethodType(method); |
| 370 | |
| 371 | if (method_type == METHODTYPE_NO_STREAMING) { |
| 372 | // unary calls have an extra synchronous stub method |
Jan Tattermusch | 055c2dd | 2016-03-22 14:43:56 -0700 | [diff] [blame^] | 373 | out->Print("public virtual $response$ $methodname$($request$ request, Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken))\n", |
| 374 | "methodname", method->name(), "request", |
| 375 | GetClassName(method->input_type()), "response", |
| 376 | GetClassName(method->output_type())); |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 377 | out->Print("{\n"); |
| 378 | out->Indent(); |
Jan Tattermusch | 055c2dd | 2016-03-22 14:43:56 -0700 | [diff] [blame^] | 379 | out->Print("return $methodname$(request, new CallOptions(headers, deadline, cancellationToken));\n", |
| 380 | "methodname", method->name()); |
| 381 | out->Outdent(); |
| 382 | out->Print("}\n"); |
| 383 | |
| 384 | // overload taking CallOptions as a param |
| 385 | out->Print("public virtual $response$ $methodname$($request$ request, CallOptions options)\n", |
| 386 | "methodname", method->name(), "request", |
| 387 | GetClassName(method->input_type()), "response", |
| 388 | GetClassName(method->output_type())); |
| 389 | out->Print("{\n"); |
| 390 | out->Indent(); |
| 391 | out->Print("return CallInvoker.BlockingUnaryCall($methodfield$, options, request);\n", |
Jan Tattermusch | 641cb1b | 2015-08-05 03:51:46 -0700 | [diff] [blame] | 392 | "methodfield", GetMethodFieldName(method)); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 393 | out->Outdent(); |
| 394 | out->Print("}\n"); |
| 395 | } |
| 396 | |
| 397 | std::string method_name = method->name(); |
| 398 | if (method_type == METHODTYPE_NO_STREAMING) { |
| 399 | method_name += "Async"; // prevent name clash with synchronous method. |
| 400 | } |
| 401 | out->Print( |
Jan Tattermusch | 055c2dd | 2016-03-22 14:43:56 -0700 | [diff] [blame^] | 402 | "public virtual $returntype$ $methodname$($request_maybe$Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken))\n", |
| 403 | "methodname", method_name, "request_maybe", |
| 404 | GetMethodRequestParamMaybe(method), "returntype", |
| 405 | GetMethodReturnTypeClient(method)); |
| 406 | out->Print("{\n"); |
| 407 | out->Indent(); |
| 408 | |
| 409 | out->Print("return $methodname$($request_maybe$new CallOptions(headers, deadline, cancellationToken));\n", |
| 410 | "methodname", method_name, |
| 411 | "request_maybe", GetMethodRequestParamMaybe(method, true)); |
| 412 | out->Outdent(); |
| 413 | out->Print("}\n"); |
| 414 | |
| 415 | // overload taking CallOptions as a param |
| 416 | out->Print( |
| 417 | "public virtual $returntype$ $methodname$($request_maybe$CallOptions options)\n", |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 418 | "methodname", method_name, "request_maybe", |
| 419 | GetMethodRequestParamMaybe(method), "returntype", |
| 420 | GetMethodReturnTypeClient(method)); |
| 421 | out->Print("{\n"); |
| 422 | out->Indent(); |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 423 | switch (GetMethodType(method)) { |
| 424 | case METHODTYPE_NO_STREAMING: |
Jan Tattermusch | 055c2dd | 2016-03-22 14:43:56 -0700 | [diff] [blame^] | 425 | out->Print("return CallInvoker.AsyncUnaryCall($methodfield$, options, request);\n", |
| 426 | "methodfield", GetMethodFieldName(method)); |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 427 | break; |
| 428 | case METHODTYPE_CLIENT_STREAMING: |
Jan Tattermusch | 055c2dd | 2016-03-22 14:43:56 -0700 | [diff] [blame^] | 429 | out->Print("return CallInvoker.AsyncClientStreamingCall($methodfield$, options);\n", |
| 430 | "methodfield", GetMethodFieldName(method)); |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 431 | break; |
| 432 | case METHODTYPE_SERVER_STREAMING: |
| 433 | out->Print( |
Jan Tattermusch | 055c2dd | 2016-03-22 14:43:56 -0700 | [diff] [blame^] | 434 | "return CallInvoker.AsyncServerStreamingCall($methodfield$, options, request);\n", |
| 435 | "methodfield", GetMethodFieldName(method)); |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 436 | break; |
| 437 | case METHODTYPE_BIDI_STREAMING: |
Jan Tattermusch | 055c2dd | 2016-03-22 14:43:56 -0700 | [diff] [blame^] | 438 | out->Print("return CallInvoker.AsyncDuplexStreamingCall($methodfield$, options);\n", |
| 439 | "methodfield", GetMethodFieldName(method)); |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 440 | break; |
| 441 | default: |
| 442 | GOOGLE_LOG(FATAL)<< "Can't get here."; |
| 443 | } |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 444 | out->Outdent(); |
| 445 | out->Print("}\n"); |
| 446 | } |
Jan Tattermusch | 055c2dd | 2016-03-22 14:43:56 -0700 | [diff] [blame^] | 447 | |
| 448 | // override NewInstance method |
| 449 | out->Print("protected override $name$ NewInstance(CallInvoker callInvoker)\n", |
| 450 | "name", GetClientClassName(service)); |
| 451 | out->Print("{\n"); |
| 452 | out->Indent(); |
| 453 | out->Print("return new $name$(callInvoker);\n", |
| 454 | "name", GetClientClassName(service)); |
| 455 | out->Outdent(); |
| 456 | out->Print("}\n"); |
| 457 | |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 458 | out->Outdent(); |
| 459 | out->Print("}\n"); |
| 460 | out->Print("\n"); |
| 461 | } |
| 462 | |
Jan Tattermusch | 10a002d | 2016-03-14 15:22:19 -0700 | [diff] [blame] | 463 | void GenerateBindServiceMethod(Printer* out, const ServiceDescriptor *service, |
| 464 | bool use_server_class) { |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 465 | out->Print( |
| 466 | "// creates service definition that can be registered with a server\n"); |
| 467 | out->Print( |
| 468 | "public static ServerServiceDefinition BindService($interface$ serviceImpl)\n", |
Jan Tattermusch | 10a002d | 2016-03-14 15:22:19 -0700 | [diff] [blame] | 469 | "interface", use_server_class ? GetServerClassName(service) : |
| 470 | GetServerInterfaceName(service)); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 471 | out->Print("{\n"); |
| 472 | out->Indent(); |
| 473 | |
| 474 | out->Print( |
| 475 | "return ServerServiceDefinition.CreateBuilder($servicenamefield$)\n", |
| 476 | "servicenamefield", GetServiceNameFieldName()); |
| 477 | out->Indent(); |
| 478 | out->Indent(); |
| 479 | for (int i = 0; i < service->method_count(); i++) { |
| 480 | const MethodDescriptor *method = service->method(i); |
| 481 | out->Print(".AddMethod($methodfield$, serviceImpl.$methodname$)", |
| 482 | "methodfield", GetMethodFieldName(method), "methodname", |
| 483 | method->name()); |
| 484 | if (i == service->method_count() - 1) { |
| 485 | out->Print(".Build();"); |
| 486 | } |
| 487 | out->Print("\n"); |
| 488 | } |
| 489 | out->Outdent(); |
| 490 | out->Outdent(); |
| 491 | |
| 492 | out->Outdent(); |
| 493 | out->Print("}\n"); |
| 494 | out->Print("\n"); |
| 495 | } |
| 496 | |
| 497 | void GenerateNewStubMethods(Printer* out, const ServiceDescriptor *service) { |
Jan Tattermusch | b533281 | 2015-07-14 19:29:35 -0700 | [diff] [blame] | 498 | out->Print("// creates a new client\n"); |
| 499 | out->Print("public static $classname$ NewClient(Channel channel)\n", |
| 500 | "classname", GetClientClassName(service)); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 501 | out->Print("{\n"); |
| 502 | out->Indent(); |
| 503 | out->Print("return new $classname$(channel);\n", "classname", |
| 504 | GetClientClassName(service)); |
| 505 | out->Outdent(); |
| 506 | out->Print("}\n"); |
| 507 | out->Print("\n"); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | void GenerateService(Printer* out, const ServiceDescriptor *service) { |
| 511 | out->Print("public static class $classname$\n", "classname", |
| 512 | GetServiceClassName(service)); |
| 513 | out->Print("{\n"); |
| 514 | out->Indent(); |
| 515 | out->Print("static readonly string $servicenamefield$ = \"$servicename$\";\n", |
| 516 | "servicenamefield", GetServiceNameFieldName(), "servicename", |
| 517 | service->full_name()); |
| 518 | out->Print("\n"); |
| 519 | |
| 520 | GenerateMarshallerFields(out, service); |
| 521 | for (int i = 0; i < service->method_count(); i++) { |
| 522 | GenerateStaticMethodField(out, service->method(i)); |
| 523 | } |
Jan Tattermusch | e6af5d1 | 2015-08-03 10:57:43 -0700 | [diff] [blame] | 524 | GenerateServiceDescriptorProperty(out, service); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 525 | GenerateClientInterface(out, service); |
Jan Tattermusch | 055c2dd | 2016-03-22 14:43:56 -0700 | [diff] [blame^] | 526 | //GenerateClientBaseClass(out, service); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 527 | GenerateServerInterface(out, service); |
Jan Tattermusch | 10a002d | 2016-03-14 15:22:19 -0700 | [diff] [blame] | 528 | GenerateServerClass(out, service); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 529 | GenerateClientStub(out, service); |
Jan Tattermusch | 10a002d | 2016-03-14 15:22:19 -0700 | [diff] [blame] | 530 | GenerateBindServiceMethod(out, service, false); |
| 531 | GenerateBindServiceMethod(out, service, true); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 532 | GenerateNewStubMethods(out, service); |
| 533 | |
| 534 | out->Outdent(); |
| 535 | out->Print("}\n"); |
| 536 | } |
| 537 | |
| 538 | } // anonymous namespace |
| 539 | |
| 540 | grpc::string GetServices(const FileDescriptor *file) { |
| 541 | grpc::string output; |
Jan Tattermusch | 5dcebd9 | 2015-05-27 15:30:59 -0700 | [diff] [blame] | 542 | { |
| 543 | // 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] | 544 | |
Jan Tattermusch | 5dcebd9 | 2015-05-27 15:30:59 -0700 | [diff] [blame] | 545 | 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()); |
| 557 | out.Print("#region Designer generated code\n"); |
| 558 | out.Print("\n"); |
| 559 | out.Print("using System;\n"); |
| 560 | out.Print("using System.Threading;\n"); |
| 561 | out.Print("using System.Threading.Tasks;\n"); |
| 562 | out.Print("using Grpc.Core;\n"); |
Jan Tattermusch | 5dcebd9 | 2015-05-27 15:30:59 -0700 | [diff] [blame] | 563 | out.Print("\n"); |
| 564 | |
| 565 | out.Print("namespace $namespace$ {\n", "namespace", GetFileNamespace(file)); |
| 566 | out.Indent(); |
| 567 | for (int i = 0; i < file->service_count(); i++) { |
| 568 | GenerateService(&out, file->service(i)); |
| 569 | } |
| 570 | out.Outdent(); |
| 571 | out.Print("}\n"); |
| 572 | out.Print("#endregion\n"); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 573 | } |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 574 | return output; |
| 575 | } |
| 576 | |
| 577 | } // namespace grpc_csharp_generator |