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/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 
28 namespace grpc {
29 
30 class Channel;
31 class ClientContext;
32 class CompletionQueue;
33 
34 namespace internal {
35 class RpcMethod;
36 /// Wrapper that performs a blocking unary call
37 template <class InputMessage, class OutputMessage>
BlockingUnaryCall(ChannelInterface * channel,const RpcMethod & method,ClientContext * context,const InputMessage & request,OutputMessage * result)38 Status BlockingUnaryCall(ChannelInterface* channel, const RpcMethod& method,
39                          ClientContext* context, const InputMessage& request,
40                          OutputMessage* result) {
41   return BlockingUnaryCallImpl<InputMessage, OutputMessage>(
42              channel, method, context, request, result)
43       .status();
44 }
45 
46 template <class InputMessage, class OutputMessage>
47 class BlockingUnaryCallImpl {
48  public:
BlockingUnaryCallImpl(ChannelInterface * channel,const RpcMethod & method,ClientContext * context,const InputMessage & request,OutputMessage * result)49   BlockingUnaryCallImpl(ChannelInterface* channel, const RpcMethod& method,
50                         ClientContext* context, const InputMessage& request,
51                         OutputMessage* result) {
52     CompletionQueue cq(grpc_completion_queue_attributes{
53         GRPC_CQ_CURRENT_VERSION, GRPC_CQ_PLUCK, GRPC_CQ_DEFAULT_POLLING,
54         nullptr});  // Pluckable completion queue
55     Call call(channel->CreateCall(method, context, &cq));
56     CallOpSet<CallOpSendInitialMetadata, CallOpSendMessage,
57               CallOpRecvInitialMetadata, CallOpRecvMessage<OutputMessage>,
58               CallOpClientSendClose, CallOpClientRecvStatus>
59         ops;
60     status_ = ops.SendMessage(request);
61     if (!status_.ok()) {
62       return;
63     }
64     ops.SendInitialMetadata(context->send_initial_metadata_,
65                             context->initial_metadata_flags());
66     ops.RecvInitialMetadata(context);
67     ops.RecvMessage(result);
68     ops.AllowNoMessage();
69     ops.ClientSendClose();
70     ops.ClientRecvStatus(context, &status_);
71     call.PerformOps(&ops);
72     if (cq.Pluck(&ops)) {
73       if (!ops.got_message && status_.ok()) {
74         status_ = Status(StatusCode::UNIMPLEMENTED,
75                          "No message returned for unary request");
76       }
77     } else {
78       GPR_CODEGEN_ASSERT(!status_.ok());
79     }
80   }
status()81   Status status() { return status_; }
82 
83  private:
84   Status status_;
85 };
86 
87 }  // namespace internal
88 }  // namespace grpc
89 
90 #endif  // GRPCPP_IMPL_CODEGEN_CLIENT_UNARY_CALL_H
91