Imported Upstream version 1.21.0
[platform/upstream/grpc.git] / src / cpp / client / generic_stub.cc
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 #include <functional>
20
21 #include <grpcpp/generic/generic_stub.h>
22 #include <grpcpp/impl/rpc_method.h>
23 #include <grpcpp/support/client_callback.h>
24
25 namespace grpc_impl {
26
27 namespace {
28 std::unique_ptr<grpc::GenericClientAsyncReaderWriter> CallInternal(
29     grpc::ChannelInterface* channel, grpc::ClientContext* context,
30     const grpc::string& method, grpc::CompletionQueue* cq, bool start,
31     void* tag) {
32   return std::unique_ptr<grpc::GenericClientAsyncReaderWriter>(
33       grpc::internal::ClientAsyncReaderWriterFactory<grpc::ByteBuffer,
34                                                      grpc::ByteBuffer>::
35           Create(channel, cq,
36                  grpc::internal::RpcMethod(
37                      method.c_str(), grpc::internal::RpcMethod::BIDI_STREAMING),
38                  context, start, tag));
39 }
40
41 }  // namespace
42
43 // begin a call to a named method
44 std::unique_ptr<grpc::GenericClientAsyncReaderWriter> GenericStub::Call(
45     grpc::ClientContext* context, const grpc::string& method,
46     grpc::CompletionQueue* cq, void* tag) {
47   return CallInternal(channel_.get(), context, method, cq, true, tag);
48 }
49
50 // setup a call to a named method
51 std::unique_ptr<grpc::GenericClientAsyncReaderWriter> GenericStub::PrepareCall(
52     grpc::ClientContext* context, const grpc::string& method,
53     grpc::CompletionQueue* cq) {
54   return CallInternal(channel_.get(), context, method, cq, false, nullptr);
55 }
56
57 // setup a unary call to a named method
58 std::unique_ptr<grpc::GenericClientAsyncResponseReader>
59 GenericStub::PrepareUnaryCall(grpc::ClientContext* context,
60                               const grpc::string& method,
61                               const grpc::ByteBuffer& request,
62                               grpc::CompletionQueue* cq) {
63   return std::unique_ptr<grpc::GenericClientAsyncResponseReader>(
64       grpc::internal::ClientAsyncResponseReaderFactory<
65           grpc::ByteBuffer>::Create(channel_.get(), cq,
66                                     grpc::internal::RpcMethod(
67                                         method.c_str(),
68                                         grpc::internal::RpcMethod::NORMAL_RPC),
69                                     context, request, false));
70 }
71
72 void GenericStub::experimental_type::UnaryCall(
73     grpc::ClientContext* context, const grpc::string& method,
74     const grpc::ByteBuffer* request, grpc::ByteBuffer* response,
75     std::function<void(grpc::Status)> on_completion) {
76   grpc::internal::CallbackUnaryCall(
77       stub_->channel_.get(),
78       grpc::internal::RpcMethod(method.c_str(),
79                                 grpc::internal::RpcMethod::NORMAL_RPC),
80       context, request, response, std::move(on_completion));
81 }
82
83 void GenericStub::experimental_type::PrepareBidiStreamingCall(
84     grpc::ClientContext* context, const grpc::string& method,
85     grpc::experimental::ClientBidiReactor<grpc::ByteBuffer, grpc::ByteBuffer>*
86         reactor) {
87   grpc::internal::ClientCallbackReaderWriterFactory<
88       grpc::ByteBuffer,
89       grpc::ByteBuffer>::Create(stub_->channel_.get(),
90                                 grpc::internal::RpcMethod(
91                                     method.c_str(),
92                                     grpc::internal::RpcMethod::BIDI_STREAMING),
93                                 context, reactor);
94 }
95
96 }  // namespace grpc_impl