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 | |
| 115 | std::string GetMethodRequestParamMaybe(const MethodDescriptor *method) { |
| 116 | if (method->client_streaming()) { |
| 117 | return ""; |
| 118 | } |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 119 | return GetClassName(method->input_type()) + " request, "; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | std::string GetMethodReturnTypeClient(const MethodDescriptor *method) { |
| 123 | switch (GetMethodType(method)) { |
| 124 | case METHODTYPE_NO_STREAMING: |
Jan Tattermusch | 5269d16 | 2015-07-21 11:56:42 -0700 | [diff] [blame] | 125 | return "AsyncUnaryCall<" + GetClassName(method->output_type()) + ">"; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 126 | case METHODTYPE_CLIENT_STREAMING: |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 127 | return "AsyncClientStreamingCall<" + GetClassName(method->input_type()) |
| 128 | + ", " + GetClassName(method->output_type()) + ">"; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 129 | case METHODTYPE_SERVER_STREAMING: |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 130 | return "AsyncServerStreamingCall<" + GetClassName(method->output_type()) |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 131 | + ">"; |
| 132 | case METHODTYPE_BIDI_STREAMING: |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 133 | return "AsyncDuplexStreamingCall<" + GetClassName(method->input_type()) |
| 134 | + ", " + GetClassName(method->output_type()) + ">"; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 135 | } |
| 136 | GOOGLE_LOG(FATAL)<< "Can't get here."; |
| 137 | return ""; |
| 138 | } |
| 139 | |
| 140 | std::string GetMethodRequestParamServer(const MethodDescriptor *method) { |
| 141 | switch (GetMethodType(method)) { |
| 142 | case METHODTYPE_NO_STREAMING: |
| 143 | case METHODTYPE_SERVER_STREAMING: |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 144 | return GetClassName(method->input_type()) + " request"; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 145 | case METHODTYPE_CLIENT_STREAMING: |
| 146 | case METHODTYPE_BIDI_STREAMING: |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 147 | return "IAsyncStreamReader<" + GetClassName(method->input_type()) |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 148 | + "> requestStream"; |
| 149 | } |
| 150 | GOOGLE_LOG(FATAL)<< "Can't get here."; |
| 151 | return ""; |
| 152 | } |
| 153 | |
| 154 | std::string GetMethodReturnTypeServer(const MethodDescriptor *method) { |
| 155 | switch (GetMethodType(method)) { |
| 156 | case METHODTYPE_NO_STREAMING: |
| 157 | case METHODTYPE_CLIENT_STREAMING: |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 158 | return "Task<" + GetClassName(method->output_type()) + ">"; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 159 | case METHODTYPE_SERVER_STREAMING: |
| 160 | case METHODTYPE_BIDI_STREAMING: |
| 161 | return "Task"; |
| 162 | } |
| 163 | GOOGLE_LOG(FATAL)<< "Can't get here."; |
| 164 | return ""; |
| 165 | } |
| 166 | |
| 167 | std::string GetMethodResponseStreamMaybe(const MethodDescriptor *method) { |
| 168 | switch (GetMethodType(method)) { |
| 169 | case METHODTYPE_NO_STREAMING: |
| 170 | case METHODTYPE_CLIENT_STREAMING: |
| 171 | return ""; |
| 172 | case METHODTYPE_SERVER_STREAMING: |
| 173 | case METHODTYPE_BIDI_STREAMING: |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 174 | return ", IServerStreamWriter<" + GetClassName(method->output_type()) |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 175 | + "> responseStream"; |
| 176 | } |
| 177 | GOOGLE_LOG(FATAL)<< "Can't get here."; |
| 178 | return ""; |
| 179 | } |
| 180 | |
| 181 | // Gets vector of all messages used as input or output types. |
| 182 | std::vector<const Descriptor*> GetUsedMessages( |
| 183 | const ServiceDescriptor *service) { |
| 184 | std::set<const Descriptor*> descriptor_set; |
| 185 | std::vector<const Descriptor*> result; // vector is to maintain stable ordering |
| 186 | for (int i = 0; i < service->method_count(); i++) { |
| 187 | const MethodDescriptor *method = service->method(i); |
| 188 | if (descriptor_set.find(method->input_type()) == descriptor_set.end()) { |
| 189 | descriptor_set.insert(method->input_type()); |
| 190 | result.push_back(method->input_type()); |
| 191 | } |
| 192 | if (descriptor_set.find(method->output_type()) == descriptor_set.end()) { |
| 193 | descriptor_set.insert(method->output_type()); |
| 194 | result.push_back(method->output_type()); |
| 195 | } |
| 196 | } |
| 197 | return result; |
| 198 | } |
| 199 | |
| 200 | void GenerateMarshallerFields(Printer* out, const ServiceDescriptor *service) { |
| 201 | std::vector<const Descriptor*> used_messages = GetUsedMessages(service); |
| 202 | for (size_t i = 0; i < used_messages.size(); i++) { |
| 203 | const Descriptor *message = used_messages[i]; |
| 204 | out->Print( |
Jan Tattermusch | ad75dd1 | 2015-08-03 09:43:07 -0700 | [diff] [blame] | 205 | "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] | 206 | "fieldname", GetMarshallerFieldName(message), "type", |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 207 | GetClassName(message)); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 208 | } |
| 209 | out->Print("\n"); |
| 210 | } |
| 211 | |
| 212 | void GenerateStaticMethodField(Printer* out, const MethodDescriptor *method) { |
| 213 | out->Print( |
| 214 | "static readonly Method<$request$, $response$> $fieldname$ = new Method<$request$, $response$>(\n", |
| 215 | "fieldname", GetMethodFieldName(method), "request", |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 216 | GetClassName(method->input_type()), "response", |
| 217 | GetClassName(method->output_type())); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 218 | out->Indent(); |
| 219 | out->Indent(); |
| 220 | out->Print("$methodtype$,\n", "methodtype", |
| 221 | GetCSharpMethodType(GetMethodType(method))); |
Jan Tattermusch | a9ddd02 | 2015-08-05 03:04:58 -0700 | [diff] [blame] | 222 | out->Print("$servicenamefield$,\n", "servicenamefield", |
| 223 | GetServiceNameFieldName()); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 224 | out->Print("\"$methodname$\",\n", "methodname", method->name()); |
| 225 | out->Print("$requestmarshaller$,\n", "requestmarshaller", |
| 226 | GetMarshallerFieldName(method->input_type())); |
| 227 | out->Print("$responsemarshaller$);\n", "responsemarshaller", |
| 228 | GetMarshallerFieldName(method->output_type())); |
| 229 | out->Print("\n"); |
| 230 | out->Outdent(); |
| 231 | out->Outdent(); |
| 232 | } |
| 233 | |
Jan Tattermusch | e6af5d1 | 2015-08-03 10:57:43 -0700 | [diff] [blame] | 234 | void GenerateServiceDescriptorProperty(Printer* out, const ServiceDescriptor *service) { |
yang-g | c3ee1d5 | 2015-08-28 11:33:52 -0700 | [diff] [blame] | 235 | std::ostringstream index; |
| 236 | index << service->index(); |
Jan Tattermusch | e6af5d1 | 2015-08-03 10:57:43 -0700 | [diff] [blame] | 237 | out->Print("// service descriptor\n"); |
| 238 | out->Print("public static global::Google.Protobuf.Reflection.ServiceDescriptor Descriptor\n"); |
| 239 | out->Print("{\n"); |
| 240 | out->Print(" get { return $umbrella$.Descriptor.Services[$index$]; }\n", |
Jan Tattermusch | da717f4 | 2016-01-20 13:12:35 -0800 | [diff] [blame] | 241 | "umbrella", GetReflectionClassName(service->file()), "index", |
yang-g | c3ee1d5 | 2015-08-28 11:33:52 -0700 | [diff] [blame] | 242 | index.str()); |
Jan Tattermusch | e6af5d1 | 2015-08-03 10:57:43 -0700 | [diff] [blame] | 243 | out->Print("}\n"); |
| 244 | out->Print("\n"); |
| 245 | } |
| 246 | |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 247 | void GenerateClientInterface(Printer* out, const ServiceDescriptor *service) { |
Jan Tattermusch | b533281 | 2015-07-14 19:29:35 -0700 | [diff] [blame] | 248 | out->Print("// client interface\n"); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 249 | out->Print("public interface $name$\n", "name", |
| 250 | GetClientInterfaceName(service)); |
| 251 | out->Print("{\n"); |
| 252 | out->Indent(); |
| 253 | for (int i = 0; i < service->method_count(); i++) { |
| 254 | const MethodDescriptor *method = service->method(i); |
| 255 | MethodType method_type = GetMethodType(method); |
| 256 | |
| 257 | if (method_type == METHODTYPE_NO_STREAMING) { |
| 258 | // unary calls have an extra synchronous stub method |
| 259 | out->Print( |
Jan Tattermusch | 7452956 | 2015-07-23 14:04:51 -0700 | [diff] [blame] | 260 | "$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] | 261 | "methodname", method->name(), "request", |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 262 | GetClassName(method->input_type()), "response", |
| 263 | GetClassName(method->output_type())); |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 264 | |
Jan Tattermusch | e5e57ad | 2015-08-05 14:54:08 -0700 | [diff] [blame] | 265 | // overload taking CallOptions as a param |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 266 | out->Print( |
Jan Tattermusch | e5e57ad | 2015-08-05 14:54:08 -0700 | [diff] [blame] | 267 | "$response$ $methodname$($request$ request, CallOptions options);\n", |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 268 | "methodname", method->name(), "request", |
| 269 | GetClassName(method->input_type()), "response", |
| 270 | GetClassName(method->output_type())); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | std::string method_name = method->name(); |
| 274 | if (method_type == METHODTYPE_NO_STREAMING) { |
| 275 | method_name += "Async"; // prevent name clash with synchronous method. |
| 276 | } |
| 277 | out->Print( |
Jan Tattermusch | 7452956 | 2015-07-23 14:04:51 -0700 | [diff] [blame] | 278 | "$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] | 279 | "methodname", method_name, "request_maybe", |
| 280 | GetMethodRequestParamMaybe(method), "returntype", |
| 281 | GetMethodReturnTypeClient(method)); |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 282 | |
Jan Tattermusch | e5e57ad | 2015-08-05 14:54:08 -0700 | [diff] [blame] | 283 | // overload taking CallOptions as a param |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 284 | out->Print( |
Jan Tattermusch | e5e57ad | 2015-08-05 14:54:08 -0700 | [diff] [blame] | 285 | "$returntype$ $methodname$($request_maybe$CallOptions options);\n", |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 286 | "methodname", method_name, "request_maybe", |
| 287 | GetMethodRequestParamMaybe(method), "returntype", |
| 288 | GetMethodReturnTypeClient(method)); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 289 | } |
| 290 | out->Outdent(); |
| 291 | out->Print("}\n"); |
| 292 | out->Print("\n"); |
| 293 | } |
| 294 | |
| 295 | void GenerateServerInterface(Printer* out, const ServiceDescriptor *service) { |
| 296 | out->Print("// server-side interface\n"); |
Jan Tattermusch | 10a002d | 2016-03-14 15:22:19 -0700 | [diff] [blame^] | 297 | out->Print("[System.Obsolete(\"Service implementations should inherit" |
| 298 | " from the generated abstract base class instead.\")]\n"); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 299 | out->Print("public interface $name$\n", "name", |
| 300 | GetServerInterfaceName(service)); |
| 301 | out->Print("{\n"); |
| 302 | out->Indent(); |
| 303 | for (int i = 0; i < service->method_count(); i++) { |
| 304 | const MethodDescriptor *method = service->method(i); |
Craig Tiller | b256faa | 2015-07-23 11:28:16 -0700 | [diff] [blame] | 305 | out->Print( |
| 306 | "$returntype$ $methodname$($request$$response_stream_maybe$, " |
| 307 | "ServerCallContext context);\n", |
| 308 | "methodname", method->name(), "returntype", |
| 309 | GetMethodReturnTypeServer(method), "request", |
| 310 | GetMethodRequestParamServer(method), "response_stream_maybe", |
| 311 | GetMethodResponseStreamMaybe(method)); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 312 | } |
| 313 | out->Outdent(); |
| 314 | out->Print("}\n"); |
| 315 | out->Print("\n"); |
| 316 | } |
| 317 | |
Jan Tattermusch | 10a002d | 2016-03-14 15:22:19 -0700 | [diff] [blame^] | 318 | void GenerateServerClass(Printer* out, const ServiceDescriptor *service) { |
| 319 | out->Print("// server-side abstract class\n"); |
| 320 | out->Print("public abstract class $name$\n", "name", |
| 321 | GetServerClassName(service)); |
| 322 | out->Print("{\n"); |
| 323 | out->Indent(); |
| 324 | for (int i = 0; i < service->method_count(); i++) { |
| 325 | const MethodDescriptor *method = service->method(i); |
| 326 | out->Print( |
| 327 | "public virtual $returntype$ $methodname$($request$$response_stream_maybe$, " |
| 328 | "ServerCallContext context)\n", |
| 329 | "methodname", method->name(), "returntype", |
| 330 | GetMethodReturnTypeServer(method), "request", |
| 331 | GetMethodRequestParamServer(method), "response_stream_maybe", |
| 332 | GetMethodResponseStreamMaybe(method)); |
| 333 | out->Print("{\n"); |
| 334 | out->Indent(); |
| 335 | out->Print("throw new RpcException(" |
| 336 | "new Status(StatusCode.Unimplemented, \"\"));\n"); |
| 337 | out->Outdent(); |
| 338 | out->Print("}\n\n"); |
| 339 | } |
| 340 | out->Outdent(); |
| 341 | out->Print("}\n"); |
| 342 | out->Print("\n"); |
| 343 | } |
| 344 | |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 345 | void GenerateClientStub(Printer* out, const ServiceDescriptor *service) { |
| 346 | out->Print("// client stub\n"); |
| 347 | out->Print( |
Jan Tattermusch | b533281 | 2015-07-14 19:29:35 -0700 | [diff] [blame] | 348 | "public class $name$ : ClientBase, $interface$\n", |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 349 | "name", GetClientClassName(service), "interface", |
| 350 | GetClientInterfaceName(service)); |
| 351 | out->Print("{\n"); |
| 352 | out->Indent(); |
| 353 | |
| 354 | // constructors |
| 355 | out->Print( |
Jan Tattermusch | b533281 | 2015-07-14 19:29:35 -0700 | [diff] [blame] | 356 | "public $name$(Channel channel) : base(channel)\n", |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 357 | "name", GetClientClassName(service)); |
| 358 | out->Print("{\n"); |
| 359 | out->Print("}\n"); |
| 360 | |
| 361 | for (int i = 0; i < service->method_count(); i++) { |
| 362 | const MethodDescriptor *method = service->method(i); |
| 363 | MethodType method_type = GetMethodType(method); |
| 364 | |
| 365 | if (method_type == METHODTYPE_NO_STREAMING) { |
| 366 | // unary calls have an extra synchronous stub method |
| 367 | out->Print( |
Jan Tattermusch | 7452956 | 2015-07-23 14:04:51 -0700 | [diff] [blame] | 368 | "public $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] | 369 | "methodname", method->name(), "request", |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 370 | GetClassName(method->input_type()), "response", |
| 371 | GetClassName(method->output_type())); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 372 | out->Print("{\n"); |
| 373 | out->Indent(); |
Jan Tattermusch | e5e57ad | 2015-08-05 14:54:08 -0700 | [diff] [blame] | 374 | out->Print("var call = CreateCall($methodfield$, new CallOptions(headers, deadline, cancellationToken));\n", |
Jan Tattermusch | 641cb1b | 2015-08-05 03:51:46 -0700 | [diff] [blame] | 375 | "methodfield", GetMethodFieldName(method)); |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 376 | out->Print("return Calls.BlockingUnaryCall(call, request);\n"); |
| 377 | out->Outdent(); |
| 378 | out->Print("}\n"); |
| 379 | |
Jan Tattermusch | e5e57ad | 2015-08-05 14:54:08 -0700 | [diff] [blame] | 380 | // overload taking CallOptions as a param |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 381 | out->Print( |
Jan Tattermusch | e5e57ad | 2015-08-05 14:54:08 -0700 | [diff] [blame] | 382 | "public $response$ $methodname$($request$ request, CallOptions options)\n", |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 383 | "methodname", method->name(), "request", |
| 384 | GetClassName(method->input_type()), "response", |
| 385 | GetClassName(method->output_type())); |
| 386 | out->Print("{\n"); |
| 387 | out->Indent(); |
Jan Tattermusch | e5e57ad | 2015-08-05 14:54:08 -0700 | [diff] [blame] | 388 | out->Print("var call = CreateCall($methodfield$, options);\n", |
Jan Tattermusch | 641cb1b | 2015-08-05 03:51:46 -0700 | [diff] [blame] | 389 | "methodfield", GetMethodFieldName(method)); |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 390 | out->Print("return Calls.BlockingUnaryCall(call, request);\n"); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 391 | out->Outdent(); |
| 392 | out->Print("}\n"); |
| 393 | } |
| 394 | |
| 395 | std::string method_name = method->name(); |
| 396 | if (method_type == METHODTYPE_NO_STREAMING) { |
| 397 | method_name += "Async"; // prevent name clash with synchronous method. |
| 398 | } |
| 399 | out->Print( |
Jan Tattermusch | 7452956 | 2015-07-23 14:04:51 -0700 | [diff] [blame] | 400 | "public $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] | 401 | "methodname", method_name, "request_maybe", |
| 402 | GetMethodRequestParamMaybe(method), "returntype", |
| 403 | GetMethodReturnTypeClient(method)); |
| 404 | out->Print("{\n"); |
| 405 | out->Indent(); |
Jan Tattermusch | e5e57ad | 2015-08-05 14:54:08 -0700 | [diff] [blame] | 406 | out->Print("var call = CreateCall($methodfield$, new CallOptions(headers, deadline, cancellationToken));\n", |
Jan Tattermusch | 641cb1b | 2015-08-05 03:51:46 -0700 | [diff] [blame] | 407 | "methodfield", GetMethodFieldName(method)); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 408 | switch (GetMethodType(method)) { |
| 409 | case METHODTYPE_NO_STREAMING: |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 410 | out->Print("return Calls.AsyncUnaryCall(call, request);\n"); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 411 | break; |
| 412 | case METHODTYPE_CLIENT_STREAMING: |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 413 | out->Print("return Calls.AsyncClientStreamingCall(call);\n"); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 414 | break; |
| 415 | case METHODTYPE_SERVER_STREAMING: |
| 416 | out->Print( |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 417 | "return Calls.AsyncServerStreamingCall(call, request);\n"); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 418 | break; |
| 419 | case METHODTYPE_BIDI_STREAMING: |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 420 | out->Print("return Calls.AsyncDuplexStreamingCall(call);\n"); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 421 | break; |
| 422 | default: |
| 423 | GOOGLE_LOG(FATAL)<< "Can't get here."; |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 424 | } |
| 425 | out->Outdent(); |
| 426 | out->Print("}\n"); |
| 427 | |
Jan Tattermusch | e5e57ad | 2015-08-05 14:54:08 -0700 | [diff] [blame] | 428 | // overload taking CallOptions as a param |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 429 | out->Print( |
Jan Tattermusch | e5e57ad | 2015-08-05 14:54:08 -0700 | [diff] [blame] | 430 | "public $returntype$ $methodname$($request_maybe$CallOptions options)\n", |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 431 | "methodname", method_name, "request_maybe", |
| 432 | GetMethodRequestParamMaybe(method), "returntype", |
| 433 | GetMethodReturnTypeClient(method)); |
| 434 | out->Print("{\n"); |
| 435 | out->Indent(); |
Jan Tattermusch | e5e57ad | 2015-08-05 14:54:08 -0700 | [diff] [blame] | 436 | out->Print("var call = CreateCall($methodfield$, options);\n", |
Jan Tattermusch | 641cb1b | 2015-08-05 03:51:46 -0700 | [diff] [blame] | 437 | "methodfield", GetMethodFieldName(method)); |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 438 | switch (GetMethodType(method)) { |
| 439 | case METHODTYPE_NO_STREAMING: |
| 440 | out->Print("return Calls.AsyncUnaryCall(call, request);\n"); |
| 441 | break; |
| 442 | case METHODTYPE_CLIENT_STREAMING: |
| 443 | out->Print("return Calls.AsyncClientStreamingCall(call);\n"); |
| 444 | break; |
| 445 | case METHODTYPE_SERVER_STREAMING: |
| 446 | out->Print( |
| 447 | "return Calls.AsyncServerStreamingCall(call, request);\n"); |
| 448 | break; |
| 449 | case METHODTYPE_BIDI_STREAMING: |
| 450 | out->Print("return Calls.AsyncDuplexStreamingCall(call);\n"); |
| 451 | break; |
| 452 | default: |
| 453 | GOOGLE_LOG(FATAL)<< "Can't get here."; |
| 454 | } |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 455 | out->Outdent(); |
| 456 | out->Print("}\n"); |
| 457 | } |
| 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); |
| 526 | GenerateServerInterface(out, service); |
Jan Tattermusch | 10a002d | 2016-03-14 15:22:19 -0700 | [diff] [blame^] | 527 | GenerateServerClass(out, service); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 528 | GenerateClientStub(out, service); |
Jan Tattermusch | 10a002d | 2016-03-14 15:22:19 -0700 | [diff] [blame^] | 529 | GenerateBindServiceMethod(out, service, false); |
| 530 | GenerateBindServiceMethod(out, service, true); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 531 | GenerateNewStubMethods(out, service); |
| 532 | |
| 533 | out->Outdent(); |
| 534 | out->Print("}\n"); |
| 535 | } |
| 536 | |
| 537 | } // anonymous namespace |
| 538 | |
| 539 | grpc::string GetServices(const FileDescriptor *file) { |
| 540 | grpc::string output; |
Jan Tattermusch | 5dcebd9 | 2015-05-27 15:30:59 -0700 | [diff] [blame] | 541 | { |
| 542 | // 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] | 543 | |
Jan Tattermusch | 5dcebd9 | 2015-05-27 15:30:59 -0700 | [diff] [blame] | 544 | StringOutputStream output_stream(&output); |
| 545 | Printer out(&output_stream, '$'); |
| 546 | |
| 547 | // Don't write out any output if there no services, to avoid empty service |
| 548 | // files being generated for proto files that don't declare any. |
| 549 | if (file->service_count() == 0) { |
| 550 | return output; |
| 551 | } |
| 552 | |
| 553 | // Write out a file header. |
| 554 | out.Print("// Generated by the protocol buffer compiler. DO NOT EDIT!\n"); |
| 555 | out.Print("// source: $filename$\n", "filename", file->name()); |
| 556 | out.Print("#region Designer generated code\n"); |
| 557 | out.Print("\n"); |
| 558 | out.Print("using System;\n"); |
| 559 | out.Print("using System.Threading;\n"); |
| 560 | out.Print("using System.Threading.Tasks;\n"); |
| 561 | out.Print("using Grpc.Core;\n"); |
Jan Tattermusch | 5dcebd9 | 2015-05-27 15:30:59 -0700 | [diff] [blame] | 562 | out.Print("\n"); |
| 563 | |
| 564 | out.Print("namespace $namespace$ {\n", "namespace", GetFileNamespace(file)); |
| 565 | out.Indent(); |
| 566 | for (int i = 0; i < file->service_count(); i++) { |
| 567 | GenerateService(&out, file->service(i)); |
| 568 | } |
| 569 | out.Outdent(); |
| 570 | out.Print("}\n"); |
| 571 | out.Print("#endregion\n"); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 572 | } |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 573 | return output; |
| 574 | } |
| 575 | |
| 576 | } // namespace grpc_csharp_generator |