stream.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. #ifndef __GRPCPP_STREAM_H__
  34. #define __GRPCPP_STREAM_H__
  35. #include <grpc++/call.h>
  36. #include <grpc++/channel_interface.h>
  37. #include <grpc++/completion_queue.h>
  38. #include <grpc++/status.h>
  39. #include <grpc/support/log.h>
  40. namespace grpc {
  41. // DELETE DELETE DELETE
  42. // DELETE DELETE DELETE
  43. // DELETE DELETE DELETE
  44. // DELETE DELETE DELETE
  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. class StreamContextInterface {
  66. public:
  67. template <class T> bool Write(T, bool);
  68. template <class T> void Start(T);
  69. template <class T> bool Read(T);
  70. google::protobuf::Message *request();
  71. };
  72. // Common interface for all client side streaming.
  73. class ClientStreamingInterface {
  74. public:
  75. virtual ~ClientStreamingInterface() {}
  76. // Wait until the stream finishes, and return the final status. When the
  77. // client side declares it has no more message to send, either implicitly or
  78. // by calling WritesDone, it needs to make sure there is no more message to
  79. // be received from the server, either implicitly or by getting a false from
  80. // a Read(). Otherwise, this implicitly cancels the stream.
  81. virtual Status Finish() = 0;
  82. };
  83. // An interface that yields a sequence of R messages.
  84. template <class R>
  85. class ReaderInterface {
  86. public:
  87. virtual ~ReaderInterface() {}
  88. // Blocking read a message and parse to msg. Returns true on success.
  89. // The method returns false when there will be no more incoming messages,
  90. // either because the other side has called WritesDone or the stream has
  91. // failed (or been cancelled).
  92. virtual bool Read(R* msg) = 0;
  93. };
  94. // An interface that can be fed a sequence of W messages.
  95. template <class W>
  96. class WriterInterface {
  97. public:
  98. virtual ~WriterInterface() {}
  99. // Blocking write msg to the stream. Returns true on success.
  100. // Returns false when the stream has been closed.
  101. virtual bool Write(const W& msg) = 0;
  102. };
  103. template <class R>
  104. class ClientReader final : public ClientStreamingInterface,
  105. public ReaderInterface<R> {
  106. public:
  107. // Blocking create a stream and write the first request out.
  108. ClientReader(ChannelInterface *channel, const RpcMethod &method,
  109. ClientContext *context,
  110. const google::protobuf::Message &request)
  111. : call_(channel->CreateCall(method, context, &cq_)) {
  112. CallOpBuffer buf;
  113. buf.AddSendMessage(request);
  114. buf.AddClientSendClose();
  115. call_.PerformOps(&buf, (void *)1);
  116. cq_.Pluck((void *)1);
  117. }
  118. virtual bool Read(R *msg) override {
  119. CallOpBuffer buf;
  120. buf.AddRecvMessage(msg);
  121. call_.PerformOps(&buf, (void *)2);
  122. return cq_.Pluck((void *)2);
  123. }
  124. virtual Status Finish() override {
  125. CallOpBuffer buf;
  126. Status status;
  127. buf.AddClientRecvStatus(&status);
  128. call_.PerformOps(&buf, (void *)3);
  129. GPR_ASSERT(cq_.Pluck((void *)3));
  130. return status;
  131. }
  132. private:
  133. CompletionQueue cq_;
  134. Call call_;
  135. };
  136. template <class W>
  137. class ClientWriter final : public ClientStreamingInterface,
  138. public WriterInterface<W> {
  139. public:
  140. // Blocking create a stream.
  141. ClientWriter(ChannelInterface *channel, const RpcMethod &method,
  142. ClientContext *context,
  143. google::protobuf::Message *response)
  144. : response_(response),
  145. call_(channel->CreateCall(method, context, &cq_)) {}
  146. virtual bool Write(const W& msg) override {
  147. CallOpBuffer buf;
  148. buf.AddSendMessage(msg);
  149. call_.PerformOps(&buf, (void *)2);
  150. return cq_.Pluck((void *)2);
  151. }
  152. virtual bool WritesDone() {
  153. CallOpBuffer buf;
  154. buf.AddClientSendClose();
  155. call_.PerformOps(&buf, (void *)3);
  156. return cq_.Pluck((void *)3);
  157. }
  158. // Read the final response and wait for the final status.
  159. virtual Status Finish() override {
  160. CallOpBuffer buf;
  161. Status status;
  162. buf.AddClientRecvStatus(&status);
  163. call_.PerformOps(&buf, (void *)4);
  164. GPR_ASSERT(cq_.Pluck((void *)4));
  165. return status;
  166. }
  167. private:
  168. google::protobuf::Message *const response_;
  169. CompletionQueue cq_;
  170. Call call_;
  171. };
  172. // Client-side interface for bi-directional streaming.
  173. template <class W, class R>
  174. class ClientReaderWriter final : public ClientStreamingInterface,
  175. public WriterInterface<W>,
  176. public ReaderInterface<R> {
  177. public:
  178. // Blocking create a stream.
  179. ClientReaderWriter(ChannelInterface *channel,
  180. const RpcMethod &method, ClientContext *context)
  181. : call_(channel->CreateCall(method, context, &cq_)) {}
  182. virtual bool Read(R *msg) override {
  183. CallOpBuffer buf;
  184. buf.AddRecvMessage(msg);
  185. call_.PerformOps(&buf, (void *)2);
  186. return cq_.Pluck((void *)2);
  187. }
  188. virtual bool Write(const W& msg) override {
  189. CallOpBuffer buf;
  190. buf.AddSendMessage(msg);
  191. call_.PerformOps(&buf, (void *)3);
  192. return cq_.Pluck((void *)3);
  193. }
  194. virtual bool WritesDone() {
  195. CallOpBuffer buf;
  196. buf.AddClientSendClose();
  197. call_.PerformOps(&buf, (void *)4);
  198. return cq_.Pluck((void *)4);
  199. }
  200. virtual Status Finish() override {
  201. CallOpBuffer buf;
  202. Status status;
  203. buf.AddClientRecvStatus(&status);
  204. call_.PerformOps(&buf, (void *)5);
  205. GPR_ASSERT(cq_.Pluck((void *)5));
  206. return status;
  207. }
  208. private:
  209. CompletionQueue cq_;
  210. Call call_;
  211. };
  212. template <class R>
  213. class ServerReader final : public ReaderInterface<R> {
  214. public:
  215. explicit ServerReader(Call* call) : call_(call) {}
  216. virtual bool Read(R* msg) override {
  217. CallOpBuffer buf;
  218. buf.AddRecvMessage(msg);
  219. call_->PerformOps(&buf, (void *)2);
  220. return call_->cq()->Pluck((void *)2);
  221. }
  222. private:
  223. Call* call_;
  224. };
  225. template <class W>
  226. class ServerWriter : public WriterInterface<W> {
  227. public:
  228. explicit ServerWriter(StreamContextInterface* context) : context_(context) {
  229. GPR_ASSERT(context_);
  230. context_->Start(true);
  231. context_->Read(context_->request());
  232. }
  233. virtual bool Write(const W& msg) {
  234. return context_->Write(const_cast<W*>(&msg), false);
  235. }
  236. private:
  237. StreamContextInterface* const context_; // not owned
  238. };
  239. // Server-side interface for bi-directional streaming.
  240. template <class W, class R>
  241. class ServerReaderWriter : public WriterInterface<W>,
  242. public ReaderInterface<R> {
  243. public:
  244. explicit ServerReaderWriter(StreamContextInterface* context)
  245. : context_(context) {
  246. GPR_ASSERT(context_);
  247. context_->Start(true);
  248. }
  249. virtual bool Read(R* msg) { return context_->Read(msg); }
  250. virtual bool Write(const W& msg) {
  251. return context_->Write(const_cast<W*>(&msg), false);
  252. }
  253. private:
  254. StreamContextInterface* const context_; // not owned
  255. };
  256. template <class W>
  257. class ServerAsyncResponseWriter {
  258. public:
  259. explicit ServerAsyncResponseWriter(StreamContextInterface* context) : context_(context) {
  260. GPR_ASSERT(context_);
  261. context_->Start(true);
  262. context_->Read(context_->request());
  263. }
  264. virtual bool Write(const W& msg) {
  265. return context_->Write(const_cast<W*>(&msg), false);
  266. }
  267. private:
  268. StreamContextInterface* const context_; // not owned
  269. };
  270. template <class R>
  271. class ServerAsyncReader : public ReaderInterface<R> {
  272. public:
  273. explicit ServerAsyncReader(StreamContextInterface* context) : context_(context) {
  274. GPR_ASSERT(context_);
  275. context_->Start(true);
  276. }
  277. virtual bool Read(R* msg) { return context_->Read(msg); }
  278. private:
  279. StreamContextInterface* const context_; // not owned
  280. };
  281. template <class W>
  282. class ServerAsyncWriter : public WriterInterface<W> {
  283. public:
  284. explicit ServerAsyncWriter(StreamContextInterface* context) : context_(context) {
  285. GPR_ASSERT(context_);
  286. context_->Start(true);
  287. context_->Read(context_->request());
  288. }
  289. virtual bool Write(const W& msg) {
  290. return context_->Write(const_cast<W*>(&msg), false);
  291. }
  292. private:
  293. StreamContextInterface* const context_; // not owned
  294. };
  295. // Server-side interface for bi-directional streaming.
  296. template <class W, class R>
  297. class ServerAsyncReaderWriter : public WriterInterface<W>,
  298. public ReaderInterface<R> {
  299. public:
  300. explicit ServerAsyncReaderWriter(StreamContextInterface* context)
  301. : context_(context) {
  302. GPR_ASSERT(context_);
  303. context_->Start(true);
  304. }
  305. virtual bool Read(R* msg) { return context_->Read(msg); }
  306. virtual bool Write(const W& msg) {
  307. return context_->Write(const_cast<W*>(&msg), false);
  308. }
  309. private:
  310. StreamContextInterface* const context_; // not owned
  311. };
  312. } // namespace grpc
  313. #endif // __GRPCPP_STREAM_H__