From 62d2896665d7685c7f2d9b29a9b3b6d4bc72719b Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Fri, 10 Apr 2015 17:11:51 -0700 Subject: Add facility to register methods in C++ --- src/cpp/client/channel.cc | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/cpp/client/channel.cc') diff --git a/src/cpp/client/channel.cc b/src/cpp/client/channel.cc index 72123abbc8..5a39a3b801 100644 --- a/src/cpp/client/channel.cc +++ b/src/cpp/client/channel.cc @@ -82,4 +82,8 @@ void Channel::PerformOpsOnCall(CallOpBuffer* buf, Call* call) { GRPC_TIMER_MARK(PERFORM_OPS_END, call->call()); } +void *Channel::RegisterMethod(const char *method) { + return grpc_channel_register_call(c_channel_, method, target_.c_str()); +} + } // namespace grpc -- cgit v1.2.3 From 15f9f95312666574a8a42c91e35fcd4b0aab5987 Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Tue, 14 Apr 2015 14:01:39 -0700 Subject: Use channel tags to call methods --- src/cpp/client/channel.cc | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src/cpp/client/channel.cc') diff --git a/src/cpp/client/channel.cc b/src/cpp/client/channel.cc index 5a39a3b801..7a9ba477f0 100644 --- a/src/cpp/client/channel.cc +++ b/src/cpp/client/channel.cc @@ -61,7 +61,12 @@ Channel::~Channel() { grpc_channel_destroy(c_channel_); } Call Channel::CreateCall(const RpcMethod& method, ClientContext* context, CompletionQueue* cq) { - auto c_call = grpc_channel_create_call(c_channel_, cq->cq(), method.name(), + auto c_call = method.channel_tag()? + grpc_channel_create_registered_call( + c_channel_, cq->cq(), + method.channel_tag(), context->RawDeadline()) : + + grpc_channel_create_call(c_channel_, cq->cq(), method.name(), context->authority().empty() ? target_.c_str() : context->authority().c_str(), -- cgit v1.2.3 From 277d3cff7e07c8cc480a58edc972337b93503030 Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Tue, 14 Apr 2015 14:04:51 -0700 Subject: clang-format --- include/grpc++/impl/internal_stub.h | 3 +- include/grpc++/impl/rpc_method.h | 7 +-- src/compiler/cpp_generator.cc | 97 +++++++++++++++++++++---------------- src/cpp/client/channel.cc | 24 ++++----- src/cpp/client/generic_stub.cc | 12 ++--- 5 files changed, 78 insertions(+), 65 deletions(-) (limited to 'src/cpp/client/channel.cc') diff --git a/include/grpc++/impl/internal_stub.h b/include/grpc++/impl/internal_stub.h index 1b223d034f..370a3b8ac5 100644 --- a/include/grpc++/impl/internal_stub.h +++ b/include/grpc++/impl/internal_stub.h @@ -42,7 +42,8 @@ namespace grpc { class InternalStub { public: - InternalStub(const std::shared_ptr& channel) : channel_(channel) {} + InternalStub(const std::shared_ptr& channel) + : channel_(channel) {} virtual ~InternalStub() {} ChannelInterface* channel() { return channel_.get(); } diff --git a/include/grpc++/impl/rpc_method.h b/include/grpc++/impl/rpc_method.h index 3dfcdbad8f..50a160b08c 100644 --- a/include/grpc++/impl/rpc_method.h +++ b/include/grpc++/impl/rpc_method.h @@ -45,16 +45,17 @@ class RpcMethod { BIDI_STREAMING }; - RpcMethod(const char* name, RpcType type, void *channel_tag) : name_(name), method_type_(type), channel_tag_(channel_tag) {} + RpcMethod(const char* name, RpcType type, void* channel_tag) + : name_(name), method_type_(type), channel_tag_(channel_tag) {} const char* name() const { return name_; } RpcType method_type() const { return method_type_; } - void *channel_tag() const { return channel_tag_; } + void* channel_tag() const { return channel_tag_; } private: const char* const name_; const RpcType method_type_; - void * const channel_tag_; + void* const channel_tag_; }; } // namespace grpc diff --git a/src/compiler/cpp_generator.cc b/src/compiler/cpp_generator.cc index ff3758ccfa..1324198847 100644 --- a/src/compiler/cpp_generator.cc +++ b/src/compiler/cpp_generator.cc @@ -110,7 +110,7 @@ bool HasBidiStreaming(const grpc::protobuf::FileDescriptor *file) { return false; } -grpc::string FilenameIdentifier(const grpc::string& filename) { +grpc::string FilenameIdentifier(const grpc::string &filename) { grpc::string result; for (unsigned i = 0; i < filename.size(); i++) { char c = filename[i]; @@ -173,7 +173,9 @@ grpc::string GetHeaderIncludes(const grpc::protobuf::FileDescriptor *file, temp.append("template class ClientWriter;\n"); temp.append("template class ServerReader;\n"); temp.append("template class ClientAsyncWriter;\n"); - temp.append("template class ServerAsyncReader;\n"); + temp.append( + "template class " + "ServerAsyncReader;\n"); } if (HasServerOnlyStreaming(file)) { temp.append("template class ClientReader;\n"); @@ -247,11 +249,11 @@ void PrintHeaderClientMethod(grpc::protobuf::io::Printer *printer, *vars, "std::unique_ptr< ::grpc::ClientReader< $Response$>> $Method$(" "::grpc::ClientContext* context, const $Request$& request);\n"); - printer->Print( - *vars, - "std::unique_ptr< ::grpc::ClientAsyncReader< $Response$>> Async$Method$(" - "::grpc::ClientContext* context, const $Request$& request, " - "::grpc::CompletionQueue* cq, void* tag);\n"); + printer->Print(*vars, + "std::unique_ptr< ::grpc::ClientAsyncReader< $Response$>> " + "Async$Method$(" + "::grpc::ClientContext* context, const $Request$& request, " + "::grpc::CompletionQueue* cq, void* tag);\n"); } else if (BidiStreaming(method)) { printer->Print( *vars, @@ -272,10 +274,9 @@ void PrintHeaderClientMethodData(grpc::protobuf::io::Printer *printer, printer->Print(*vars, "const ::grpc::RpcMethod rpcmethod_$Method$_;\n"); } -void PrintHeaderServerMethodSync( - grpc::protobuf::io::Printer *printer, - const grpc::protobuf::MethodDescriptor *method, - std::map *vars) { +void PrintHeaderServerMethodSync(grpc::protobuf::io::Printer *printer, + const grpc::protobuf::MethodDescriptor *method, + std::map *vars) { (*vars)["Method"] = method->name(); (*vars)["Request"] = grpc_cpp_generator::ClassName(method->input_type(), true); @@ -359,7 +360,8 @@ void PrintHeaderService(grpc::protobuf::io::Printer *printer, "class Stub GRPC_FINAL : public ::grpc::InternalStub {\n" " public:\n"); printer->Indent(); - printer->Print("Stub(const std::shared_ptr< ::grpc::ChannelInterface>& channel);\n"); + printer->Print( + "Stub(const std::shared_ptr< ::grpc::ChannelInterface>& channel);\n"); for (int i = 0; i < service->method_count(); ++i) { PrintHeaderClientMethod(printer, service->method(i), vars); } @@ -607,11 +609,12 @@ void PrintSourceClientMethod(grpc::protobuf::io::Printer *printer, "rpcmethod_$Method$_, " "context));\n" "}\n\n"); - printer->Print(*vars, - "std::unique_ptr< ::grpc::ClientAsyncReaderWriter< " - "$Request$, $Response$>> " - "$ns$$Service$::Stub::Async$Method$(::grpc::ClientContext* context, " - "::grpc::CompletionQueue* cq, void* tag) {\n"); + printer->Print( + *vars, + "std::unique_ptr< ::grpc::ClientAsyncReaderWriter< " + "$Request$, $Response$>> " + "$ns$$Service$::Stub::Async$Method$(::grpc::ClientContext* context, " + "::grpc::CompletionQueue* cq, void* tag) {\n"); printer->Print(*vars, " return std::unique_ptr< ::grpc::ClientAsyncReaderWriter< " "$Request$, $Response$>>(new " @@ -689,9 +692,9 @@ void PrintSourceServerAsyncMethod( "$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(*vars, + " AsynchronousService::RequestAsyncUnary($Idx$, context, " + "request, response, cq, tag);\n"); printer->Print("}\n\n"); } else if (ClientOnlyStreaming(method)) { printer->Print(*vars, @@ -699,9 +702,9 @@ void PrintSourceServerAsyncMethod( "::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(*vars, + " AsynchronousService::RequestClientStreaming($Idx$, " + "context, reader, cq, tag);\n"); printer->Print("}\n\n"); } else if (ServerOnlyStreaming(method)) { printer->Print(*vars, @@ -710,9 +713,9 @@ void PrintSourceServerAsyncMethod( "$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(*vars, + " AsynchronousService::RequestServerStreaming($Idx$, " + "context, request, writer, cq, tag);\n"); printer->Print("}\n\n"); } else if (BidiStreaming(method)) { printer->Print( @@ -721,9 +724,9 @@ void PrintSourceServerAsyncMethod( "::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(*vars, + " AsynchronousService::RequestBidiStreaming($Idx$, " + "context, stream, cq, tag);\n"); printer->Print("}\n\n"); } } @@ -733,7 +736,8 @@ void PrintSourceService(grpc::protobuf::io::Printer *printer, std::map *vars) { (*vars)["Service"] = service->name(); - printer->Print(*vars, "static const char* $prefix$$Service$_method_names[] = {\n"); + printer->Print(*vars, + "static const char* $prefix$$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"); @@ -744,10 +748,13 @@ void PrintSourceService(grpc::protobuf::io::Printer *printer, *vars, "std::unique_ptr< $ns$$Service$::Stub> $ns$$Service$::NewStub(" "const std::shared_ptr< ::grpc::ChannelInterface>& channel) {\n" - " std::unique_ptr< $ns$$Service$::Stub> stub(new $ns$$Service$::Stub(channel));\n" + " std::unique_ptr< $ns$$Service$::Stub> stub(new " + "$ns$$Service$::Stub(channel));\n" " return stub;\n" "}\n\n"); - printer->Print(*vars, "$ns$$Service$::Stub::Stub(const std::shared_ptr< ::grpc::ChannelInterface>& channel)\n"); + printer->Print(*vars, + "$ns$$Service$::Stub::Stub(const std::shared_ptr< " + "::grpc::ChannelInterface>& channel)\n"); printer->Indent(); printer->Print(": ::grpc::InternalStub(channel)"); for (int i = 0; i < service->method_count(); ++i) { @@ -763,11 +770,13 @@ void PrintSourceService(grpc::protobuf::io::Printer *printer, } else { (*vars)["StreamingType"] = "BIDI_STREAMING"; } - printer->Print(*vars, ", rpcmethod_$Method$_(" - "$prefix$$Service$_method_names[$Idx$], " - "::grpc::RpcMethod::$StreamingType$, " - "channel->RegisterMethod($prefix$$Service$_method_names[$Idx$])" - ")\n"); + printer->Print( + *vars, + ", rpcmethod_$Method$_(" + "$prefix$$Service$_method_names[$Idx$], " + "::grpc::RpcMethod::$StreamingType$, " + "channel->RegisterMethod($prefix$$Service$_method_names[$Idx$])" + ")\n"); } printer->Print("{}\n\n"); printer->Outdent(); @@ -778,11 +787,12 @@ void PrintSourceService(grpc::protobuf::io::Printer *printer, } (*vars)["MethodCount"] = as_string(service->method_count()); - printer->Print( - *vars, - "$ns$$Service$::AsyncService::AsyncService(::grpc::CompletionQueue* cq) : " - "::grpc::AsynchronousService(cq, $prefix$$Service$_method_names, $MethodCount$) " - "{}\n\n"); + printer->Print(*vars, + "$ns$$Service$::AsyncService::AsyncService(::grpc::" + "CompletionQueue* cq) : " + "::grpc::AsynchronousService(cq, " + "$prefix$$Service$_method_names, $MethodCount$) " + "{}\n\n"); printer->Print(*vars, "$ns$$Service$::Service::~Service() {\n" @@ -815,7 +825,8 @@ void PrintSourceService(grpc::protobuf::io::Printer *printer, "service_->AddMethod(new ::grpc::RpcServiceMethod(\n" " $prefix$$Service$_method_names[$Idx$],\n" " ::grpc::RpcMethod::NORMAL_RPC,\n" - " new ::grpc::RpcMethodHandler< $ns$$Service$::Service, $Request$, " + " new ::grpc::RpcMethodHandler< $ns$$Service$::Service, " + "$Request$, " "$Response$>(\n" " std::function< ::grpc::Status($ns$$Service$::Service*, " "::grpc::ServerContext*, const $Request$*, $Response$*)>(" diff --git a/src/cpp/client/channel.cc b/src/cpp/client/channel.cc index 7a9ba477f0..478f223322 100644 --- a/src/cpp/client/channel.cc +++ b/src/cpp/client/channel.cc @@ -61,17 +61,17 @@ Channel::~Channel() { grpc_channel_destroy(c_channel_); } Call Channel::CreateCall(const RpcMethod& method, ClientContext* context, CompletionQueue* cq) { - auto c_call = method.channel_tag()? - grpc_channel_create_registered_call( - c_channel_, cq->cq(), - method.channel_tag(), context->RawDeadline()) : - - grpc_channel_create_call(c_channel_, cq->cq(), method.name(), - context->authority().empty() - ? target_.c_str() - : context->authority().c_str(), - context->RawDeadline()); - GRPC_TIMER_MARK(CALL_CREATED,c_call); + auto c_call = + method.channel_tag() + ? grpc_channel_create_registered_call(c_channel_, cq->cq(), + method.channel_tag(), + context->RawDeadline()) + : grpc_channel_create_call(c_channel_, cq->cq(), method.name(), + context->authority().empty() + ? target_.c_str() + : context->authority().c_str(), + context->RawDeadline()); + GRPC_TIMER_MARK(CALL_CREATED, c_call); context->set_call(c_call); return Call(c_call, this, cq); } @@ -87,7 +87,7 @@ void Channel::PerformOpsOnCall(CallOpBuffer* buf, Call* call) { GRPC_TIMER_MARK(PERFORM_OPS_END, call->call()); } -void *Channel::RegisterMethod(const char *method) { +void* Channel::RegisterMethod(const char* method) { return grpc_channel_register_call(c_channel_, method, target_.c_str()); } diff --git a/src/cpp/client/generic_stub.cc b/src/cpp/client/generic_stub.cc index 53084b33cc..0c90578ae5 100644 --- a/src/cpp/client/generic_stub.cc +++ b/src/cpp/client/generic_stub.cc @@ -39,13 +39,13 @@ namespace grpc { // begin a call to a named method std::unique_ptr GenericStub::Call( - ClientContext* context, const grpc::string& method, - CompletionQueue* cq, void* tag) { + ClientContext* context, const grpc::string& method, CompletionQueue* cq, + void* tag) { return std::unique_ptr( new GenericClientAsyncReaderWriter( - channel_.get(), cq, RpcMethod(method.c_str(), RpcMethod::BIDI_STREAMING, nullptr), context, tag)); + channel_.get(), cq, + RpcMethod(method.c_str(), RpcMethod::BIDI_STREAMING, nullptr), + context, tag)); } - -} // namespace grpc - +} // namespace grpc -- cgit v1.2.3