client_unary_call.h 4.0 KB

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