Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
murgatroid99 | 3466c4b | 2016-01-12 10:26:04 -0800 | [diff] [blame] | 3 | * Copyright 2015-2016, 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 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 34 | /// A completion queue implements a concurrent producer-consumer queue, with two |
| 35 | /// main methods, \a Next and \a AsyncNext. |
Nicolas "Pixel" Noble | 1ff52d5 | 2015-03-01 05:24:36 +0100 | [diff] [blame] | 36 | #ifndef GRPCXX_COMPLETION_QUEUE_H |
| 37 | #define GRPCXX_COMPLETION_QUEUE_H |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 38 | |
Yang Gao | b7b965c | 2015-03-20 15:27:51 -0700 | [diff] [blame] | 39 | #include <grpc/support/time.h> |
Yang Gao | c4b6ffb | 2015-04-23 16:35:24 -0700 | [diff] [blame] | 40 | #include <grpc++/impl/grpc_library.h> |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 41 | #include <grpc++/support/status.h> |
| 42 | #include <grpc++/support/time.h> |
Craig Tiller | 80e00a8 | 2015-02-09 20:54:25 -0800 | [diff] [blame] | 43 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 44 | struct grpc_completion_queue; |
| 45 | |
| 46 | namespace grpc { |
| 47 | |
Craig Tiller | de91706 | 2015-02-09 17:15:03 -0800 | [diff] [blame] | 48 | template <class R> |
| 49 | class ClientReader; |
| 50 | template <class W> |
| 51 | class ClientWriter; |
Ian Coolidge | 8e7a4b6 | 2016-01-11 13:31:03 -0800 | [diff] [blame] | 52 | template <class W, class R> |
Craig Tiller | de91706 | 2015-02-09 17:15:03 -0800 | [diff] [blame] | 53 | class ClientReaderWriter; |
| 54 | template <class R> |
| 55 | class ServerReader; |
| 56 | template <class W> |
| 57 | class ServerWriter; |
Ian Coolidge | 8e7a4b6 | 2016-01-11 13:31:03 -0800 | [diff] [blame] | 58 | template <class W, class R> |
Craig Tiller | de91706 | 2015-02-09 17:15:03 -0800 | [diff] [blame] | 59 | class ServerReaderWriter; |
Craig Tiller | 50a7a68 | 2015-06-04 12:53:40 -0700 | [diff] [blame] | 60 | template <class ServiceType, class RequestType, class ResponseType> |
| 61 | class RpcMethodHandler; |
| 62 | template <class ServiceType, class RequestType, class ResponseType> |
| 63 | class ClientStreamingHandler; |
| 64 | template <class ServiceType, class RequestType, class ResponseType> |
| 65 | class ServerStreamingHandler; |
| 66 | template <class ServiceType, class RequestType, class ResponseType> |
| 67 | class BidiStreamingHandler; |
yang-g | 9b7757d | 2015-08-13 11:15:53 -0700 | [diff] [blame] | 68 | class UnknownMethodHandler; |
Craig Tiller | de91706 | 2015-02-09 17:15:03 -0800 | [diff] [blame] | 69 | |
yang-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame] | 70 | class Channel; |
David Garcia Quintas | e50c6c2 | 2016-01-13 16:02:00 -0800 | [diff] [blame] | 71 | class ChannelInterface; |
Craig Tiller | 81fafa8 | 2015-06-04 08:51:17 -0700 | [diff] [blame] | 72 | class ClientContext; |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 73 | class CompletionQueueTag; |
Craig Tiller | 5095071 | 2015-02-09 10:38:38 -0800 | [diff] [blame] | 74 | class CompletionQueue; |
Craig Tiller | 81fafa8 | 2015-06-04 08:51:17 -0700 | [diff] [blame] | 75 | class RpcMethod; |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 76 | class Server; |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 77 | class ServerBuilder; |
Craig Tiller | 645466e | 2015-02-18 09:18:33 -0800 | [diff] [blame] | 78 | class ServerContext; |
Craig Tiller | 5095071 | 2015-02-09 10:38:38 -0800 | [diff] [blame] | 79 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 80 | /// A thin wrapper around \a grpc_completion_queue (see / \a |
| 81 | /// src/core/surface/completion_queue.h). |
Yang Gao | c4b6ffb | 2015-04-23 16:35:24 -0700 | [diff] [blame] | 82 | class CompletionQueue : public GrpcLibrary { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 83 | public: |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 84 | /// Default constructor. Implicitly creates a \a grpc_completion_queue |
| 85 | /// instance. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 86 | CompletionQueue(); |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 87 | |
| 88 | /// Wrap \a take, taking ownership of the instance. |
| 89 | /// |
| 90 | /// \param take The completion queue instance to wrap. Ownership is taken. |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 91 | explicit CompletionQueue(grpc_completion_queue* take); |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 92 | |
| 93 | /// Destructor. Destroys the owned wrapped completion queue / instance. |
Yang Gao | c4b6ffb | 2015-04-23 16:35:24 -0700 | [diff] [blame] | 94 | ~CompletionQueue() GRPC_OVERRIDE; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 95 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 96 | /// Tri-state return for AsyncNext: SHUTDOWN, GOT_EVENT, TIMEOUT. |
| 97 | enum NextStatus { |
| 98 | SHUTDOWN, ///< The completion queue has been shutdown. |
| 99 | GOT_EVENT, ///< Got a new event; \a tag will be filled in with its |
| 100 | ///< associated value; \a ok indicating its success. |
| 101 | TIMEOUT ///< deadline was reached. |
| 102 | }; |
Vijay Pai | 3e0a46a | 2015-03-12 05:16:31 -0700 | [diff] [blame] | 103 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 104 | /// Read from the queue, blocking up to \a deadline (or the queue's shutdown). |
| 105 | /// Both \a tag and \a ok are updated upon success (if an event is available |
| 106 | /// within the \a deadline). A \a tag points to an arbitrary location usually |
| 107 | /// employed to uniquely identify an event. |
| 108 | /// |
| 109 | /// \param tag[out] Upon sucess, updated to point to the event's tag. |
| 110 | /// \param ok[out] Upon sucess, true if read a regular event, false otherwise. |
| 111 | /// \param deadline[in] How long to block in wait for an event. |
| 112 | /// |
| 113 | /// \return The type of event read. |
Craig Tiller | ce40de5 | 2015-06-05 07:14:58 -0700 | [diff] [blame] | 114 | template <typename T> |
Nicolas Noble | 8921916 | 2015-04-07 18:01:18 -0700 | [diff] [blame] | 115 | NextStatus AsyncNext(void** tag, bool* ok, const T& deadline) { |
| 116 | TimePoint<T> deadline_tp(deadline); |
| 117 | return AsyncNextInternal(tag, ok, deadline_tp.raw_time()); |
| 118 | } |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 119 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 120 | /// Read from the queue, blocking until an event is available or the queue is |
| 121 | /// shutting down. |
| 122 | /// |
| 123 | /// \param tag[out] Updated to point to the read event's tag. |
| 124 | /// \param ok[out] true if read a regular event, false otherwise. |
| 125 | /// |
| 126 | /// \return true if read a regular event, false if the queue is shutting down. |
Yang Gao | 757afae | 2015-03-17 15:49:26 -0700 | [diff] [blame] | 127 | bool Next(void** tag, bool* ok) { |
Craig Tiller | 143e7bf | 2015-07-13 08:41:49 -0700 | [diff] [blame] | 128 | return (AsyncNextInternal(tag, ok, gpr_inf_future(GPR_CLOCK_REALTIME)) != |
| 129 | SHUTDOWN); |
Yang Gao | 050e37a | 2015-03-19 07:27:28 -0700 | [diff] [blame] | 130 | } |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 131 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 132 | /// Request the shutdown of the queue. |
| 133 | /// |
| 134 | /// \warning This method must be called at some point. Once invoked, \a Next |
| 135 | /// will start to return false and \a AsyncNext will return \a |
| 136 | /// NextStatus::SHUTDOWN. Only once either one of these methods does that |
| 137 | /// (that is, once the queue has been \em drained) can an instance of this |
| 138 | /// class be destroyed. |
Yang Gao | 050e37a | 2015-03-19 07:27:28 -0700 | [diff] [blame] | 139 | void Shutdown(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 140 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 141 | /// Returns a \em raw pointer to the underlying \a grpc_completion_queue |
| 142 | /// instance. |
| 143 | /// |
| 144 | /// \warning Remember that the returned instance is owned. No transfer of |
| 145 | /// owership is performed. |
Yang Gao | 050e37a | 2015-03-19 07:27:28 -0700 | [diff] [blame] | 146 | grpc_completion_queue* cq() { return cq_; } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 147 | |
Yang Gao | 050e37a | 2015-03-19 07:27:28 -0700 | [diff] [blame] | 148 | private: |
| 149 | // Friend synchronous wrappers so that they can access Pluck(), which is |
| 150 | // a semi-private API geared towards the synchronous implementation. |
| 151 | template <class R> |
| 152 | friend class ::grpc::ClientReader; |
| 153 | template <class W> |
| 154 | friend class ::grpc::ClientWriter; |
Ian Coolidge | 8e7a4b6 | 2016-01-11 13:31:03 -0800 | [diff] [blame] | 155 | template <class W, class R> |
Yang Gao | 050e37a | 2015-03-19 07:27:28 -0700 | [diff] [blame] | 156 | friend class ::grpc::ClientReaderWriter; |
| 157 | template <class R> |
| 158 | friend class ::grpc::ServerReader; |
| 159 | template <class W> |
| 160 | friend class ::grpc::ServerWriter; |
Ian Coolidge | 8e7a4b6 | 2016-01-11 13:31:03 -0800 | [diff] [blame] | 161 | template <class W, class R> |
Yang Gao | 050e37a | 2015-03-19 07:27:28 -0700 | [diff] [blame] | 162 | friend class ::grpc::ServerReaderWriter; |
Craig Tiller | 50a7a68 | 2015-06-04 12:53:40 -0700 | [diff] [blame] | 163 | template <class ServiceType, class RequestType, class ResponseType> |
| 164 | friend class RpcMethodHandler; |
| 165 | template <class ServiceType, class RequestType, class ResponseType> |
| 166 | friend class ClientStreamingHandler; |
| 167 | template <class ServiceType, class RequestType, class ResponseType> |
| 168 | friend class ServerStreamingHandler; |
| 169 | template <class ServiceType, class RequestType, class ResponseType> |
| 170 | friend class BidiStreamingHandler; |
yang-g | 9b7757d | 2015-08-13 11:15:53 -0700 | [diff] [blame] | 171 | friend class UnknownMethodHandler; |
Yang Gao | 050e37a | 2015-03-19 07:27:28 -0700 | [diff] [blame] | 172 | friend class ::grpc::Server; |
| 173 | friend class ::grpc::ServerContext; |
Craig Tiller | 81fafa8 | 2015-06-04 08:51:17 -0700 | [diff] [blame] | 174 | template <class InputMessage, class OutputMessage> |
David Garcia Quintas | f3ddb7c | 2016-01-20 16:02:22 -0800 | [diff] [blame^] | 175 | friend Status BlockingUnaryCall(ChannelInterface* channel, |
| 176 | const RpcMethod& method, |
Yang Gao | 050e37a | 2015-03-19 07:27:28 -0700 | [diff] [blame] | 177 | ClientContext* context, |
Craig Tiller | 81fafa8 | 2015-06-04 08:51:17 -0700 | [diff] [blame] | 178 | const InputMessage& request, |
| 179 | OutputMessage* result); |
Craig Tiller | de91706 | 2015-02-09 17:15:03 -0800 | [diff] [blame] | 180 | |
Yang Gao | b7b965c | 2015-03-20 15:27:51 -0700 | [diff] [blame] | 181 | NextStatus AsyncNextInternal(void** tag, bool* ok, gpr_timespec deadline); |
| 182 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 183 | /// Wraps \a grpc_completion_queue_pluck. |
| 184 | /// \warning Must not be mixed with calls to \a Next. |
Yang Gao | 050e37a | 2015-03-19 07:27:28 -0700 | [diff] [blame] | 185 | bool Pluck(CompletionQueueTag* tag); |
Craig Tiller | de91706 | 2015-02-09 17:15:03 -0800 | [diff] [blame] | 186 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 187 | /// Performs a single polling pluck on \a tag. |
Yang Gao | 050e37a | 2015-03-19 07:27:28 -0700 | [diff] [blame] | 188 | void TryPluck(CompletionQueueTag* tag); |
Craig Tiller | 645466e | 2015-02-18 09:18:33 -0800 | [diff] [blame] | 189 | |
Yang Gao | 050e37a | 2015-03-19 07:27:28 -0700 | [diff] [blame] | 190 | grpc_completion_queue* cq_; // owned |
| 191 | }; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 192 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 193 | /// An interface allowing implementors to process and filter event tags. |
| 194 | class CompletionQueueTag { |
| 195 | public: |
| 196 | virtual ~CompletionQueueTag() {} |
| 197 | // Called prior to returning from Next(), return value is the status of the |
| 198 | // operation (return status is the default thing to do). If this function |
| 199 | // returns false, the tag is dropped and not returned from the completion |
| 200 | // queue |
| 201 | virtual bool FinalizeResult(void** tag, bool* status) = 0; |
| 202 | }; |
| 203 | |
| 204 | /// A specific type of completion queue used by the processing of notifications |
| 205 | /// by servers. Instantiated by \a ServerBuilder. |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 206 | class ServerCompletionQueue : public CompletionQueue { |
| 207 | private: |
| 208 | friend class ServerBuilder; |
| 209 | ServerCompletionQueue() {} |
| 210 | }; |
| 211 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 212 | } // namespace grpc |
| 213 | |
Nicolas "Pixel" Noble | 1ff52d5 | 2015-03-01 05:24:36 +0100 | [diff] [blame] | 214 | #endif // GRPCXX_COMPLETION_QUEUE_H |