grpcpp/impl/codegen/async_unary_call.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_IMPL_CODEGEN_ASYNC_UNARY_CALL_H
20 #define GRPCPP_IMPL_CODEGEN_ASYNC_UNARY_CALL_H
21 
22 // IWYU pragma: private, include <grpcpp/support/async_unary_call.h>
23 
32 
33 namespace grpc {
34 
35 // Forward declaration for use in Helper class
36 template <class R>
38 
41 template <class R>
43  public:
45 
49  virtual void StartCall() = 0;
50 
57  virtual void ReadInitialMetadata(void* tag) = 0;
58 
73  virtual void Finish(R* msg, grpc::Status* status, void* tag) = 0;
74 };
75 
76 namespace internal {
77 
79  public:
92  template <class R, class W, class BaseR = R, class BaseW = W>
96  const W& request) /* __attribute__((noinline)) */ {
100  call.call(), sizeof(ClientAsyncResponseReader<R>)))
102  SetupRequest<BaseR, BaseW>(
103  call.call(), &result->single_buf_, &result->read_initial_metadata_,
104  &result->finish_, static_cast<const BaseW&>(request));
105 
106  return result;
107  }
108 
109  // Various helper functions to reduce templating use
110 
111  template <class R, class W>
112  static void SetupRequest(
113  grpc_call* call,
117  read_initial_metadata,
119  void(ClientContext*, internal::Call*, bool initial_metadata_read,
121  internal::CallOpSetInterface**, void*, Status*, void*)>* finish,
122  const W& request) {
123  using SingleBufType =
130  SingleBufType* single_buf =
132  call, sizeof(SingleBufType))) SingleBufType;
133  *single_buf_ptr = single_buf;
134  // TODO(ctiller): don't assert
135  GPR_CODEGEN_ASSERT(single_buf->SendMessage(request).ok());
136  single_buf->ClientSendClose();
137 
138  // The purpose of the following functions is to type-erase the actual
139  // templated type of the CallOpSet being used by hiding that type inside the
140  // function definition rather than specifying it as an argument of the
141  // function or a member of the class. The type-erased CallOpSet will get
142  // static_cast'ed back to the real type so that it can be used properly.
143  *read_initial_metadata =
145  internal::CallOpSendInitialMetadata* single_buf_view, void* tag) {
146  auto* single_buf = static_cast<SingleBufType*>(single_buf_view);
147  single_buf->set_output_tag(tag);
148  single_buf->RecvInitialMetadata(context);
149  call->PerformOps(single_buf);
150  };
151 
152  // Note that this function goes one step further than the previous one
153  // because it type-erases the message being written down to a void*. This
154  // will be static-cast'ed back to the class specified here by hiding that
155  // class information inside the function definition. Note that this feature
156  // expects the class being specified here for R to be a base-class of the
157  // "real" R without any multiple-inheritance (as applies in protbuf wrt
158  // MessageLite)
160  bool initial_metadata_read,
161  internal::CallOpSendInitialMetadata* single_buf_view,
162  internal::CallOpSetInterface** finish_buf_ptr, void* msg,
163  Status* status, void* tag) {
164  if (initial_metadata_read) {
165  using FinishBufType =
168  FinishBufType* finish_buf =
170  call->call(), sizeof(FinishBufType))) FinishBufType;
171  *finish_buf_ptr = finish_buf;
172  finish_buf->set_output_tag(tag);
173  finish_buf->RecvMessage(static_cast<R*>(msg));
174  finish_buf->AllowNoMessage();
175  finish_buf->ClientRecvStatus(context, status);
176  call->PerformOps(finish_buf);
177  } else {
178  auto* single_buf = static_cast<SingleBufType*>(single_buf_view);
179  single_buf->set_output_tag(tag);
180  single_buf->RecvInitialMetadata(context);
181  single_buf->RecvMessage(static_cast<R*>(msg));
182  single_buf->AllowNoMessage();
183  single_buf->ClientRecvStatus(context, status);
184  call->PerformOps(single_buf);
185  }
186  };
187  }
188 
193  }
194 };
195 
196 // TODO(vjpai): This templated factory is deprecated and will be replaced by
197 //. the non-templated helper as soon as possible.
198 template <class R>
200  public:
201  template <class W>
205  const W& request, bool start) {
206  auto* result = ClientAsyncResponseReaderHelper::Create<R>(
208  if (start) {
209  result->StartCall();
210  }
211  return result;
212  }
213 };
214 
215 } // namespace internal
216 
219 template <class R>
220 class ClientAsyncResponseReader final
221  : public ClientAsyncResponseReaderInterface<R> {
222  public:
223  // always allocated against a call arena, no memory free required
224  static void operator delete(void* /*ptr*/, std::size_t size) {
226  }
227 
228  // This operator should never be called as the memory should be freed as part
229  // of the arena destruction. It only exists to provide a matching operator
230  // delete to the operator new so that some compilers will not complain (see
231  // https://github.com/grpc/grpc/issues/11301) Note at the time of adding this
232  // there are no tests catching the compiler warning.
233  static void operator delete(void*, void*) { GPR_CODEGEN_ASSERT(false); }
234 
235  void StartCall() override {
237  started_ = true;
239  }
240 
247  void ReadInitialMetadata(void* tag) override {
251  initial_metadata_read_ = true;
252  }
253 
259  void Finish(R* msg, grpc::Status* status, void* tag) override {
262  static_cast<void*>(msg), status, tag);
263  }
264 
265  private:
269  bool started_ = false;
271 
274  : context_(context), call_(call) {}
275 
276  // disable operator new
277  static void* operator new(std::size_t size);
278  static void* operator new(std::size_t /*size*/, void* p) { return p; }
279 
286  bool initial_metadata_read,
288  internal::CallOpSetInterface**, void*, Status*, void*)>
290 };
291 
294 template <class W>
297  public:
299  : call_(nullptr, nullptr, nullptr), ctx_(ctx) {}
300 
308  void SendInitialMetadata(void* tag) override {
310 
312  meta_buf_.SendInitialMetadata(&ctx_->initial_metadata_,
314  if (ctx_->compression_level_set()) {
315  meta_buf_.set_compression_level(ctx_->compression_level());
316  }
319  }
320 
340  void Finish(const W& msg, const grpc::Status& status, void* tag) {
344  finish_buf_.SendInitialMetadata(&ctx_->initial_metadata_,
346  if (ctx_->compression_level_set()) {
347  finish_buf_.set_compression_level(ctx_->compression_level());
348  }
350  }
351  // The response is dropped if the status is not OK.
352  if (status.ok()) {
353  finish_buf_.ServerSendStatus(&ctx_->trailing_metadata_,
354  finish_buf_.SendMessage(msg));
355  } else {
356  finish_buf_.ServerSendStatus(&ctx_->trailing_metadata_, status);
357  }
359  }
360 
377  void FinishWithError(const grpc::Status& status, void* tag) {
378  GPR_CODEGEN_ASSERT(!status.ok());
381  finish_buf_.SendInitialMetadata(&ctx_->initial_metadata_,
383  if (ctx_->compression_level_set()) {
384  finish_buf_.set_compression_level(ctx_->compression_level());
385  }
387  }
388  finish_buf_.ServerSendStatus(&ctx_->trailing_metadata_, status);
390  }
391 
392  private:
393  void BindCall(grpc::internal::Call* call) override { call_ = *call; }
394 
403 };
404 
405 } // namespace grpc
406 
407 namespace std {
408 template <class R>
409 class default_delete<grpc::ClientAsyncResponseReader<R>> {
410  public:
411  void operator()(void* /*p*/) {}
412 };
413 template <class R>
414 class default_delete<grpc::ClientAsyncResponseReaderInterface<R>> {
415  public:
416  void operator()(void* /*p*/) {}
417 };
418 } // namespace std
419 
420 #endif // GRPCPP_IMPL_CODEGEN_ASYNC_UNARY_CALL_H
grpc::ClientContext::send_initial_metadata_
std::multimap< std::string, std::string > send_initial_metadata_
Definition: grpcpp/impl/codegen/client_context.h:502
grpc::ClientAsyncResponseReader::started_
bool started_
Definition: grpcpp/impl/codegen/async_unary_call.h:269
grpc::internal::CallOpClientRecvStatus::ClientRecvStatus
void ClientRecvStatus(grpc::ClientContext *context, Status *status)
Definition: call_op_set.h:781
grpc::internal::CallOpSendMessage
Definition: call_op_set.h:289
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
grpc::status
auto status
Definition: cpp/client/credentials_test.cc:200
grpc::ServerContext
Definition: grpcpp/impl/codegen/server_context.h:566
ctx
Definition: benchmark-async.c:30
finish
static int finish(struct hexdump_ctx *ctx)
Definition: hexdump.c:148
grpc
Definition: grpcpp/alarm.h:33
grpc::internal::ClientAsyncResponseReaderHelper::StartCall
static void StartCall(grpc::ClientContext *context, grpc::internal::CallOpSendInitialMetadata *single_buf)
Definition: grpcpp/impl/codegen/async_unary_call.h:189
grpc::ServerContextBase::trailing_metadata_
std::multimap< std::string, std::string > trailing_metadata_
Definition: grpcpp/impl/codegen/server_context.h:476
service_type.h
grpc::ClientAsyncResponseReader::Finish
void Finish(R *msg, grpc::Status *status, void *tag) override
Definition: grpcpp/impl/codegen/async_unary_call.h:259
benchmark.request
request
Definition: benchmark.py:77
grpc::internal::CallOpSetInterface
Definition: call_op_set_interface.h:36
client_context.h
grpc::internal::CallOpSet::set_output_tag
void set_output_tag(void *return_tag)
Definition: call_op_set.h:943
grpc::internal::CallOpSet::set_core_cq_tag
void set_core_cq_tag(void *core_cq_tag)
Definition: call_op_set.h:951
grpc::CoreCodegenInterface::grpc_call_arena_alloc
virtual void * grpc_call_arena_alloc(grpc_call *call, size_t length)=0
grpc::ClientAsyncResponseReader::read_initial_metadata_
std::function< void(ClientContext *, internal::Call *, internal::CallOpSendInitialMetadata *, void *)> read_initial_metadata_
Definition: grpcpp/impl/codegen/async_unary_call.h:284
grpc::internal::CallOpSendInitialMetadata::SendInitialMetadata
void SendInitialMetadata(std::multimap< std::string, std::string > *metadata, uint32_t flags)
Definition: call_op_set.h:225
grpc::internal::ClientAsyncResponseReaderFactory::Create
static ClientAsyncResponseReader< R > * Create(grpc::ChannelInterface *channel, grpc::CompletionQueue *cq, const grpc::internal::RpcMethod &method, grpc::ClientContext *context, const W &request, bool start)
Definition: grpcpp/impl/codegen/async_unary_call.h:202
std::default_delete< grpc::ClientAsyncResponseReaderInterface< R > >::operator()
void operator()(void *)
Definition: grpcpp/impl/codegen/async_unary_call.h:416
grpc::ClientAsyncResponseReaderInterface::Finish
virtual void Finish(R *msg, grpc::Status *status, void *tag)=0
call
FilterStackCall * call
Definition: call.cc:750
grpc::ClientAsyncResponseReader::single_buf_
internal::CallOpSendInitialMetadata * single_buf_
Definition: grpcpp/impl/codegen/async_unary_call.h:280
grpc::internal::CallOpServerSendStatus
Definition: call_op_set.h:661
grpc::internal::CallOpClientRecvStatus
Definition: call_op_set.h:776
grpc::g_core_codegen_interface
CoreCodegenInterface * g_core_codegen_interface
Definition: include/grpcpp/impl/codegen/completion_queue.h:98
grpc::ClientAsyncResponseReader::finish_buf_
internal::CallOpSetInterface * finish_buf_
Definition: grpcpp/impl/codegen/async_unary_call.h:281
grpc::ServerAsyncResponseWriter::call_
grpc::internal::Call call_
Definition: grpcpp/impl/codegen/async_unary_call.h:395
start
static uint64_t start
Definition: benchmark-pound.c:74
grpc::ChannelInterface
Codegen interface for grpc::Channel.
Definition: grpcpp/impl/codegen/channel_interface.h:73
W
#define W
Definition: zlib/crc32.c:85
channel
wrapped_grpc_channel * channel
Definition: src/php/ext/grpc/call.h:33
grpc::ClientAsyncResponseReaderInterface::ReadInitialMetadata
virtual void ReadInitialMetadata(void *tag)=0
grpc::ServerContextBase::initial_metadata_flags
uint32_t initial_metadata_flags() const
Definition: grpcpp/impl/codegen/server_context.h:427
grpc::internal::RpcMethod
Descriptor of an RPC method.
Definition: grpcpp/impl/codegen/rpc_method.h:31
tag
static void * tag(intptr_t t)
Definition: bad_client.cc:318
grpc::ClientAsyncResponseReader::finish_
std::function< void(ClientContext *, internal::Call *, bool initial_metadata_read, internal::CallOpSendInitialMetadata *, internal::CallOpSetInterface **, void *, Status *, void *)> finish_
Definition: grpcpp/impl/codegen/async_unary_call.h:289
grpc::ClientAsyncResponseReader::ReadInitialMetadata
void ReadInitialMetadata(void *tag) override
Definition: grpcpp/impl/codegen/async_unary_call.h:247
grpc_call
struct grpc_call grpc_call
Definition: grpc_types.h:70
grpc::ServerContextBase::initial_metadata_
std::multimap< std::string, std::string > initial_metadata_
Definition: grpcpp/impl/codegen/server_context.h:475
grpc::internal::Call::PerformOps
void PerformOps(CallOpSetInterface *ops)
Definition: include/grpcpp/impl/codegen/call.h:67
grpc::internal::ClientAsyncResponseReaderHelper::SetupRequest
static void SetupRequest(grpc_call *call, grpc::internal::CallOpSendInitialMetadata **single_buf_ptr, std::function< void(ClientContext *, internal::Call *, internal::CallOpSendInitialMetadata *, void *)> *read_initial_metadata, std::function< void(ClientContext *, internal::Call *, bool initial_metadata_read, internal::CallOpSendInitialMetadata *, internal::CallOpSetInterface **, void *, Status *, void *)> *finish, const W &request)
Definition: grpcpp/impl/codegen/async_unary_call.h:112
grpc::ClientContext::initial_metadata_received_
bool initial_metadata_received_
Definition: grpcpp/impl/codegen/client_context.h:490
grpc::ServerAsyncResponseWriter::finish_buf_
grpc::internal::CallOpSet< grpc::internal::CallOpSendInitialMetadata, grpc::internal::CallOpSendMessage, grpc::internal::CallOpServerSendStatus > finish_buf_
Definition: grpcpp/impl/codegen/async_unary_call.h:402
grpc::internal::CallOpRecvMessage
Definition: include/grpcpp/impl/codegen/byte_buffer.h:52
grpc::ServerContext::compression_level
grpc_compression_level compression_level() const
Return the compression algorithm to be used by the server call.
Definition: grpcpp/impl/codegen/server_context.h:236
grpc::ServerAsyncResponseWriter::BindCall
void BindCall(grpc::internal::Call *call) override
Definition: grpcpp/impl/codegen/async_unary_call.h:393
grpc::ClientAsyncResponseReader::ClientAsyncResponseReader
ClientAsyncResponseReader(grpc::internal::Call call, grpc::ClientContext *context)
Definition: grpcpp/impl/codegen/async_unary_call.h:272
grpc::ClientAsyncResponseReaderInterface::StartCall
virtual void StartCall()=0
call_op_set_interface.h
grpc::internal::Call
Straightforward wrapping of the C call object.
Definition: include/grpcpp/impl/codegen/call.h:37
grpc::internal::CallOpSet
Definition: call_op_set.h:859
msg
std::string msg
Definition: client_interceptors_end2end_test.cc:372
grpc::internal::CallOpRecvInitialMetadata
Definition: call_op_set.h:728
grpc::ClientContext::initial_metadata_flags
uint32_t initial_metadata_flags() const
Definition: grpcpp/impl/codegen/client_context.h:474
grpc::ClientContext
Definition: grpcpp/impl/codegen/client_context.h:195
grpc::ServerAsyncResponseWriter::Finish
void Finish(const W &msg, const grpc::Status &status, void *tag)
Definition: grpcpp/impl/codegen/async_unary_call.h:340
grpc::ServerContextBase::sent_initial_metadata_
bool sent_initial_metadata_
Definition: grpcpp/impl/codegen/server_context.h:472
grpc::ClientAsyncResponseReaderInterface
Definition: grpcpp/impl/codegen/async_unary_call.h:42
grpc::ClientAsyncResponseReaderInterface::~ClientAsyncResponseReaderInterface
virtual ~ClientAsyncResponseReaderInterface()
Definition: grpcpp/impl/codegen/async_unary_call.h:44
status.h
grpc::internal::ServerAsyncStreamingInterface
Definition: grpcpp/impl/codegen/service_type.h:39
std::default_delete< grpc::ClientAsyncResponseReader< R > >::operator()
void operator()(void *)
Definition: grpcpp/impl/codegen/async_unary_call.h:411
grpc::internal::CallOpSendInitialMetadata
Definition: call_op_set.h:219
grpc::ServerAsyncResponseWriter::FinishWithError
void FinishWithError(const grpc::Status &status, void *tag)
Definition: grpcpp/impl/codegen/async_unary_call.h:377
grpc::ServerAsyncResponseWriter::ServerAsyncResponseWriter
ServerAsyncResponseWriter(grpc::ServerContext *ctx)
Definition: grpcpp/impl/codegen/async_unary_call.h:298
channel_interface.h
grpc::ClientAsyncResponseReader::StartCall
void StartCall() override
Definition: grpcpp/impl/codegen/async_unary_call.h:235
grpc::internal::ClientAsyncResponseReaderHelper::Create
static ClientAsyncResponseReader< R > * Create(grpc::ChannelInterface *channel, grpc::CompletionQueue *cq, const grpc::internal::RpcMethod &method, grpc::ClientContext *context, const W &request)
Definition: grpcpp/impl/codegen/async_unary_call.h:93
call_op_set.h
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
server_context.h
grpc::ClientAsyncResponseReader
Definition: grpcpp/impl/codegen/async_unary_call.h:37
grpc::ServerAsyncResponseWriter::SendInitialMetadata
void SendInitialMetadata(void *tag) override
Definition: grpcpp/impl/codegen/async_unary_call.h:308
grpc::Status
Definition: include/grpcpp/impl/codegen/status.h:35
grpc::ClientAsyncResponseReader::call_
grpc::internal::Call call_
Definition: grpcpp/impl/codegen/async_unary_call.h:268
grpc::internal::CallOpClientSendClose
Definition: call_op_set.h:626
grpc::ClientAsyncResponseReader::context_
grpc::ClientContext *const context_
Definition: grpcpp/impl/codegen/async_unary_call.h:267
GPR_CODEGEN_ASSERT
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: grpcpp/impl/codegen/core_codegen_interface.h:151
grpc::ServerAsyncResponseWriter::ctx_
grpc::ServerContext * ctx_
Definition: grpcpp/impl/codegen/async_unary_call.h:396
grpc::ServerAsyncResponseWriter::meta_buf_
grpc::internal::CallOpSet< grpc::internal::CallOpSendInitialMetadata > meta_buf_
Definition: grpcpp/impl/codegen/async_unary_call.h:398
internal
Definition: benchmark/test/output_test_helper.cc:20
context
grpc::ClientContext context
Definition: istio_echo_server_lib.cc:61
grpc::internal::ClientAsyncResponseReaderHelper
Definition: grpcpp/impl/codegen/async_unary_call.h:78
grpc::CompletionQueue
Definition: include/grpcpp/impl/codegen/completion_queue.h:104
grpc::ServerAsyncResponseWriter
Definition: grpcpp/impl/codegen/async_unary_call.h:295
grpc::ClientAsyncResponseReader::initial_metadata_read_
bool initial_metadata_read_
Definition: grpcpp/impl/codegen/async_unary_call.h:270
function
std::function< bool(GrpcTool *, int, const char **, const CliCredentials &, GrpcToolOutputCallback)> function
Definition: grpc_tool.cc:250
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
method
NSString * method
Definition: ProtoMethod.h:28
grpc::ServerContext::compression_level_set
bool compression_level_set() const
Definition: grpcpp/impl/codegen/server_context.h:251
cq
static grpc_completion_queue * cq
Definition: test/core/fling/client.cc:37
GPR_CODEGEN_DEBUG_ASSERT
#define GPR_CODEGEN_DEBUG_ASSERT(x)
Codegen specific version of GPR_DEBUG_ASSERT.
Definition: grpcpp/impl/codegen/core_codegen_interface.h:160
call.h
grpc::internal::ClientAsyncResponseReaderFactory
Definition: grpcpp/impl/codegen/async_unary_call.h:199


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:35