generic_stub.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. //
  3. // Copyright 2015 gRPC authors.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. //
  18. #ifndef GRPCPP_GENERIC_GENERIC_STUB_H
  19. #define GRPCPP_GENERIC_GENERIC_STUB_H
  20. #include <functional>
  21. #include <grpcpp/client_context.h>
  22. #include <grpcpp/impl/rpc_method.h>
  23. #include <grpcpp/support/async_stream.h>
  24. #include <grpcpp/support/async_unary_call.h>
  25. #include <grpcpp/support/byte_buffer.h>
  26. #include <grpcpp/support/client_callback.h>
  27. #include <grpcpp/support/status.h>
  28. #include <grpcpp/support/stub_options.h>
  29. namespace grpc {
  30. class CompletionQueue;
  31. typedef ClientAsyncReaderWriter<ByteBuffer, ByteBuffer>
  32. GenericClientAsyncReaderWriter;
  33. typedef ClientAsyncResponseReader<ByteBuffer> GenericClientAsyncResponseReader;
  34. /// Generic stubs provide a type-unaware interface to call gRPC methods
  35. /// by name. In practice, the Request and Response types should be basic
  36. /// types like grpc::ByteBuffer or proto::MessageLite (the base protobuf).
  37. template <class RequestType, class ResponseType>
  38. class TemplatedGenericStub final {
  39. public:
  40. explicit TemplatedGenericStub(std::shared_ptr<grpc::ChannelInterface> channel)
  41. : channel_(channel) {}
  42. /// Setup a call to a named method \a method using \a context, but don't
  43. /// start it. Let it be started explicitly with StartCall and a tag.
  44. /// The return value only indicates whether or not registration of the call
  45. /// succeeded (i.e. the call won't proceed if the return value is nullptr).
  46. std::unique_ptr<ClientAsyncReaderWriter<RequestType, ResponseType>>
  47. PrepareCall(ClientContext* context, const TString& method,
  48. grpc::CompletionQueue* cq) {
  49. return CallInternal(channel_.get(), context, method, /*options=*/{}, cq,
  50. false, nullptr);
  51. }
  52. /// Setup a unary call to a named method \a method using \a context, and don't
  53. /// start it. Let it be started explicitly with StartCall.
  54. /// The return value only indicates whether or not registration of the call
  55. /// succeeded (i.e. the call won't proceed if the return value is nullptr).
  56. std::unique_ptr<ClientAsyncResponseReader<ResponseType>> PrepareUnaryCall(
  57. ClientContext* context, const TString& method,
  58. const RequestType& request, grpc::CompletionQueue* cq) {
  59. return std::unique_ptr<ClientAsyncResponseReader<ResponseType>>(
  60. internal::ClientAsyncResponseReaderHelper::Create<ResponseType>(
  61. channel_.get(), cq,
  62. grpc::internal::RpcMethod(method.c_str(),
  63. /*suffix_for_stats=*/nullptr,
  64. grpc::internal::RpcMethod::NORMAL_RPC),
  65. context, request));
  66. }
  67. /// DEPRECATED for multi-threaded use
  68. /// Begin a call to a named method \a method using \a context.
  69. /// A tag \a tag will be delivered to \a cq when the call has been started
  70. /// (i.e, initial metadata has been sent).
  71. /// The return value only indicates whether or not registration of the call
  72. /// succeeded (i.e. the call won't proceed if the return value is nullptr).
  73. std::unique_ptr<ClientAsyncReaderWriter<RequestType, ResponseType>> Call(
  74. ClientContext* context, const TString& method,
  75. grpc::CompletionQueue* cq, void* tag) {
  76. return CallInternal(channel_.get(), context, method, /*options=*/{}, cq,
  77. true, tag);
  78. }
  79. /// Setup and start a unary call to a named method \a method using
  80. /// \a context and specifying the \a request and \a response buffers.
  81. void UnaryCall(ClientContext* context, const TString& method,
  82. StubOptions options, const RequestType* request,
  83. ResponseType* response,
  84. std::function<void(grpc::Status)> on_completion) {
  85. UnaryCallInternal(context, method, options, request, response,
  86. std::move(on_completion));
  87. }
  88. /// Setup a unary call to a named method \a method using
  89. /// \a context and specifying the \a request and \a response buffers.
  90. /// Like any other reactor-based RPC, it will not be activated until
  91. /// StartCall is invoked on its reactor.
  92. void PrepareUnaryCall(ClientContext* context, const TString& method,
  93. StubOptions options, const RequestType* request,
  94. ResponseType* response, ClientUnaryReactor* reactor) {
  95. PrepareUnaryCallInternal(context, method, options, request, response,
  96. reactor);
  97. }
  98. /// Setup a call to a named method \a method using \a context and tied to
  99. /// \a reactor . Like any other bidi streaming RPC, it will not be activated
  100. /// until StartCall is invoked on its reactor.
  101. void PrepareBidiStreamingCall(
  102. ClientContext* context, const TString& method, StubOptions options,
  103. ClientBidiReactor<RequestType, ResponseType>* reactor) {
  104. PrepareBidiStreamingCallInternal(context, method, options, reactor);
  105. }
  106. private:
  107. std::shared_ptr<grpc::ChannelInterface> channel_;
  108. void UnaryCallInternal(ClientContext* context, const TString& method,
  109. StubOptions options, const RequestType* request,
  110. ResponseType* response,
  111. std::function<void(grpc::Status)> on_completion) {
  112. internal::CallbackUnaryCall(
  113. channel_.get(),
  114. grpc::internal::RpcMethod(method.c_str(), options.suffix_for_stats(),
  115. grpc::internal::RpcMethod::NORMAL_RPC),
  116. context, request, response, std::move(on_completion));
  117. }
  118. void PrepareUnaryCallInternal(ClientContext* context,
  119. const TString& method, StubOptions options,
  120. const RequestType* request,
  121. ResponseType* response,
  122. ClientUnaryReactor* reactor) {
  123. internal::ClientCallbackUnaryFactory::Create<RequestType, ResponseType>(
  124. channel_.get(),
  125. grpc::internal::RpcMethod(method.c_str(), options.suffix_for_stats(),
  126. grpc::internal::RpcMethod::NORMAL_RPC),
  127. context, request, response, reactor);
  128. }
  129. void PrepareBidiStreamingCallInternal(
  130. ClientContext* context, const TString& method, StubOptions options,
  131. ClientBidiReactor<RequestType, ResponseType>* reactor) {
  132. internal::ClientCallbackReaderWriterFactory<RequestType, ResponseType>::
  133. Create(channel_.get(),
  134. grpc::internal::RpcMethod(
  135. method.c_str(), options.suffix_for_stats(),
  136. grpc::internal::RpcMethod::BIDI_STREAMING),
  137. context, reactor);
  138. }
  139. std::unique_ptr<ClientAsyncReaderWriter<RequestType, ResponseType>>
  140. CallInternal(grpc::ChannelInterface* channel, ClientContext* context,
  141. const TString& method, StubOptions options,
  142. grpc::CompletionQueue* cq, bool start, void* tag) {
  143. return std::unique_ptr<ClientAsyncReaderWriter<RequestType, ResponseType>>(
  144. internal::ClientAsyncReaderWriterFactory<RequestType, ResponseType>::
  145. Create(channel, cq,
  146. grpc::internal::RpcMethod(
  147. method.c_str(), options.suffix_for_stats(),
  148. grpc::internal::RpcMethod::BIDI_STREAMING),
  149. context, start, tag));
  150. }
  151. };
  152. typedef TemplatedGenericStub<grpc::ByteBuffer, grpc::ByteBuffer> GenericStub;
  153. } // namespace grpc
  154. #endif // GRPCPP_GENERIC_GENERIC_STUB_H