GRPC C++  1.30.0
generic_stub_impl.h
Go to the documentation of this file.
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_GENERIC_GENERIC_STUB_IMPL_H
20 #define GRPCPP_GENERIC_GENERIC_STUB_IMPL_H
21 
22 #include <functional>
23 
24 #include <grpcpp/client_context.h>
25 #include <grpcpp/impl/rpc_method.h>
30 #include <grpcpp/support/status.h>
31 
32 namespace grpc {
33 
34 typedef ::grpc_impl::ClientAsyncReaderWriter<ByteBuffer, ByteBuffer>
36 typedef ::grpc_impl::ClientAsyncResponseReader<ByteBuffer>
38 } // namespace grpc
39 namespace grpc_impl {
40 class CompletionQueue;
41 
45 template <class RequestType, class ResponseType>
46 class TemplatedGenericStub final {
47  public:
48  explicit TemplatedGenericStub(std::shared_ptr<grpc::ChannelInterface> channel)
49  : channel_(channel) {}
50 
55  std::unique_ptr<ClientAsyncReaderWriter<RequestType, ResponseType>>
56  PrepareCall(ClientContext* context, const grpc::string& method,
57  CompletionQueue* cq) {
58  return CallInternal(channel_.get(), context, method, cq, false, nullptr);
59  }
60 
65  std::unique_ptr<ClientAsyncResponseReader<ResponseType>> PrepareUnaryCall(
66  ClientContext* context, const grpc::string& method,
67  const RequestType& request, CompletionQueue* cq) {
68  return std::unique_ptr<ClientAsyncResponseReader<ResponseType>>(
70  channel_.get(), cq,
71  grpc::internal::RpcMethod(method.c_str(),
73  context, request, false));
74  }
75 
82  std::unique_ptr<ClientAsyncReaderWriter<RequestType, ResponseType>> Call(
83  ClientContext* context, const grpc::string& method, CompletionQueue* cq,
84  void* tag) {
85  return CallInternal(channel_.get(), context, method, cq, true, tag);
86  }
87 
88 #ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL
89  void UnaryCall(ClientContext* context, const grpc::string& method,
92  const RequestType* request, ResponseType* response,
93  std::function<void(grpc::Status)> on_completion) {
94  UnaryCallInternal(context, method, request, response,
95  std::move(on_completion));
96  }
97 
102  void PrepareUnaryCall(ClientContext* context, const grpc::string& method,
103  const RequestType* request, ResponseType* response,
104  ClientUnaryReactor* reactor) {
105  PrepareUnaryCallInternal(context, method, request, response, reactor);
106  }
107 
111  void PrepareBidiStreamingCall(
112  ClientContext* context, const grpc::string& method,
113  ClientBidiReactor<RequestType, ResponseType>* reactor) {
114  PrepareBidiStreamingCallInternal(context, method, reactor);
115  }
116 #endif
117 
122  public:
123  explicit experimental_type(TemplatedGenericStub* stub) : stub_(stub) {}
124 
127  void UnaryCall(ClientContext* context, const grpc::string& method,
128  const RequestType* request, ResponseType* response,
129  std::function<void(grpc::Status)> on_completion) {
130  stub_->UnaryCallInternal(context, method, request, response,
131  std::move(on_completion));
132  }
133 
138  void PrepareUnaryCall(ClientContext* context, const grpc::string& method,
139  const RequestType* request, ResponseType* response,
140  ClientUnaryReactor* reactor) {
141  stub_->PrepareUnaryCallInternal(context, method, request, response,
142  reactor);
143  }
144 
149  ClientContext* context, const grpc::string& method,
151  stub_->PrepareBidiStreamingCallInternal(context, method, reactor);
152  }
153 
154  private:
155  TemplatedGenericStub* stub_;
156  };
157 
162 
163  private:
164  std::shared_ptr<grpc::ChannelInterface> channel_;
165 
166  void UnaryCallInternal(ClientContext* context, const grpc::string& method,
167  const RequestType* request, ResponseType* response,
168  std::function<void(grpc::Status)> on_completion) {
170  channel_.get(),
171  grpc::internal::RpcMethod(method.c_str(),
173  context, request, response, std::move(on_completion));
174  }
175 
176  void PrepareUnaryCallInternal(ClientContext* context,
177  const grpc::string& method,
178  const RequestType* request,
179  ResponseType* response,
180  ClientUnaryReactor* reactor) {
181  internal::ClientCallbackUnaryFactory::Create<RequestType, ResponseType>(
182  channel_.get(),
183  grpc::internal::RpcMethod(method.c_str(),
185  context, request, response, reactor);
186  }
187 
188  void PrepareBidiStreamingCallInternal(
189  ClientContext* context, const grpc::string& method,
190  ClientBidiReactor<RequestType, ResponseType>* reactor) {
192  Create(channel_.get(),
195  context, reactor);
196  }
197 
198  std::unique_ptr<ClientAsyncReaderWriter<RequestType, ResponseType>>
199  CallInternal(grpc::ChannelInterface* channel, ClientContext* context,
200  const grpc::string& method, CompletionQueue* cq, bool start,
201  void* tag) {
202  return std::unique_ptr<ClientAsyncReaderWriter<RequestType, ResponseType>>(
204  Create(
205  channel, cq,
208  context, start, tag));
209  }
210 };
211 
213 
214 } // namespace grpc_impl
215 
216 #endif // GRPCPP_GENERIC_GENERIC_STUB_IMPL_H
grpc_impl::TemplatedGenericStub::experimental_type::PrepareUnaryCall
void PrepareUnaryCall(ClientContext *context, const grpc::string &method, const RequestType *request, ResponseType *response, ClientUnaryReactor *reactor)
Setup a unary call to a named method method using context and specifying the request and response buf...
Definition: generic_stub_impl.h:138
grpc
This header provides an object that reads bytes directly from a grpc::ByteBuffer, via the ZeroCopyInp...
Definition: alarm.h:24
grpc::CompletionQueue
::grpc_impl::CompletionQueue CompletionQueue
Definition: completion_queue.h:26
grpc::GenericClientAsyncReaderWriter
::grpc_impl::ClientAsyncReaderWriter< ByteBuffer, ByteBuffer > GenericClientAsyncReaderWriter
Definition: generic_stub_impl.h:35
status.h
rpc_method.h
grpc_impl::ClientBidiReactor
ClientBidiReactor is the interface for a bidirectional streaming RPC.
Definition: client_callback_impl.h:108
grpc::Status
Did it work? If it didn't, why?
Definition: status.h:31
grpc_impl::TemplatedGenericStub::PrepareUnaryCall
std::unique_ptr< ClientAsyncResponseReader< ResponseType > > PrepareUnaryCall(ClientContext *context, const grpc::string &method, const RequestType &request, CompletionQueue *cq)
Setup a unary call to a named method method using context, and don't start it.
Definition: generic_stub_impl.h:65
grpc::internal::RpcMethod::BIDI_STREAMING
Definition: rpc_method.h:35
grpc_impl::TemplatedGenericStub::Call
std::unique_ptr< ClientAsyncReaderWriter< RequestType, ResponseType > > Call(ClientContext *context, const grpc::string &method, CompletionQueue *cq, void *tag)
DEPRECATED for multi-threaded use Begin a call to a named method method using context.
Definition: generic_stub_impl.h:82
async_unary_call_impl.h
grpc_impl::TemplatedGenericStub::experimental_type::experimental_type
experimental_type(TemplatedGenericStub *stub)
Definition: generic_stub_impl.h:123
grpc_impl::ClientUnaryReactor
ClientUnaryReactor is a reactor-style interface for a unary RPC.
Definition: client_callback_impl.h:402
grpc_impl::TemplatedGenericStub::experimental
experimental_type experimental()
NOTE: The function experimental() is not stable public API.
Definition: generic_stub_impl.h:161
grpc::experimental::ClientUnaryReactor
::grpc_impl::ClientUnaryReactor ClientUnaryReactor
Definition: client_callback.h:71
grpc_impl::TemplatedGenericStub
Generic stubs provide a type-unaware interface to call gRPC methods by name.
Definition: generic_stub_impl.h:46
grpc_impl::internal::ClientAsyncReaderWriterFactory::Create
static ClientAsyncReaderWriter< W, R > * Create(::grpc::ChannelInterface *channel, ::grpc_impl::CompletionQueue *cq, const ::grpc::internal::RpcMethod &method, ::grpc_impl::ClientContext *context, bool start, void *tag)
Create a stream object.
Definition: async_stream_impl.h:495
grpc::ChannelInterface
Codegen interface for grpc::Channel.
Definition: channel_interface.h:74
grpc_impl::TemplatedGenericStub::TemplatedGenericStub
TemplatedGenericStub(std::shared_ptr< grpc::ChannelInterface > channel)
Definition: generic_stub_impl.h:48
grpc::internal::RpcMethod::NORMAL_RPC
Definition: rpc_method.h:32
grpc_impl::internal::ClientAsyncResponseReaderFactory::Create
static ClientAsyncResponseReader< R > * Create(::grpc::ChannelInterface *channel, ::grpc_impl::CompletionQueue *cq, const ::grpc::internal::RpcMethod &method, ::grpc_impl::ClientContext *context, const W &request, bool start)
Start a call and write the request out if start is set.
Definition: async_unary_call_impl.h:79
client_callback_impl.h
grpc_impl::TemplatedGenericStub::experimental_type::PrepareBidiStreamingCall
void PrepareBidiStreamingCall(ClientContext *context, const grpc::string &method, ClientBidiReactor< RequestType, ResponseType > *reactor)
Setup a call to a named method method using context and tied to reactor .
Definition: generic_stub_impl.h:148
client_context.h
grpc_impl::CompletionQueue
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue....
Definition: completion_queue_impl.h:103
byte_buffer.h
grpc_impl::internal::ClientCallbackReaderWriterFactory::Create
static void Create(::grpc::ChannelInterface *channel, const ::grpc::internal::RpcMethod &method, ::grpc_impl::ClientContext *context, ClientBidiReactor< Request, Response > *reactor)
Definition: client_callback_impl.h:627
grpc_impl::TemplatedGenericStub::experimental_type::UnaryCall
void UnaryCall(ClientContext *context, const grpc::string &method, const RequestType *request, ResponseType *response, std::function< void(grpc::Status)> on_completion)
Setup and start a unary call to a named method method using context and specifying the request and re...
Definition: generic_stub_impl.h:127
grpc::string
std::string string
Definition: config.h:35
grpc_impl::GenericStub
TemplatedGenericStub< grpc::ByteBuffer, grpc::ByteBuffer > GenericStub
Definition: generic_stub_impl.h:212
grpc_impl
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm_impl.h:33
grpc::GenericClientAsyncResponseReader
::grpc_impl::ClientAsyncResponseReader< ByteBuffer > GenericClientAsyncResponseReader
Definition: generic_stub_impl.h:37
async_stream_impl.h
grpc_impl::internal::CallbackUnaryCall
void CallbackUnaryCall(::grpc::ChannelInterface *channel, const ::grpc::internal::RpcMethod &method, ::grpc_impl::ClientContext *context, const InputMessage *request, OutputMessage *result, std::function< void(::grpc::Status)> on_completion)
Perform a callback-based unary call TODO(vjpai): Combine as much as possible with the blocking unary ...
Definition: client_callback_impl.h:46
grpc_impl::ClientContext
A ClientContext allows the person implementing a service client to:
Definition: client_context_impl.h:184
grpc_impl::TemplatedGenericStub::experimental_type
NOTE: class experimental_type is not part of the public API of this class TODO(vjpai): Move these con...
Definition: generic_stub_impl.h:121
grpc::ClientContext
::grpc_impl::ClientContext ClientContext
Definition: client_context.h:26
grpc::internal::RpcMethod
Descriptor of an RPC method.
Definition: rpc_method.h:29
grpc_impl::TemplatedGenericStub::PrepareCall
std::unique_ptr< ClientAsyncReaderWriter< RequestType, ResponseType > > PrepareCall(ClientContext *context, const grpc::string &method, CompletionQueue *cq)
Setup a call to a named method method using context, but don't start it.
Definition: generic_stub_impl.h:56