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