GRPC C++  1.8.0
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 GRPCXX_IMPL_CODEGEN_ASYNC_UNARY_CALL_H
20 #define GRPCXX_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);
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  void StartCall() override {
107  assert(!started_);
108  started_ = true;
109  StartCallInternal();
110  }
111 
118  void ReadInitialMetadata(void* tag) override {
119  assert(started_);
120  GPR_CODEGEN_ASSERT(!context_->initial_metadata_received_);
121 
122  meta_buf.set_output_tag(tag);
123  meta_buf.RecvInitialMetadata(context_);
124  call_.PerformOps(&meta_buf);
125  }
126 
132  void Finish(R* msg, Status* status, void* tag) override {
133  assert(started_);
134  finish_buf.set_output_tag(tag);
135  if (!context_->initial_metadata_received_) {
136  finish_buf.RecvInitialMetadata(context_);
137  }
138  finish_buf.RecvMessage(msg);
139  finish_buf.AllowNoMessage();
140  finish_buf.ClientRecvStatus(context_, status);
141  call_.PerformOps(&finish_buf);
142  }
143 
144  private:
146  ClientContext* const context_;
148  bool started_;
149 
150  template <class W>
152  const W& request, bool start)
153  : context_(context), call_(call), started_(start) {
154  // Bind the metadata at time of StartCallInternal but set up the rest here
155  // TODO(ctiller): don't assert
156  GPR_CODEGEN_ASSERT(init_buf.SendMessage(request).ok());
157  init_buf.ClientSendClose();
158  if (start) StartCallInternal();
159  }
160 
161  void StartCallInternal() {
162  init_buf.SendInitialMetadata(context_->send_initial_metadata_,
163  context_->initial_metadata_flags());
164  call_.PerformOps(&init_buf);
165  }
166 
167  // disable operator new
168  static void* operator new(std::size_t size);
169  static void* operator new(std::size_t size, void* p) { return p; }
170 
174  init_buf;
176  meta_buf;
180  finish_buf;
181 };
182 
185 template <class W>
188  public:
190  : call_(nullptr, nullptr, nullptr), ctx_(ctx) {}
191 
199  void SendInitialMetadata(void* tag) override {
200  GPR_CODEGEN_ASSERT(!ctx_->sent_initial_metadata_);
201 
202  meta_buf_.set_output_tag(tag);
203  meta_buf_.SendInitialMetadata(ctx_->initial_metadata_,
204  ctx_->initial_metadata_flags());
205  if (ctx_->compression_level_set()) {
206  meta_buf_.set_compression_level(ctx_->compression_level());
207  }
208  ctx_->sent_initial_metadata_ = true;
209  call_.PerformOps(&meta_buf_);
210  }
211 
227  void Finish(const W& msg, const Status& status, void* tag) {
228  finish_buf_.set_output_tag(tag);
229  if (!ctx_->sent_initial_metadata_) {
230  finish_buf_.SendInitialMetadata(ctx_->initial_metadata_,
231  ctx_->initial_metadata_flags());
232  if (ctx_->compression_level_set()) {
233  finish_buf_.set_compression_level(ctx_->compression_level());
234  }
235  ctx_->sent_initial_metadata_ = true;
236  }
237  // The response is dropped if the status is not OK.
238  if (status.ok()) {
239  finish_buf_.ServerSendStatus(ctx_->trailing_metadata_,
240  finish_buf_.SendMessage(msg));
241  } else {
242  finish_buf_.ServerSendStatus(ctx_->trailing_metadata_, status);
243  }
244  call_.PerformOps(&finish_buf_);
245  }
246 
259  void FinishWithError(const Status& status, void* tag) {
260  GPR_CODEGEN_ASSERT(!status.ok());
261  finish_buf_.set_output_tag(tag);
262  if (!ctx_->sent_initial_metadata_) {
263  finish_buf_.SendInitialMetadata(ctx_->initial_metadata_,
264  ctx_->initial_metadata_flags());
265  if (ctx_->compression_level_set()) {
266  finish_buf_.set_compression_level(ctx_->compression_level());
267  }
268  ctx_->sent_initial_metadata_ = true;
269  }
270  finish_buf_.ServerSendStatus(ctx_->trailing_metadata_, status);
271  call_.PerformOps(&finish_buf_);
272  }
273 
274  private:
275  void BindCall(::grpc::internal::Call* call) override { call_ = *call; }
276 
278  ServerContext* ctx_;
280  meta_buf_;
284  finish_buf_;
285 };
286 
287 } // namespace grpc
288 
289 namespace std {
290 template <class R>
291 class default_delete<grpc::ClientAsyncResponseReader<R>> {
292  public:
293  void operator()(void* p) {}
294 };
295 template <class R>
296 class default_delete<grpc::ClientAsyncResponseReaderInterface<R>> {
297  public:
298  void operator()(void* p) {}
299 };
300 } // namespace std
301 
302 #endif // GRPCXX_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:259
virtual ~ClientAsyncResponseReaderInterface()
Definition: async_unary_call.h:40
grpc_compression_level compression_level() const
Return the compression algorithm to be used by the server call.
Definition: server_context.h:171
virtual void Finish(R *msg, Status *status, void *tag)=0
Request to receive the server's response msg and final status for the call, and to notify tag on this...
void ClientSendClose()
Definition: call.h:467
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:227
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:135
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:106
void RecvInitialMetadata(ClientContext *context)
Definition: call.h:534
virtual void ReadInitialMetadata(void *tag)=0
Request notification of the reading of initial metadata.
void ClientRecvStatus(ClientContext *context, Status *status)
Definition: call.h:563
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:293
void SendInitialMetadata(void *tag) override
See ServerAsyncStreamingInterface::SendInitialMetadata for semantics.
Definition: async_unary_call.h:199
A CallOpSet that does not post completions to the completion queue.
Definition: call.h:674
void set_compression_level(grpc_compression_level level)
Definition: call.h:233
Status SendMessage(const M &message, WriteOptions options) GRPC_MUST_USE_RESULT
Send message using options for the write.
Definition: call.h:317
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:285
void operator()(void *p)
Definition: async_unary_call.h:298
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
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:118
Definition: byte_buffer.h:37
void SendInitialMetadata(const std::multimap< grpc::string, grpc::string > &metadata, uint32_t flags)
Definition: call.h:222
A ServerContext allows the person implementing a service handler to:
Definition: server_context.h:96
void AllowNoMessage()
Definition: call.h:348
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue.h).
Definition: completion_queue.h:94
virtual void * grpc_call_arena_alloc(grpc_call *call, size_t length)=0
grpc_call * call() const
Definition: call.h:703
ServerAsyncResponseWriter(ServerContext *ctx)
Definition: async_unary_call.h:189
void ServerSendStatus(const std::multimap< grpc::string, grpc::string > &trailing_metadata, const Status &status)
Definition: call.h:487
void RecvMessage(R *message)
Definition: call.h:345
void set_output_tag(void *return_tag)
Definition: call.h:660
bool ok() const
Is the status OK?
Definition: status.h:64
Did it work? If it didn'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:186
void Finish(R *msg, Status *status, void *tag) override
See ClientAysncResponseReaderInterface::Finish for semantics.
Definition: async_unary_call.h:132
void PerformOps(CallOpSetInterface *ops)
Definition: call.h:699
bool compression_level_set() const
Return a bool indicating whether the compression level for this call has been set (either implicitly ...
Definition: server_context.h:186
Straightforward wrapping of the C call object.
Definition: call.h:683