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