generic_stub.cc 3.1 KB

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