• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
26 
27 namespace {
CallInternal(ChannelInterface * channel,ClientContext * context,const grpc::string & method,CompletionQueue * cq,bool start,void * tag)28 std::unique_ptr<GenericClientAsyncReaderWriter> CallInternal(
29     ChannelInterface* channel, ClientContext* context,
30     const grpc::string& method, CompletionQueue* cq, bool start, void* tag) {
31   return std::unique_ptr<GenericClientAsyncReaderWriter>(
32       internal::ClientAsyncReaderWriterFactory<ByteBuffer, ByteBuffer>::Create(
33           channel, cq,
34           internal::RpcMethod(method.c_str(),
35                               internal::RpcMethod::BIDI_STREAMING),
36           context, start, tag));
37 }
38 
39 }  // namespace
40 
41 // begin a call to a named method
Call(ClientContext * context,const grpc::string & method,CompletionQueue * cq,void * tag)42 std::unique_ptr<GenericClientAsyncReaderWriter> GenericStub::Call(
43     ClientContext* context, const grpc::string& method, CompletionQueue* cq,
44     void* tag) {
45   return CallInternal(channel_.get(), context, method, cq, true, tag);
46 }
47 
48 // setup a call to a named method
PrepareCall(ClientContext * context,const grpc::string & method,CompletionQueue * cq)49 std::unique_ptr<GenericClientAsyncReaderWriter> GenericStub::PrepareCall(
50     ClientContext* context, const grpc::string& method, CompletionQueue* cq) {
51   return CallInternal(channel_.get(), context, method, cq, false, nullptr);
52 }
53 
54 // setup a unary call to a named method
PrepareUnaryCall(ClientContext * context,const grpc::string & method,const ByteBuffer & request,CompletionQueue * cq)55 std::unique_ptr<GenericClientAsyncResponseReader> GenericStub::PrepareUnaryCall(
56     ClientContext* context, const grpc::string& method,
57     const ByteBuffer& request, CompletionQueue* cq) {
58   return std::unique_ptr<GenericClientAsyncResponseReader>(
59       internal::ClientAsyncResponseReaderFactory<ByteBuffer>::Create(
60           channel_.get(), cq,
61           internal::RpcMethod(method.c_str(), internal::RpcMethod::NORMAL_RPC),
62           context, request, false));
63 }
64 
UnaryCall(ClientContext * context,const grpc::string & method,const ByteBuffer * request,ByteBuffer * response,std::function<void (Status)> on_completion)65 void GenericStub::experimental_type::UnaryCall(
66     ClientContext* context, const grpc::string& method,
67     const ByteBuffer* request, ByteBuffer* response,
68     std::function<void(Status)> on_completion) {
69   internal::CallbackUnaryCall(
70       stub_->channel_.get(),
71       internal::RpcMethod(method.c_str(), internal::RpcMethod::NORMAL_RPC),
72       context, request, response, std::move(on_completion));
73 }
74 
75 }  // namespace grpc
76