Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 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 | |
Nicolas "Pixel" Noble | 1ff52d5 | 2015-03-01 05:24:36 +0100 | [diff] [blame] | 34 | #ifndef GRPCXX_STREAM_H |
| 35 | #define GRPCXX_STREAM_H |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 36 | |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 37 | #include <grpc++/channel_interface.h> |
Yang Gao | 968ca53 | 2015-02-11 16:23:47 -0800 | [diff] [blame] | 38 | #include <grpc++/client_context.h> |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 39 | #include <grpc++/completion_queue.h> |
Craig Tiller | 0156752 | 2015-02-11 21:08:49 -0800 | [diff] [blame] | 40 | #include <grpc++/server_context.h> |
Craig Tiller | 20f4af2 | 2015-02-10 09:52:15 -0800 | [diff] [blame] | 41 | #include <grpc++/impl/call.h> |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 42 | #include <grpc++/impl/service_type.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 43 | #include <grpc++/status.h> |
| 44 | #include <grpc/support/log.h> |
| 45 | |
| 46 | namespace grpc { |
| 47 | |
| 48 | // Common interface for all client side streaming. |
| 49 | class ClientStreamingInterface { |
| 50 | public: |
| 51 | virtual ~ClientStreamingInterface() {} |
| 52 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 53 | // Wait until the stream finishes, and return the final status. When the |
| 54 | // client side declares it has no more message to send, either implicitly or |
| 55 | // by calling WritesDone, it needs to make sure there is no more message to |
| 56 | // be received from the server, either implicitly or by getting a false from |
| 57 | // a Read(). Otherwise, this implicitly cancels the stream. |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 58 | virtual Status Finish() = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | // An interface that yields a sequence of R messages. |
| 62 | template <class R> |
| 63 | class ReaderInterface { |
| 64 | public: |
| 65 | virtual ~ReaderInterface() {} |
| 66 | |
| 67 | // Blocking read a message and parse to msg. Returns true on success. |
| 68 | // The method returns false when there will be no more incoming messages, |
| 69 | // either because the other side has called WritesDone or the stream has |
| 70 | // failed (or been cancelled). |
| 71 | virtual bool Read(R* msg) = 0; |
| 72 | }; |
| 73 | |
| 74 | // An interface that can be fed a sequence of W messages. |
| 75 | template <class W> |
| 76 | class WriterInterface { |
| 77 | public: |
| 78 | virtual ~WriterInterface() {} |
| 79 | |
| 80 | // Blocking write msg to the stream. Returns true on success. |
| 81 | // Returns false when the stream has been closed. |
| 82 | virtual bool Write(const W& msg) = 0; |
| 83 | }; |
| 84 | |
| 85 | template <class R> |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 86 | class ClientReader GRPC_FINAL : public ClientStreamingInterface, |
| 87 | public ReaderInterface<R> { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 88 | public: |
| 89 | // Blocking create a stream and write the first request out. |
Craig Tiller | 573523f | 2015-02-17 07:38:26 -0800 | [diff] [blame] | 90 | ClientReader(ChannelInterface* channel, const RpcMethod& method, |
Yang Gao | 7694c35 | 2015-03-03 09:48:06 -0800 | [diff] [blame] | 91 | ClientContext* context, const grpc::protobuf::Message& request) |
Yang Gao | 968ca53 | 2015-02-11 16:23:47 -0800 | [diff] [blame] | 92 | : context_(context), call_(channel->CreateCall(method, context, &cq_)) { |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 93 | CallOpBuffer buf; |
Craig Tiller | a847a8f | 2015-02-11 21:20:25 -0800 | [diff] [blame] | 94 | buf.AddSendInitialMetadata(&context->send_initial_metadata_); |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 95 | buf.AddSendMessage(request); |
| 96 | buf.AddClientSendClose(); |
Craig Tiller | de91706 | 2015-02-09 17:15:03 -0800 | [diff] [blame] | 97 | call_.PerformOps(&buf); |
| 98 | cq_.Pluck(&buf); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Yang Gao | fd7199f | 2015-02-11 23:14:49 -0800 | [diff] [blame] | 101 | // Blocking wait for initial metadata from server. The received metadata |
Yang Gao | ea222b2 | 2015-02-12 09:38:51 -0800 | [diff] [blame] | 102 | // can only be accessed after this call returns. Should only be called before |
| 103 | // the first read. Calling this method is optional, and if it is not called |
| 104 | // the metadata will be available in ClientContext after the first read. |
Yang Gao | fd7199f | 2015-02-11 23:14:49 -0800 | [diff] [blame] | 105 | void WaitForInitialMetadata() { |
Yang Gao | ea222b2 | 2015-02-12 09:38:51 -0800 | [diff] [blame] | 106 | GPR_ASSERT(!context_->initial_metadata_received_); |
| 107 | |
| 108 | CallOpBuffer buf; |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 109 | buf.AddRecvInitialMetadata(context_); |
Yang Gao | ea222b2 | 2015-02-12 09:38:51 -0800 | [diff] [blame] | 110 | call_.PerformOps(&buf); |
| 111 | GPR_ASSERT(cq_.Pluck(&buf)); |
Yang Gao | fd7199f | 2015-02-11 23:14:49 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 114 | virtual bool Read(R* msg) GRPC_OVERRIDE { |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 115 | CallOpBuffer buf; |
Yang Gao | ea222b2 | 2015-02-12 09:38:51 -0800 | [diff] [blame] | 116 | if (!context_->initial_metadata_received_) { |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 117 | buf.AddRecvInitialMetadata(context_); |
Yang Gao | ea222b2 | 2015-02-12 09:38:51 -0800 | [diff] [blame] | 118 | } |
Yang Gao | 0c7aafa | 2015-02-12 22:51:38 -0800 | [diff] [blame] | 119 | buf.AddRecvMessage(msg); |
Craig Tiller | de91706 | 2015-02-09 17:15:03 -0800 | [diff] [blame] | 120 | call_.PerformOps(&buf); |
Yang Gao | 0c7aafa | 2015-02-12 22:51:38 -0800 | [diff] [blame] | 121 | return cq_.Pluck(&buf) && buf.got_message; |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 122 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 123 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 124 | virtual Status Finish() GRPC_OVERRIDE { |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 125 | CallOpBuffer buf; |
| 126 | Status status; |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 127 | buf.AddClientRecvStatus(context_, &status); |
Craig Tiller | de91706 | 2015-02-09 17:15:03 -0800 | [diff] [blame] | 128 | call_.PerformOps(&buf); |
| 129 | GPR_ASSERT(cq_.Pluck(&buf)); |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 130 | return status; |
| 131 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 132 | |
| 133 | private: |
Yang Gao | 968ca53 | 2015-02-11 16:23:47 -0800 | [diff] [blame] | 134 | ClientContext* context_; |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 135 | CompletionQueue cq_; |
| 136 | Call call_; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | template <class W> |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 140 | class ClientWriter GRPC_FINAL : public ClientStreamingInterface, |
| 141 | public WriterInterface<W> { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 142 | public: |
| 143 | // Blocking create a stream. |
Craig Tiller | 573523f | 2015-02-17 07:38:26 -0800 | [diff] [blame] | 144 | ClientWriter(ChannelInterface* channel, const RpcMethod& method, |
Yang Gao | 7694c35 | 2015-03-03 09:48:06 -0800 | [diff] [blame] | 145 | ClientContext* context, grpc::protobuf::Message* response) |
Craig Tiller | 573523f | 2015-02-17 07:38:26 -0800 | [diff] [blame] | 146 | : context_(context), |
| 147 | response_(response), |
Craig Tiller | 0156752 | 2015-02-11 21:08:49 -0800 | [diff] [blame] | 148 | call_(channel->CreateCall(method, context, &cq_)) { |
| 149 | CallOpBuffer buf; |
| 150 | buf.AddSendInitialMetadata(&context->send_initial_metadata_); |
| 151 | call_.PerformOps(&buf); |
| 152 | cq_.Pluck(&buf); |
| 153 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 154 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 155 | virtual bool Write(const W& msg) GRPC_OVERRIDE { |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 156 | CallOpBuffer buf; |
| 157 | buf.AddSendMessage(msg); |
Craig Tiller | de91706 | 2015-02-09 17:15:03 -0800 | [diff] [blame] | 158 | call_.PerformOps(&buf); |
| 159 | return cq_.Pluck(&buf); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 162 | virtual bool WritesDone() { |
| 163 | CallOpBuffer buf; |
| 164 | buf.AddClientSendClose(); |
Craig Tiller | de91706 | 2015-02-09 17:15:03 -0800 | [diff] [blame] | 165 | call_.PerformOps(&buf); |
| 166 | return cq_.Pluck(&buf); |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 167 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 168 | |
| 169 | // Read the final response and wait for the final status. |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 170 | virtual Status Finish() GRPC_OVERRIDE { |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 171 | CallOpBuffer buf; |
| 172 | Status status; |
Yang Gao | 0c7aafa | 2015-02-12 22:51:38 -0800 | [diff] [blame] | 173 | buf.AddRecvMessage(response_); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 174 | buf.AddClientRecvStatus(context_, &status); |
Craig Tiller | de91706 | 2015-02-09 17:15:03 -0800 | [diff] [blame] | 175 | call_.PerformOps(&buf); |
Yang Gao | 0c7aafa | 2015-02-12 22:51:38 -0800 | [diff] [blame] | 176 | GPR_ASSERT(cq_.Pluck(&buf) && buf.got_message); |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 177 | return status; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | private: |
Yang Gao | 968ca53 | 2015-02-11 16:23:47 -0800 | [diff] [blame] | 181 | ClientContext* context_; |
Yang Gao | 7694c35 | 2015-03-03 09:48:06 -0800 | [diff] [blame] | 182 | grpc::protobuf::Message* const response_; |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 183 | CompletionQueue cq_; |
| 184 | Call call_; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | // Client-side interface for bi-directional streaming. |
| 188 | template <class W, class R> |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 189 | class ClientReaderWriter GRPC_FINAL : public ClientStreamingInterface, |
| 190 | public WriterInterface<W>, |
| 191 | public ReaderInterface<R> { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 192 | public: |
| 193 | // Blocking create a stream. |
Craig Tiller | 573523f | 2015-02-17 07:38:26 -0800 | [diff] [blame] | 194 | ClientReaderWriter(ChannelInterface* channel, const RpcMethod& method, |
| 195 | ClientContext* context) |
Craig Tiller | a847a8f | 2015-02-11 21:20:25 -0800 | [diff] [blame] | 196 | : context_(context), call_(channel->CreateCall(method, context, &cq_)) { |
| 197 | CallOpBuffer buf; |
| 198 | buf.AddSendInitialMetadata(&context->send_initial_metadata_); |
| 199 | call_.PerformOps(&buf); |
| 200 | GPR_ASSERT(cq_.Pluck(&buf)); |
| 201 | } |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 202 | |
Yang Gao | fd7199f | 2015-02-11 23:14:49 -0800 | [diff] [blame] | 203 | // Blocking wait for initial metadata from server. The received metadata |
Yang Gao | ea222b2 | 2015-02-12 09:38:51 -0800 | [diff] [blame] | 204 | // can only be accessed after this call returns. Should only be called before |
| 205 | // the first read. Calling this method is optional, and if it is not called |
| 206 | // the metadata will be available in ClientContext after the first read. |
Yang Gao | fd7199f | 2015-02-11 23:14:49 -0800 | [diff] [blame] | 207 | void WaitForInitialMetadata() { |
Yang Gao | ea222b2 | 2015-02-12 09:38:51 -0800 | [diff] [blame] | 208 | GPR_ASSERT(!context_->initial_metadata_received_); |
| 209 | |
| 210 | CallOpBuffer buf; |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 211 | buf.AddRecvInitialMetadata(context_); |
Yang Gao | ea222b2 | 2015-02-12 09:38:51 -0800 | [diff] [blame] | 212 | call_.PerformOps(&buf); |
| 213 | GPR_ASSERT(cq_.Pluck(&buf)); |
Yang Gao | fd7199f | 2015-02-11 23:14:49 -0800 | [diff] [blame] | 214 | } |
| 215 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 216 | virtual bool Read(R* msg) GRPC_OVERRIDE { |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 217 | CallOpBuffer buf; |
Yang Gao | ea222b2 | 2015-02-12 09:38:51 -0800 | [diff] [blame] | 218 | if (!context_->initial_metadata_received_) { |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 219 | buf.AddRecvInitialMetadata(context_); |
Yang Gao | ea222b2 | 2015-02-12 09:38:51 -0800 | [diff] [blame] | 220 | } |
Yang Gao | 0c7aafa | 2015-02-12 22:51:38 -0800 | [diff] [blame] | 221 | buf.AddRecvMessage(msg); |
Craig Tiller | de91706 | 2015-02-09 17:15:03 -0800 | [diff] [blame] | 222 | call_.PerformOps(&buf); |
Yang Gao | 0c7aafa | 2015-02-12 22:51:38 -0800 | [diff] [blame] | 223 | return cq_.Pluck(&buf) && buf.got_message; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 224 | } |
| 225 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 226 | virtual bool Write(const W& msg) GRPC_OVERRIDE { |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 227 | CallOpBuffer buf; |
| 228 | buf.AddSendMessage(msg); |
Craig Tiller | de91706 | 2015-02-09 17:15:03 -0800 | [diff] [blame] | 229 | call_.PerformOps(&buf); |
| 230 | return cq_.Pluck(&buf); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 231 | } |
| 232 | |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 233 | virtual bool WritesDone() { |
| 234 | CallOpBuffer buf; |
| 235 | buf.AddClientSendClose(); |
Craig Tiller | de91706 | 2015-02-09 17:15:03 -0800 | [diff] [blame] | 236 | call_.PerformOps(&buf); |
| 237 | return cq_.Pluck(&buf); |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 238 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 239 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 240 | virtual Status Finish() GRPC_OVERRIDE { |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 241 | CallOpBuffer buf; |
| 242 | Status status; |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 243 | buf.AddClientRecvStatus(context_, &status); |
Craig Tiller | de91706 | 2015-02-09 17:15:03 -0800 | [diff] [blame] | 244 | call_.PerformOps(&buf); |
| 245 | GPR_ASSERT(cq_.Pluck(&buf)); |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 246 | return status; |
| 247 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 248 | |
| 249 | private: |
Yang Gao | 968ca53 | 2015-02-11 16:23:47 -0800 | [diff] [blame] | 250 | ClientContext* context_; |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 251 | CompletionQueue cq_; |
| 252 | Call call_; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 253 | }; |
| 254 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 255 | template <class R> |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 256 | class ServerReader GRPC_FINAL : public ReaderInterface<R> { |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 257 | public: |
Yang Gao | b492f06 | 2015-02-11 23:43:20 -0800 | [diff] [blame] | 258 | ServerReader(Call* call, ServerContext* ctx) : call_(call), ctx_(ctx) {} |
| 259 | |
| 260 | void SendInitialMetadata() { |
Yang Gao | ea222b2 | 2015-02-12 09:38:51 -0800 | [diff] [blame] | 261 | GPR_ASSERT(!ctx_->sent_initial_metadata_); |
| 262 | |
| 263 | CallOpBuffer buf; |
| 264 | buf.AddSendInitialMetadata(&ctx_->initial_metadata_); |
| 265 | ctx_->sent_initial_metadata_ = true; |
| 266 | call_->PerformOps(&buf); |
| 267 | call_->cq()->Pluck(&buf); |
Yang Gao | b492f06 | 2015-02-11 23:43:20 -0800 | [diff] [blame] | 268 | } |
Yang Gao | 8a3bbb5 | 2015-02-09 14:10:59 -0800 | [diff] [blame] | 269 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 270 | virtual bool Read(R* msg) GRPC_OVERRIDE { |
Yang Gao | 8a3bbb5 | 2015-02-09 14:10:59 -0800 | [diff] [blame] | 271 | CallOpBuffer buf; |
Yang Gao | 0c7aafa | 2015-02-12 22:51:38 -0800 | [diff] [blame] | 272 | buf.AddRecvMessage(msg); |
Craig Tiller | de91706 | 2015-02-09 17:15:03 -0800 | [diff] [blame] | 273 | call_->PerformOps(&buf); |
Yang Gao | 0c7aafa | 2015-02-12 22:51:38 -0800 | [diff] [blame] | 274 | return call_->cq()->Pluck(&buf) && buf.got_message; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 275 | } |
| 276 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 277 | private: |
Craig Tiller | 0156752 | 2015-02-11 21:08:49 -0800 | [diff] [blame] | 278 | Call* const call_; |
| 279 | ServerContext* const ctx_; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 280 | }; |
| 281 | |
| 282 | template <class W> |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 283 | class ServerWriter GRPC_FINAL : public WriterInterface<W> { |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 284 | public: |
Yang Gao | b492f06 | 2015-02-11 23:43:20 -0800 | [diff] [blame] | 285 | ServerWriter(Call* call, ServerContext* ctx) : call_(call), ctx_(ctx) {} |
| 286 | |
| 287 | void SendInitialMetadata() { |
Yang Gao | ea222b2 | 2015-02-12 09:38:51 -0800 | [diff] [blame] | 288 | GPR_ASSERT(!ctx_->sent_initial_metadata_); |
| 289 | |
| 290 | CallOpBuffer buf; |
| 291 | buf.AddSendInitialMetadata(&ctx_->initial_metadata_); |
| 292 | ctx_->sent_initial_metadata_ = true; |
| 293 | call_->PerformOps(&buf); |
| 294 | call_->cq()->Pluck(&buf); |
Yang Gao | b492f06 | 2015-02-11 23:43:20 -0800 | [diff] [blame] | 295 | } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 296 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 297 | virtual bool Write(const W& msg) GRPC_OVERRIDE { |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 298 | CallOpBuffer buf; |
Yang Gao | ea222b2 | 2015-02-12 09:38:51 -0800 | [diff] [blame] | 299 | if (!ctx_->sent_initial_metadata_) { |
| 300 | buf.AddSendInitialMetadata(&ctx_->initial_metadata_); |
| 301 | ctx_->sent_initial_metadata_ = true; |
| 302 | } |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 303 | buf.AddSendMessage(msg); |
Craig Tiller | de91706 | 2015-02-09 17:15:03 -0800 | [diff] [blame] | 304 | call_->PerformOps(&buf); |
| 305 | return call_->cq()->Pluck(&buf); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | private: |
Craig Tiller | 0156752 | 2015-02-11 21:08:49 -0800 | [diff] [blame] | 309 | Call* const call_; |
| 310 | ServerContext* const ctx_; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 311 | }; |
| 312 | |
| 313 | // Server-side interface for bi-directional streaming. |
| 314 | template <class W, class R> |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 315 | class ServerReaderWriter GRPC_FINAL : public WriterInterface<W>, |
| 316 | public ReaderInterface<R> { |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 317 | public: |
Yang Gao | b492f06 | 2015-02-11 23:43:20 -0800 | [diff] [blame] | 318 | ServerReaderWriter(Call* call, ServerContext* ctx) : call_(call), ctx_(ctx) {} |
| 319 | |
| 320 | void SendInitialMetadata() { |
Yang Gao | ea222b2 | 2015-02-12 09:38:51 -0800 | [diff] [blame] | 321 | GPR_ASSERT(!ctx_->sent_initial_metadata_); |
| 322 | |
| 323 | CallOpBuffer buf; |
| 324 | buf.AddSendInitialMetadata(&ctx_->initial_metadata_); |
| 325 | ctx_->sent_initial_metadata_ = true; |
| 326 | call_->PerformOps(&buf); |
| 327 | call_->cq()->Pluck(&buf); |
Yang Gao | b492f06 | 2015-02-11 23:43:20 -0800 | [diff] [blame] | 328 | } |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 329 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 330 | virtual bool Read(R* msg) GRPC_OVERRIDE { |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 331 | CallOpBuffer buf; |
Yang Gao | 0c7aafa | 2015-02-12 22:51:38 -0800 | [diff] [blame] | 332 | buf.AddRecvMessage(msg); |
Craig Tiller | de91706 | 2015-02-09 17:15:03 -0800 | [diff] [blame] | 333 | call_->PerformOps(&buf); |
Yang Gao | 0c7aafa | 2015-02-12 22:51:38 -0800 | [diff] [blame] | 334 | return call_->cq()->Pluck(&buf) && buf.got_message; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 335 | } |
| 336 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 337 | virtual bool Write(const W& msg) GRPC_OVERRIDE { |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 338 | CallOpBuffer buf; |
Yang Gao | ea222b2 | 2015-02-12 09:38:51 -0800 | [diff] [blame] | 339 | if (!ctx_->sent_initial_metadata_) { |
| 340 | buf.AddSendInitialMetadata(&ctx_->initial_metadata_); |
| 341 | ctx_->sent_initial_metadata_ = true; |
| 342 | } |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 343 | buf.AddSendMessage(msg); |
Craig Tiller | de91706 | 2015-02-09 17:15:03 -0800 | [diff] [blame] | 344 | call_->PerformOps(&buf); |
| 345 | return call_->cq()->Pluck(&buf); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | private: |
Craig Tiller | 0156752 | 2015-02-11 21:08:49 -0800 | [diff] [blame] | 349 | Call* const call_; |
| 350 | ServerContext* const ctx_; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 351 | }; |
| 352 | |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 353 | // Async interfaces |
| 354 | // Common interface for all client side streaming. |
| 355 | class ClientAsyncStreamingInterface { |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 356 | public: |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 357 | virtual ~ClientAsyncStreamingInterface() {} |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 358 | |
Yang Gao | 424bc92 | 2015-02-12 10:24:39 -0800 | [diff] [blame] | 359 | virtual void ReadInitialMetadata(void* tag) = 0; |
| 360 | |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 361 | virtual void Finish(Status* status, void* tag) = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 362 | }; |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 363 | |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 364 | // An interface that yields a sequence of R messages. |
| 365 | template <class R> |
| 366 | class AsyncReaderInterface { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 367 | public: |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 368 | virtual ~AsyncReaderInterface() {} |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 369 | |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 370 | virtual void Read(R* msg, void* tag) = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 371 | }; |
| 372 | |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 373 | // An interface that can be fed a sequence of W messages. |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 374 | template <class W> |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 375 | class AsyncWriterInterface { |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 376 | public: |
Craig Tiller | 5319164 | 2015-02-09 16:15:23 -0800 | [diff] [blame] | 377 | virtual ~AsyncWriterInterface() {} |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 378 | |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 379 | virtual void Write(const W& msg, void* tag) = 0; |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 380 | }; |
| 381 | |
| 382 | template <class R> |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 383 | class ClientAsyncReader GRPC_FINAL : public ClientAsyncStreamingInterface, |
| 384 | public AsyncReaderInterface<R> { |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 385 | public: |
Yang Gao | 424bc92 | 2015-02-12 10:24:39 -0800 | [diff] [blame] | 386 | // Create a stream and write the first request out. |
Craig Tiller | 573523f | 2015-02-17 07:38:26 -0800 | [diff] [blame] | 387 | ClientAsyncReader(ChannelInterface* channel, CompletionQueue* cq, |
| 388 | const RpcMethod& method, ClientContext* context, |
Yang Gao | 7694c35 | 2015-03-03 09:48:06 -0800 | [diff] [blame] | 389 | const grpc::protobuf::Message& request, void* tag) |
Yang Gao | 5705fe3 | 2015-02-12 14:30:34 -0800 | [diff] [blame] | 390 | : context_(context), call_(channel->CreateCall(method, context, cq)) { |
Craig Tiller | 549a74d | 2015-02-09 22:13:44 -0800 | [diff] [blame] | 391 | init_buf_.Reset(tag); |
Yang Gao | 424bc92 | 2015-02-12 10:24:39 -0800 | [diff] [blame] | 392 | init_buf_.AddSendInitialMetadata(&context->send_initial_metadata_); |
Craig Tiller | 549a74d | 2015-02-09 22:13:44 -0800 | [diff] [blame] | 393 | init_buf_.AddSendMessage(request); |
| 394 | init_buf_.AddClientSendClose(); |
| 395 | call_.PerformOps(&init_buf_); |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 396 | } |
| 397 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 398 | void ReadInitialMetadata(void* tag) GRPC_OVERRIDE { |
Yang Gao | 424bc92 | 2015-02-12 10:24:39 -0800 | [diff] [blame] | 399 | GPR_ASSERT(!context_->initial_metadata_received_); |
| 400 | |
Yang Gao | 5705fe3 | 2015-02-12 14:30:34 -0800 | [diff] [blame] | 401 | meta_buf_.Reset(tag); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 402 | meta_buf_.AddRecvInitialMetadata(context_); |
Yang Gao | 5705fe3 | 2015-02-12 14:30:34 -0800 | [diff] [blame] | 403 | call_.PerformOps(&meta_buf_); |
Yang Gao | 424bc92 | 2015-02-12 10:24:39 -0800 | [diff] [blame] | 404 | } |
| 405 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 406 | void Read(R* msg, void* tag) GRPC_OVERRIDE { |
Craig Tiller | 549a74d | 2015-02-09 22:13:44 -0800 | [diff] [blame] | 407 | read_buf_.Reset(tag); |
Yang Gao | 424bc92 | 2015-02-12 10:24:39 -0800 | [diff] [blame] | 408 | if (!context_->initial_metadata_received_) { |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 409 | read_buf_.AddRecvInitialMetadata(context_); |
Yang Gao | 424bc92 | 2015-02-12 10:24:39 -0800 | [diff] [blame] | 410 | } |
Yang Gao | 0c7aafa | 2015-02-12 22:51:38 -0800 | [diff] [blame] | 411 | read_buf_.AddRecvMessage(msg); |
Craig Tiller | 549a74d | 2015-02-09 22:13:44 -0800 | [diff] [blame] | 412 | call_.PerformOps(&read_buf_); |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 413 | } |
| 414 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 415 | void Finish(Status* status, void* tag) GRPC_OVERRIDE { |
Craig Tiller | 549a74d | 2015-02-09 22:13:44 -0800 | [diff] [blame] | 416 | finish_buf_.Reset(tag); |
Yang Gao | 424bc92 | 2015-02-12 10:24:39 -0800 | [diff] [blame] | 417 | if (!context_->initial_metadata_received_) { |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 418 | finish_buf_.AddRecvInitialMetadata(context_); |
Yang Gao | 424bc92 | 2015-02-12 10:24:39 -0800 | [diff] [blame] | 419 | } |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 420 | finish_buf_.AddClientRecvStatus(context_, status); |
Craig Tiller | 549a74d | 2015-02-09 22:13:44 -0800 | [diff] [blame] | 421 | call_.PerformOps(&finish_buf_); |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 422 | } |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 423 | |
| 424 | private: |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 425 | ClientContext* context_; |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 426 | Call call_; |
Craig Tiller | 549a74d | 2015-02-09 22:13:44 -0800 | [diff] [blame] | 427 | CallOpBuffer init_buf_; |
Yang Gao | 5705fe3 | 2015-02-12 14:30:34 -0800 | [diff] [blame] | 428 | CallOpBuffer meta_buf_; |
Craig Tiller | 549a74d | 2015-02-09 22:13:44 -0800 | [diff] [blame] | 429 | CallOpBuffer read_buf_; |
| 430 | CallOpBuffer finish_buf_; |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 431 | }; |
| 432 | |
| 433 | template <class W> |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 434 | class ClientAsyncWriter GRPC_FINAL : public ClientAsyncStreamingInterface, |
| 435 | public AsyncWriterInterface<W> { |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 436 | public: |
Craig Tiller | 573523f | 2015-02-17 07:38:26 -0800 | [diff] [blame] | 437 | ClientAsyncWriter(ChannelInterface* channel, CompletionQueue* cq, |
| 438 | const RpcMethod& method, ClientContext* context, |
Yang Gao | 7694c35 | 2015-03-03 09:48:06 -0800 | [diff] [blame] | 439 | grpc::protobuf::Message* response, void* tag) |
Craig Tiller | 573523f | 2015-02-17 07:38:26 -0800 | [diff] [blame] | 440 | : context_(context), |
| 441 | response_(response), |
Yang Gao | 5705fe3 | 2015-02-12 14:30:34 -0800 | [diff] [blame] | 442 | call_(channel->CreateCall(method, context, cq)) { |
Yang Gao | 424bc92 | 2015-02-12 10:24:39 -0800 | [diff] [blame] | 443 | init_buf_.Reset(tag); |
| 444 | init_buf_.AddSendInitialMetadata(&context->send_initial_metadata_); |
| 445 | call_.PerformOps(&init_buf_); |
| 446 | } |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 447 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 448 | void ReadInitialMetadata(void* tag) GRPC_OVERRIDE { |
Yang Gao | 424bc92 | 2015-02-12 10:24:39 -0800 | [diff] [blame] | 449 | GPR_ASSERT(!context_->initial_metadata_received_); |
| 450 | |
Yang Gao | 5705fe3 | 2015-02-12 14:30:34 -0800 | [diff] [blame] | 451 | meta_buf_.Reset(tag); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 452 | meta_buf_.AddRecvInitialMetadata(context_); |
Yang Gao | 5705fe3 | 2015-02-12 14:30:34 -0800 | [diff] [blame] | 453 | call_.PerformOps(&meta_buf_); |
Yang Gao | 424bc92 | 2015-02-12 10:24:39 -0800 | [diff] [blame] | 454 | } |
| 455 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 456 | void Write(const W& msg, void* tag) GRPC_OVERRIDE { |
Craig Tiller | 549a74d | 2015-02-09 22:13:44 -0800 | [diff] [blame] | 457 | write_buf_.Reset(tag); |
| 458 | write_buf_.AddSendMessage(msg); |
| 459 | call_.PerformOps(&write_buf_); |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 460 | } |
| 461 | |
Yang Gao | 068c85b | 2015-02-12 15:21:24 -0800 | [diff] [blame] | 462 | void WritesDone(void* tag) { |
Craig Tiller | 549a74d | 2015-02-09 22:13:44 -0800 | [diff] [blame] | 463 | writes_done_buf_.Reset(tag); |
| 464 | writes_done_buf_.AddClientSendClose(); |
| 465 | call_.PerformOps(&writes_done_buf_); |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 466 | } |
| 467 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 468 | void Finish(Status* status, void* tag) GRPC_OVERRIDE { |
Craig Tiller | 549a74d | 2015-02-09 22:13:44 -0800 | [diff] [blame] | 469 | finish_buf_.Reset(tag); |
Yang Gao | 424bc92 | 2015-02-12 10:24:39 -0800 | [diff] [blame] | 470 | if (!context_->initial_metadata_received_) { |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 471 | finish_buf_.AddRecvInitialMetadata(context_); |
Yang Gao | 424bc92 | 2015-02-12 10:24:39 -0800 | [diff] [blame] | 472 | } |
Yang Gao | 0c7aafa | 2015-02-12 22:51:38 -0800 | [diff] [blame] | 473 | finish_buf_.AddRecvMessage(response_); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 474 | finish_buf_.AddClientRecvStatus(context_, status); |
Craig Tiller | 549a74d | 2015-02-09 22:13:44 -0800 | [diff] [blame] | 475 | call_.PerformOps(&finish_buf_); |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | private: |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 479 | ClientContext* context_; |
Yang Gao | 7694c35 | 2015-03-03 09:48:06 -0800 | [diff] [blame] | 480 | grpc::protobuf::Message* const response_; |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 481 | Call call_; |
Yang Gao | 424bc92 | 2015-02-12 10:24:39 -0800 | [diff] [blame] | 482 | CallOpBuffer init_buf_; |
Yang Gao | 5705fe3 | 2015-02-12 14:30:34 -0800 | [diff] [blame] | 483 | CallOpBuffer meta_buf_; |
Craig Tiller | 549a74d | 2015-02-09 22:13:44 -0800 | [diff] [blame] | 484 | CallOpBuffer write_buf_; |
| 485 | CallOpBuffer writes_done_buf_; |
| 486 | CallOpBuffer finish_buf_; |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 487 | }; |
| 488 | |
| 489 | // Client-side interface for bi-directional streaming. |
| 490 | template <class W, class R> |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 491 | class ClientAsyncReaderWriter GRPC_FINAL : public ClientAsyncStreamingInterface, |
| 492 | public AsyncWriterInterface<W>, |
| 493 | public AsyncReaderInterface<R> { |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 494 | public: |
Craig Tiller | 573523f | 2015-02-17 07:38:26 -0800 | [diff] [blame] | 495 | ClientAsyncReaderWriter(ChannelInterface* channel, CompletionQueue* cq, |
| 496 | const RpcMethod& method, ClientContext* context, |
| 497 | void* tag) |
Yang Gao | 068c85b | 2015-02-12 15:21:24 -0800 | [diff] [blame] | 498 | : context_(context), call_(channel->CreateCall(method, context, cq)) { |
Yang Gao | 424bc92 | 2015-02-12 10:24:39 -0800 | [diff] [blame] | 499 | init_buf_.Reset(tag); |
| 500 | init_buf_.AddSendInitialMetadata(&context->send_initial_metadata_); |
| 501 | call_.PerformOps(&init_buf_); |
| 502 | } |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 503 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 504 | void ReadInitialMetadata(void* tag) GRPC_OVERRIDE { |
Yang Gao | 424bc92 | 2015-02-12 10:24:39 -0800 | [diff] [blame] | 505 | GPR_ASSERT(!context_->initial_metadata_received_); |
| 506 | |
Yang Gao | 5705fe3 | 2015-02-12 14:30:34 -0800 | [diff] [blame] | 507 | meta_buf_.Reset(tag); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 508 | meta_buf_.AddRecvInitialMetadata(context_); |
Yang Gao | 5705fe3 | 2015-02-12 14:30:34 -0800 | [diff] [blame] | 509 | call_.PerformOps(&meta_buf_); |
Yang Gao | 424bc92 | 2015-02-12 10:24:39 -0800 | [diff] [blame] | 510 | } |
| 511 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 512 | void Read(R* msg, void* tag) GRPC_OVERRIDE { |
Craig Tiller | 549a74d | 2015-02-09 22:13:44 -0800 | [diff] [blame] | 513 | read_buf_.Reset(tag); |
Yang Gao | 424bc92 | 2015-02-12 10:24:39 -0800 | [diff] [blame] | 514 | if (!context_->initial_metadata_received_) { |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 515 | read_buf_.AddRecvInitialMetadata(context_); |
Yang Gao | 424bc92 | 2015-02-12 10:24:39 -0800 | [diff] [blame] | 516 | } |
Yang Gao | 0c7aafa | 2015-02-12 22:51:38 -0800 | [diff] [blame] | 517 | read_buf_.AddRecvMessage(msg); |
Craig Tiller | 549a74d | 2015-02-09 22:13:44 -0800 | [diff] [blame] | 518 | call_.PerformOps(&read_buf_); |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 519 | } |
| 520 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 521 | void Write(const W& msg, void* tag) GRPC_OVERRIDE { |
Craig Tiller | 549a74d | 2015-02-09 22:13:44 -0800 | [diff] [blame] | 522 | write_buf_.Reset(tag); |
| 523 | write_buf_.AddSendMessage(msg); |
| 524 | call_.PerformOps(&write_buf_); |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 525 | } |
| 526 | |
Yang Gao | 068c85b | 2015-02-12 15:21:24 -0800 | [diff] [blame] | 527 | void WritesDone(void* tag) { |
Craig Tiller | 549a74d | 2015-02-09 22:13:44 -0800 | [diff] [blame] | 528 | writes_done_buf_.Reset(tag); |
| 529 | writes_done_buf_.AddClientSendClose(); |
| 530 | call_.PerformOps(&writes_done_buf_); |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 531 | } |
| 532 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 533 | void Finish(Status* status, void* tag) GRPC_OVERRIDE { |
Craig Tiller | 549a74d | 2015-02-09 22:13:44 -0800 | [diff] [blame] | 534 | finish_buf_.Reset(tag); |
Yang Gao | 424bc92 | 2015-02-12 10:24:39 -0800 | [diff] [blame] | 535 | if (!context_->initial_metadata_received_) { |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 536 | finish_buf_.AddRecvInitialMetadata(context_); |
Yang Gao | 424bc92 | 2015-02-12 10:24:39 -0800 | [diff] [blame] | 537 | } |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 538 | finish_buf_.AddClientRecvStatus(context_, status); |
Craig Tiller | 549a74d | 2015-02-09 22:13:44 -0800 | [diff] [blame] | 539 | call_.PerformOps(&finish_buf_); |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | private: |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 543 | ClientContext* context_; |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 544 | Call call_; |
Yang Gao | 424bc92 | 2015-02-12 10:24:39 -0800 | [diff] [blame] | 545 | CallOpBuffer init_buf_; |
Yang Gao | 5705fe3 | 2015-02-12 14:30:34 -0800 | [diff] [blame] | 546 | CallOpBuffer meta_buf_; |
Craig Tiller | 549a74d | 2015-02-09 22:13:44 -0800 | [diff] [blame] | 547 | CallOpBuffer read_buf_; |
| 548 | CallOpBuffer write_buf_; |
| 549 | CallOpBuffer writes_done_buf_; |
| 550 | CallOpBuffer finish_buf_; |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 551 | }; |
| 552 | |
Yang Gao | 005f18a | 2015-02-13 10:22:33 -0800 | [diff] [blame] | 553 | template <class W, class R> |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 554 | class ServerAsyncReader GRPC_FINAL : public ServerAsyncStreamingInterface, |
| 555 | public AsyncReaderInterface<R> { |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 556 | public: |
Craig Tiller | d4ebeeb | 2015-02-12 14:57:17 -0800 | [diff] [blame] | 557 | explicit ServerAsyncReader(ServerContext* ctx) |
| 558 | : call_(nullptr, nullptr, nullptr), ctx_(ctx) {} |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 559 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 560 | void SendInitialMetadata(void* tag) GRPC_OVERRIDE { |
Yang Gao | a38feb9 | 2015-02-12 12:05:20 -0800 | [diff] [blame] | 561 | GPR_ASSERT(!ctx_->sent_initial_metadata_); |
| 562 | |
| 563 | meta_buf_.Reset(tag); |
| 564 | meta_buf_.AddSendInitialMetadata(&ctx_->initial_metadata_); |
| 565 | ctx_->sent_initial_metadata_ = true; |
Craig Tiller | d4ebeeb | 2015-02-12 14:57:17 -0800 | [diff] [blame] | 566 | call_.PerformOps(&meta_buf_); |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 567 | } |
| 568 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 569 | void Read(R* msg, void* tag) GRPC_OVERRIDE { |
Yang Gao | a38feb9 | 2015-02-12 12:05:20 -0800 | [diff] [blame] | 570 | read_buf_.Reset(tag); |
| 571 | read_buf_.AddRecvMessage(msg); |
Craig Tiller | d4ebeeb | 2015-02-12 14:57:17 -0800 | [diff] [blame] | 572 | call_.PerformOps(&read_buf_); |
Yang Gao | a38feb9 | 2015-02-12 12:05:20 -0800 | [diff] [blame] | 573 | } |
| 574 | |
Yang Gao | 005f18a | 2015-02-13 10:22:33 -0800 | [diff] [blame] | 575 | void Finish(const W& msg, const Status& status, void* tag) { |
| 576 | finish_buf_.Reset(tag); |
| 577 | if (!ctx_->sent_initial_metadata_) { |
| 578 | finish_buf_.AddSendInitialMetadata(&ctx_->initial_metadata_); |
| 579 | ctx_->sent_initial_metadata_ = true; |
| 580 | } |
| 581 | // The response is dropped if the status is not OK. |
| 582 | if (status.IsOk()) { |
| 583 | finish_buf_.AddSendMessage(msg); |
| 584 | } |
Yang Gao | 005f18a | 2015-02-13 10:22:33 -0800 | [diff] [blame] | 585 | finish_buf_.AddServerSendStatus(&ctx_->trailing_metadata_, status); |
| 586 | call_.PerformOps(&finish_buf_); |
| 587 | } |
| 588 | |
| 589 | void FinishWithError(const Status& status, void* tag) { |
| 590 | GPR_ASSERT(!status.IsOk()); |
Yang Gao | a38feb9 | 2015-02-12 12:05:20 -0800 | [diff] [blame] | 591 | finish_buf_.Reset(tag); |
| 592 | if (!ctx_->sent_initial_metadata_) { |
| 593 | finish_buf_.AddSendInitialMetadata(&ctx_->initial_metadata_); |
| 594 | ctx_->sent_initial_metadata_ = true; |
| 595 | } |
Yang Gao | a38feb9 | 2015-02-12 12:05:20 -0800 | [diff] [blame] | 596 | finish_buf_.AddServerSendStatus(&ctx_->trailing_metadata_, status); |
Craig Tiller | d4ebeeb | 2015-02-12 14:57:17 -0800 | [diff] [blame] | 597 | call_.PerformOps(&finish_buf_); |
Yang Gao | a38feb9 | 2015-02-12 12:05:20 -0800 | [diff] [blame] | 598 | } |
| 599 | |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 600 | private: |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 601 | void BindCall(Call* call) GRPC_OVERRIDE { call_ = *call; } |
Craig Tiller | d4ebeeb | 2015-02-12 14:57:17 -0800 | [diff] [blame] | 602 | |
| 603 | Call call_; |
Yang Gao | a38feb9 | 2015-02-12 12:05:20 -0800 | [diff] [blame] | 604 | ServerContext* ctx_; |
| 605 | CallOpBuffer meta_buf_; |
| 606 | CallOpBuffer read_buf_; |
| 607 | CallOpBuffer finish_buf_; |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 608 | }; |
| 609 | |
| 610 | template <class W> |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 611 | class ServerAsyncWriter GRPC_FINAL : public ServerAsyncStreamingInterface, |
| 612 | public AsyncWriterInterface<W> { |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 613 | public: |
Craig Tiller | d4ebeeb | 2015-02-12 14:57:17 -0800 | [diff] [blame] | 614 | explicit ServerAsyncWriter(ServerContext* ctx) |
| 615 | : call_(nullptr, nullptr, nullptr), ctx_(ctx) {} |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 616 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 617 | void SendInitialMetadata(void* tag) GRPC_OVERRIDE { |
Yang Gao | a38feb9 | 2015-02-12 12:05:20 -0800 | [diff] [blame] | 618 | GPR_ASSERT(!ctx_->sent_initial_metadata_); |
| 619 | |
| 620 | meta_buf_.Reset(tag); |
| 621 | meta_buf_.AddSendInitialMetadata(&ctx_->initial_metadata_); |
| 622 | ctx_->sent_initial_metadata_ = true; |
Craig Tiller | d4ebeeb | 2015-02-12 14:57:17 -0800 | [diff] [blame] | 623 | call_.PerformOps(&meta_buf_); |
Yang Gao | a38feb9 | 2015-02-12 12:05:20 -0800 | [diff] [blame] | 624 | } |
| 625 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 626 | void Write(const W& msg, void* tag) GRPC_OVERRIDE { |
Yang Gao | a38feb9 | 2015-02-12 12:05:20 -0800 | [diff] [blame] | 627 | write_buf_.Reset(tag); |
| 628 | if (!ctx_->sent_initial_metadata_) { |
| 629 | write_buf_.AddSendInitialMetadata(&ctx_->initial_metadata_); |
| 630 | ctx_->sent_initial_metadata_ = true; |
| 631 | } |
| 632 | write_buf_.AddSendMessage(msg); |
Craig Tiller | d4ebeeb | 2015-02-12 14:57:17 -0800 | [diff] [blame] | 633 | call_.PerformOps(&write_buf_); |
Yang Gao | a38feb9 | 2015-02-12 12:05:20 -0800 | [diff] [blame] | 634 | } |
| 635 | |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 636 | void Finish(const Status& status, void* tag) { |
Yang Gao | a38feb9 | 2015-02-12 12:05:20 -0800 | [diff] [blame] | 637 | finish_buf_.Reset(tag); |
| 638 | if (!ctx_->sent_initial_metadata_) { |
| 639 | finish_buf_.AddSendInitialMetadata(&ctx_->initial_metadata_); |
| 640 | ctx_->sent_initial_metadata_ = true; |
| 641 | } |
Yang Gao | a38feb9 | 2015-02-12 12:05:20 -0800 | [diff] [blame] | 642 | finish_buf_.AddServerSendStatus(&ctx_->trailing_metadata_, status); |
Craig Tiller | d4ebeeb | 2015-02-12 14:57:17 -0800 | [diff] [blame] | 643 | call_.PerformOps(&finish_buf_); |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | private: |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 647 | void BindCall(Call* call) GRPC_OVERRIDE { call_ = *call; } |
Craig Tiller | d4ebeeb | 2015-02-12 14:57:17 -0800 | [diff] [blame] | 648 | |
| 649 | Call call_; |
Yang Gao | a38feb9 | 2015-02-12 12:05:20 -0800 | [diff] [blame] | 650 | ServerContext* ctx_; |
| 651 | CallOpBuffer meta_buf_; |
| 652 | CallOpBuffer write_buf_; |
| 653 | CallOpBuffer finish_buf_; |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 654 | }; |
| 655 | |
| 656 | // Server-side interface for bi-directional streaming. |
| 657 | template <class W, class R> |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 658 | class ServerAsyncReaderWriter GRPC_FINAL : public ServerAsyncStreamingInterface, |
| 659 | public AsyncWriterInterface<W>, |
| 660 | public AsyncReaderInterface<R> { |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 661 | public: |
Craig Tiller | d4ebeeb | 2015-02-12 14:57:17 -0800 | [diff] [blame] | 662 | explicit ServerAsyncReaderWriter(ServerContext* ctx) |
| 663 | : call_(nullptr, nullptr, nullptr), ctx_(ctx) {} |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 664 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 665 | void SendInitialMetadata(void* tag) GRPC_OVERRIDE { |
Yang Gao | a38feb9 | 2015-02-12 12:05:20 -0800 | [diff] [blame] | 666 | GPR_ASSERT(!ctx_->sent_initial_metadata_); |
| 667 | |
| 668 | meta_buf_.Reset(tag); |
| 669 | meta_buf_.AddSendInitialMetadata(&ctx_->initial_metadata_); |
| 670 | ctx_->sent_initial_metadata_ = true; |
Craig Tiller | d4ebeeb | 2015-02-12 14:57:17 -0800 | [diff] [blame] | 671 | call_.PerformOps(&meta_buf_); |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 672 | } |
| 673 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 674 | virtual void Read(R* msg, void* tag) GRPC_OVERRIDE { |
Yang Gao | a38feb9 | 2015-02-12 12:05:20 -0800 | [diff] [blame] | 675 | read_buf_.Reset(tag); |
| 676 | read_buf_.AddRecvMessage(msg); |
Craig Tiller | d4ebeeb | 2015-02-12 14:57:17 -0800 | [diff] [blame] | 677 | call_.PerformOps(&read_buf_); |
Yang Gao | a38feb9 | 2015-02-12 12:05:20 -0800 | [diff] [blame] | 678 | } |
| 679 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 680 | virtual void Write(const W& msg, void* tag) GRPC_OVERRIDE { |
Yang Gao | a38feb9 | 2015-02-12 12:05:20 -0800 | [diff] [blame] | 681 | write_buf_.Reset(tag); |
| 682 | if (!ctx_->sent_initial_metadata_) { |
| 683 | write_buf_.AddSendInitialMetadata(&ctx_->initial_metadata_); |
| 684 | ctx_->sent_initial_metadata_ = true; |
| 685 | } |
| 686 | write_buf_.AddSendMessage(msg); |
Craig Tiller | d4ebeeb | 2015-02-12 14:57:17 -0800 | [diff] [blame] | 687 | call_.PerformOps(&write_buf_); |
Yang Gao | a38feb9 | 2015-02-12 12:05:20 -0800 | [diff] [blame] | 688 | } |
| 689 | |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 690 | void Finish(const Status& status, void* tag) { |
Yang Gao | a38feb9 | 2015-02-12 12:05:20 -0800 | [diff] [blame] | 691 | finish_buf_.Reset(tag); |
| 692 | if (!ctx_->sent_initial_metadata_) { |
| 693 | finish_buf_.AddSendInitialMetadata(&ctx_->initial_metadata_); |
| 694 | ctx_->sent_initial_metadata_ = true; |
| 695 | } |
Yang Gao | a38feb9 | 2015-02-12 12:05:20 -0800 | [diff] [blame] | 696 | finish_buf_.AddServerSendStatus(&ctx_->trailing_metadata_, status); |
Craig Tiller | d4ebeeb | 2015-02-12 14:57:17 -0800 | [diff] [blame] | 697 | call_.PerformOps(&finish_buf_); |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | private: |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 701 | void BindCall(Call* call) GRPC_OVERRIDE { call_ = *call; } |
Craig Tiller | d4ebeeb | 2015-02-12 14:57:17 -0800 | [diff] [blame] | 702 | |
| 703 | Call call_; |
Yang Gao | a38feb9 | 2015-02-12 12:05:20 -0800 | [diff] [blame] | 704 | ServerContext* ctx_; |
| 705 | CallOpBuffer meta_buf_; |
| 706 | CallOpBuffer read_buf_; |
| 707 | CallOpBuffer write_buf_; |
| 708 | CallOpBuffer finish_buf_; |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 709 | }; |
| 710 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 711 | } // namespace grpc |
| 712 | |
Nicolas "Pixel" Noble | 1ff52d5 | 2015-03-01 05:24:36 +0100 | [diff] [blame] | 713 | #endif // GRPCXX_STREAM_H |