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