GRPC C++  1.32.0
generic_stub.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_H
20 #define GRPCPP_GENERIC_GENERIC_STUB_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 
35 
36 typedef ::grpc_impl::ClientAsyncReaderWriter<ByteBuffer, ByteBuffer>
38 typedef ::grpc_impl::ClientAsyncResponseReader<ByteBuffer>
40 
44 template <class RequestType, class ResponseType>
45 class TemplatedGenericStub final {
46  public:
47  explicit TemplatedGenericStub(std::shared_ptr<grpc::ChannelInterface> channel)
48  : channel_(channel) {}
49 
54  std::unique_ptr<
56  PrepareCall(ClientContext* context, const std::string& method,
57  ::grpc::CompletionQueue* cq) {
58  return CallInternal(channel_.get(), context, method, cq, false, nullptr);
59  }
60 
65  std::unique_ptr<::grpc_impl::ClientAsyncResponseReader<ResponseType>>
66  PrepareUnaryCall(ClientContext* context, const std::string& method,
67  const RequestType& request, ::grpc::CompletionQueue* cq) {
68  return std::unique_ptr<
71  ResponseType>::Create(channel_.get(), cq,
73  method.c_str(),
75  context, request, false));
76  }
77 
84  std::unique_ptr<
86  Call(ClientContext* context, const std::string& method,
87  ::grpc::CompletionQueue* cq, void* tag) {
88  return CallInternal(channel_.get(), context, method, cq, true, tag);
89  }
90 
91 #ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL
92  void UnaryCall(ClientContext* context, const std::string& method,
95  const RequestType* request, ResponseType* response,
96  std::function<void(grpc::Status)> on_completion) {
97  UnaryCallInternal(context, method, request, response,
98  std::move(on_completion));
99  }
100 
105  void PrepareUnaryCall(ClientContext* context, const std::string& method,
106  const RequestType* request, ResponseType* response,
107  ::grpc_impl::ClientUnaryReactor* reactor) {
108  PrepareUnaryCallInternal(context, method, request, response, reactor);
109  }
110 
114  void PrepareBidiStreamingCall(
115  ClientContext* context, const std::string& method,
117  PrepareBidiStreamingCallInternal(context, method, reactor);
118  }
119 #endif
120 
125  public:
126  explicit experimental_type(TemplatedGenericStub* stub) : stub_(stub) {}
127 
130  void UnaryCall(ClientContext* context, const std::string& method,
131  const RequestType* request, ResponseType* response,
132  std::function<void(grpc::Status)> on_completion) {
133  stub_->UnaryCallInternal(context, method, request, response,
134  std::move(on_completion));
135  }
136 
141  void PrepareUnaryCall(ClientContext* context, const std::string& method,
142  const RequestType* request, ResponseType* response,
143  ::grpc_impl::ClientUnaryReactor* reactor) {
144  stub_->PrepareUnaryCallInternal(context, method, request, response,
145  reactor);
146  }
147 
152  ClientContext* context, const std::string& method,
154  stub_->PrepareBidiStreamingCallInternal(context, method, reactor);
155  }
156 
157  private:
158  TemplatedGenericStub* stub_;
159  };
160 
165 
166  private:
167  std::shared_ptr<grpc::ChannelInterface> channel_;
168 
169  void UnaryCallInternal(ClientContext* context, const std::string& method,
170  const RequestType* request, ResponseType* response,
171  std::function<void(grpc::Status)> on_completion) {
173  channel_.get(),
174  grpc::internal::RpcMethod(method.c_str(),
176  context, request, response, std::move(on_completion));
177  }
178 
179  void PrepareUnaryCallInternal(ClientContext* context,
180  const std::string& method,
181  const RequestType* request,
182  ResponseType* response,
183  ::grpc_impl::ClientUnaryReactor* reactor) {
185  ResponseType>(
186  channel_.get(),
187  grpc::internal::RpcMethod(method.c_str(),
189  context, request, response, reactor);
190  }
191 
192  void PrepareBidiStreamingCallInternal(
193  ClientContext* context, const std::string& method,
197  channel_.get(),
200  context, reactor);
201  }
202 
203  std::unique_ptr<
205  CallInternal(grpc::ChannelInterface* channel, ClientContext* context,
206  const std::string& method, ::grpc::CompletionQueue* cq,
207  bool start, void* tag) {
208  return std::unique_ptr<
212  channel, cq,
215  context, start, tag));
216  }
217 };
218 
220 
221 } // namespace grpc
222 
223 #endif // GRPCPP_GENERIC_GENERIC_STUB_H
grpc
This header provides an object that reads bytes directly from a grpc::ByteBuffer, via the ZeroCopyInp...
Definition: alarm.h:24
grpc::TemplatedGenericStub::Call
std::unique_ptr< ::grpc_impl::ClientAsyncReaderWriter< RequestType, ResponseType > > Call(ClientContext *context, const std::string &method, ::grpc::CompletionQueue *cq, void *tag)
DEPRECATED for multi-threaded use Begin a call to a named method method using context.
Definition: generic_stub.h:86
grpc::TemplatedGenericStub::TemplatedGenericStub
TemplatedGenericStub(std::shared_ptr< grpc::ChannelInterface > channel)
Definition: generic_stub.h:47
grpc::GenericClientAsyncReaderWriter
::grpc_impl::ClientAsyncReaderWriter< ByteBuffer, ByteBuffer > GenericClientAsyncReaderWriter
Definition: generic_stub.h:34
grpc::TemplatedGenericStub::experimental_type::UnaryCall
void UnaryCall(ClientContext *context, const std::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.h:130
grpc_impl::internal::CallbackUnaryCall
void CallbackUnaryCall(::grpc::ChannelInterface *channel, const ::grpc::internal::RpcMethod &method, ::grpc::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
status.h
rpc_method.h
grpc_impl::ClientBidiReactor
ClientBidiReactor is the interface for a bidirectional streaming RPC.
Definition: client_callback_impl.h:131
grpc::Status
Did it work? If it didn't, why?
Definition: status.h:31
grpc_impl::ClientAsyncResponseReader
Async API for client-side unary RPCs, where the message response received from the server is of type ...
Definition: async_unary_call_impl.h:94
grpc::internal::RpcMethod::BIDI_STREAMING
@ BIDI_STREAMING
Definition: rpc_method.h:35
async_unary_call_impl.h
grpc::ClientContext
A ClientContext allows the person implementing a service client to:
Definition: client_context.h:195
grpc_impl::ClientUnaryReactor
ClientUnaryReactor is a reactor-style interface for a unary RPC.
Definition: client_callback_impl.h:426
grpc::ChannelInterface
Codegen interface for grpc::Channel.
Definition: channel_interface.h:74
grpc::TemplatedGenericStub::PrepareCall
std::unique_ptr< ::grpc_impl::ClientAsyncReaderWriter< RequestType, ResponseType > > PrepareCall(ClientContext *context, const std::string &method, ::grpc::CompletionQueue *cq)
Setup a call to a named method method using context, but don't start it.
Definition: generic_stub.h:56
grpc::internal::RpcMethod::NORMAL_RPC
@ NORMAL_RPC
Definition: rpc_method.h:32
grpc_impl::ClientAsyncReaderWriter
Async client-side interface for bi-directional streaming, where the outgoing message stream going to ...
Definition: async_stream_impl.h:511
client_callback_impl.h
client_context.h
grpc::TemplatedGenericStub
Generic stubs provide a type-unaware interface to call gRPC methods by name.
Definition: generic_stub.h:45
byte_buffer.h
grpc::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.h:124
grpc_impl::internal::ClientAsyncResponseReaderFactory
Definition: async_unary_call_impl.h:70
grpc::GenericStub
TemplatedGenericStub< grpc::ByteBuffer, grpc::ByteBuffer > GenericStub
Definition: generic_stub.h:219
grpc::TemplatedGenericStub::experimental
experimental_type experimental()
NOTE: The function experimental() is not stable public API.
Definition: generic_stub.h:164
grpc_impl::internal::ClientCallbackUnaryFactory::Create
static void Create(::grpc::ChannelInterface *channel, const ::grpc::internal::RpcMethod &method, ::grpc::ClientContext *context, const Request *request, Response *response, ClientUnaryReactor *reactor)
Definition: client_callback_impl.h:1180
grpc::CompletionQueue
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue....
Definition: completion_queue.h:99
grpc::GenericClientAsyncResponseReader
::grpc_impl::ClientAsyncResponseReader< ByteBuffer > GenericClientAsyncResponseReader
Definition: generic_stub.h:39
grpc_impl::internal::ClientAsyncReaderWriterFactory::Create
static ClientAsyncReaderWriter< W, R > * Create(::grpc::ChannelInterface *channel, ::grpc::CompletionQueue *cq, const ::grpc::internal::RpcMethod &method, ::grpc::ClientContext *context, bool start, void *tag)
Create a stream object.
Definition: async_stream_impl.h:493
async_stream_impl.h
grpc::TemplatedGenericStub::experimental_type::PrepareBidiStreamingCall
void PrepareBidiStreamingCall(ClientContext *context, const std::string &method, ::grpc_impl::ClientBidiReactor< RequestType, ResponseType > *reactor)
Setup a call to a named method method using context and tied to reactor .
Definition: generic_stub.h:151
grpc::internal::RpcMethod
Descriptor of an RPC method.
Definition: rpc_method.h:29
grpc::TemplatedGenericStub::experimental_type::experimental_type
experimental_type(TemplatedGenericStub *stub)
Definition: generic_stub.h:126
grpc::TemplatedGenericStub::PrepareUnaryCall
std::unique_ptr<::grpc_impl::ClientAsyncResponseReader< ResponseType > > PrepareUnaryCall(ClientContext *context, const std::string &method, const RequestType &request, ::grpc::CompletionQueue *cq)
Setup a unary call to a named method method using context, and don't start it.
Definition: generic_stub.h:66
grpc_impl::internal::ClientCallbackReaderWriterFactory::Create
static void Create(::grpc::ChannelInterface *channel, const ::grpc::internal::RpcMethod &method, ::grpc::ClientContext *context, ClientBidiReactor< Request, Response > *reactor)
Definition: client_callback_impl.h:691
grpc::TemplatedGenericStub::experimental_type::PrepareUnaryCall
void PrepareUnaryCall(ClientContext *context, const std::string &method, const RequestType *request, ResponseType *response, ::grpc_impl::ClientUnaryReactor *reactor)
Setup a unary call to a named method method using context and specifying the request and response buf...
Definition: generic_stub.h:141