generic_stub.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #include <functional>
  19. #include <grpcpp/generic/generic_stub.h>
  20. #include <grpcpp/impl/rpc_method.h>
  21. #include <grpcpp/support/client_callback.h>
  22. namespace grpc_impl {
  23. namespace {
  24. std::unique_ptr<grpc::GenericClientAsyncReaderWriter> CallInternal(
  25. grpc::ChannelInterface* channel, grpc::ClientContext* context,
  26. const grpc::string& method, grpc::CompletionQueue* cq, bool start,
  27. void* tag) {
  28. return std::unique_ptr<grpc::GenericClientAsyncReaderWriter>(
  29. grpc::internal::ClientAsyncReaderWriterFactory<grpc::ByteBuffer,
  30. grpc::ByteBuffer>::
  31. Create(channel, cq,
  32. grpc::internal::RpcMethod(
  33. method.c_str(), grpc::internal::RpcMethod::BIDI_STREAMING),
  34. context, start, tag));
  35. }
  36. } // namespace
  37. // begin a call to a named method
  38. std::unique_ptr<grpc::GenericClientAsyncReaderWriter> GenericStub::Call(
  39. grpc::ClientContext* context, const grpc::string& method,
  40. grpc::CompletionQueue* cq, void* tag) {
  41. return CallInternal(channel_.get(), context, method, cq, true, tag);
  42. }
  43. // setup a call to a named method
  44. std::unique_ptr<grpc::GenericClientAsyncReaderWriter> GenericStub::PrepareCall(
  45. grpc::ClientContext* context, const grpc::string& method,
  46. grpc::CompletionQueue* cq) {
  47. return CallInternal(channel_.get(), context, method, cq, false, nullptr);
  48. }
  49. // setup a unary call to a named method
  50. std::unique_ptr<grpc::GenericClientAsyncResponseReader>
  51. GenericStub::PrepareUnaryCall(grpc::ClientContext* context,
  52. const grpc::string& method,
  53. const grpc::ByteBuffer& request,
  54. grpc::CompletionQueue* cq) {
  55. return std::unique_ptr<grpc::GenericClientAsyncResponseReader>(
  56. grpc::internal::ClientAsyncResponseReaderFactory<
  57. grpc::ByteBuffer>::Create(channel_.get(), cq,
  58. grpc::internal::RpcMethod(
  59. method.c_str(),
  60. grpc::internal::RpcMethod::NORMAL_RPC),
  61. context, request, false));
  62. }
  63. void GenericStub::experimental_type::UnaryCall(
  64. grpc::ClientContext* context, const grpc::string& method,
  65. const grpc::ByteBuffer* request, grpc::ByteBuffer* response,
  66. std::function<void(grpc::Status)> on_completion) {
  67. grpc::internal::CallbackUnaryCall(
  68. stub_->channel_.get(),
  69. grpc::internal::RpcMethod(method.c_str(),
  70. grpc::internal::RpcMethod::NORMAL_RPC),
  71. context, request, response, std::move(on_completion));
  72. }
  73. void GenericStub::experimental_type::PrepareBidiStreamingCall(
  74. grpc::ClientContext* context, const grpc::string& method,
  75. grpc::experimental::ClientBidiReactor<grpc::ByteBuffer, grpc::ByteBuffer>*
  76. reactor) {
  77. grpc::internal::ClientCallbackReaderWriterFactory<
  78. grpc::ByteBuffer,
  79. grpc::ByteBuffer>::Create(stub_->channel_.get(),
  80. grpc::internal::RpcMethod(
  81. method.c_str(),
  82. grpc::internal::RpcMethod::BIDI_STREAMING),
  83. context, reactor);
  84. }
  85. } // namespace grpc_impl