GRPC C++  1.3.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, Google Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following disclaimer
14  * in the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of Google Inc. nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 
34 #ifndef GRPCXX_IMPL_CODEGEN_ASYNC_UNARY_CALL_H
35 #define GRPCXX_IMPL_CODEGEN_ASYNC_UNARY_CALL_H
36 
43 
44 namespace grpc {
45 
46 class CompletionQueue;
47 extern CoreCodegenInterface* g_core_codegen_interface;
48 
49 template <class R>
51  public:
53  virtual void ReadInitialMetadata(void* tag) = 0;
54  virtual void Finish(R* msg, Status* status, void* tag) = 0;
55 };
56 
57 template <class R>
60  public:
61  template <class W>
63  const RpcMethod& method, ClientContext* context,
64  const W& request)
65  : context_(context),
66  call_(channel->CreateCall(method, context, cq)),
67  collection_(std::make_shared<CallOpSetCollection>()) {
68  collection_->init_buf_.SetCollection(collection_);
69  collection_->init_buf_.SendInitialMetadata(
70  context->send_initial_metadata_, context->initial_metadata_flags());
71  // TODO(ctiller): don't assert
72  GPR_CODEGEN_ASSERT(collection_->init_buf_.SendMessage(request).ok());
73  collection_->init_buf_.ClientSendClose();
74  call_.PerformOps(&collection_->init_buf_);
75  }
76 
77  void ReadInitialMetadata(void* tag) {
78  GPR_CODEGEN_ASSERT(!context_->initial_metadata_received_);
79 
80  collection_->meta_buf_.SetCollection(collection_);
81  collection_->meta_buf_.set_output_tag(tag);
82  collection_->meta_buf_.RecvInitialMetadata(context_);
83  call_.PerformOps(&collection_->meta_buf_);
84  }
85 
86  void Finish(R* msg, Status* status, void* tag) {
87  collection_->finish_buf_.SetCollection(collection_);
88  collection_->finish_buf_.set_output_tag(tag);
89  if (!context_->initial_metadata_received_) {
90  collection_->finish_buf_.RecvInitialMetadata(context_);
91  }
92  collection_->finish_buf_.RecvMessage(msg);
93  collection_->finish_buf_.AllowNoMessage();
94  collection_->finish_buf_.ClientRecvStatus(context_, status);
95  call_.PerformOps(&collection_->finish_buf_);
96  }
97 
98  private:
99  ClientContext* context_;
100  Call call_;
101 
102  class CallOpSetCollection : public CallOpSetCollectionInterface {
103  public:
106  init_buf_;
110  finish_buf_;
111  };
112  std::shared_ptr<CallOpSetCollection> collection_;
113 };
114 
115 template <class W>
117  public:
119  : call_(nullptr, nullptr, nullptr), ctx_(ctx) {}
120 
121  void SendInitialMetadata(void* tag) override {
122  GPR_CODEGEN_ASSERT(!ctx_->sent_initial_metadata_);
123 
124  meta_buf_.set_output_tag(tag);
125  meta_buf_.SendInitialMetadata(ctx_->initial_metadata_,
126  ctx_->initial_metadata_flags());
127  if (ctx_->compression_level_set()) {
128  meta_buf_.set_compression_level(ctx_->compression_level());
129  }
130  ctx_->sent_initial_metadata_ = true;
131  call_.PerformOps(&meta_buf_);
132  }
133 
134  void Finish(const W& msg, const Status& status, void* tag) {
135  finish_buf_.set_output_tag(tag);
136  if (!ctx_->sent_initial_metadata_) {
137  finish_buf_.SendInitialMetadata(ctx_->initial_metadata_,
138  ctx_->initial_metadata_flags());
139  if (ctx_->compression_level_set()) {
140  finish_buf_.set_compression_level(ctx_->compression_level());
141  }
142  ctx_->sent_initial_metadata_ = true;
143  }
144  // The response is dropped if the status is not OK.
145  if (status.ok()) {
146  finish_buf_.ServerSendStatus(ctx_->trailing_metadata_,
147  finish_buf_.SendMessage(msg));
148  } else {
149  finish_buf_.ServerSendStatus(ctx_->trailing_metadata_, status);
150  }
151  call_.PerformOps(&finish_buf_);
152  }
153 
154  void FinishWithError(const Status& status, void* tag) {
155  GPR_CODEGEN_ASSERT(!status.ok());
156  finish_buf_.set_output_tag(tag);
157  if (!ctx_->sent_initial_metadata_) {
158  finish_buf_.SendInitialMetadata(ctx_->initial_metadata_,
159  ctx_->initial_metadata_flags());
160  if (ctx_->compression_level_set()) {
161  finish_buf_.set_compression_level(ctx_->compression_level());
162  }
163  ctx_->sent_initial_metadata_ = true;
164  }
165  finish_buf_.ServerSendStatus(ctx_->trailing_metadata_, status);
166  call_.PerformOps(&finish_buf_);
167  }
168 
169  private:
170  void BindCall(Call* call) override { call_ = *call; }
171 
172  Call call_;
173  ServerContext* ctx_;
174  CallOpSet<CallOpSendInitialMetadata> meta_buf_;
175  CallOpSet<CallOpSendInitialMetadata, CallOpSendMessage,
176  CallOpServerSendStatus>
177  finish_buf_;
178 };
179 
180 } // namespace grpc
181 
182 #endif // GRPCXX_IMPL_CODEGEN_ASYNC_UNARY_CALL_H
void FinishWithError(const Status &status, void *tag)
Definition: async_unary_call.h:154
virtual ~ClientAsyncResponseReaderInterface()
Definition: async_unary_call.h:52
grpc_compression_level compression_level() const
Definition: server_context.h:131
virtual void Finish(R *msg, Status *status, void *tag)=0
void Finish(const W &msg, const Status &status, void *tag)
Definition: async_unary_call.h:134
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:122
Definition: call.h:536
Definition: service_type.h:53
void Finish(R *msg, Status *status, void *tag)
Definition: async_unary_call.h:86
virtual void ReadInitialMetadata(void *tag)=0
An abstract collection of CallOpSet's, to be used whenever CallOpSet objects must be thought of as a ...
Definition: call.h:590
Definition: async_unary_call.h:58
Definition: client_context.h:154
void SendInitialMetadata(void *tag) override
Definition: async_unary_call.h:121
void ReadInitialMetadata(void *tag)
Definition: async_unary_call.h:77
Definition: call.h:268
Definition: call.h:440
Definition: async_unary_call.h:50
Definition: call.h:675
Codegen interface for grpc::Channel.
Definition: channel_interface.h:64
CoreCodegenInterface * g_core_codegen_interface
Definition: call.h:63
Primary implementaiton of CallOpSetInterface.
Definition: call.h:623
Definition: server_context.h:94
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue.h).
Definition: completion_queue.h:101
ClientAsyncResponseReader(ChannelInterface *channel, CompletionQueue *cq, const RpcMethod &method, ClientContext *context, const W &request)
Definition: async_unary_call.h:62
ServerAsyncResponseWriter(ServerContext *ctx)
Definition: async_unary_call.h:118
Definition: rpc_method.h:43
void PerformOps(CallOpSetInterface *ops)
Definition: call.h:691
bool ok() const
Is the status OK?
Definition: status.h:76
Did it work? If it didn't, why?
Definition: status.h:45
Definition: async_unary_call.h:116
Definition: call.h:217
A CallOpSet that does not post completions to the completion queue.
Definition: call.h:666
bool compression_level_set() const
Definition: server_context.h:140