client_unary_call.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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_IMPL_CODEGEN_CLIENT_UNARY_CALL_H
  19. #define GRPCPP_IMPL_CODEGEN_CLIENT_UNARY_CALL_H
  20. // IWYU pragma: private, include <grpcpp/impl/client_unary_call.h>
  21. #include <grpcpp/impl/codegen/call.h>
  22. #include <grpcpp/impl/codegen/call_op_set.h>
  23. #include <grpcpp/impl/codegen/channel_interface.h>
  24. #include <grpcpp/impl/codegen/config.h>
  25. #include <grpcpp/impl/codegen/core_codegen_interface.h>
  26. #include <grpcpp/impl/codegen/status.h>
  27. namespace grpc {
  28. class ClientContext;
  29. namespace internal {
  30. class RpcMethod;
  31. /// Wrapper that performs a blocking unary call. May optionally specify the base
  32. /// class of the Request and Response so that the internal calls and structures
  33. /// below this may be based on those base classes and thus achieve code reuse
  34. /// across different RPCs (e.g., for protobuf, MessageLite would be a base
  35. /// class).
  36. template <class InputMessage, class OutputMessage,
  37. class BaseInputMessage = InputMessage,
  38. class BaseOutputMessage = OutputMessage>
  39. Status BlockingUnaryCall(ChannelInterface* channel, const RpcMethod& method,
  40. grpc::ClientContext* context,
  41. const InputMessage& request, OutputMessage* result) {
  42. static_assert(std::is_base_of<BaseInputMessage, InputMessage>::value,
  43. "Invalid input message specification");
  44. static_assert(std::is_base_of<BaseOutputMessage, OutputMessage>::value,
  45. "Invalid output message specification");
  46. return BlockingUnaryCallImpl<BaseInputMessage, BaseOutputMessage>(
  47. channel, method, context, request, result)
  48. .status();
  49. }
  50. template <class InputMessage, class OutputMessage>
  51. class BlockingUnaryCallImpl {
  52. public:
  53. BlockingUnaryCallImpl(ChannelInterface* channel, const RpcMethod& method,
  54. grpc::ClientContext* context,
  55. const InputMessage& request, OutputMessage* result) {
  56. grpc::CompletionQueue cq(grpc_completion_queue_attributes{
  57. GRPC_CQ_CURRENT_VERSION, GRPC_CQ_PLUCK, GRPC_CQ_DEFAULT_POLLING,
  58. nullptr}); // Pluckable completion queue
  59. grpc::internal::Call call(channel->CreateCall(method, context, &cq));
  60. CallOpSet<CallOpSendInitialMetadata, CallOpSendMessage,
  61. CallOpRecvInitialMetadata, CallOpRecvMessage<OutputMessage>,
  62. CallOpClientSendClose, CallOpClientRecvStatus>
  63. ops;
  64. status_ = ops.SendMessagePtr(&request);
  65. if (!status_.ok()) {
  66. return;
  67. }
  68. ops.SendInitialMetadata(&context->send_initial_metadata_,
  69. context->initial_metadata_flags());
  70. ops.RecvInitialMetadata(context);
  71. ops.RecvMessage(result);
  72. ops.AllowNoMessage();
  73. ops.ClientSendClose();
  74. ops.ClientRecvStatus(context, &status_);
  75. call.PerformOps(&ops);
  76. cq.Pluck(&ops);
  77. // Some of the ops might fail. If the ops fail in the core layer, status
  78. // would reflect the error. But, if the ops fail in the C++ layer, the
  79. // status would still be the same as the one returned by gRPC Core. This can
  80. // happen if deserialization of the message fails.
  81. // TODO(yashykt): If deserialization fails, but the status received is OK,
  82. // then it might be a good idea to change the status to something better
  83. // than StatusCode::UNIMPLEMENTED to reflect this.
  84. if (!ops.got_message && status_.ok()) {
  85. status_ = Status(StatusCode::UNIMPLEMENTED,
  86. "No message returned for unary request");
  87. }
  88. }
  89. Status status() { return status_; }
  90. private:
  91. Status status_;
  92. };
  93. } // namespace internal
  94. } // namespace grpc
  95. #endif // GRPCPP_IMPL_CODEGEN_CLIENT_UNARY_CALL_H