GRPC C++  1.36.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 {
33 
35 
39 
43 template <class RequestType, class ResponseType>
44 class TemplatedGenericStub final {
45  public:
46  explicit TemplatedGenericStub(std::shared_ptr<grpc::ChannelInterface> channel)
47  : channel_(channel) {}
48 
53  std::unique_ptr<ClientAsyncReaderWriter<RequestType, ResponseType>>
54  PrepareCall(ClientContext* context, const std::string& method,
55  ::grpc::CompletionQueue* cq) {
56  return CallInternal(channel_.get(), context, method, cq, false, nullptr);
57  }
58 
63  std::unique_ptr<ClientAsyncResponseReader<ResponseType>> PrepareUnaryCall(
64  ClientContext* context, const std::string& method,
65  const RequestType& request, ::grpc::CompletionQueue* cq) {
66  return std::unique_ptr<ClientAsyncResponseReader<ResponseType>>(
67  internal::ClientAsyncResponseReaderHelper::Create<ResponseType>(
68  channel_.get(), cq,
69  grpc::internal::RpcMethod(method.c_str(),
71  context, request));
72  }
73 
80  std::unique_ptr<ClientAsyncReaderWriter<RequestType, ResponseType>> Call(
81  ClientContext* context, const std::string& method,
82  ::grpc::CompletionQueue* cq, void* tag) {
83  return CallInternal(channel_.get(), context, method, cq, true, tag);
84  }
85 
86 #ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL
87  void UnaryCall(ClientContext* context, const std::string& method,
90  const RequestType* request, ResponseType* response,
91  std::function<void(grpc::Status)> on_completion) {
92  UnaryCallInternal(context, method, request, response,
93  std::move(on_completion));
94  }
95 
100  void PrepareUnaryCall(ClientContext* context, const std::string& method,
101  const RequestType* request, ResponseType* response,
102  ClientUnaryReactor* reactor) {
103  PrepareUnaryCallInternal(context, method, request, response, reactor);
104  }
105 
109  void PrepareBidiStreamingCall(
110  ClientContext* context, const std::string& method,
111  ClientBidiReactor<RequestType, ResponseType>* reactor) {
112  PrepareBidiStreamingCallInternal(context, method, reactor);
113  }
114 #endif
115 
120  public:
121  explicit experimental_type(TemplatedGenericStub* stub) : stub_(stub) {}
122 
125  void UnaryCall(ClientContext* context, const std::string& method,
126  const RequestType* request, ResponseType* response,
127  std::function<void(grpc::Status)> on_completion) {
128  stub_->UnaryCallInternal(context, method, request, response,
129  std::move(on_completion));
130  }
131 
136  void PrepareUnaryCall(ClientContext* context, const std::string& method,
137  const RequestType* request, ResponseType* response,
138  ClientUnaryReactor* reactor) {
139  stub_->PrepareUnaryCallInternal(context, method, request, response,
140  reactor);
141  }
142 
147  ClientContext* context, const std::string& method,
149  stub_->PrepareBidiStreamingCallInternal(context, method, reactor);
150  }
151 
152  private:
153  TemplatedGenericStub* stub_;
154  };
155 
160 
161  private:
162  std::shared_ptr<grpc::ChannelInterface> channel_;
163 
164  void UnaryCallInternal(ClientContext* context, const std::string& method,
165  const RequestType* request, ResponseType* response,
166  std::function<void(grpc::Status)> on_completion) {
168  channel_.get(),
169  grpc::internal::RpcMethod(method.c_str(),
171  context, request, response, std::move(on_completion));
172  }
173 
174  void PrepareUnaryCallInternal(ClientContext* context,
175  const std::string& method,
176  const RequestType* request,
177  ResponseType* response,
178  ClientUnaryReactor* reactor) {
179  internal::ClientCallbackUnaryFactory::Create<RequestType, ResponseType>(
180  channel_.get(),
181  grpc::internal::RpcMethod(method.c_str(),
183  context, request, response, reactor);
184  }
185 
186  void PrepareBidiStreamingCallInternal(
187  ClientContext* context, const std::string& method,
188  ClientBidiReactor<RequestType, ResponseType>* reactor) {
190  Create(channel_.get(),
193  context, reactor);
194  }
195 
196  std::unique_ptr<ClientAsyncReaderWriter<RequestType, ResponseType>>
197  CallInternal(grpc::ChannelInterface* channel, ClientContext* context,
198  const std::string& method, ::grpc::CompletionQueue* cq,
199  bool start, void* tag) {
200  return std::unique_ptr<ClientAsyncReaderWriter<RequestType, ResponseType>>(
202  Create(
203  channel, cq,
206  context, start, tag));
207  }
208 };
209 
211 
212 } // namespace grpc
213 
214 #endif // GRPCPP_GENERIC_GENERIC_STUB_H
grpc
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm.h:33
grpc::ClientUnaryReactor
ClientUnaryReactor is a reactor-style interface for a unary RPC.
Definition: client_callback.h:431
grpc::TemplatedGenericStub::TemplatedGenericStub
TemplatedGenericStub(std::shared_ptr< grpc::ChannelInterface > channel)
Definition: generic_stub.h:46
grpc::GenericClientAsyncResponseReader
ClientAsyncResponseReader< ByteBuffer > GenericClientAsyncResponseReader
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:125
status.h
rpc_method.h
grpc::TemplatedGenericStub::experimental_type::PrepareBidiStreamingCall
void PrepareBidiStreamingCall(ClientContext *context, const std::string &method, ClientBidiReactor< RequestType, ResponseType > *reactor)
Setup a call to a named method method using context and tied to reactor .
Definition: generic_stub.h:146
grpc::experimental::ClientUnaryReactor
::grpc::ClientUnaryReactor ClientUnaryReactor
Definition: client_callback.h:1237
client_callback.h
grpc::Status
Did it work? If it didn't, why?
Definition: status.h:31
grpc::internal::RpcMethod::BIDI_STREAMING
@ BIDI_STREAMING
Definition: rpc_method.h:35
grpc::ClientContext
A ClientContext allows the person implementing a service client to:
Definition: client_context.h:192
grpc::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.h:493
grpc::ClientAsyncResponseReader
Async API for client-side unary RPCs, where the message response received from the server is of type ...
Definition: async_unary_call.h:35
grpc::GenericClientAsyncReaderWriter
ClientAsyncReaderWriter< ByteBuffer, ByteBuffer > GenericClientAsyncReaderWriter
Definition: generic_stub.h:34
grpc::ChannelInterface
Codegen interface for grpc::Channel.
Definition: channel_interface.h:71
async_unary_call.h
grpc::TemplatedGenericStub::experimental_type::PrepareUnaryCall
void PrepareUnaryCall(ClientContext *context, const std::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.h:136
grpc::internal::RpcMethod::NORMAL_RPC
@ NORMAL_RPC
Definition: rpc_method.h:32
grpc::internal::ClientCallbackReaderWriterFactory::Create
static void Create(::grpc::ChannelInterface *channel, const ::grpc::internal::RpcMethod &method, ::grpc::ClientContext *context, ClientBidiReactor< Request, Response > *reactor)
Definition: client_callback.h:700
grpc::TemplatedGenericStub::Call
std::unique_ptr< 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:80
grpc::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.
Definition: client_callback.h:47
client_context.h
grpc::TemplatedGenericStub
Generic stubs provide a type-unaware interface to call gRPC methods by name.
Definition: generic_stub.h:44
grpc::ClientAsyncReaderWriter
Async client-side interface for bi-directional streaming, where the outgoing message stream going to ...
Definition: async_stream.h:511
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:119
grpc::TemplatedGenericStub::PrepareCall
std::unique_ptr< 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:54
grpc::GenericStub
TemplatedGenericStub< grpc::ByteBuffer, grpc::ByteBuffer > GenericStub
Definition: generic_stub.h:210
grpc::TemplatedGenericStub::experimental
experimental_type experimental()
NOTE: The function experimental() is not stable public API.
Definition: generic_stub.h:159
grpc::CompletionQueue
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue....
Definition: completion_queue.h:102
grpc::ClientBidiReactor
ClientBidiReactor is the interface for a bidirectional streaming RPC.
Definition: client_callback.h:136
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:121
grpc::TemplatedGenericStub::PrepareUnaryCall
std::unique_ptr< 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:63
async_stream.h