GRPC C++  0.13.1-pre1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
async_unary_call.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015-2016, 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 #include <grpc/impl/codegen/log.h>
44 
45 namespace grpc {
46 
47 class CompletionQueue;
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_(new CallOpSetCollection) {
68  collection_->init_buf_.SetCollection(collection_);
69  collection_->init_buf_.SendInitialMetadata(context->send_initial_metadata_);
70  // TODO(ctiller): don't assert
71  GPR_ASSERT(collection_->init_buf_.SendMessage(request).ok());
72  collection_->init_buf_.ClientSendClose();
73  call_.PerformOps(&collection_->init_buf_);
74  }
75 
76  void ReadInitialMetadata(void* tag) {
77  GPR_ASSERT(!context_->initial_metadata_received_);
78 
79  collection_->meta_buf_.SetCollection(collection_);
80  collection_->meta_buf_.set_output_tag(tag);
81  collection_->meta_buf_.RecvInitialMetadata(context_);
82  call_.PerformOps(&collection_->meta_buf_);
83  }
84 
85  void Finish(R* msg, Status* status, void* tag) {
86  collection_->finish_buf_.SetCollection(collection_);
87  collection_->finish_buf_.set_output_tag(tag);
88  if (!context_->initial_metadata_received_) {
89  collection_->finish_buf_.RecvInitialMetadata(context_);
90  }
91  collection_->finish_buf_.RecvMessage(msg);
92  collection_->finish_buf_.ClientRecvStatus(context_, status);
93  call_.PerformOps(&collection_->finish_buf_);
94  }
95 
96  private:
97  ClientContext* context_;
98  Call call_;
99 
100  class CallOpSetCollection : public CallOpSetCollectionInterface {
101  public:
103  CallOpClientSendClose> init_buf_;
106  CallOpClientRecvStatus> finish_buf_;
107  };
108  std::shared_ptr<CallOpSetCollection> collection_;
109 };
110 
111 template <class W>
114  public:
116  : call_(nullptr, nullptr, nullptr), ctx_(ctx) {}
117 
119  GPR_ASSERT(!ctx_->sent_initial_metadata_);
120 
121  meta_buf_.set_output_tag(tag);
122  meta_buf_.SendInitialMetadata(ctx_->initial_metadata_);
123  ctx_->sent_initial_metadata_ = true;
124  call_.PerformOps(&meta_buf_);
125  }
126 
127  void Finish(const W& msg, const Status& status, void* tag) {
128  finish_buf_.set_output_tag(tag);
129  if (!ctx_->sent_initial_metadata_) {
130  finish_buf_.SendInitialMetadata(ctx_->initial_metadata_);
131  ctx_->sent_initial_metadata_ = true;
132  }
133  // The response is dropped if the status is not OK.
134  if (status.ok()) {
135  finish_buf_.ServerSendStatus(ctx_->trailing_metadata_,
136  finish_buf_.SendMessage(msg));
137  } else {
138  finish_buf_.ServerSendStatus(ctx_->trailing_metadata_, status);
139  }
140  call_.PerformOps(&finish_buf_);
141  }
142 
143  void FinishWithError(const Status& status, void* tag) {
144  GPR_ASSERT(!status.ok());
145  finish_buf_.set_output_tag(tag);
146  if (!ctx_->sent_initial_metadata_) {
147  finish_buf_.SendInitialMetadata(ctx_->initial_metadata_);
148  ctx_->sent_initial_metadata_ = true;
149  }
150  finish_buf_.ServerSendStatus(ctx_->trailing_metadata_, status);
151  call_.PerformOps(&finish_buf_);
152  }
153 
154  private:
155  void BindCall(Call* call) GRPC_OVERRIDE { call_ = *call; }
156 
157  Call call_;
158  ServerContext* ctx_;
159  CallOpSet<CallOpSendInitialMetadata> meta_buf_;
160  CallOpSet<CallOpSendInitialMetadata, CallOpSendMessage,
161  CallOpServerSendStatus> finish_buf_;
162 };
163 
164 } // namespace grpc
165 
166 #endif // GRPCXX_IMPL_CODEGEN_ASYNC_UNARY_CALL_H
void FinishWithError(const Status &status, void *tag)
Definition: async_unary_call.h:143
virtual ~ClientAsyncResponseReaderInterface()
Definition: async_unary_call.h:52
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:127
Definition: call.h:431
Definition: service_type.h:52
void SendInitialMetadata(void *tag) GRPC_OVERRIDE
Definition: async_unary_call.h:118
void Finish(R *msg, Status *status, void *tag)
Definition: async_unary_call.h:85
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:486
Definition: async_unary_call.h:58
Definition: client_context.h:152
void ReadInitialMetadata(void *tag)
Definition: async_unary_call.h:76
Definition: call.h:182
Definition: call.h:338
Definition: async_unary_call.h:50
Definition: call.h:576
Codegen interface for grpc::Channel.
Definition: channel_interface.h:64
Primary implementaiton of CallOpSetInterface.
Definition: call.h:524
Definition: server_context.h:90
A thin wrapper around grpc_completion_queue (see / src/core/surface/completion_queue.h).
Definition: completion_queue.h:81
#define GRPC_FINAL
Definition: config.h:71
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:115
Definition: rpc_method.h:43
void PerformOps(CallOpSetInterface *ops)
bool ok() const
Is the status OK?
Definition: status.h:67
Did it work? If it didn't, why?
Definition: status.h:45
Definition: async_unary_call.h:112
Definition: call.h:150
A CallOpSet that does not post completions to the completion queue.
Definition: call.h:567
#define GRPC_OVERRIDE
Definition: config.h:77