GRPC C++ 0.10.0.0

- Main Page
- Namespaces
- Data Structures
- Files

- File List
- Globals

All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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_ASYNC_UNARY_CALL_H
35 #define GRPCXX_ASYNC_UNARY_CALL_H
36
37 #include <grpc++/channel_interface.h>
38 #include <grpc++/client_context.h>
39 #include <grpc++/completion_queue.h>
40 #include <grpc++/server_context.h>
41 #include <grpc++/impl/call.h>
42 #include <grpc++/impl/service_type.h>
43 #include <grpc++/status.h>
44 #include <grpc/support/log.h>
45
46 namespace grpc {
47
48 template <class R>
49 class ClientAsyncResponseReaderInterface {
50 public:
51 virtual ~ClientAsyncResponseReaderInterface() {}
52 virtual void ReadInitialMetadata(void* tag) = 0;
53 virtual void Finish(R* msg, Status* status, void* tag) = 0;
54 };
55
56 template <class R>
57 class ClientAsyncResponseReader GRPC_FINAL
58 : public ClientAsyncResponseReaderInterface<R> {
59 public:
60 template <class W>
61 ClientAsyncResponseReader(ChannelInterface* channel, CompletionQueue* cq,
62 const RpcMethod& method, ClientContext* context,
63 const W& request)
64 : context_(context), call_(channel->CreateCall(method, context, cq)) {
65 init_buf_.SendInitialMetadata(context->send_initial_metadata_);
66 // TODO(ctiller): don't assert
67 GPR_ASSERT(init_buf_.SendMessage(request).ok());
68 init_buf_.ClientSendClose();
69 call_.PerformOps(&init_buf_);
70 }
71
72 void ReadInitialMetadata(void* tag) {
73 GPR_ASSERT(!context_->initial_metadata_received_);
74
75 meta_buf_.set_output_tag(tag);
76 meta_buf_.RecvInitialMetadata(context_);
77 call_.PerformOps(&meta_buf_);
78 }
79
80 void Finish(R* msg, Status* status, void* tag) {
81 finish_buf_.set_output_tag(tag);
82 if (!context_->initial_metadata_received_) {
83 finish_buf_.RecvInitialMetadata(context_);
84 }
85 finish_buf_.RecvMessage(msg);
86 finish_buf_.ClientRecvStatus(context_, status);
87 call_.PerformOps(&finish_buf_);
88 }
89
90 private:
91 ClientContext* context_;
92 Call call_;
93 SneakyCallOpSet<CallOpSendInitialMetadata, CallOpSendMessage,
94 CallOpClientSendClose> init_buf_;
95 CallOpSet<CallOpRecvInitialMetadata> meta_buf_;
96 CallOpSet<CallOpRecvInitialMetadata, CallOpRecvMessage<R>,
97 CallOpClientRecvStatus> finish_buf_;
98 };
99
100 template <class W>
101 class ServerAsyncResponseWriter GRPC_FINAL
102 : public ServerAsyncStreamingInterface {
103 public:
104 explicit ServerAsyncResponseWriter(ServerContext* ctx)
105 : call_(nullptr, nullptr, nullptr), ctx_(ctx) {}
106
107 void SendInitialMetadata(void* tag) GRPC_OVERRIDE {
108 GPR_ASSERT(!ctx_->sent_initial_metadata_);
109
110 meta_buf_.set_output_tag(tag);
111 meta_buf_.SendInitialMetadata(ctx_->initial_metadata_);
112 ctx_->sent_initial_metadata_ = true;
113 call_.PerformOps(&meta_buf_);
114 }
115
116 void Finish(const W& msg, const Status& status, void* tag) {
117 finish_buf_.set_output_tag(tag);
118 if (!ctx_->sent_initial_metadata_) {
119 finish_buf_.SendInitialMetadata(ctx_->initial_metadata_);
120 ctx_->sent_initial_metadata_ = true;
121 }
122 // The response is dropped if the status is not OK.
123 if (status.ok()) {
124 finish_buf_.ServerSendStatus(
125 ctx_->trailing_metadata_, finish_buf_.SendMessage(msg));
126 } else {
127 finish_buf_.ServerSendStatus(ctx_->trailing_metadata_, status);
128 }
129 call_.PerformOps(&finish_buf_);
130 }
131
132 void FinishWithError(const Status& status, void* tag) {
133 GPR_ASSERT(!status.ok());
134 finish_buf_.set_output_tag(tag);
135 if (!ctx_->sent_initial_metadata_) {
136 finish_buf_.SendInitialMetadata(ctx_->initial_metadata_);
137 ctx_->sent_initial_metadata_ = true;
138 }
139 finish_buf_.ServerSendStatus(ctx_->trailing_metadata_, status);
140 call_.PerformOps(&finish_buf_);
141 }
142
143 private:
144 void BindCall(Call* call) GRPC_OVERRIDE { call_ = *call; }
145
146 Call call_;
147 ServerContext* ctx_;
148 CallOpSet<CallOpSendInitialMetadata> meta_buf_;
149 CallOpSet<CallOpSendInitialMetadata, CallOpSendMessage,
150 CallOpServerSendStatus> finish_buf_;
151 };
152
153 } // namespace grpc
154
155 #endif // GRPCXX_ASYNC_UNARY_CALL_H
grpc::ServerAsyncResponseWriter::FinishWithError
void FinishWithError(const Status &status, void *tag)
Definition: async_unary_call.h:132
grpc::ClientAsyncResponseReaderInterface::~ClientAsyncResponseReaderInterface
virtual ~ClientAsyncResponseReaderInterface()
Definition: async_unary_call.h:51
grpc::ClientAsyncResponseReaderInterface::Finish
virtual void Finish(R *msg, Status *status, void *tag)=0
completion_queue.h
grpc::ServerAsyncResponseWriter::Finish
void Finish(const W &msg, const Status &status, void *tag)
Definition: async_unary_call.h:116
grpc::CallOpClientRecvStatus
Definition: call.h:431
grpc::ServerAsyncStreamingInterface
Definition: service_type.h:57
grpc::ServerAsyncResponseWriter::SendInitialMetadata
void SendInitialMetadata(void *tag) GRPC_OVERRIDE
Definition: async_unary_call.h:107
grpc::ClientAsyncResponseReader::Finish
void Finish(R *msg, Status *status, void *tag)
Definition: async_unary_call.h:80
grpc::ClientAsyncResponseReaderInterface::ReadInitialMetadata
virtual void ReadInitialMetadata(void *tag)=0
channel_interface.h
GRPC_FINAL
#define GRPC_FINAL
Definition: config.h:71
grpc::ClientAsyncResponseReader
Definition: async_unary_call.h:57
grpc::ClientContext
Definition: client_context.h:74
grpc::ClientAsyncResponseReader::ReadInitialMetadata
void ReadInitialMetadata(void *tag)
Definition: async_unary_call.h:72
grpc::CallOpSendMessage
Definition: call.h:190
grpc::CallOpClientSendClose
Definition: call.h:341
grpc::ClientAsyncResponseReaderInterface
Definition: async_unary_call.h:49
grpc::Call
Definition: call.h:565
grpc::ChannelInterface
Definition: channel_interface.h:52
call.h
grpc::CallOpSet
Primary implementaiton of CallOpSetInterface.
Definition: call.h:506
grpc::ServerContext
Definition: server_context.h:86
grpc::CompletionQueue
Definition: completion_queue.h:87
grpc::ClientAsyncResponseReader::ClientAsyncResponseReader
ClientAsyncResponseReader(ChannelInterface *channel, CompletionQueue *cq, const RpcMethod &method, ClientContext *context, const W &request)
Definition: async_unary_call.h:61
status.h
grpc::ServerAsyncResponseWriter::ServerAsyncResponseWriter
ServerAsyncResponseWriter(ServerContext *ctx)
Definition: async_unary_call.h:104
grpc::RpcMethod
Definition: rpc_method.h:39
client_context.h
grpc::Call::PerformOps
void PerformOps(CallOpSetInterface *ops)
Definition: call.cc:85
grpc::Status::ok
bool ok() const
Definition: status.h:55
service_type.h
grpc::Status
Definition: status.h:42
grpc::ServerAsyncResponseWriter
Definition: async_unary_call.h:101
grpc::CallOpSendInitialMetadata
Definition: call.h:159
grpc::SneakyCallOpSet
A CallOpSet that does not post completions to the completion queue.
Definition: call.h:548
GRPC_OVERRIDE
#define GRPC_OVERRIDE
Definition: config.h:77
server_context.h