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_COMPLETION_QUEUE_H |
| 35 | #define GRPCXX_COMPLETION_QUEUE_H |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 36 | |
vjpai | 7aadf46 | 2015-03-16 23:58:44 -0700 | [diff] [blame] | 37 | #include <chrono> |
Craig Tiller | 80e00a8 | 2015-02-09 20:54:25 -0800 | [diff] [blame] | 38 | #include <grpc++/impl/client_unary_call.h> |
Yang Gao | c4b6ffb | 2015-04-23 16:35:24 -0700 | [diff] [blame^] | 39 | #include <grpc++/impl/grpc_library.h> |
Yang Gao | b7b965c | 2015-03-20 15:27:51 -0700 | [diff] [blame] | 40 | #include <grpc/support/time.h> |
Craig Tiller | 80e00a8 | 2015-02-09 20:54:25 -0800 | [diff] [blame] | 41 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 42 | struct grpc_completion_queue; |
| 43 | |
| 44 | namespace grpc { |
| 45 | |
Craig Tiller | de91706 | 2015-02-09 17:15:03 -0800 | [diff] [blame] | 46 | template <class R> |
| 47 | class ClientReader; |
| 48 | template <class W> |
| 49 | class ClientWriter; |
| 50 | template <class R, class W> |
| 51 | class ClientReaderWriter; |
| 52 | template <class R> |
| 53 | class ServerReader; |
| 54 | template <class W> |
| 55 | class ServerWriter; |
| 56 | template <class R, class W> |
| 57 | class ServerReaderWriter; |
| 58 | |
Craig Tiller | 5095071 | 2015-02-09 10:38:38 -0800 | [diff] [blame] | 59 | class CompletionQueue; |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 60 | class Server; |
Craig Tiller | 645466e | 2015-02-18 09:18:33 -0800 | [diff] [blame] | 61 | class ServerContext; |
Craig Tiller | 5095071 | 2015-02-09 10:38:38 -0800 | [diff] [blame] | 62 | |
| 63 | class CompletionQueueTag { |
| 64 | public: |
Yang Gao | 31150f0 | 2015-02-12 16:44:00 -0800 | [diff] [blame] | 65 | virtual ~CompletionQueueTag() {} |
Craig Tiller | de91706 | 2015-02-09 17:15:03 -0800 | [diff] [blame] | 66 | // Called prior to returning from Next(), return value |
| 67 | // is the status of the operation (return status is the default thing |
| 68 | // to do) |
Craig Tiller | 645466e | 2015-02-18 09:18:33 -0800 | [diff] [blame] | 69 | // If this function returns false, the tag is dropped and not returned |
| 70 | // from the completion queue |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 71 | virtual bool FinalizeResult(void** tag, bool* status) = 0; |
Craig Tiller | 5095071 | 2015-02-09 10:38:38 -0800 | [diff] [blame] | 72 | }; |
| 73 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 74 | // grpc_completion_queue wrapper class |
Yang Gao | c4b6ffb | 2015-04-23 16:35:24 -0700 | [diff] [blame^] | 75 | class CompletionQueue : public GrpcLibrary { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 76 | public: |
| 77 | CompletionQueue(); |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 78 | explicit CompletionQueue(grpc_completion_queue* take); |
Yang Gao | c4b6ffb | 2015-04-23 16:35:24 -0700 | [diff] [blame^] | 79 | ~CompletionQueue() GRPC_OVERRIDE; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 80 | |
Vijay Pai | c41bf3c | 2015-03-12 05:19:34 -0700 | [diff] [blame] | 81 | // Tri-state return for AsyncNext: SHUTDOWN, GOT_EVENT, TIMEOUT |
Yang Gao | 757afae | 2015-03-17 15:49:26 -0700 | [diff] [blame] | 82 | enum NextStatus { SHUTDOWN, GOT_EVENT, TIMEOUT }; |
Vijay Pai | 3e0a46a | 2015-03-12 05:16:31 -0700 | [diff] [blame] | 83 | |
Vijay Pai | 3e0a46a | 2015-03-12 05:16:31 -0700 | [diff] [blame] | 84 | // Nonblocking (until deadline) read from queue. |
| 85 | // Cannot rely on result of tag or ok if return is TIMEOUT |
Yang Gao | 757afae | 2015-03-17 15:49:26 -0700 | [diff] [blame] | 86 | NextStatus AsyncNext(void** tag, bool* ok, |
| 87 | std::chrono::system_clock::time_point deadline); |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 88 | |
Vijay Pai | c41bf3c | 2015-03-12 05:19:34 -0700 | [diff] [blame] | 89 | // Blocking (until deadline) read from queue. |
| 90 | // Returns false if the queue is ready for destruction, true if event |
Yang Gao | 050e37a | 2015-03-19 07:27:28 -0700 | [diff] [blame] | 91 | |
Yang Gao | 757afae | 2015-03-17 15:49:26 -0700 | [diff] [blame] | 92 | bool Next(void** tag, bool* ok) { |
Yang Gao | b7b965c | 2015-03-20 15:27:51 -0700 | [diff] [blame] | 93 | return (AsyncNextInternal(tag, ok, gpr_inf_future) != SHUTDOWN); |
Yang Gao | 050e37a | 2015-03-19 07:27:28 -0700 | [diff] [blame] | 94 | } |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 95 | |
Yang Gao | 050e37a | 2015-03-19 07:27:28 -0700 | [diff] [blame] | 96 | // Shutdown has to be called, and the CompletionQueue can only be |
| 97 | // destructed when false is returned from Next(). |
| 98 | void Shutdown(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 99 | |
Yang Gao | 050e37a | 2015-03-19 07:27:28 -0700 | [diff] [blame] | 100 | grpc_completion_queue* cq() { return cq_; } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 101 | |
Yang Gao | 050e37a | 2015-03-19 07:27:28 -0700 | [diff] [blame] | 102 | private: |
| 103 | // Friend synchronous wrappers so that they can access Pluck(), which is |
| 104 | // a semi-private API geared towards the synchronous implementation. |
| 105 | template <class R> |
| 106 | friend class ::grpc::ClientReader; |
| 107 | template <class W> |
| 108 | friend class ::grpc::ClientWriter; |
| 109 | template <class R, class W> |
| 110 | friend class ::grpc::ClientReaderWriter; |
| 111 | template <class R> |
| 112 | friend class ::grpc::ServerReader; |
| 113 | template <class W> |
| 114 | friend class ::grpc::ServerWriter; |
| 115 | template <class R, class W> |
| 116 | friend class ::grpc::ServerReaderWriter; |
| 117 | friend class ::grpc::Server; |
| 118 | friend class ::grpc::ServerContext; |
| 119 | friend Status BlockingUnaryCall(ChannelInterface* channel, |
| 120 | const RpcMethod& method, |
| 121 | ClientContext* context, |
| 122 | const grpc::protobuf::Message& request, |
| 123 | grpc::protobuf::Message* result); |
Craig Tiller | de91706 | 2015-02-09 17:15:03 -0800 | [diff] [blame] | 124 | |
Yang Gao | b7b965c | 2015-03-20 15:27:51 -0700 | [diff] [blame] | 125 | NextStatus AsyncNextInternal(void** tag, bool* ok, gpr_timespec deadline); |
| 126 | |
Yang Gao | 050e37a | 2015-03-19 07:27:28 -0700 | [diff] [blame] | 127 | // Wraps grpc_completion_queue_pluck. |
| 128 | // Cannot be mixed with calls to Next(). |
| 129 | bool Pluck(CompletionQueueTag* tag); |
Craig Tiller | de91706 | 2015-02-09 17:15:03 -0800 | [diff] [blame] | 130 | |
Yang Gao | 050e37a | 2015-03-19 07:27:28 -0700 | [diff] [blame] | 131 | // Does a single polling pluck on tag |
| 132 | void TryPluck(CompletionQueueTag* tag); |
Craig Tiller | 645466e | 2015-02-18 09:18:33 -0800 | [diff] [blame] | 133 | |
Yang Gao | 050e37a | 2015-03-19 07:27:28 -0700 | [diff] [blame] | 134 | grpc_completion_queue* cq_; // owned |
| 135 | }; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 136 | |
| 137 | } // namespace grpc |
| 138 | |
Nicolas "Pixel" Noble | 1ff52d5 | 2015-03-01 05:24:36 +0100 | [diff] [blame] | 139 | #endif // GRPCXX_COMPLETION_QUEUE_H |