GRPC C++  1.10.0
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  meta_buf.set_output_tag(tag);
130  meta_buf.RecvInitialMetadata(context_);
131  call_.PerformOps(&meta_buf);
132  }
133 
139  void Finish(R* msg, Status* status, void* tag) override {
140  assert(started_);
141  finish_buf.set_output_tag(tag);
142  if (!context_->initial_metadata_received_) {
143  finish_buf.RecvInitialMetadata(context_);
144  }
145  finish_buf.RecvMessage(msg);
146  finish_buf.AllowNoMessage();
147  finish_buf.ClientRecvStatus(context_, status);
148  call_.PerformOps(&finish_buf);
149  }
150 
151  private:
153  ClientContext* const context_;
155  bool started_;
156 
157  template <class W>
159  const W& request, bool start)
160  : context_(context), call_(call), started_(start) {
161  // Bind the metadata at time of StartCallInternal but set up the rest here
162  // TODO(ctiller): don't assert
163  GPR_CODEGEN_ASSERT(init_buf.SendMessage(request).ok());
164  init_buf.ClientSendClose();
165  if (start) StartCallInternal();
166  }
167 
168  void StartCallInternal() {
169  init_buf.SendInitialMetadata(context_->send_initial_metadata_,
170  context_->initial_metadata_flags());
171  call_.PerformOps(&init_buf);
172  }
173 
174  // disable operator new
175  static void* operator new(std::size_t size);
176  static void* operator new(std::size_t size, void* p) { return p; }
177 
181  init_buf;
183  meta_buf;
187  finish_buf;
188 };
189 
192 template <class W>
195  public:
197  : call_(nullptr, nullptr, nullptr), ctx_(ctx) {}
198 
206  void SendInitialMetadata(void* tag) override {
207  GPR_CODEGEN_ASSERT(!ctx_->sent_initial_metadata_);
208 
209  meta_buf_.set_output_tag(tag);
210  meta_buf_.SendInitialMetadata(ctx_->initial_metadata_,
211  ctx_->initial_metadata_flags());
212  if (ctx_->compression_level_set()) {
213  meta_buf_.set_compression_level(ctx_->compression_level());
214  }
215  ctx_->sent_initial_metadata_ = true;
216  call_.PerformOps(&meta_buf_);
217  }
218 
234  void Finish(const W& msg, const Status& status, void* tag) {
235  finish_buf_.set_output_tag(tag);
236  if (!ctx_->sent_initial_metadata_) {
237  finish_buf_.SendInitialMetadata(ctx_->initial_metadata_,
238  ctx_->initial_metadata_flags());
239  if (ctx_->compression_level_set()) {
240  finish_buf_.set_compression_level(ctx_->compression_level());
241  }
242  ctx_->sent_initial_metadata_ = true;
243  }
244  // The response is dropped if the status is not OK.
245  if (status.ok()) {
246  finish_buf_.ServerSendStatus(ctx_->trailing_metadata_,
247  finish_buf_.SendMessage(msg));
248  } else {
249  finish_buf_.ServerSendStatus(ctx_->trailing_metadata_, status);
250  }
251  call_.PerformOps(&finish_buf_);
252  }
253 
266  void FinishWithError(const Status& status, void* tag) {
267  GPR_CODEGEN_ASSERT(!status.ok());
268  finish_buf_.set_output_tag(tag);
269  if (!ctx_->sent_initial_metadata_) {
270  finish_buf_.SendInitialMetadata(ctx_->initial_metadata_,
271  ctx_->initial_metadata_flags());
272  if (ctx_->compression_level_set()) {
273  finish_buf_.set_compression_level(ctx_->compression_level());
274  }
275  ctx_->sent_initial_metadata_ = true;
276  }
277  finish_buf_.ServerSendStatus(ctx_->trailing_metadata_, status);
278  call_.PerformOps(&finish_buf_);
279  }
280 
281  private:
282  void BindCall(::grpc::internal::Call* call) override { call_ = *call; }
283 
285  ServerContext* ctx_;
287  meta_buf_;
291  finish_buf_;
292 };
293 
294 } // namespace grpc
295 
296 namespace std {
297 template <class R>
298 class default_delete<grpc::ClientAsyncResponseReader<R>> {
299  public:
300  void operator()(void* p) {}
301 };
302 template <class R>
303 class default_delete<grpc::ClientAsyncResponseReaderInterface<R>> {
304  public:
305  void operator()(void* p) {}
306 };
307 } // namespace std
308 
309 #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:266
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:133
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:234
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:296
virtual void ReadInitialMetadata(void *tag)=0
Request notification of the reading of initial metadata.
grpc_call * call() const
Definition: call.h:695
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:300
void SendInitialMetadata(void *tag) override
See ServerAsyncStreamingInterface::SendInitialMetadata for semantics.
Definition: async_unary_call.h:206
A CallOpSet that does not post completions to the completion queue.
Definition: call.h:666
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:305
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:37
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:64
ServerAsyncResponseWriter(ServerContext *ctx)
Definition: async_unary_call.h:196
Did it work? If it didn&#39;t, why?
Definition: status.h:30
Async server-side API for handling unary calls, where the single response message sent to the client ...
Definition: async_unary_call.h:193
void Finish(R *msg, Status *status, void *tag) override
See ClientAysncResponseReaderInterface::Finish for semantics.
Definition: async_unary_call.h:139
void PerformOps(CallOpSetInterface *ops)
Definition: call.h:691
Straightforward wrapping of the C call object.
Definition: call.h:675