Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2014, 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 __GRPCPP_STREAM_H__ |
| 35 | #define __GRPCPP_STREAM_H__ |
| 36 | |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 37 | #include <grpc++/call.h> |
| 38 | #include <grpc++/channel_interface.h> |
| 39 | #include <grpc++/completion_queue.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 40 | #include <grpc++/status.h> |
| 41 | #include <grpc/support/log.h> |
| 42 | |
| 43 | namespace grpc { |
| 44 | |
Craig Tiller | 1d2e219 | 2015-02-09 15:25:21 -0800 | [diff] [blame] | 45 | // DELETE DELETE DELETE |
| 46 | // DELETE DELETE DELETE |
| 47 | // DELETE DELETE DELETE |
| 48 | // DELETE DELETE DELETE |
| 49 | // DELETE DELETE DELETE |
| 50 | // DELETE DELETE DELETE |
| 51 | // DELETE DELETE DELETE |
| 52 | // DELETE DELETE DELETE |
| 53 | // DELETE DELETE DELETE |
| 54 | // DELETE DELETE DELETE |
| 55 | // DELETE DELETE DELETE |
| 56 | // DELETE DELETE DELETE |
| 57 | // DELETE DELETE DELETE |
| 58 | // DELETE DELETE DELETE |
| 59 | // DELETE DELETE DELETE |
| 60 | // DELETE DELETE DELETE |
| 61 | // DELETE DELETE DELETE |
| 62 | // DELETE DELETE DELETE |
| 63 | // DELETE DELETE DELETE |
| 64 | // DELETE DELETE DELETE |
| 65 | // DELETE DELETE DELETE |
| 66 | // DELETE DELETE DELETE |
| 67 | // DELETE DELETE DELETE |
| 68 | // DELETE DELETE DELETE |
| 69 | class StreamContextInterface { |
| 70 | public: |
| 71 | template <class T> bool Write(T, bool); |
| 72 | template <class T> void Start(T); |
| 73 | template <class T> bool Read(T); |
| 74 | google::protobuf::Message *request(); |
| 75 | }; |
| 76 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 77 | // Common interface for all client side streaming. |
| 78 | class ClientStreamingInterface { |
| 79 | public: |
| 80 | virtual ~ClientStreamingInterface() {} |
| 81 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 82 | // Wait until the stream finishes, and return the final status. When the |
| 83 | // client side declares it has no more message to send, either implicitly or |
| 84 | // by calling WritesDone, it needs to make sure there is no more message to |
| 85 | // be received from the server, either implicitly or by getting a false from |
| 86 | // a Read(). Otherwise, this implicitly cancels the stream. |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 87 | virtual Status Finish() = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | // An interface that yields a sequence of R messages. |
| 91 | template <class R> |
| 92 | class ReaderInterface { |
| 93 | public: |
| 94 | virtual ~ReaderInterface() {} |
| 95 | |
| 96 | // Blocking read a message and parse to msg. Returns true on success. |
| 97 | // The method returns false when there will be no more incoming messages, |
| 98 | // either because the other side has called WritesDone or the stream has |
| 99 | // failed (or been cancelled). |
| 100 | virtual bool Read(R* msg) = 0; |
| 101 | }; |
| 102 | |
| 103 | // An interface that can be fed a sequence of W messages. |
| 104 | template <class W> |
| 105 | class WriterInterface { |
| 106 | public: |
| 107 | virtual ~WriterInterface() {} |
| 108 | |
| 109 | // Blocking write msg to the stream. Returns true on success. |
| 110 | // Returns false when the stream has been closed. |
| 111 | virtual bool Write(const W& msg) = 0; |
| 112 | }; |
| 113 | |
| 114 | template <class R> |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 115 | class ClientReader final : public ClientStreamingInterface, |
| 116 | public ReaderInterface<R> { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 117 | public: |
| 118 | // Blocking create a stream and write the first request out. |
Yang Gao | 5f4f0c3 | 2015-02-09 13:45:28 -0800 | [diff] [blame] | 119 | ClientReader(ChannelInterface *channel, const RpcMethod &method, |
| 120 | ClientContext *context, |
| 121 | const google::protobuf::Message &request) |
Craig Tiller | 5095071 | 2015-02-09 10:38:38 -0800 | [diff] [blame] | 122 | : call_(channel->CreateCall(method, context, &cq_)) { |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 123 | CallOpBuffer buf; |
| 124 | buf.AddSendMessage(request); |
| 125 | buf.AddClientSendClose(); |
Craig Tiller | de91706 | 2015-02-09 17:15:03 -0800 | [diff] [blame] | 126 | call_.PerformOps(&buf); |
| 127 | cq_.Pluck(&buf); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 128 | } |
| 129 | |
Yang Gao | 8a3bbb5 | 2015-02-09 14:10:59 -0800 | [diff] [blame] | 130 | virtual bool Read(R *msg) override { |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 131 | CallOpBuffer buf; |
| 132 | buf.AddRecvMessage(msg); |
Craig Tiller | de91706 | 2015-02-09 17:15:03 -0800 | [diff] [blame] | 133 | call_.PerformOps(&buf); |
| 134 | return cq_.Pluck(&buf); |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 135 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 136 | |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 137 | virtual Status Finish() override { |
| 138 | CallOpBuffer buf; |
| 139 | Status status; |
| 140 | buf.AddClientRecvStatus(&status); |
Craig Tiller | de91706 | 2015-02-09 17:15:03 -0800 | [diff] [blame] | 141 | call_.PerformOps(&buf); |
| 142 | GPR_ASSERT(cq_.Pluck(&buf)); |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 143 | return status; |
| 144 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 145 | |
| 146 | private: |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 147 | CompletionQueue cq_; |
| 148 | Call call_; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | template <class W> |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 152 | class ClientWriter final : public ClientStreamingInterface, |
| 153 | public WriterInterface<W> { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 154 | public: |
| 155 | // Blocking create a stream. |
Yang Gao | 8a3bbb5 | 2015-02-09 14:10:59 -0800 | [diff] [blame] | 156 | ClientWriter(ChannelInterface *channel, const RpcMethod &method, |
| 157 | ClientContext *context, |
| 158 | google::protobuf::Message *response) |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 159 | : response_(response), |
Craig Tiller | 5095071 | 2015-02-09 10:38:38 -0800 | [diff] [blame] | 160 | call_(channel->CreateCall(method, context, &cq_)) {} |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 161 | |
Yang Gao | 8a3bbb5 | 2015-02-09 14:10:59 -0800 | [diff] [blame] | 162 | virtual bool Write(const W& msg) override { |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 163 | CallOpBuffer buf; |
| 164 | buf.AddSendMessage(msg); |
Craig Tiller | de91706 | 2015-02-09 17:15:03 -0800 | [diff] [blame] | 165 | call_.PerformOps(&buf); |
| 166 | return cq_.Pluck(&buf); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 167 | } |
| 168 | |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 169 | virtual bool WritesDone() { |
| 170 | CallOpBuffer buf; |
| 171 | buf.AddClientSendClose(); |
Craig Tiller | de91706 | 2015-02-09 17:15:03 -0800 | [diff] [blame] | 172 | call_.PerformOps(&buf); |
| 173 | return cq_.Pluck(&buf); |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 174 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 175 | |
| 176 | // Read the final response and wait for the final status. |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 177 | virtual Status Finish() override { |
| 178 | CallOpBuffer buf; |
| 179 | Status status; |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 180 | buf.AddRecvMessage(response_); |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 181 | buf.AddClientRecvStatus(&status); |
Craig Tiller | de91706 | 2015-02-09 17:15:03 -0800 | [diff] [blame] | 182 | call_.PerformOps(&buf); |
| 183 | GPR_ASSERT(cq_.Pluck(&buf)); |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 184 | return status; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | private: |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 188 | google::protobuf::Message *const response_; |
| 189 | CompletionQueue cq_; |
| 190 | Call call_; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | // Client-side interface for bi-directional streaming. |
| 194 | template <class W, class R> |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 195 | class ClientReaderWriter final : public ClientStreamingInterface, |
| 196 | public WriterInterface<W>, |
| 197 | public ReaderInterface<R> { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 198 | public: |
| 199 | // Blocking create a stream. |
Yang Gao | 8a3bbb5 | 2015-02-09 14:10:59 -0800 | [diff] [blame] | 200 | ClientReaderWriter(ChannelInterface *channel, |
| 201 | const RpcMethod &method, ClientContext *context) |
Craig Tiller | 5095071 | 2015-02-09 10:38:38 -0800 | [diff] [blame] | 202 | : call_(channel->CreateCall(method, context, &cq_)) {} |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 203 | |
Yang Gao | 8a3bbb5 | 2015-02-09 14:10:59 -0800 | [diff] [blame] | 204 | virtual bool Read(R *msg) override { |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 205 | CallOpBuffer buf; |
| 206 | buf.AddRecvMessage(msg); |
Craig Tiller | de91706 | 2015-02-09 17:15:03 -0800 | [diff] [blame] | 207 | call_.PerformOps(&buf); |
| 208 | return cq_.Pluck(&buf); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Yang Gao | 8a3bbb5 | 2015-02-09 14:10:59 -0800 | [diff] [blame] | 211 | virtual bool Write(const W& msg) override { |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 212 | CallOpBuffer buf; |
| 213 | buf.AddSendMessage(msg); |
Craig Tiller | de91706 | 2015-02-09 17:15:03 -0800 | [diff] [blame] | 214 | call_.PerformOps(&buf); |
| 215 | return cq_.Pluck(&buf); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 216 | } |
| 217 | |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 218 | virtual bool WritesDone() { |
| 219 | CallOpBuffer buf; |
| 220 | buf.AddClientSendClose(); |
Craig Tiller | de91706 | 2015-02-09 17:15:03 -0800 | [diff] [blame] | 221 | call_.PerformOps(&buf); |
| 222 | return cq_.Pluck(&buf); |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 223 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 224 | |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 225 | virtual Status Finish() override { |
| 226 | CallOpBuffer buf; |
| 227 | Status status; |
| 228 | buf.AddClientRecvStatus(&status); |
Craig Tiller | de91706 | 2015-02-09 17:15:03 -0800 | [diff] [blame] | 229 | call_.PerformOps(&buf); |
| 230 | GPR_ASSERT(cq_.Pluck(&buf)); |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 231 | return status; |
| 232 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 233 | |
| 234 | private: |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 235 | CompletionQueue cq_; |
| 236 | Call call_; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 237 | }; |
| 238 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 239 | template <class R> |
Yang Gao | 8a3bbb5 | 2015-02-09 14:10:59 -0800 | [diff] [blame] | 240 | class ServerReader final : public ReaderInterface<R> { |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 241 | public: |
Craig Tiller | 1d2e219 | 2015-02-09 15:25:21 -0800 | [diff] [blame] | 242 | explicit ServerReader(Call* call) : call_(call) {} |
Yang Gao | 8a3bbb5 | 2015-02-09 14:10:59 -0800 | [diff] [blame] | 243 | |
| 244 | virtual bool Read(R* msg) override { |
| 245 | CallOpBuffer buf; |
| 246 | buf.AddRecvMessage(msg); |
Craig Tiller | de91706 | 2015-02-09 17:15:03 -0800 | [diff] [blame] | 247 | call_->PerformOps(&buf); |
| 248 | return call_->cq()->Pluck(&buf); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 249 | } |
| 250 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 251 | private: |
Yang Gao | 8a3bbb5 | 2015-02-09 14:10:59 -0800 | [diff] [blame] | 252 | Call* call_; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 253 | }; |
| 254 | |
| 255 | template <class W> |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 256 | class ServerWriter final : public WriterInterface<W> { |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 257 | public: |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 258 | explicit ServerWriter(Call* call) : call_(call) {} |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 259 | |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 260 | virtual bool Write(const W& msg) override { |
| 261 | CallOpBuffer buf; |
| 262 | buf.AddSendMessage(msg); |
Craig Tiller | de91706 | 2015-02-09 17:15:03 -0800 | [diff] [blame] | 263 | call_->PerformOps(&buf); |
| 264 | return call_->cq()->Pluck(&buf); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | private: |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 268 | Call* call_; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 269 | }; |
| 270 | |
| 271 | // Server-side interface for bi-directional streaming. |
| 272 | template <class W, class R> |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 273 | class ServerReaderWriter final : public WriterInterface<W>, |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 274 | public ReaderInterface<R> { |
| 275 | public: |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 276 | explicit ServerReaderWriter(Call* call) : call_(call) {} |
| 277 | |
| 278 | virtual bool Read(R* msg) override { |
| 279 | CallOpBuffer buf; |
| 280 | buf.AddRecvMessage(msg); |
Craig Tiller | de91706 | 2015-02-09 17:15:03 -0800 | [diff] [blame] | 281 | call_->PerformOps(&buf); |
| 282 | return call_->cq()->Pluck(&buf); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 283 | } |
| 284 | |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 285 | virtual bool Write(const W& msg) override { |
| 286 | CallOpBuffer buf; |
| 287 | buf.AddSendMessage(msg); |
Craig Tiller | de91706 | 2015-02-09 17:15:03 -0800 | [diff] [blame] | 288 | call_->PerformOps(&buf); |
| 289 | return call_->cq()->Pluck(&buf); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | private: |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 293 | CompletionQueue* cq_; |
| 294 | Call* call_; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 295 | }; |
| 296 | |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 297 | // Async interfaces |
| 298 | // Common interface for all client side streaming. |
| 299 | class ClientAsyncStreamingInterface { |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 300 | public: |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 301 | virtual ~ClientAsyncStreamingInterface() {} |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 302 | |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 303 | virtual void Finish(Status* status, void* tag) = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 304 | }; |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 305 | |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 306 | // An interface that yields a sequence of R messages. |
| 307 | template <class R> |
| 308 | class AsyncReaderInterface { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 309 | public: |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 310 | virtual ~AsyncReaderInterface() {} |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 311 | |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 312 | virtual void Read(R* msg, void* tag) = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 313 | }; |
| 314 | |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 315 | // An interface that can be fed a sequence of W messages. |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 316 | template <class W> |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 317 | class AsyncWriterInterface { |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 318 | public: |
Craig Tiller | 5319164 | 2015-02-09 16:15:23 -0800 | [diff] [blame] | 319 | virtual ~AsyncWriterInterface() {} |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 320 | |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 321 | virtual void Write(const W& msg, void* tag) = 0; |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 322 | }; |
| 323 | |
| 324 | template <class R> |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 325 | class ClientAsyncReader final : public ClientAsyncStreamingInterface, |
| 326 | public AsyncReaderInterface<R> { |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 327 | public: |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 328 | // Blocking create a stream and write the first request out. |
| 329 | ClientAsyncReader(ChannelInterface *channel, const RpcMethod &method, |
| 330 | ClientContext *context, |
| 331 | const google::protobuf::Message &request, void* tag) |
| 332 | : call_(channel->CreateCall(method, context, &cq_)) { |
Craig Tiller | 549a74d | 2015-02-09 22:13:44 -0800 | [diff] [blame^] | 333 | init_buf_.Reset(tag); |
| 334 | init_buf_.AddSendMessage(request); |
| 335 | init_buf_.AddClientSendClose(); |
| 336 | call_.PerformOps(&init_buf_); |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 337 | } |
| 338 | |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 339 | virtual void Read(R *msg, void* tag) override { |
Craig Tiller | 549a74d | 2015-02-09 22:13:44 -0800 | [diff] [blame^] | 340 | read_buf_.Reset(tag); |
| 341 | read_buf_.AddRecvMessage(msg); |
| 342 | call_.PerformOps(&read_buf_); |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | virtual void Finish(Status* status, void* tag) override { |
Craig Tiller | 549a74d | 2015-02-09 22:13:44 -0800 | [diff] [blame^] | 346 | finish_buf_.Reset(tag); |
| 347 | finish_buf_.AddClientRecvStatus(status); |
| 348 | call_.PerformOps(&finish_buf_); |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 349 | } |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 350 | |
| 351 | private: |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 352 | CompletionQueue cq_; |
| 353 | Call call_; |
Craig Tiller | 549a74d | 2015-02-09 22:13:44 -0800 | [diff] [blame^] | 354 | CallOpBuffer init_buf_; |
| 355 | CallOpBuffer read_buf_; |
| 356 | CallOpBuffer finish_buf_; |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 357 | }; |
| 358 | |
| 359 | template <class W> |
Craig Tiller | 5319164 | 2015-02-09 16:15:23 -0800 | [diff] [blame] | 360 | class ClientAsyncWriter final : public ClientAsyncStreamingInterface, |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 361 | public WriterInterface<W> { |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 362 | public: |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 363 | // Blocking create a stream. |
| 364 | ClientAsyncWriter(ChannelInterface *channel, const RpcMethod &method, |
| 365 | ClientContext *context, |
| 366 | google::protobuf::Message *response) |
| 367 | : response_(response), |
| 368 | call_(channel->CreateCall(method, context, &cq_)) {} |
| 369 | |
| 370 | virtual void Write(const W& msg, void* tag) override { |
Craig Tiller | 549a74d | 2015-02-09 22:13:44 -0800 | [diff] [blame^] | 371 | write_buf_.Reset(tag); |
| 372 | write_buf_.AddSendMessage(msg); |
| 373 | call_.PerformOps(&write_buf_); |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 374 | } |
| 375 | |
Craig Tiller | 549a74d | 2015-02-09 22:13:44 -0800 | [diff] [blame^] | 376 | virtual void WritesDone(void* tag) override { |
| 377 | writes_done_buf_.Reset(tag); |
| 378 | writes_done_buf_.AddClientSendClose(); |
| 379 | call_.PerformOps(&writes_done_buf_); |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | virtual void Finish(Status* status, void* tag) override { |
Craig Tiller | 549a74d | 2015-02-09 22:13:44 -0800 | [diff] [blame^] | 383 | finish_buf_.Reset(tag); |
| 384 | finish_buf_.AddRecvMessage(response_); |
| 385 | finish_buf_.AddClientRecvStatus(status); |
| 386 | call_.PerformOps(&finish_buf_); |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | private: |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 390 | google::protobuf::Message *const response_; |
| 391 | CompletionQueue cq_; |
| 392 | Call call_; |
Craig Tiller | 549a74d | 2015-02-09 22:13:44 -0800 | [diff] [blame^] | 393 | CallOpBuffer write_buf_; |
| 394 | CallOpBuffer writes_done_buf_; |
| 395 | CallOpBuffer finish_buf_; |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 396 | }; |
| 397 | |
| 398 | // Client-side interface for bi-directional streaming. |
| 399 | template <class W, class R> |
| 400 | class ClientAsyncReaderWriter final : public ClientAsyncStreamingInterface, |
| 401 | public AsyncWriterInterface<W>, |
| 402 | public AsyncReaderInterface<R> { |
| 403 | public: |
| 404 | ClientAsyncReaderWriter(ChannelInterface *channel, |
| 405 | const RpcMethod &method, ClientContext *context) |
| 406 | : call_(channel->CreateCall(method, context, &cq_)) {} |
| 407 | |
| 408 | virtual void Read(R *msg, void* tag) override { |
Craig Tiller | 549a74d | 2015-02-09 22:13:44 -0800 | [diff] [blame^] | 409 | read_buf_.Reset(tag); |
| 410 | read_buf_.AddRecvMessage(msg); |
| 411 | call_.PerformOps(&read_buf_); |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | virtual void Write(const W& msg, void* tag) override { |
Craig Tiller | 549a74d | 2015-02-09 22:13:44 -0800 | [diff] [blame^] | 415 | write_buf_.Reset(tag); |
| 416 | write_buf_.AddSendMessage(msg); |
| 417 | call_.PerformOps(&write_buf_); |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 418 | } |
| 419 | |
Craig Tiller | 549a74d | 2015-02-09 22:13:44 -0800 | [diff] [blame^] | 420 | virtual void WritesDone(void* tag) override { |
| 421 | writes_done_buf_.Reset(tag); |
| 422 | writes_done_buf_.AddClientSendClose(); |
| 423 | call_.PerformOps(&writes_done_buf_); |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | virtual void Finish(Status* status, void* tag) override { |
Craig Tiller | 549a74d | 2015-02-09 22:13:44 -0800 | [diff] [blame^] | 427 | finish_buf_.Reset(tag); |
| 428 | finish_buf_.AddClientRecvStatus(status); |
| 429 | call_.PerformOps(&finish_buf_); |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | private: |
| 433 | CompletionQueue cq_; |
| 434 | Call call_; |
Craig Tiller | 549a74d | 2015-02-09 22:13:44 -0800 | [diff] [blame^] | 435 | CallOpBuffer read_buf_; |
| 436 | CallOpBuffer write_buf_; |
| 437 | CallOpBuffer writes_done_buf_; |
| 438 | CallOpBuffer finish_buf_; |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 439 | }; |
| 440 | |
| 441 | // TODO(yangg) Move out of stream.h |
| 442 | template <class W> |
| 443 | class ServerAsyncResponseWriter final { |
| 444 | public: |
| 445 | explicit ServerAsyncResponseWriter(Call* call) : call_(call) {} |
| 446 | |
| 447 | virtual void Write(const W& msg, void* tag) override { |
| 448 | CallOpBuffer buf; |
| 449 | buf.AddSendMessage(msg); |
Craig Tiller | de91706 | 2015-02-09 17:15:03 -0800 | [diff] [blame] | 450 | call_->PerformOps(&buf); |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | private: |
| 454 | Call* call_; |
| 455 | }; |
| 456 | |
| 457 | template <class R> |
| 458 | class ServerAsyncReader : public AsyncReaderInterface<R> { |
| 459 | public: |
| 460 | explicit ServerAsyncReader(Call* call) : call_(call) {} |
| 461 | |
| 462 | virtual void Read(R* msg, void* tag) { |
| 463 | // TODO |
| 464 | } |
| 465 | |
| 466 | private: |
| 467 | Call* call_; |
| 468 | }; |
| 469 | |
| 470 | template <class W> |
| 471 | class ServerAsyncWriter : public AsyncWriterInterface<W> { |
| 472 | public: |
| 473 | explicit ServerAsyncWriter(Call* call) : call_(call) {} |
| 474 | |
| 475 | virtual void Write(const W& msg, void* tag) { |
| 476 | // TODO |
| 477 | } |
| 478 | |
| 479 | private: |
| 480 | Call* call_; |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 481 | }; |
| 482 | |
| 483 | // Server-side interface for bi-directional streaming. |
| 484 | template <class W, class R> |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 485 | class ServerAsyncReaderWriter : public AsyncWriterInterface<W>, |
| 486 | public AsyncReaderInterface<R> { |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 487 | public: |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 488 | explicit ServerAsyncReaderWriter(Call* call) : call_(call) {} |
| 489 | |
| 490 | virtual void Read(R* msg, void* tag) { |
| 491 | // TODO |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 492 | } |
| 493 | |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 494 | virtual void Write(const W& msg, void* tag) { |
| 495 | // TODO |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | private: |
Yang Gao | 75ec2b1 | 2015-02-09 16:03:41 -0800 | [diff] [blame] | 499 | Call* call_; |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 500 | }; |
| 501 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 502 | } // namespace grpc |
| 503 | |
| 504 | #endif // __GRPCPP_STREAM_H__ |