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