GRPC C++  1.31.1
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_impl {
33 class CompletionQueue;
34 } // namespace grpc_impl
35 namespace grpc {
36 
37 typedef ::grpc_impl::ClientAsyncReaderWriter<ByteBuffer, ByteBuffer>
39 typedef ::grpc_impl::ClientAsyncResponseReader<ByteBuffer>
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<
57  PrepareCall(ClientContext* context, const std::string& method,
59  return CallInternal(channel_.get(), context, method, cq, false, nullptr);
60  }
61 
66  std::unique_ptr<::grpc_impl::ClientAsyncResponseReader<ResponseType>>
67  PrepareUnaryCall(ClientContext* context, const std::string& method,
68  const RequestType& request,
70  return std::unique_ptr<
73  ResponseType>::Create(channel_.get(), cq,
75  method.c_str(),
77  context, request, false));
78  }
79 
86  std::unique_ptr<
88  Call(ClientContext* context, const std::string& method,
89  ::grpc_impl::CompletionQueue* cq, void* tag) {
90  return CallInternal(channel_.get(), context, method, cq, true, tag);
91  }
92 
93 #ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL
94  void UnaryCall(ClientContext* context, const std::string& method,
97  const RequestType* request, ResponseType* response,
98  std::function<void(grpc::Status)> on_completion) {
99  UnaryCallInternal(context, method, request, response,
100  std::move(on_completion));
101  }
102 
107  void PrepareUnaryCall(ClientContext* context, const std::string& method,
108  const RequestType* request, ResponseType* response,
109  ::grpc_impl::ClientUnaryReactor* reactor) {
110  PrepareUnaryCallInternal(context, method, request, response, reactor);
111  }
112 
116  void PrepareBidiStreamingCall(
117  ClientContext* context, const std::string& method,
119  PrepareBidiStreamingCallInternal(context, method, reactor);
120  }
121 #endif
122 
127  public:
128  explicit experimental_type(TemplatedGenericStub* stub) : stub_(stub) {}
129 
132  void UnaryCall(ClientContext* context, const std::string& method,
133  const RequestType* request, ResponseType* response,
134  std::function<void(grpc::Status)> on_completion) {
135  stub_->UnaryCallInternal(context, method, request, response,
136  std::move(on_completion));
137  }
138 
143  void PrepareUnaryCall(ClientContext* context, const std::string& method,
144  const RequestType* request, ResponseType* response,
145  ::grpc_impl::ClientUnaryReactor* reactor) {
146  stub_->PrepareUnaryCallInternal(context, method, request, response,
147  reactor);
148  }
149 
154  ClientContext* context, const std::string& method,
156  stub_->PrepareBidiStreamingCallInternal(context, method, reactor);
157  }
158 
159  private:
160  TemplatedGenericStub* stub_;
161  };
162 
167 
168  private:
169  std::shared_ptr<grpc::ChannelInterface> channel_;
170 
171  void UnaryCallInternal(ClientContext* context, const std::string& method,
172  const RequestType* request, ResponseType* response,
173  std::function<void(grpc::Status)> on_completion) {
175  channel_.get(),
176  grpc::internal::RpcMethod(method.c_str(),
178  context, request, response, std::move(on_completion));
179  }
180 
181  void PrepareUnaryCallInternal(ClientContext* context,
182  const std::string& method,
183  const RequestType* request,
184  ResponseType* response,
185  ::grpc_impl::ClientUnaryReactor* reactor) {
187  ResponseType>(
188  channel_.get(),
189  grpc::internal::RpcMethod(method.c_str(),
191  context, request, response, reactor);
192  }
193 
194  void PrepareBidiStreamingCallInternal(
195  ClientContext* context, const std::string& method,
199  channel_.get(),
202  context, reactor);
203  }
204 
205  std::unique_ptr<
207  CallInternal(grpc::ChannelInterface* channel, ClientContext* context,
208  const std::string& method, ::grpc_impl::CompletionQueue* cq,
209  bool start, void* tag) {
210  return std::unique_ptr<
214  channel, cq,
217  context, start, tag));
218  }
219 };
220 
222 
223 } // namespace grpc
224 
225 #endif // GRPCPP_GENERIC_GENERIC_STUB_H
grpc::TemplatedGenericStub::PrepareCall
std::unique_ptr< ::grpc_impl::ClientAsyncReaderWriter< RequestType, ResponseType > > PrepareCall(ClientContext *context, const std::string &method, ::grpc_impl::CompletionQueue *cq)
Setup a call to a named method method using context, but don't start it.
Definition: generic_stub.h:57
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::TemplatedGenericStub::TemplatedGenericStub
TemplatedGenericStub(std::shared_ptr< grpc::ChannelInterface > channel)
Definition: generic_stub.h:48
grpc::GenericClientAsyncReaderWriter
::grpc_impl::ClientAsyncReaderWriter< ByteBuffer, ByteBuffer > GenericClientAsyncReaderWriter
Definition: generic_stub.h:38
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:132
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::TemplatedGenericStub::Call
std::unique_ptr< ::grpc_impl::ClientAsyncReaderWriter< RequestType, ResponseType > > Call(ClientContext *context, const std::string &method, ::grpc_impl::CompletionQueue *cq, void *tag)
DEPRECATED for multi-threaded use Begin a call to a named method method using context.
Definition: generic_stub.h:88
grpc::internal::RpcMethod::BIDI_STREAMING
@ BIDI_STREAMING
Definition: rpc_method.h:35
async_unary_call_impl.h
grpc_impl::ClientUnaryReactor
ClientUnaryReactor is a reactor-style interface for a unary RPC.
Definition: client_callback_impl.h:426
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::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:513
grpc_impl::internal::ClientCallbackUnaryFactory::Create
static void Create(::grpc::ChannelInterface *channel, const ::grpc::internal::RpcMethod &method, ::grpc_impl::ClientContext *context, const Request *request, Response *response, ClientUnaryReactor *reactor)
Definition: client_callback_impl.h:1183
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:46
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:691
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:126
grpc_impl::internal::ClientAsyncResponseReaderFactory
Definition: async_unary_call_impl.h:70
grpc::GenericStub
TemplatedGenericStub< grpc::ByteBuffer, grpc::ByteBuffer > GenericStub
Definition: generic_stub.h:221
grpc::TemplatedGenericStub::experimental
experimental_type experimental()
NOTE: The function experimental() is not stable public API.
Definition: generic_stub.h:166
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.h:40
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::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:153
grpc_impl::ClientContext
A ClientContext allows the person implementing a service client to:
Definition: client_context_impl.h:184
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::TemplatedGenericStub::experimental_type::experimental_type
experimental_type(TemplatedGenericStub *stub)
Definition: generic_stub.h:128
grpc::TemplatedGenericStub::PrepareUnaryCall
std::unique_ptr<::grpc_impl::ClientAsyncResponseReader< ResponseType > > PrepareUnaryCall(ClientContext *context, const std::string &method, const RequestType &request, ::grpc_impl::CompletionQueue *cq)
Setup a unary call to a named method method using context, and don't start it.
Definition: generic_stub.h:67
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:143