|
|
@ -1,6 +1,6 @@ |
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* |
|
|
|
* |
|
|
|
* Copyright 2014, Google Inc. |
|
|
|
* Copyright 2015, Google Inc. |
|
|
|
* All rights reserved. |
|
|
|
* All rights reserved. |
|
|
|
* |
|
|
|
* |
|
|
|
* Redistribution and use in source and binary forms, with or without |
|
|
|
* Redistribution and use in source and binary forms, with or without |
|
|
@ -41,10 +41,18 @@ |
|
|
|
#include <google/protobuf/descriptor.pb.h> |
|
|
|
#include <google/protobuf/descriptor.pb.h> |
|
|
|
#include <google/protobuf/io/printer.h> |
|
|
|
#include <google/protobuf/io/printer.h> |
|
|
|
#include <google/protobuf/io/zero_copy_stream_impl_lite.h> |
|
|
|
#include <google/protobuf/io/zero_copy_stream_impl_lite.h> |
|
|
|
|
|
|
|
#include <sstream> |
|
|
|
|
|
|
|
|
|
|
|
namespace grpc_cpp_generator { |
|
|
|
namespace grpc_cpp_generator { |
|
|
|
namespace { |
|
|
|
namespace { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <class T> |
|
|
|
|
|
|
|
std::string as_string(T x) { |
|
|
|
|
|
|
|
std::ostringstream out; |
|
|
|
|
|
|
|
out << x; |
|
|
|
|
|
|
|
return out.str(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool NoStreaming(const google::protobuf::MethodDescriptor *method) { |
|
|
|
bool NoStreaming(const google::protobuf::MethodDescriptor *method) { |
|
|
|
return !method->client_streaming() && !method->server_streaming(); |
|
|
|
return !method->client_streaming() && !method->server_streaming(); |
|
|
|
} |
|
|
|
} |
|
|
@ -61,6 +69,17 @@ bool BidiStreaming(const google::protobuf::MethodDescriptor *method) { |
|
|
|
return method->client_streaming() && method->server_streaming(); |
|
|
|
return method->client_streaming() && method->server_streaming(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool HasUnaryCalls(const google::protobuf::FileDescriptor *file) { |
|
|
|
|
|
|
|
for (int i = 0; i < file->service_count(); i++) { |
|
|
|
|
|
|
|
for (int j = 0; j < file->service(i)->method_count(); j++) { |
|
|
|
|
|
|
|
if (NoStreaming(file->service(i)->method(j))) { |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool HasClientOnlyStreaming(const google::protobuf::FileDescriptor *file) { |
|
|
|
bool HasClientOnlyStreaming(const google::protobuf::FileDescriptor *file) { |
|
|
|
for (int i = 0; i < file->service_count(); i++) { |
|
|
|
for (int i = 0; i < file->service_count(); i++) { |
|
|
|
for (int j = 0; j < file->service(i)->method_count(); j++) { |
|
|
|
for (int j = 0; j < file->service(i)->method_count(); j++) { |
|
|
@ -97,20 +116,32 @@ bool HasBidiStreaming(const google::protobuf::FileDescriptor *file) { |
|
|
|
|
|
|
|
|
|
|
|
std::string GetHeaderIncludes(const google::protobuf::FileDescriptor *file) { |
|
|
|
std::string GetHeaderIncludes(const google::protobuf::FileDescriptor *file) { |
|
|
|
std::string temp = |
|
|
|
std::string temp = |
|
|
|
"#include \"grpc++/impl/internal_stub.h\"\n" |
|
|
|
"#include <grpc++/impl/internal_stub.h>\n" |
|
|
|
"#include \"grpc++/status.h\"\n" |
|
|
|
"#include <grpc++/impl/service_type.h>\n" |
|
|
|
|
|
|
|
"#include <grpc++/status.h>\n" |
|
|
|
"\n" |
|
|
|
"\n" |
|
|
|
"namespace grpc {\n" |
|
|
|
"namespace grpc {\n" |
|
|
|
|
|
|
|
"class CompletionQueue;\n" |
|
|
|
"class ChannelInterface;\n" |
|
|
|
"class ChannelInterface;\n" |
|
|
|
"class RpcService;\n" |
|
|
|
"class RpcService;\n" |
|
|
|
"class ServerContext;\n"; |
|
|
|
"class ServerContext;\n"; |
|
|
|
|
|
|
|
if (HasUnaryCalls(file)) { |
|
|
|
|
|
|
|
temp.append( |
|
|
|
|
|
|
|
"template <class OutMessage> class ClientAsyncResponseReader;\n"); |
|
|
|
|
|
|
|
temp.append( |
|
|
|
|
|
|
|
"template <class OutMessage> class ServerAsyncResponseWriter;\n"); |
|
|
|
|
|
|
|
} |
|
|
|
if (HasClientOnlyStreaming(file)) { |
|
|
|
if (HasClientOnlyStreaming(file)) { |
|
|
|
temp.append("template <class OutMessage> class ClientWriter;\n"); |
|
|
|
temp.append("template <class OutMessage> class ClientWriter;\n"); |
|
|
|
temp.append("template <class InMessage> class ServerReader;\n"); |
|
|
|
temp.append("template <class InMessage> class ServerReader;\n"); |
|
|
|
|
|
|
|
temp.append("template <class OutMessage> class ClientAsyncWriter;\n"); |
|
|
|
|
|
|
|
temp.append("template <class OutMessage, class InMessage> class ServerAsyncReader;\n"); |
|
|
|
} |
|
|
|
} |
|
|
|
if (HasServerOnlyStreaming(file)) { |
|
|
|
if (HasServerOnlyStreaming(file)) { |
|
|
|
temp.append("template <class InMessage> class ClientReader;\n"); |
|
|
|
temp.append("template <class InMessage> class ClientReader;\n"); |
|
|
|
temp.append("template <class OutMessage> class ServerWriter;\n"); |
|
|
|
temp.append("template <class OutMessage> class ServerWriter;\n"); |
|
|
|
|
|
|
|
temp.append("template <class OutMessage> class ClientAsyncReader;\n"); |
|
|
|
|
|
|
|
temp.append("template <class InMessage> class ServerAsyncWriter;\n"); |
|
|
|
} |
|
|
|
} |
|
|
|
if (HasBidiStreaming(file)) { |
|
|
|
if (HasBidiStreaming(file)) { |
|
|
|
temp.append( |
|
|
|
temp.append( |
|
|
@ -119,16 +150,25 @@ std::string GetHeaderIncludes(const google::protobuf::FileDescriptor *file) { |
|
|
|
temp.append( |
|
|
|
temp.append( |
|
|
|
"template <class OutMessage, class InMessage>\n" |
|
|
|
"template <class OutMessage, class InMessage>\n" |
|
|
|
"class ServerReaderWriter;\n"); |
|
|
|
"class ServerReaderWriter;\n"); |
|
|
|
|
|
|
|
temp.append( |
|
|
|
|
|
|
|
"template <class OutMessage, class InMessage>\n" |
|
|
|
|
|
|
|
"class ClientAsyncReaderWriter;\n"); |
|
|
|
|
|
|
|
temp.append( |
|
|
|
|
|
|
|
"template <class OutMessage, class InMessage>\n" |
|
|
|
|
|
|
|
"class ServerAsyncReaderWriter;\n"); |
|
|
|
} |
|
|
|
} |
|
|
|
temp.append("} // namespace grpc\n"); |
|
|
|
temp.append("} // namespace grpc\n"); |
|
|
|
return temp; |
|
|
|
return temp; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::string GetSourceIncludes() { |
|
|
|
std::string GetSourceIncludes() { |
|
|
|
return "#include \"grpc++/channel_interface.h\"\n" |
|
|
|
return "#include <grpc++/async_unary_call.h>\n" |
|
|
|
"#include \"grpc++/impl/rpc_method.h\"\n" |
|
|
|
"#include <grpc++/channel_interface.h>\n" |
|
|
|
"#include \"grpc++/impl/rpc_service_method.h\"\n" |
|
|
|
"#include <grpc++/impl/client_unary_call.h>\n" |
|
|
|
"#include \"grpc++/stream.h\"\n"; |
|
|
|
"#include <grpc++/impl/rpc_method.h>\n" |
|
|
|
|
|
|
|
"#include <grpc++/impl/rpc_service_method.h>\n" |
|
|
|
|
|
|
|
"#include <grpc++/impl/service_type.h>\n" |
|
|
|
|
|
|
|
"#include <grpc++/stream.h>\n"; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void PrintHeaderClientMethod(google::protobuf::io::Printer *printer, |
|
|
|
void PrintHeaderClientMethod(google::protobuf::io::Printer *printer, |
|
|
@ -142,27 +182,44 @@ void PrintHeaderClientMethod(google::protobuf::io::Printer *printer, |
|
|
|
if (NoStreaming(method)) { |
|
|
|
if (NoStreaming(method)) { |
|
|
|
printer->Print(*vars, |
|
|
|
printer->Print(*vars, |
|
|
|
"::grpc::Status $Method$(::grpc::ClientContext* context, " |
|
|
|
"::grpc::Status $Method$(::grpc::ClientContext* context, " |
|
|
|
"const $Request$& request, $Response$* response);\n\n"); |
|
|
|
"const $Request$& request, $Response$* response);\n"); |
|
|
|
|
|
|
|
printer->Print(*vars, |
|
|
|
|
|
|
|
"::grpc::ClientAsyncResponseReader< $Response$>* " |
|
|
|
|
|
|
|
"$Method$(::grpc::ClientContext* context, " |
|
|
|
|
|
|
|
"const $Request$& request, " |
|
|
|
|
|
|
|
"::grpc::CompletionQueue* cq, void* tag);\n"); |
|
|
|
} else if (ClientOnlyStreaming(method)) { |
|
|
|
} else if (ClientOnlyStreaming(method)) { |
|
|
|
printer->Print( |
|
|
|
printer->Print(*vars, |
|
|
|
*vars, |
|
|
|
"::grpc::ClientWriter< $Request$>* $Method$(" |
|
|
|
"::grpc::ClientWriter< $Request$>* $Method$(" |
|
|
|
"::grpc::ClientContext* context, $Response$* response);\n"); |
|
|
|
"::grpc::ClientContext* context, $Response$* response);\n\n"); |
|
|
|
printer->Print(*vars, |
|
|
|
|
|
|
|
"::grpc::ClientAsyncWriter< $Request$>* $Method$(" |
|
|
|
|
|
|
|
"::grpc::ClientContext* context, $Response$* response, " |
|
|
|
|
|
|
|
"::grpc::CompletionQueue* cq, void* tag);\n"); |
|
|
|
} else if (ServerOnlyStreaming(method)) { |
|
|
|
} else if (ServerOnlyStreaming(method)) { |
|
|
|
printer->Print( |
|
|
|
printer->Print( |
|
|
|
*vars, |
|
|
|
*vars, |
|
|
|
"::grpc::ClientReader< $Response$>* $Method$(" |
|
|
|
"::grpc::ClientReader< $Response$>* $Method$(" |
|
|
|
"::grpc::ClientContext* context, const $Request$* request);\n\n"); |
|
|
|
"::grpc::ClientContext* context, const $Request$& request);\n"); |
|
|
|
|
|
|
|
printer->Print(*vars, |
|
|
|
|
|
|
|
"::grpc::ClientAsyncReader< $Response$>* $Method$(" |
|
|
|
|
|
|
|
"::grpc::ClientContext* context, const $Request$& request, " |
|
|
|
|
|
|
|
"::grpc::CompletionQueue* cq, void* tag);\n"); |
|
|
|
} else if (BidiStreaming(method)) { |
|
|
|
} else if (BidiStreaming(method)) { |
|
|
|
printer->Print(*vars, |
|
|
|
printer->Print(*vars, |
|
|
|
"::grpc::ClientReaderWriter< $Request$, $Response$>* " |
|
|
|
"::grpc::ClientReaderWriter< $Request$, $Response$>* " |
|
|
|
"$Method$(::grpc::ClientContext* context);\n\n"); |
|
|
|
"$Method$(::grpc::ClientContext* context);\n"); |
|
|
|
|
|
|
|
printer->Print(*vars, |
|
|
|
|
|
|
|
"::grpc::ClientAsyncReaderWriter< $Request$, $Response$>* " |
|
|
|
|
|
|
|
"$Method$(::grpc::ClientContext* context, " |
|
|
|
|
|
|
|
"::grpc::CompletionQueue* cq, void* tag);\n"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void PrintHeaderServerMethod(google::protobuf::io::Printer *printer, |
|
|
|
void PrintHeaderServerMethodSync( |
|
|
|
const google::protobuf::MethodDescriptor *method, |
|
|
|
google::protobuf::io::Printer *printer, |
|
|
|
std::map<std::string, std::string> *vars) { |
|
|
|
const google::protobuf::MethodDescriptor *method, |
|
|
|
|
|
|
|
std::map<std::string, std::string> *vars) { |
|
|
|
(*vars)["Method"] = method->name(); |
|
|
|
(*vars)["Method"] = method->name(); |
|
|
|
(*vars)["Request"] = |
|
|
|
(*vars)["Request"] = |
|
|
|
grpc_cpp_generator::ClassName(method->input_type(), true); |
|
|
|
grpc_cpp_generator::ClassName(method->input_type(), true); |
|
|
@ -194,19 +251,56 @@ void PrintHeaderServerMethod(google::protobuf::io::Printer *printer, |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void PrintHeaderServerMethodAsync( |
|
|
|
|
|
|
|
google::protobuf::io::Printer *printer, |
|
|
|
|
|
|
|
const google::protobuf::MethodDescriptor *method, |
|
|
|
|
|
|
|
std::map<std::string, std::string> *vars) { |
|
|
|
|
|
|
|
(*vars)["Method"] = method->name(); |
|
|
|
|
|
|
|
(*vars)["Request"] = |
|
|
|
|
|
|
|
grpc_cpp_generator::ClassName(method->input_type(), true); |
|
|
|
|
|
|
|
(*vars)["Response"] = |
|
|
|
|
|
|
|
grpc_cpp_generator::ClassName(method->output_type(), true); |
|
|
|
|
|
|
|
if (NoStreaming(method)) { |
|
|
|
|
|
|
|
printer->Print(*vars, |
|
|
|
|
|
|
|
"void Request$Method$(" |
|
|
|
|
|
|
|
"::grpc::ServerContext* context, $Request$* request, " |
|
|
|
|
|
|
|
"::grpc::ServerAsyncResponseWriter< $Response$>* response, " |
|
|
|
|
|
|
|
"::grpc::CompletionQueue* cq, void *tag);\n"); |
|
|
|
|
|
|
|
} else if (ClientOnlyStreaming(method)) { |
|
|
|
|
|
|
|
printer->Print(*vars, |
|
|
|
|
|
|
|
"void Request$Method$(" |
|
|
|
|
|
|
|
"::grpc::ServerContext* context, " |
|
|
|
|
|
|
|
"::grpc::ServerAsyncReader< $Response$, $Request$>* reader, " |
|
|
|
|
|
|
|
"::grpc::CompletionQueue* cq, void *tag);\n"); |
|
|
|
|
|
|
|
} else if (ServerOnlyStreaming(method)) { |
|
|
|
|
|
|
|
printer->Print(*vars, |
|
|
|
|
|
|
|
"void Request$Method$(" |
|
|
|
|
|
|
|
"::grpc::ServerContext* context, $Request$* request, " |
|
|
|
|
|
|
|
"::grpc::ServerAsyncWriter< $Response$>* writer, " |
|
|
|
|
|
|
|
"::grpc::CompletionQueue* cq, void *tag);\n"); |
|
|
|
|
|
|
|
} else if (BidiStreaming(method)) { |
|
|
|
|
|
|
|
printer->Print( |
|
|
|
|
|
|
|
*vars, |
|
|
|
|
|
|
|
"void Request$Method$(" |
|
|
|
|
|
|
|
"::grpc::ServerContext* context, " |
|
|
|
|
|
|
|
"::grpc::ServerAsyncReaderWriter< $Response$, $Request$>* stream, " |
|
|
|
|
|
|
|
"::grpc::CompletionQueue* cq, void *tag);\n"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void PrintHeaderService(google::protobuf::io::Printer *printer, |
|
|
|
void PrintHeaderService(google::protobuf::io::Printer *printer, |
|
|
|
const google::protobuf::ServiceDescriptor *service, |
|
|
|
const google::protobuf::ServiceDescriptor *service, |
|
|
|
std::map<std::string, std::string> *vars) { |
|
|
|
std::map<std::string, std::string> *vars) { |
|
|
|
(*vars)["Service"] = service->name(); |
|
|
|
(*vars)["Service"] = service->name(); |
|
|
|
|
|
|
|
|
|
|
|
printer->Print(*vars, |
|
|
|
printer->Print(*vars, |
|
|
|
"class $Service$ {\n" |
|
|
|
"class $Service$ final {\n" |
|
|
|
" public:\n"); |
|
|
|
" public:\n"); |
|
|
|
printer->Indent(); |
|
|
|
printer->Indent(); |
|
|
|
|
|
|
|
|
|
|
|
// Client side
|
|
|
|
// Client side
|
|
|
|
printer->Print( |
|
|
|
printer->Print( |
|
|
|
"class Stub : public ::grpc::InternalStub {\n" |
|
|
|
"class Stub final : public ::grpc::InternalStub {\n" |
|
|
|
" public:\n"); |
|
|
|
" public:\n"); |
|
|
|
printer->Indent(); |
|
|
|
printer->Indent(); |
|
|
|
for (int i = 0; i < service->method_count(); ++i) { |
|
|
|
for (int i = 0; i < service->method_count(); ++i) { |
|
|
@ -220,23 +314,37 @@ void PrintHeaderService(google::protobuf::io::Printer *printer, |
|
|
|
|
|
|
|
|
|
|
|
printer->Print("\n"); |
|
|
|
printer->Print("\n"); |
|
|
|
|
|
|
|
|
|
|
|
// Server side
|
|
|
|
// Server side - Synchronous
|
|
|
|
printer->Print( |
|
|
|
printer->Print( |
|
|
|
"class Service {\n" |
|
|
|
"class Service : public ::grpc::SynchronousService {\n" |
|
|
|
" public:\n"); |
|
|
|
" public:\n"); |
|
|
|
printer->Indent(); |
|
|
|
printer->Indent(); |
|
|
|
printer->Print("Service() : service_(nullptr) {}\n"); |
|
|
|
printer->Print("Service() : service_(nullptr) {}\n"); |
|
|
|
printer->Print("virtual ~Service();\n"); |
|
|
|
printer->Print("virtual ~Service();\n"); |
|
|
|
for (int i = 0; i < service->method_count(); ++i) { |
|
|
|
for (int i = 0; i < service->method_count(); ++i) { |
|
|
|
PrintHeaderServerMethod(printer, service->method(i), vars); |
|
|
|
PrintHeaderServerMethodSync(printer, service->method(i), vars); |
|
|
|
} |
|
|
|
} |
|
|
|
printer->Print("::grpc::RpcService* service();\n"); |
|
|
|
printer->Print("::grpc::RpcService* service() override final;\n"); |
|
|
|
printer->Outdent(); |
|
|
|
printer->Outdent(); |
|
|
|
printer->Print( |
|
|
|
printer->Print( |
|
|
|
" private:\n" |
|
|
|
" private:\n" |
|
|
|
" ::grpc::RpcService* service_;\n"); |
|
|
|
" ::grpc::RpcService* service_;\n"); |
|
|
|
printer->Print("};\n"); |
|
|
|
printer->Print("};\n"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Server side - Asynchronous
|
|
|
|
|
|
|
|
printer->Print( |
|
|
|
|
|
|
|
"class AsyncService final : public ::grpc::AsynchronousService {\n" |
|
|
|
|
|
|
|
" public:\n"); |
|
|
|
|
|
|
|
printer->Indent(); |
|
|
|
|
|
|
|
(*vars)["MethodCount"] = as_string(service->method_count()); |
|
|
|
|
|
|
|
printer->Print("explicit AsyncService(::grpc::CompletionQueue* cq);\n"); |
|
|
|
|
|
|
|
printer->Print("~AsyncService() {};\n"); |
|
|
|
|
|
|
|
for (int i = 0; i < service->method_count(); ++i) { |
|
|
|
|
|
|
|
PrintHeaderServerMethodAsync(printer, service->method(i), vars); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
printer->Outdent(); |
|
|
|
|
|
|
|
printer->Print("};\n"); |
|
|
|
|
|
|
|
|
|
|
|
printer->Outdent(); |
|
|
|
printer->Outdent(); |
|
|
|
printer->Print("};\n"); |
|
|
|
printer->Print("};\n"); |
|
|
|
} |
|
|
|
} |
|
|
@ -268,10 +376,21 @@ void PrintSourceClientMethod(google::protobuf::io::Printer *printer, |
|
|
|
"::grpc::ClientContext* context, " |
|
|
|
"::grpc::ClientContext* context, " |
|
|
|
"const $Request$& request, $Response$* response) {\n"); |
|
|
|
"const $Request$& request, $Response$* response) {\n"); |
|
|
|
printer->Print(*vars, |
|
|
|
printer->Print(*vars, |
|
|
|
" return channel()->StartBlockingRpc(" |
|
|
|
" return ::grpc::BlockingUnaryCall(channel()," |
|
|
|
"::grpc::RpcMethod(\"/$Package$$Service$/$Method$\"), " |
|
|
|
"::grpc::RpcMethod($Service$_method_names[$Idx$]), " |
|
|
|
"context, request, response);\n" |
|
|
|
"context, request, response);\n" |
|
|
|
"}\n\n"); |
|
|
|
"}\n\n"); |
|
|
|
|
|
|
|
printer->Print(*vars, |
|
|
|
|
|
|
|
"::grpc::ClientAsyncResponseReader< $Response$>* " |
|
|
|
|
|
|
|
"$Service$::Stub::$Method$(::grpc::ClientContext* context, " |
|
|
|
|
|
|
|
"const $Request$& request, " |
|
|
|
|
|
|
|
"::grpc::CompletionQueue* cq, void* tag) {\n"); |
|
|
|
|
|
|
|
printer->Print(*vars, |
|
|
|
|
|
|
|
" return new ClientAsyncResponseReader< $Response$>(" |
|
|
|
|
|
|
|
"channel(), cq, " |
|
|
|
|
|
|
|
"::grpc::RpcMethod($Service$_method_names[$Idx$]), " |
|
|
|
|
|
|
|
"context, request, tag);\n" |
|
|
|
|
|
|
|
"}\n\n"); |
|
|
|
} else if (ClientOnlyStreaming(method)) { |
|
|
|
} else if (ClientOnlyStreaming(method)) { |
|
|
|
printer->Print( |
|
|
|
printer->Print( |
|
|
|
*vars, |
|
|
|
*vars, |
|
|
@ -279,22 +398,46 @@ void PrintSourceClientMethod(google::protobuf::io::Printer *printer, |
|
|
|
"::grpc::ClientContext* context, $Response$* response) {\n"); |
|
|
|
"::grpc::ClientContext* context, $Response$* response) {\n"); |
|
|
|
printer->Print(*vars, |
|
|
|
printer->Print(*vars, |
|
|
|
" return new ::grpc::ClientWriter< $Request$>(" |
|
|
|
" return new ::grpc::ClientWriter< $Request$>(" |
|
|
|
"channel()->CreateStream(" |
|
|
|
"channel()," |
|
|
|
"::grpc::RpcMethod(\"/$Package$$Service$/$Method$\", " |
|
|
|
"::grpc::RpcMethod($Service$_method_names[$Idx$], " |
|
|
|
"::grpc::RpcMethod::RpcType::CLIENT_STREAMING), " |
|
|
|
"::grpc::RpcMethod::RpcType::CLIENT_STREAMING), " |
|
|
|
"context, nullptr, response));\n" |
|
|
|
"context, response);\n" |
|
|
|
|
|
|
|
"}\n\n"); |
|
|
|
|
|
|
|
printer->Print( |
|
|
|
|
|
|
|
*vars, |
|
|
|
|
|
|
|
"::grpc::ClientAsyncWriter< $Request$>* $Service$::Stub::$Method$(" |
|
|
|
|
|
|
|
"::grpc::ClientContext* context, $Response$* response, " |
|
|
|
|
|
|
|
"::grpc::CompletionQueue* cq, void* tag) {\n"); |
|
|
|
|
|
|
|
printer->Print(*vars, |
|
|
|
|
|
|
|
" return new ::grpc::ClientAsyncWriter< $Request$>(" |
|
|
|
|
|
|
|
"channel(), cq, " |
|
|
|
|
|
|
|
"::grpc::RpcMethod($Service$_method_names[$Idx$], " |
|
|
|
|
|
|
|
"::grpc::RpcMethod::RpcType::CLIENT_STREAMING), " |
|
|
|
|
|
|
|
"context, response, tag);\n" |
|
|
|
"}\n\n"); |
|
|
|
"}\n\n"); |
|
|
|
} else if (ServerOnlyStreaming(method)) { |
|
|
|
} else if (ServerOnlyStreaming(method)) { |
|
|
|
printer->Print( |
|
|
|
printer->Print( |
|
|
|
*vars, |
|
|
|
*vars, |
|
|
|
"::grpc::ClientReader< $Response$>* $Service$::Stub::$Method$(" |
|
|
|
"::grpc::ClientReader< $Response$>* $Service$::Stub::$Method$(" |
|
|
|
"::grpc::ClientContext* context, const $Request$* request) {\n"); |
|
|
|
"::grpc::ClientContext* context, const $Request$& request) {\n"); |
|
|
|
printer->Print(*vars, |
|
|
|
printer->Print(*vars, |
|
|
|
" return new ::grpc::ClientReader< $Response$>(" |
|
|
|
" return new ::grpc::ClientReader< $Response$>(" |
|
|
|
"channel()->CreateStream(" |
|
|
|
"channel()," |
|
|
|
"::grpc::RpcMethod(\"/$Package$$Service$/$Method$\", " |
|
|
|
"::grpc::RpcMethod($Service$_method_names[$Idx$], " |
|
|
|
|
|
|
|
"::grpc::RpcMethod::RpcType::SERVER_STREAMING), " |
|
|
|
|
|
|
|
"context, request);\n" |
|
|
|
|
|
|
|
"}\n\n"); |
|
|
|
|
|
|
|
printer->Print( |
|
|
|
|
|
|
|
*vars, |
|
|
|
|
|
|
|
"::grpc::ClientAsyncReader< $Response$>* $Service$::Stub::$Method$(" |
|
|
|
|
|
|
|
"::grpc::ClientContext* context, const $Request$& request, " |
|
|
|
|
|
|
|
"::grpc::CompletionQueue* cq, void* tag) {\n"); |
|
|
|
|
|
|
|
printer->Print(*vars, |
|
|
|
|
|
|
|
" return new ::grpc::ClientAsyncReader< $Response$>(" |
|
|
|
|
|
|
|
"channel(), cq, " |
|
|
|
|
|
|
|
"::grpc::RpcMethod($Service$_method_names[$Idx$], " |
|
|
|
"::grpc::RpcMethod::RpcType::SERVER_STREAMING), " |
|
|
|
"::grpc::RpcMethod::RpcType::SERVER_STREAMING), " |
|
|
|
"context, request, nullptr));\n" |
|
|
|
"context, request, tag);\n" |
|
|
|
"}\n\n"); |
|
|
|
"}\n\n"); |
|
|
|
} else if (BidiStreaming(method)) { |
|
|
|
} else if (BidiStreaming(method)) { |
|
|
|
printer->Print( |
|
|
|
printer->Print( |
|
|
@ -304,10 +447,23 @@ void PrintSourceClientMethod(google::protobuf::io::Printer *printer, |
|
|
|
printer->Print( |
|
|
|
printer->Print( |
|
|
|
*vars, |
|
|
|
*vars, |
|
|
|
" return new ::grpc::ClientReaderWriter< $Request$, $Response$>(" |
|
|
|
" return new ::grpc::ClientReaderWriter< $Request$, $Response$>(" |
|
|
|
"channel()->CreateStream(" |
|
|
|
"channel()," |
|
|
|
"::grpc::RpcMethod(\"/$Package$$Service$/$Method$\", " |
|
|
|
"::grpc::RpcMethod($Service$_method_names[$Idx$], " |
|
|
|
|
|
|
|
"::grpc::RpcMethod::RpcType::BIDI_STREAMING), " |
|
|
|
|
|
|
|
"context);\n" |
|
|
|
|
|
|
|
"}\n\n"); |
|
|
|
|
|
|
|
printer->Print( |
|
|
|
|
|
|
|
*vars, |
|
|
|
|
|
|
|
"::grpc::ClientAsyncReaderWriter< $Request$, $Response$>* " |
|
|
|
|
|
|
|
"$Service$::Stub::$Method$(::grpc::ClientContext* context, " |
|
|
|
|
|
|
|
"::grpc::CompletionQueue* cq, void* tag) {\n"); |
|
|
|
|
|
|
|
printer->Print( |
|
|
|
|
|
|
|
*vars, |
|
|
|
|
|
|
|
" return new ::grpc::ClientAsyncReaderWriter< $Request$, $Response$>(" |
|
|
|
|
|
|
|
"channel(), cq, " |
|
|
|
|
|
|
|
"::grpc::RpcMethod($Service$_method_names[$Idx$], " |
|
|
|
"::grpc::RpcMethod::RpcType::BIDI_STREAMING), " |
|
|
|
"::grpc::RpcMethod::RpcType::BIDI_STREAMING), " |
|
|
|
"context, nullptr, nullptr));\n" |
|
|
|
"context, tag);\n" |
|
|
|
"}\n\n"); |
|
|
|
"}\n\n"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -362,10 +518,73 @@ void PrintSourceServerMethod(google::protobuf::io::Printer *printer, |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void PrintSourceServerAsyncMethod( |
|
|
|
|
|
|
|
google::protobuf::io::Printer *printer, |
|
|
|
|
|
|
|
const google::protobuf::MethodDescriptor *method, |
|
|
|
|
|
|
|
std::map<std::string, std::string> *vars) { |
|
|
|
|
|
|
|
(*vars)["Method"] = method->name(); |
|
|
|
|
|
|
|
(*vars)["Request"] = |
|
|
|
|
|
|
|
grpc_cpp_generator::ClassName(method->input_type(), true); |
|
|
|
|
|
|
|
(*vars)["Response"] = |
|
|
|
|
|
|
|
grpc_cpp_generator::ClassName(method->output_type(), true); |
|
|
|
|
|
|
|
if (NoStreaming(method)) { |
|
|
|
|
|
|
|
printer->Print(*vars, |
|
|
|
|
|
|
|
"void $Service$::AsyncService::Request$Method$(" |
|
|
|
|
|
|
|
"::grpc::ServerContext* context, " |
|
|
|
|
|
|
|
"$Request$* request, " |
|
|
|
|
|
|
|
"::grpc::ServerAsyncResponseWriter< $Response$>* response, " |
|
|
|
|
|
|
|
"::grpc::CompletionQueue* cq, void* tag) {\n"); |
|
|
|
|
|
|
|
printer->Print( |
|
|
|
|
|
|
|
*vars, |
|
|
|
|
|
|
|
" AsynchronousService::RequestAsyncUnary($Idx$, context, request, response, cq, tag);\n"); |
|
|
|
|
|
|
|
printer->Print("}\n\n"); |
|
|
|
|
|
|
|
} else if (ClientOnlyStreaming(method)) { |
|
|
|
|
|
|
|
printer->Print(*vars, |
|
|
|
|
|
|
|
"void $Service$::AsyncService::Request$Method$(" |
|
|
|
|
|
|
|
"::grpc::ServerContext* context, " |
|
|
|
|
|
|
|
"::grpc::ServerAsyncReader< $Response$, $Request$>* reader, " |
|
|
|
|
|
|
|
"::grpc::CompletionQueue* cq, void* tag) {\n"); |
|
|
|
|
|
|
|
printer->Print( |
|
|
|
|
|
|
|
*vars, |
|
|
|
|
|
|
|
" AsynchronousService::RequestClientStreaming($Idx$, context, reader, cq, tag);\n"); |
|
|
|
|
|
|
|
printer->Print("}\n\n"); |
|
|
|
|
|
|
|
} else if (ServerOnlyStreaming(method)) { |
|
|
|
|
|
|
|
printer->Print(*vars, |
|
|
|
|
|
|
|
"void $Service$::AsyncService::Request$Method$(" |
|
|
|
|
|
|
|
"::grpc::ServerContext* context, " |
|
|
|
|
|
|
|
"$Request$* request, " |
|
|
|
|
|
|
|
"::grpc::ServerAsyncWriter< $Response$>* writer, " |
|
|
|
|
|
|
|
"::grpc::CompletionQueue* cq, void* tag) {\n"); |
|
|
|
|
|
|
|
printer->Print( |
|
|
|
|
|
|
|
*vars, |
|
|
|
|
|
|
|
" AsynchronousService::RequestServerStreaming($Idx$, context, request, writer, cq, tag);\n"); |
|
|
|
|
|
|
|
printer->Print("}\n\n"); |
|
|
|
|
|
|
|
} else if (BidiStreaming(method)) { |
|
|
|
|
|
|
|
printer->Print( |
|
|
|
|
|
|
|
*vars, |
|
|
|
|
|
|
|
"void $Service$::AsyncService::Request$Method$(" |
|
|
|
|
|
|
|
"::grpc::ServerContext* context, " |
|
|
|
|
|
|
|
"::grpc::ServerAsyncReaderWriter< $Response$, $Request$>* stream, " |
|
|
|
|
|
|
|
"::grpc::CompletionQueue* cq, void *tag) {\n"); |
|
|
|
|
|
|
|
printer->Print( |
|
|
|
|
|
|
|
*vars, |
|
|
|
|
|
|
|
" AsynchronousService::RequestBidiStreaming($Idx$, context, stream, cq, tag);\n"); |
|
|
|
|
|
|
|
printer->Print("}\n\n"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void PrintSourceService(google::protobuf::io::Printer *printer, |
|
|
|
void PrintSourceService(google::protobuf::io::Printer *printer, |
|
|
|
const google::protobuf::ServiceDescriptor *service, |
|
|
|
const google::protobuf::ServiceDescriptor *service, |
|
|
|
std::map<std::string, std::string> *vars) { |
|
|
|
std::map<std::string, std::string> *vars) { |
|
|
|
(*vars)["Service"] = service->name(); |
|
|
|
(*vars)["Service"] = service->name(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
printer->Print(*vars, "static const char* $Service$_method_names[] = {\n"); |
|
|
|
|
|
|
|
for (int i = 0; i < service->method_count(); ++i) { |
|
|
|
|
|
|
|
(*vars)["Method"] = service->method(i)->name(); |
|
|
|
|
|
|
|
printer->Print(*vars, " \"/$Package$$Service$/$Method$\",\n"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
printer->Print(*vars, "};\n\n"); |
|
|
|
|
|
|
|
|
|
|
|
printer->Print( |
|
|
|
printer->Print( |
|
|
|
*vars, |
|
|
|
*vars, |
|
|
|
"$Service$::Stub* $Service$::NewStub(" |
|
|
|
"$Service$::Stub* $Service$::NewStub(" |
|
|
@ -375,15 +594,25 @@ void PrintSourceService(google::protobuf::io::Printer *printer, |
|
|
|
" return stub;\n" |
|
|
|
" return stub;\n" |
|
|
|
"};\n\n"); |
|
|
|
"};\n\n"); |
|
|
|
for (int i = 0; i < service->method_count(); ++i) { |
|
|
|
for (int i = 0; i < service->method_count(); ++i) { |
|
|
|
|
|
|
|
(*vars)["Idx"] = as_string(i); |
|
|
|
PrintSourceClientMethod(printer, service->method(i), vars); |
|
|
|
PrintSourceClientMethod(printer, service->method(i), vars); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(*vars)["MethodCount"] = as_string(service->method_count()); |
|
|
|
|
|
|
|
printer->Print( |
|
|
|
|
|
|
|
*vars, |
|
|
|
|
|
|
|
"$Service$::AsyncService::AsyncService(::grpc::CompletionQueue* cq) : " |
|
|
|
|
|
|
|
"::grpc::AsynchronousService(cq, $Service$_method_names, $MethodCount$) " |
|
|
|
|
|
|
|
"{}\n\n"); |
|
|
|
|
|
|
|
|
|
|
|
printer->Print(*vars, |
|
|
|
printer->Print(*vars, |
|
|
|
"$Service$::Service::~Service() {\n" |
|
|
|
"$Service$::Service::~Service() {\n" |
|
|
|
" delete service_;\n" |
|
|
|
" delete service_;\n" |
|
|
|
"}\n\n"); |
|
|
|
"}\n\n"); |
|
|
|
for (int i = 0; i < service->method_count(); ++i) { |
|
|
|
for (int i = 0; i < service->method_count(); ++i) { |
|
|
|
|
|
|
|
(*vars)["Idx"] = as_string(i); |
|
|
|
PrintSourceServerMethod(printer, service->method(i), vars); |
|
|
|
PrintSourceServerMethod(printer, service->method(i), vars); |
|
|
|
|
|
|
|
PrintSourceServerAsyncMethod(printer, service->method(i), vars); |
|
|
|
} |
|
|
|
} |
|
|
|
printer->Print(*vars, |
|
|
|
printer->Print(*vars, |
|
|
|
"::grpc::RpcService* $Service$::Service::service() {\n"); |
|
|
|
"::grpc::RpcService* $Service$::Service::service() {\n"); |
|
|
@ -395,6 +624,7 @@ void PrintSourceService(google::protobuf::io::Printer *printer, |
|
|
|
printer->Print("service_ = new ::grpc::RpcService();\n"); |
|
|
|
printer->Print("service_ = new ::grpc::RpcService();\n"); |
|
|
|
for (int i = 0; i < service->method_count(); ++i) { |
|
|
|
for (int i = 0; i < service->method_count(); ++i) { |
|
|
|
const google::protobuf::MethodDescriptor *method = service->method(i); |
|
|
|
const google::protobuf::MethodDescriptor *method = service->method(i); |
|
|
|
|
|
|
|
(*vars)["Idx"] = as_string(i); |
|
|
|
(*vars)["Method"] = method->name(); |
|
|
|
(*vars)["Method"] = method->name(); |
|
|
|
(*vars)["Request"] = |
|
|
|
(*vars)["Request"] = |
|
|
|
grpc_cpp_generator::ClassName(method->input_type(), true); |
|
|
|
grpc_cpp_generator::ClassName(method->input_type(), true); |
|
|
@ -404,7 +634,7 @@ void PrintSourceService(google::protobuf::io::Printer *printer, |
|
|
|
printer->Print( |
|
|
|
printer->Print( |
|
|
|
*vars, |
|
|
|
*vars, |
|
|
|
"service_->AddMethod(new ::grpc::RpcServiceMethod(\n" |
|
|
|
"service_->AddMethod(new ::grpc::RpcServiceMethod(\n" |
|
|
|
" \"/$Package$$Service$/$Method$\",\n" |
|
|
|
" $Service$_method_names[$Idx$],\n" |
|
|
|
" ::grpc::RpcMethod::NORMAL_RPC,\n" |
|
|
|
" ::grpc::RpcMethod::NORMAL_RPC,\n" |
|
|
|
" new ::grpc::RpcMethodHandler< $Service$::Service, $Request$, " |
|
|
|
" new ::grpc::RpcMethodHandler< $Service$::Service, $Request$, " |
|
|
|
"$Response$>(\n" |
|
|
|
"$Response$>(\n" |
|
|
@ -416,7 +646,7 @@ void PrintSourceService(google::protobuf::io::Printer *printer, |
|
|
|
printer->Print( |
|
|
|
printer->Print( |
|
|
|
*vars, |
|
|
|
*vars, |
|
|
|
"service_->AddMethod(new ::grpc::RpcServiceMethod(\n" |
|
|
|
"service_->AddMethod(new ::grpc::RpcServiceMethod(\n" |
|
|
|
" \"/$Package$$Service$/$Method$\",\n" |
|
|
|
" $Service$_method_names[$Idx$],\n" |
|
|
|
" ::grpc::RpcMethod::CLIENT_STREAMING,\n" |
|
|
|
" ::grpc::RpcMethod::CLIENT_STREAMING,\n" |
|
|
|
" new ::grpc::ClientStreamingHandler< " |
|
|
|
" new ::grpc::ClientStreamingHandler< " |
|
|
|
"$Service$::Service, $Request$, $Response$>(\n" |
|
|
|
"$Service$::Service, $Request$, $Response$>(\n" |
|
|
@ -429,7 +659,7 @@ void PrintSourceService(google::protobuf::io::Printer *printer, |
|
|
|
printer->Print( |
|
|
|
printer->Print( |
|
|
|
*vars, |
|
|
|
*vars, |
|
|
|
"service_->AddMethod(new ::grpc::RpcServiceMethod(\n" |
|
|
|
"service_->AddMethod(new ::grpc::RpcServiceMethod(\n" |
|
|
|
" \"/$Package$$Service$/$Method$\",\n" |
|
|
|
" $Service$_method_names[$Idx$],\n" |
|
|
|
" ::grpc::RpcMethod::SERVER_STREAMING,\n" |
|
|
|
" ::grpc::RpcMethod::SERVER_STREAMING,\n" |
|
|
|
" new ::grpc::ServerStreamingHandler< " |
|
|
|
" new ::grpc::ServerStreamingHandler< " |
|
|
|
"$Service$::Service, $Request$, $Response$>(\n" |
|
|
|
"$Service$::Service, $Request$, $Response$>(\n" |
|
|
@ -442,7 +672,7 @@ void PrintSourceService(google::protobuf::io::Printer *printer, |
|
|
|
printer->Print( |
|
|
|
printer->Print( |
|
|
|
*vars, |
|
|
|
*vars, |
|
|
|
"service_->AddMethod(new ::grpc::RpcServiceMethod(\n" |
|
|
|
"service_->AddMethod(new ::grpc::RpcServiceMethod(\n" |
|
|
|
" \"/$Package$$Service$/$Method$\",\n" |
|
|
|
" $Service$_method_names[$Idx$],\n" |
|
|
|
" ::grpc::RpcMethod::BIDI_STREAMING,\n" |
|
|
|
" ::grpc::RpcMethod::BIDI_STREAMING,\n" |
|
|
|
" new ::grpc::BidiStreamingHandler< " |
|
|
|
" new ::grpc::BidiStreamingHandler< " |
|
|
|
"$Service$::Service, $Request$, $Response$>(\n" |
|
|
|
"$Service$::Service, $Request$, $Response$>(\n" |
|
|
|