GRPC C++  1.23.0
async_unary_call_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_IMPL_CODEGEN_ASYNC_UNARY_CALL_IMPL_H
20 #define GRPCPP_IMPL_CODEGEN_ASYNC_UNARY_CALL_IMPL_H
21 
22 #include <assert.h>
29 
30 namespace grpc_impl {
31 
34 template <class R>
36  public:
38 
42  virtual void StartCall() = 0;
43 
50  virtual void ReadInitialMetadata(void* tag) = 0;
51 
66  virtual void Finish(R* msg, ::grpc::Status* status, void* tag) = 0;
67 };
68 
69 namespace internal {
70 template <class R>
72  public:
79  template <class W>
82  const ::grpc::internal::RpcMethod& method,
83  ::grpc_impl::ClientContext* context, const W& request, bool start) {
84  ::grpc::internal::Call call = channel->CreateCall(method, context, cq);
86  call.call(), sizeof(ClientAsyncResponseReader<R>)))
87  ClientAsyncResponseReader<R>(call, context, request, start);
88  }
89 };
90 } // namespace internal
91 
94 template <class R>
97  public:
98  // always allocated against a call arena, no memory free required
99  static void operator delete(void* ptr, std::size_t size) {
100  assert(size == sizeof(ClientAsyncResponseReader));
101  }
102 
103  // This operator should never be called as the memory should be freed as part
104  // of the arena destruction. It only exists to provide a matching operator
105  // delete to the operator new so that some compilers will not complain (see
106  // https://github.com/grpc/grpc/issues/11301) Note at the time of adding this
107  // there are no tests catching the compiler warning.
108  static void operator delete(void*, void*) { assert(0); }
109 
110  void StartCall() override {
111  assert(!started_);
112  started_ = true;
113  StartCallInternal();
114  }
115 
122  void ReadInitialMetadata(void* tag) override {
123  assert(started_);
124  GPR_CODEGEN_ASSERT(!context_->initial_metadata_received_);
125 
126  single_buf.set_output_tag(tag);
127  single_buf.RecvInitialMetadata(context_);
128  call_.PerformOps(&single_buf);
129  initial_metadata_read_ = true;
130  }
131 
137  void Finish(R* msg, ::grpc::Status* status, void* tag) override {
138  assert(started_);
139  if (initial_metadata_read_) {
140  finish_buf.set_output_tag(tag);
141  finish_buf.RecvMessage(msg);
142  finish_buf.AllowNoMessage();
143  finish_buf.ClientRecvStatus(context_, status);
144  call_.PerformOps(&finish_buf);
145  } else {
146  single_buf.set_output_tag(tag);
147  single_buf.RecvInitialMetadata(context_);
148  single_buf.RecvMessage(msg);
149  single_buf.AllowNoMessage();
150  single_buf.ClientRecvStatus(context_, status);
151  call_.PerformOps(&single_buf);
152  }
153  }
154 
155  private:
157  ::grpc_impl::ClientContext* const context_;
159  bool started_;
160  bool initial_metadata_read_ = false;
161 
162  template <class W>
164  ::grpc_impl::ClientContext* context,
165  const W& request, bool start)
166  : context_(context), call_(call), started_(start) {
167  // Bind the metadata at time of StartCallInternal but set up the rest here
168  // TODO(ctiller): don't assert
169  GPR_CODEGEN_ASSERT(single_buf.SendMessage(request).ok());
170  single_buf.ClientSendClose();
171  if (start) StartCallInternal();
172  }
173 
174  void StartCallInternal() {
175  single_buf.SendInitialMetadata(&context_->send_initial_metadata_,
176  context_->initial_metadata_flags());
177  }
178 
179  // disable operator new
180  static void* operator new(std::size_t size);
181  static void* operator new(std::size_t size, void* p) { return p; }
182 
189  single_buf;
192  finish_buf;
193 };
194 
197 template <class W>
200  public:
202  : call_(nullptr, nullptr, nullptr), ctx_(ctx) {}
203 
211  void SendInitialMetadata(void* tag) override {
212  GPR_CODEGEN_ASSERT(!ctx_->sent_initial_metadata_);
213 
214  meta_buf_.set_output_tag(tag);
215  meta_buf_.SendInitialMetadata(&ctx_->initial_metadata_,
216  ctx_->initial_metadata_flags());
217  if (ctx_->compression_level_set()) {
218  meta_buf_.set_compression_level(ctx_->compression_level());
219  }
220  ctx_->sent_initial_metadata_ = true;
221  call_.PerformOps(&meta_buf_);
222  }
223 
239  void Finish(const W& msg, const ::grpc::Status& status, void* tag) {
240  finish_buf_.set_output_tag(tag);
241  finish_buf_.set_core_cq_tag(&finish_buf_);
242  if (!ctx_->sent_initial_metadata_) {
243  finish_buf_.SendInitialMetadata(&ctx_->initial_metadata_,
244  ctx_->initial_metadata_flags());
245  if (ctx_->compression_level_set()) {
246  finish_buf_.set_compression_level(ctx_->compression_level());
247  }
248  ctx_->sent_initial_metadata_ = true;
249  }
250  // The response is dropped if the status is not OK.
251  if (status.ok()) {
252  finish_buf_.ServerSendStatus(&ctx_->trailing_metadata_,
253  finish_buf_.SendMessage(msg));
254  } else {
255  finish_buf_.ServerSendStatus(&ctx_->trailing_metadata_, status);
256  }
257  call_.PerformOps(&finish_buf_);
258  }
259 
272  void FinishWithError(const ::grpc::Status& status, void* tag) {
273  GPR_CODEGEN_ASSERT(!status.ok());
274  finish_buf_.set_output_tag(tag);
275  if (!ctx_->sent_initial_metadata_) {
276  finish_buf_.SendInitialMetadata(&ctx_->initial_metadata_,
277  ctx_->initial_metadata_flags());
278  if (ctx_->compression_level_set()) {
279  finish_buf_.set_compression_level(ctx_->compression_level());
280  }
281  ctx_->sent_initial_metadata_ = true;
282  }
283  finish_buf_.ServerSendStatus(&ctx_->trailing_metadata_, status);
284  call_.PerformOps(&finish_buf_);
285  }
286 
287  private:
288  void BindCall(::grpc::internal::Call* call) override { call_ = *call; }
289 
293  meta_buf_;
297  finish_buf_;
298 };
299 
300 } // namespace grpc_impl
301 
302 namespace std {
303 template <class R>
304 class default_delete<::grpc_impl::ClientAsyncResponseReader<R>> {
305  public:
306  void operator()(void* p) {}
307 };
308 template <class R>
310  public:
311  void operator()(void* p) {}
312 };
313 } // namespace std
314 
315 #endif // GRPCPP_IMPL_CODEGEN_ASYNC_UNARY_CALL_IMPL_H
void operator()(void *p)
Definition: async_unary_call_impl.h:306
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:145
Definition: async_unary_call_impl.h:71
Primary implementation of CallOpSetInterface.
Definition: call_op_set.h:821
virtual void Finish(R *msg, ::grpc::Status *status, void *tag)=0
Request to receive the server&#39;s response msg and final status for the call, and to notify tag on this...
void ReadInitialMetadata(void *tag) override
See ClientAsyncResponseReaderInterface::ReadInitialMetadata for semantics.
Definition: async_unary_call_impl.h:122
Definition: async_unary_call_impl.h:302
grpc_call * call() const
Definition: call.h:72
A ServerContext allows the person implementing a service handler to:
Definition: server_context_impl.h:118
::google::protobuf::util::Status Status
Definition: config_protobuf.h:96
virtual ~ClientAsyncResponseReaderInterface()
Definition: async_unary_call_impl.h:37
void Finish(const W &msg, const ::grpc::Status &status, void *tag)
Indicate that the stream is to be finished and request notification when the server has sent the appr...
Definition: async_unary_call_impl.h:239
An interface relevant for async client side unary RPCs (which send one request message to a server an...
Definition: async_unary_call_impl.h:35
Definition: call_op_set.h:629
Definition: call_op_set.h:218
void StartCall() override
Start the call that was set up by the constructor, but only if the constructor was invoked through th...
Definition: async_unary_call_impl.h:110
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:80
Definition: call_op_set.h:696
void Finish(R *msg, ::grpc::Status *status, void *tag) override
See ClientAysncResponseReaderInterface::Finish for semantics.
Definition: async_unary_call_impl.h:137
void FinishWithError(const ::grpc::Status &status, void *tag)
Indicate that the stream is to be finished with a non-OK status, and request notification for when th...
Definition: async_unary_call_impl.h:272
Definition: call_op_set.h:288
Async API for client-side unary RPCs, where the message response received from the server is of type ...
Definition: async_unary_call_impl.h:95
Async server-side API for handling unary calls, where the single response message sent to the client ...
Definition: async_unary_call_impl.h:198
virtual void StartCall()=0
Start the call that was set up by the constructor, but only if the constructor was invoked through th...
Codegen interface for grpc::Channel.
Definition: channel_interface.h:70
CoreCodegenInterface * g_core_codegen_interface
Definition: completion_queue_impl.h:91
void SendInitialMetadata(void *tag) override
See ServerAsyncStreamingInterface::SendInitialMetadata for semantics.
Definition: async_unary_call_impl.h:211
Definition: byte_buffer.h:52
void operator()(void *p)
Definition: async_unary_call_impl.h:311
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm_impl.h:33
Definition: call_op_set.h:594
virtual void * grpc_call_arena_alloc(grpc_call *call, size_t length)=0
virtual void ReadInitialMetadata(void *tag)=0
Request notification of the reading of initial metadata.
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue.h).
Definition: completion_queue_impl.h:101
A ClientContext allows the person implementing a service client to:
Definition: client_context_impl.h:180
ServerAsyncResponseWriter(::grpc_impl::ServerContext *ctx)
Definition: async_unary_call_impl.h:201
Did it work? If it didn&#39;t, why?
Definition: status.h:31
void PerformOps(CallOpSetInterface *ops)
Definition: call.h:68
Definition: call_op_set.h:744
grpc_impl::ClientAsyncResponseReader< R > ClientAsyncResponseReader
Definition: async_unary_call.h:31
Straightforward wrapping of the C call object.
Definition: call.h:38