GRPC C++  1.13.0-dev
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 #include <assert.h>
29 
30 namespace grpc {
31 
32 class CompletionQueue;
33 extern CoreCodegenInterface* g_core_codegen_interface;
34 
37 template <class R>
39  public:
41 
45  virtual void StartCall() = 0;
46 
53  virtual void ReadInitialMetadata(void* tag) = 0;
54 
69  virtual void Finish(R* msg, Status* status, void* tag) = 0;
70 };
71 
72 namespace internal {
73 template <class R>
75  public:
82  template <class W>
84  ChannelInterface* channel, CompletionQueue* cq,
85  const ::grpc::internal::RpcMethod& method, ClientContext* context,
86  const W& request, bool start) {
87  ::grpc::internal::Call call = channel->CreateCall(method, context, cq);
88  return new (g_core_codegen_interface->grpc_call_arena_alloc(
89  call.call(), sizeof(ClientAsyncResponseReader<R>)))
90  ClientAsyncResponseReader<R>(call, context, request, start);
91  }
92 };
93 } // namespace internal
94 
97 template <class R>
100  public:
101  // always allocated against a call arena, no memory free required
102  static void operator delete(void* ptr, std::size_t size) {
103  assert(size == sizeof(ClientAsyncResponseReader));
104  }
105 
106  // This operator should never be called as the memory should be freed as part
107  // of the arena destruction. It only exists to provide a matching operator
108  // delete to the operator new so that some compilers will not complain (see
109  // https://github.com/grpc/grpc/issues/11301) Note at the time of adding this
110  // there are no tests catching the compiler warning.
111  static void operator delete(void*, void*) { assert(0); }
112 
113  void StartCall() override {
114  assert(!started_);
115  started_ = true;
116  StartCallInternal();
117  }
118 
125  void ReadInitialMetadata(void* tag) override {
126  assert(started_);
127  GPR_CODEGEN_ASSERT(!context_->initial_metadata_received_);
128 
129  single_buf.set_output_tag(tag);
130  single_buf.RecvInitialMetadata(context_);
131  call_.PerformOps(&single_buf);
132  initial_metadata_read_ = true;
133  }
134 
140  void Finish(R* msg, Status* status, void* tag) override {
141  assert(started_);
142  if (initial_metadata_read_) {
143  finish_buf.set_output_tag(tag);
144  finish_buf.RecvMessage(msg);
145  finish_buf.AllowNoMessage();
146  finish_buf.ClientRecvStatus(context_, status);
147  call_.PerformOps(&finish_buf);
148  } else {
149  single_buf.set_output_tag(tag);
150  single_buf.RecvInitialMetadata(context_);
151  single_buf.RecvMessage(msg);
152  single_buf.AllowNoMessage();
153  single_buf.ClientRecvStatus(context_, status);
154  call_.PerformOps(&single_buf);
155  }
156  }
157 
158  private:
160  ClientContext* const context_;
162  bool started_;
163  bool initial_metadata_read_ = false;
164 
165  template <class W>
167  const W& request, bool start)
168  : context_(context), call_(call), started_(start) {
169  // Bind the metadata at time of StartCallInternal but set up the rest here
170  // TODO(ctiller): don't assert
171  GPR_CODEGEN_ASSERT(single_buf.SendMessage(request).ok());
172  single_buf.ClientSendClose();
173  if (start) StartCallInternal();
174  }
175 
176  void StartCallInternal() {
177  single_buf.SendInitialMetadata(context_->send_initial_metadata_,
178  context_->initial_metadata_flags());
179  }
180 
181  // disable operator new
182  static void* operator new(std::size_t size);
183  static void* operator new(std::size_t size, void* p) { return p; }
184 
191  single_buf;
194  finish_buf;
195 };
196 
199 template <class W>
202  public:
204  : call_(nullptr, nullptr, nullptr), ctx_(ctx) {}
205 
213  void SendInitialMetadata(void* tag) override {
214  GPR_CODEGEN_ASSERT(!ctx_->sent_initial_metadata_);
215 
216  meta_buf_.set_output_tag(tag);
217  meta_buf_.SendInitialMetadata(ctx_->initial_metadata_,
218  ctx_->initial_metadata_flags());
219  if (ctx_->compression_level_set()) {
220  meta_buf_.set_compression_level(ctx_->compression_level());
221  }
222  ctx_->sent_initial_metadata_ = true;
223  call_.PerformOps(&meta_buf_);
224  }
225 
241  void Finish(const W& msg, const Status& status, void* tag) {
242  finish_buf_.set_output_tag(tag);
243  if (!ctx_->sent_initial_metadata_) {
244  finish_buf_.SendInitialMetadata(ctx_->initial_metadata_,
245  ctx_->initial_metadata_flags());
246  if (ctx_->compression_level_set()) {
247  finish_buf_.set_compression_level(ctx_->compression_level());
248  }
249  ctx_->sent_initial_metadata_ = true;
250  }
251  // The response is dropped if the status is not OK.
252  if (status.ok()) {
253  finish_buf_.ServerSendStatus(ctx_->trailing_metadata_,
254  finish_buf_.SendMessage(msg));
255  } else {
256  finish_buf_.ServerSendStatus(ctx_->trailing_metadata_, status);
257  }
258  call_.PerformOps(&finish_buf_);
259  }
260 
273  void FinishWithError(const Status& status, void* tag) {
274  GPR_CODEGEN_ASSERT(!status.ok());
275  finish_buf_.set_output_tag(tag);
276  if (!ctx_->sent_initial_metadata_) {
277  finish_buf_.SendInitialMetadata(ctx_->initial_metadata_,
278  ctx_->initial_metadata_flags());
279  if (ctx_->compression_level_set()) {
280  finish_buf_.set_compression_level(ctx_->compression_level());
281  }
282  ctx_->sent_initial_metadata_ = true;
283  }
284  finish_buf_.ServerSendStatus(ctx_->trailing_metadata_, status);
285  call_.PerformOps(&finish_buf_);
286  }
287 
288  private:
289  void BindCall(::grpc::internal::Call* call) override { call_ = *call; }
290 
292  ServerContext* ctx_;
294  meta_buf_;
298  finish_buf_;
299 };
300 
301 } // namespace grpc
302 
303 namespace std {
304 template <class R>
305 class default_delete<grpc::ClientAsyncResponseReader<R>> {
306  public:
307  void operator()(void* p) {}
308 };
309 template <class R>
310 class default_delete<grpc::ClientAsyncResponseReaderInterface<R>> {
311  public:
312  void operator()(void* p) {}
313 };
314 } // namespace std
315 
316 #endif // GRPCPP_IMPL_CODEGEN_ASYNC_UNARY_CALL_H
void FinishWithError(const 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.h:273
virtual ~ClientAsyncResponseReaderInterface()
Definition: async_unary_call.h:40
virtual void Finish(R *msg, 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...
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:138
virtual void StartCall()=0
Start the call that was set up by the constructor, but only if the constructor was invoked through th...
Definition: async_unary_call.h:74
void Finish(const W &msg, const 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.h:241
Primary implementation of CallOpSetInterface.
Definition: call.h:619
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.h:113
Definition: async_unary_call.h:303
virtual void ReadInitialMetadata(void *tag)=0
Request notification of the reading of initial metadata.
grpc_call * call() const
Definition: call.h:680
Async API for client-side unary RPCs, where the message response received from the server is of type ...
Definition: async_unary_call.h:98
A ClientContext allows the person implementing a service client to:
Definition: client_context.h:162
void operator()(void *p)
Definition: async_unary_call.h:307
void SendInitialMetadata(void *tag) override
See ServerAsyncStreamingInterface::SendInitialMetadata for semantics.
Definition: async_unary_call.h:213
An interface relevant for async client side unary RPCs (which send one request message to a server an...
Definition: async_unary_call.h:38
Definition: call.h:268
void operator()(void *p)
Definition: async_unary_call.h:312
static ClientAsyncResponseReader< R > * Create(ChannelInterface *channel, CompletionQueue *cq, const ::grpc::internal::RpcMethod &method, ClientContext *context, const W &request, bool start)
Start a call and write the request out if start is set.
Definition: async_unary_call.h:83
An Alarm posts the user provided tag to its associated completion queue upon expiry or cancellation...
Definition: alarm.h:31
Codegen interface for grpc::Channel.
Definition: channel_interface.h:55
CoreCodegenInterface * g_core_codegen_interface
Definition: call.h:46
void ReadInitialMetadata(void *tag) override
See ClientAsyncResponseReaderInterface::ReadInitialMetadata for semantics.
Definition: async_unary_call.h:125
Definition: byte_buffer.h:41
A ServerContext allows the person implementing a service handler to:
Definition: server_context.h:96
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue.h).
Definition: completion_queue.h:94
bool ok() const
Is the status OK?
Definition: status.h:118
ServerAsyncResponseWriter(ServerContext *ctx)
Definition: async_unary_call.h:203
Did it work? If it didn&#39;t, why?
Definition: status.h:31
Async server-side API for handling unary calls, where the single response message sent to the client ...
Definition: async_unary_call.h:200
void Finish(R *msg, Status *status, void *tag) override
See ClientAysncResponseReaderInterface::Finish for semantics.
Definition: async_unary_call.h:140
void PerformOps(CallOpSetInterface *ops)
Definition: call.h:676
Straightforward wrapping of the C call object.
Definition: call.h:660