You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

generic_stub.h 8.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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/codegen/stub_options.h>
  23. #include <grpcpp/impl/rpc_method.h>
  24. #include <grpcpp/support/async_stream.h>
  25. #include <grpcpp/support/async_unary_call.h>
  26. #include <grpcpp/support/byte_buffer.h>
  27. #include <grpcpp/support/client_callback.h>
  28. #include <grpcpp/support/status.h>
  29. namespace grpc
  30. {
  31. class CompletionQueue;
  32. typedef ClientAsyncReaderWriter<ByteBuffer, ByteBuffer>
  33. GenericClientAsyncReaderWriter;
  34. typedef ClientAsyncResponseReader<ByteBuffer> GenericClientAsyncResponseReader;
  35. /// Generic stubs provide a type-unaware interface to call gRPC methods
  36. /// by name. In practice, the Request and Response types should be basic
  37. /// types like grpc::ByteBuffer or proto::MessageLite (the base protobuf).
  38. template<class RequestType, class ResponseType>
  39. class TemplatedGenericStub final
  40. {
  41. public:
  42. explicit TemplatedGenericStub(std::shared_ptr<grpc::ChannelInterface> channel) :
  43. channel_(channel)
  44. {
  45. }
  46. /// Setup a call to a named method \a method using \a context, but don't
  47. /// start it. Let it be started explicitly with StartCall and a tag.
  48. /// The return value only indicates whether or not registration of the call
  49. /// succeeded (i.e. the call won't proceed if the return value is nullptr).
  50. std::unique_ptr<ClientAsyncReaderWriter<RequestType, ResponseType>>
  51. PrepareCall(ClientContext* context, const std::string& method, grpc::CompletionQueue* cq)
  52. {
  53. return CallInternal(channel_.get(), context, method, /*options=*/{}, cq, false, nullptr);
  54. }
  55. /// Setup a unary call to a named method \a method using \a context, and don't
  56. /// start it. Let it be started explicitly with StartCall.
  57. /// The return value only indicates whether or not registration of the call
  58. /// succeeded (i.e. the call won't proceed if the return value is nullptr).
  59. std::unique_ptr<ClientAsyncResponseReader<ResponseType>> PrepareUnaryCall(
  60. ClientContext* context, const std::string& method, const RequestType& request, grpc::CompletionQueue* cq
  61. )
  62. {
  63. return std::unique_ptr<ClientAsyncResponseReader<ResponseType>>(
  64. internal::ClientAsyncResponseReaderHelper::Create<ResponseType>(
  65. channel_.get(), cq, grpc::internal::RpcMethod(method.c_str(),
  66. /*suffix_for_stats=*/nullptr,
  67. grpc::internal::RpcMethod::NORMAL_RPC),
  68. context,
  69. request
  70. )
  71. );
  72. }
  73. /// DEPRECATED for multi-threaded use
  74. /// Begin a call to a named method \a method using \a context.
  75. /// A tag \a tag will be delivered to \a cq when the call has been started
  76. /// (i.e, initial metadata has been sent).
  77. /// The return value only indicates whether or not registration of the call
  78. /// succeeded (i.e. the call won't proceed if the return value is nullptr).
  79. std::unique_ptr<ClientAsyncReaderWriter<RequestType, ResponseType>> Call(
  80. ClientContext* context, const std::string& method, grpc::CompletionQueue* cq, void* tag
  81. )
  82. {
  83. return CallInternal(channel_.get(), context, method, /*options=*/{}, cq, true, tag);
  84. }
  85. /// Setup and start a unary call to a named method \a method using
  86. /// \a context and specifying the \a request and \a response buffers.
  87. void UnaryCall(ClientContext* context, const std::string& method, StubOptions options, const RequestType* request, ResponseType* response, std::function<void(grpc::Status)> on_completion)
  88. {
  89. UnaryCallInternal(context, method, options, request, response, std::move(on_completion));
  90. }
  91. /// Setup a unary call to a named method \a method using
  92. /// \a context and specifying the \a request and \a response buffers.
  93. /// Like any other reactor-based RPC, it will not be activated until
  94. /// StartCall is invoked on its reactor.
  95. void PrepareUnaryCall(ClientContext* context, const std::string& method, StubOptions options, const RequestType* request, ResponseType* response, ClientUnaryReactor* reactor)
  96. {
  97. PrepareUnaryCallInternal(context, method, options, request, response, reactor);
  98. }
  99. /// Setup a call to a named method \a method using \a context and tied to
  100. /// \a reactor . Like any other bidi streaming RPC, it will not be activated
  101. /// until StartCall is invoked on its reactor.
  102. void PrepareBidiStreamingCall(
  103. ClientContext* context, const std::string& method, StubOptions options, ClientBidiReactor<RequestType, ResponseType>* reactor
  104. )
  105. {
  106. PrepareBidiStreamingCallInternal(context, method, options, reactor);
  107. }
  108. private:
  109. std::shared_ptr<grpc::ChannelInterface> channel_;
  110. void UnaryCallInternal(ClientContext* context, const std::string& method, StubOptions options, const RequestType* request, ResponseType* response, std::function<void(grpc::Status)> on_completion)
  111. {
  112. internal::CallbackUnaryCall(
  113. channel_.get(),
  114. grpc::internal::RpcMethod(method.c_str(), options.suffix_for_stats(), grpc::internal::RpcMethod::NORMAL_RPC),
  115. context,
  116. request,
  117. response,
  118. std::move(on_completion)
  119. );
  120. }
  121. void PrepareUnaryCallInternal(ClientContext* context, const std::string& method, StubOptions options, const RequestType* request, ResponseType* response, ClientUnaryReactor* reactor)
  122. {
  123. internal::ClientCallbackUnaryFactory::Create<RequestType, ResponseType>(
  124. channel_.get(),
  125. grpc::internal::RpcMethod(method.c_str(), options.suffix_for_stats(), grpc::internal::RpcMethod::NORMAL_RPC),
  126. context,
  127. request,
  128. response,
  129. reactor
  130. );
  131. }
  132. void PrepareBidiStreamingCallInternal(
  133. ClientContext* context, const std::string& method, StubOptions options, ClientBidiReactor<RequestType, ResponseType>* reactor
  134. )
  135. {
  136. internal::ClientCallbackReaderWriterFactory<RequestType, ResponseType>::
  137. Create(channel_.get(), grpc::internal::RpcMethod(method.c_str(), options.suffix_for_stats(), grpc::internal::RpcMethod::BIDI_STREAMING), context, reactor);
  138. }
  139. std::unique_ptr<ClientAsyncReaderWriter<RequestType, ResponseType>>
  140. CallInternal(grpc::ChannelInterface* channel, ClientContext* context, const std::string& method, StubOptions options, grpc::CompletionQueue* cq, bool start, void* tag)
  141. {
  142. return std::unique_ptr<ClientAsyncReaderWriter<RequestType, ResponseType>>(
  143. internal::ClientAsyncReaderWriterFactory<RequestType, ResponseType>::
  144. Create(channel, cq, grpc::internal::RpcMethod(method.c_str(), options.suffix_for_stats(), grpc::internal::RpcMethod::BIDI_STREAMING), context, start, tag)
  145. );
  146. }
  147. };
  148. typedef TemplatedGenericStub<grpc::ByteBuffer, grpc::ByteBuffer> GenericStub;
  149. } // namespace grpc
  150. #endif // GRPCPP_GENERIC_GENERIC_STUB_H