Merge pull request #21916 from vjpai/templatize_generic_stub

Templatize generic stub
pull/21971/head
Vijay Pai 5 years ago committed by GitHub
commit 8eed1b016c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 72
      include/grpcpp/generic/generic_stub_impl.h

@ -39,19 +39,22 @@ typedef ::grpc_impl::ClientAsyncResponseReader<ByteBuffer>
namespace grpc_impl { namespace grpc_impl {
class CompletionQueue; class CompletionQueue;
/// Generic stubs provide a type-unsafe interface to call gRPC methods /// Generic stubs provide a type-unaware interface to call gRPC methods
/// by name. /// by name. In practice, the Request and Response types should be basic
class GenericStub final { /// types like grpc::ByteBuffer or proto::MessageLite (the base protobuf).
template <class RequestType, class ResponseType>
class TemplatedGenericStub final {
public: public:
explicit GenericStub(std::shared_ptr<grpc::ChannelInterface> channel) explicit TemplatedGenericStub(std::shared_ptr<grpc::ChannelInterface> channel)
: channel_(channel) {} : channel_(channel) {}
/// Setup a call to a named method \a method using \a context, but don't /// Setup a call to a named method \a method using \a context, but don't
/// start it. Let it be started explicitly with StartCall and a tag. /// start it. Let it be started explicitly with StartCall and a tag.
/// The return value only indicates whether or not registration of the call /// The return value only indicates whether or not registration of the call
/// succeeded (i.e. the call won't proceed if the return value is nullptr). /// succeeded (i.e. the call won't proceed if the return value is nullptr).
std::unique_ptr<grpc::GenericClientAsyncReaderWriter> PrepareCall( std::unique_ptr<ClientAsyncReaderWriter<RequestType, ResponseType>>
ClientContext* context, const grpc::string& method, CompletionQueue* cq) { PrepareCall(ClientContext* context, const grpc::string& method,
CompletionQueue* cq) {
return CallInternal(channel_.get(), context, method, cq, false, nullptr); return CallInternal(channel_.get(), context, method, cq, false, nullptr);
} }
@ -59,11 +62,11 @@ class GenericStub final {
/// start it. Let it be started explicitly with StartCall. /// start it. Let it be started explicitly with StartCall.
/// The return value only indicates whether or not registration of the call /// The return value only indicates whether or not registration of the call
/// succeeded (i.e. the call won't proceed if the return value is nullptr). /// succeeded (i.e. the call won't proceed if the return value is nullptr).
std::unique_ptr<grpc::GenericClientAsyncResponseReader> PrepareUnaryCall( std::unique_ptr<ClientAsyncResponseReader<ResponseType>> PrepareUnaryCall(
ClientContext* context, const grpc::string& method, ClientContext* context, const grpc::string& method,
const grpc::ByteBuffer& request, CompletionQueue* cq) { const RequestType& request, CompletionQueue* cq) {
return std::unique_ptr<grpc::GenericClientAsyncResponseReader>( return std::unique_ptr<ClientAsyncResponseReader<ResponseType>>(
internal::ClientAsyncResponseReaderFactory<grpc::ByteBuffer>::Create( internal::ClientAsyncResponseReaderFactory<ResponseType>::Create(
channel_.get(), cq, channel_.get(), cq,
grpc::internal::RpcMethod(method.c_str(), grpc::internal::RpcMethod(method.c_str(),
grpc::internal::RpcMethod::NORMAL_RPC), grpc::internal::RpcMethod::NORMAL_RPC),
@ -76,7 +79,7 @@ class GenericStub final {
/// (i.e, initial metadata has been sent). /// (i.e, initial metadata has been sent).
/// The return value only indicates whether or not registration of the call /// The return value only indicates whether or not registration of the call
/// succeeded (i.e. the call won't proceed if the return value is nullptr). /// succeeded (i.e. the call won't proceed if the return value is nullptr).
std::unique_ptr<grpc::GenericClientAsyncReaderWriter> Call( std::unique_ptr<ClientAsyncReaderWriter<RequestType, ResponseType>> Call(
ClientContext* context, const grpc::string& method, CompletionQueue* cq, ClientContext* context, const grpc::string& method, CompletionQueue* cq,
void* tag) { void* tag) {
return CallInternal(channel_.get(), context, method, cq, true, tag); return CallInternal(channel_.get(), context, method, cq, true, tag);
@ -86,7 +89,7 @@ class GenericStub final {
/// Setup and start a unary call to a named method \a method using /// Setup and start a unary call to a named method \a method using
/// \a context and specifying the \a request and \a response buffers. /// \a context and specifying the \a request and \a response buffers.
void UnaryCall(ClientContext* context, const grpc::string& method, void UnaryCall(ClientContext* context, const grpc::string& method,
const grpc::ByteBuffer* request, grpc::ByteBuffer* response, const RequestType* request, ResponseType* response,
std::function<void(grpc::Status)> on_completion) { std::function<void(grpc::Status)> on_completion) {
UnaryCallInternal(context, method, request, response, UnaryCallInternal(context, method, request, response,
std::move(on_completion)); std::move(on_completion));
@ -97,8 +100,7 @@ class GenericStub final {
/// Like any other reactor-based RPC, it will not be activated until /// Like any other reactor-based RPC, it will not be activated until
/// StartCall is invoked on its reactor. /// StartCall is invoked on its reactor.
void PrepareUnaryCall(ClientContext* context, const grpc::string& method, void PrepareUnaryCall(ClientContext* context, const grpc::string& method,
const grpc::ByteBuffer* request, const RequestType* request, ResponseType* response,
grpc::ByteBuffer* response,
ClientUnaryReactor* reactor) { ClientUnaryReactor* reactor) {
PrepareUnaryCallInternal(context, method, request, response, reactor); PrepareUnaryCallInternal(context, method, request, response, reactor);
} }
@ -108,7 +110,7 @@ class GenericStub final {
/// until StartCall is invoked on its reactor. /// until StartCall is invoked on its reactor.
void PrepareBidiStreamingCall( void PrepareBidiStreamingCall(
ClientContext* context, const grpc::string& method, ClientContext* context, const grpc::string& method,
ClientBidiReactor<grpc::ByteBuffer, grpc::ByteBuffer>* reactor) { ClientBidiReactor<RequestType, ResponseType>* reactor) {
PrepareBidiStreamingCallInternal(context, method, reactor); PrepareBidiStreamingCallInternal(context, method, reactor);
} }
#endif #endif
@ -118,12 +120,12 @@ class GenericStub final {
/// they are no longer experimental /// they are no longer experimental
class experimental_type { class experimental_type {
public: public:
explicit experimental_type(GenericStub* stub) : stub_(stub) {} explicit experimental_type(TemplatedGenericStub* stub) : stub_(stub) {}
/// Setup and start a unary call to a named method \a method using /// Setup and start a unary call to a named method \a method using
/// \a context and specifying the \a request and \a response buffers. /// \a context and specifying the \a request and \a response buffers.
void UnaryCall(ClientContext* context, const grpc::string& method, void UnaryCall(ClientContext* context, const grpc::string& method,
const grpc::ByteBuffer* request, grpc::ByteBuffer* response, const RequestType* request, ResponseType* response,
std::function<void(grpc::Status)> on_completion) { std::function<void(grpc::Status)> on_completion) {
stub_->UnaryCallInternal(context, method, request, response, stub_->UnaryCallInternal(context, method, request, response,
std::move(on_completion)); std::move(on_completion));
@ -134,8 +136,7 @@ class GenericStub final {
/// Like any other reactor-based RPC, it will not be activated until /// Like any other reactor-based RPC, it will not be activated until
/// StartCall is invoked on its reactor. /// StartCall is invoked on its reactor.
void PrepareUnaryCall(ClientContext* context, const grpc::string& method, void PrepareUnaryCall(ClientContext* context, const grpc::string& method,
const grpc::ByteBuffer* request, const RequestType* request, ResponseType* response,
grpc::ByteBuffer* response,
ClientUnaryReactor* reactor) { ClientUnaryReactor* reactor) {
stub_->PrepareUnaryCallInternal(context, method, request, response, stub_->PrepareUnaryCallInternal(context, method, request, response,
reactor); reactor);
@ -146,12 +147,12 @@ class GenericStub final {
/// until StartCall is invoked on its reactor. /// until StartCall is invoked on its reactor.
void PrepareBidiStreamingCall( void PrepareBidiStreamingCall(
ClientContext* context, const grpc::string& method, ClientContext* context, const grpc::string& method,
ClientBidiReactor<grpc::ByteBuffer, grpc::ByteBuffer>* reactor) { ClientBidiReactor<RequestType, ResponseType>* reactor) {
stub_->PrepareBidiStreamingCallInternal(context, method, reactor); stub_->PrepareBidiStreamingCallInternal(context, method, reactor);
} }
private: private:
GenericStub* stub_; TemplatedGenericStub* stub_;
}; };
/// NOTE: The function experimental() is not stable public API. It is a view /// NOTE: The function experimental() is not stable public API. It is a view
@ -163,8 +164,7 @@ class GenericStub final {
std::shared_ptr<grpc::ChannelInterface> channel_; std::shared_ptr<grpc::ChannelInterface> channel_;
void UnaryCallInternal(ClientContext* context, const grpc::string& method, void UnaryCallInternal(ClientContext* context, const grpc::string& method,
const grpc::ByteBuffer* request, const RequestType* request, ResponseType* response,
grpc::ByteBuffer* response,
std::function<void(grpc::Status)> on_completion) { std::function<void(grpc::Status)> on_completion) {
internal::CallbackUnaryCall( internal::CallbackUnaryCall(
channel_.get(), channel_.get(),
@ -175,11 +175,10 @@ class GenericStub final {
void PrepareUnaryCallInternal(ClientContext* context, void PrepareUnaryCallInternal(ClientContext* context,
const grpc::string& method, const grpc::string& method,
const grpc::ByteBuffer* request, const RequestType* request,
grpc::ByteBuffer* response, ResponseType* response,
ClientUnaryReactor* reactor) { ClientUnaryReactor* reactor) {
internal::ClientCallbackUnaryFactory::Create<grpc::ByteBuffer, internal::ClientCallbackUnaryFactory::Create<RequestType, ResponseType>(
grpc::ByteBuffer>(
channel_.get(), channel_.get(),
grpc::internal::RpcMethod(method.c_str(), grpc::internal::RpcMethod(method.c_str(),
grpc::internal::RpcMethod::NORMAL_RPC), grpc::internal::RpcMethod::NORMAL_RPC),
@ -188,21 +187,20 @@ class GenericStub final {
void PrepareBidiStreamingCallInternal( void PrepareBidiStreamingCallInternal(
ClientContext* context, const grpc::string& method, ClientContext* context, const grpc::string& method,
ClientBidiReactor<grpc::ByteBuffer, grpc::ByteBuffer>* reactor) { ClientBidiReactor<RequestType, ResponseType>* reactor) {
internal::ClientCallbackReaderWriterFactory<grpc::ByteBuffer, internal::ClientCallbackReaderWriterFactory<RequestType, ResponseType>::
grpc::ByteBuffer>::
Create(channel_.get(), Create(channel_.get(),
grpc::internal::RpcMethod( grpc::internal::RpcMethod(
method.c_str(), grpc::internal::RpcMethod::BIDI_STREAMING), method.c_str(), grpc::internal::RpcMethod::BIDI_STREAMING),
context, reactor); context, reactor);
} }
std::unique_ptr<grpc::GenericClientAsyncReaderWriter> CallInternal( std::unique_ptr<ClientAsyncReaderWriter<RequestType, ResponseType>>
grpc::ChannelInterface* channel, ClientContext* context, CallInternal(grpc::ChannelInterface* channel, ClientContext* context,
const grpc::string& method, CompletionQueue* cq, bool start, void* tag) { const grpc::string& method, CompletionQueue* cq, bool start,
return std::unique_ptr<grpc::GenericClientAsyncReaderWriter>( void* tag) {
internal::ClientAsyncReaderWriterFactory<grpc::ByteBuffer, return std::unique_ptr<ClientAsyncReaderWriter<RequestType, ResponseType>>(
grpc::ByteBuffer>:: internal::ClientAsyncReaderWriterFactory<RequestType, ResponseType>::
Create( Create(
channel, cq, channel, cq,
grpc::internal::RpcMethod( grpc::internal::RpcMethod(
@ -211,6 +209,8 @@ class GenericStub final {
} }
}; };
typedef TemplatedGenericStub<grpc::ByteBuffer, grpc::ByteBuffer> GenericStub;
} // namespace grpc_impl } // namespace grpc_impl
#endif // GRPCPP_GENERIC_GENERIC_STUB_IMPL_H #endif // GRPCPP_GENERIC_GENERIC_STUB_IMPL_H

Loading…
Cancel
Save