blob: c1f41723229bc215a1c21b16ad328003d77750e7 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
murgatroid993466c4b2016-01-12 10:26:04 -08003 * Copyright 2015-2016, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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 Tillerd6599a32015-09-03 09:37:02 -070034/// A completion queue implements a concurrent producer-consumer queue, with two
35/// main methods, \a Next and \a AsyncNext.
Nicolas "Pixel" Noble1ff52d52015-03-01 05:24:36 +010036#ifndef GRPCXX_COMPLETION_QUEUE_H
37#define GRPCXX_COMPLETION_QUEUE_H
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080038
Yang Gaob7b965c2015-03-20 15:27:51 -070039#include <grpc/support/time.h>
Yang Gaoc4b6ffb2015-04-23 16:35:24 -070040#include <grpc++/impl/grpc_library.h>
yang-g9e2f90c2015-08-21 15:35:03 -070041#include <grpc++/support/status.h>
42#include <grpc++/support/time.h>
Craig Tiller80e00a82015-02-09 20:54:25 -080043
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080044struct grpc_completion_queue;
45
46namespace grpc {
47
Craig Tillerde917062015-02-09 17:15:03 -080048template <class R>
49class ClientReader;
50template <class W>
51class ClientWriter;
Ian Coolidge8e7a4b62016-01-11 13:31:03 -080052template <class W, class R>
Craig Tillerde917062015-02-09 17:15:03 -080053class ClientReaderWriter;
54template <class R>
55class ServerReader;
56template <class W>
57class ServerWriter;
Ian Coolidge8e7a4b62016-01-11 13:31:03 -080058template <class W, class R>
Craig Tillerde917062015-02-09 17:15:03 -080059class ServerReaderWriter;
Craig Tiller50a7a682015-06-04 12:53:40 -070060template <class ServiceType, class RequestType, class ResponseType>
61class RpcMethodHandler;
62template <class ServiceType, class RequestType, class ResponseType>
63class ClientStreamingHandler;
64template <class ServiceType, class RequestType, class ResponseType>
65class ServerStreamingHandler;
66template <class ServiceType, class RequestType, class ResponseType>
67class BidiStreamingHandler;
yang-g9b7757d2015-08-13 11:15:53 -070068class UnknownMethodHandler;
Craig Tillerde917062015-02-09 17:15:03 -080069
yang-g8c2be9f2015-08-19 16:28:09 -070070class Channel;
David Garcia Quintase50c6c22016-01-13 16:02:00 -080071class ChannelInterface;
Craig Tiller81fafa82015-06-04 08:51:17 -070072class ClientContext;
Craig Tillerd6599a32015-09-03 09:37:02 -070073class CompletionQueueTag;
Craig Tiller50950712015-02-09 10:38:38 -080074class CompletionQueue;
Craig Tiller81fafa82015-06-04 08:51:17 -070075class RpcMethod;
Craig Tillerc4165772015-02-11 10:51:04 -080076class Server;
Craig Tillerf9e6adf2015-05-06 11:45:59 -070077class ServerBuilder;
Craig Tiller645466e2015-02-18 09:18:33 -080078class ServerContext;
Craig Tiller50950712015-02-09 10:38:38 -080079
Craig Tillerd6599a32015-09-03 09:37:02 -070080/// A thin wrapper around \a grpc_completion_queue (see / \a
81/// src/core/surface/completion_queue.h).
Yang Gaoc4b6ffb2015-04-23 16:35:24 -070082class CompletionQueue : public GrpcLibrary {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080083 public:
Craig Tillerd6599a32015-09-03 09:37:02 -070084 /// Default constructor. Implicitly creates a \a grpc_completion_queue
85 /// instance.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080086 CompletionQueue();
Craig Tillerd6599a32015-09-03 09:37:02 -070087
88 /// Wrap \a take, taking ownership of the instance.
89 ///
90 /// \param take The completion queue instance to wrap. Ownership is taken.
Yang Gao6baa9b62015-03-17 10:49:39 -070091 explicit CompletionQueue(grpc_completion_queue* take);
Craig Tillerd6599a32015-09-03 09:37:02 -070092
93 /// Destructor. Destroys the owned wrapped completion queue / instance.
Yang Gaoc4b6ffb2015-04-23 16:35:24 -070094 ~CompletionQueue() GRPC_OVERRIDE;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080095
Craig Tillerd6599a32015-09-03 09:37:02 -070096 /// 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 Pai3e0a46a2015-03-12 05:16:31 -0700103
Craig Tillerd6599a32015-09-03 09:37:02 -0700104 /// 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 Tillerce40de52015-06-05 07:14:58 -0700114 template <typename T>
Nicolas Noble89219162015-04-07 18:01:18 -0700115 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 Tillerc4965752015-02-09 09:51:00 -0800119
Craig Tillerd6599a32015-09-03 09:37:02 -0700120 /// 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 Gao757afae2015-03-17 15:49:26 -0700127 bool Next(void** tag, bool* ok) {
Craig Tiller143e7bf2015-07-13 08:41:49 -0700128 return (AsyncNextInternal(tag, ok, gpr_inf_future(GPR_CLOCK_REALTIME)) !=
129 SHUTDOWN);
Yang Gao050e37a2015-03-19 07:27:28 -0700130 }
Craig Tillerc4965752015-02-09 09:51:00 -0800131
Craig Tillerd6599a32015-09-03 09:37:02 -0700132 /// 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 Gao050e37a2015-03-19 07:27:28 -0700139 void Shutdown();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800140
Craig Tillerd6599a32015-09-03 09:37:02 -0700141 /// 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 Gao050e37a2015-03-19 07:27:28 -0700146 grpc_completion_queue* cq() { return cq_; }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800147
Yang Gao050e37a2015-03-19 07:27:28 -0700148 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 Coolidge8e7a4b62016-01-11 13:31:03 -0800155 template <class W, class R>
Yang Gao050e37a2015-03-19 07:27:28 -0700156 friend class ::grpc::ClientReaderWriter;
157 template <class R>
158 friend class ::grpc::ServerReader;
159 template <class W>
160 friend class ::grpc::ServerWriter;
Ian Coolidge8e7a4b62016-01-11 13:31:03 -0800161 template <class W, class R>
Yang Gao050e37a2015-03-19 07:27:28 -0700162 friend class ::grpc::ServerReaderWriter;
Craig Tiller50a7a682015-06-04 12:53:40 -0700163 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-g9b7757d2015-08-13 11:15:53 -0700171 friend class UnknownMethodHandler;
Yang Gao050e37a2015-03-19 07:27:28 -0700172 friend class ::grpc::Server;
173 friend class ::grpc::ServerContext;
Craig Tiller81fafa82015-06-04 08:51:17 -0700174 template <class InputMessage, class OutputMessage>
David Garcia Quintasf3ddb7c2016-01-20 16:02:22 -0800175 friend Status BlockingUnaryCall(ChannelInterface* channel,
176 const RpcMethod& method,
Yang Gao050e37a2015-03-19 07:27:28 -0700177 ClientContext* context,
Craig Tiller81fafa82015-06-04 08:51:17 -0700178 const InputMessage& request,
179 OutputMessage* result);
Craig Tillerde917062015-02-09 17:15:03 -0800180
Yang Gaob7b965c2015-03-20 15:27:51 -0700181 NextStatus AsyncNextInternal(void** tag, bool* ok, gpr_timespec deadline);
182
Craig Tillerd6599a32015-09-03 09:37:02 -0700183 /// Wraps \a grpc_completion_queue_pluck.
184 /// \warning Must not be mixed with calls to \a Next.
Yang Gao050e37a2015-03-19 07:27:28 -0700185 bool Pluck(CompletionQueueTag* tag);
Craig Tillerde917062015-02-09 17:15:03 -0800186
Craig Tillerd6599a32015-09-03 09:37:02 -0700187 /// Performs a single polling pluck on \a tag.
Yang Gao050e37a2015-03-19 07:27:28 -0700188 void TryPluck(CompletionQueueTag* tag);
Craig Tiller645466e2015-02-18 09:18:33 -0800189
Yang Gao050e37a2015-03-19 07:27:28 -0700190 grpc_completion_queue* cq_; // owned
191};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800192
Craig Tillerd6599a32015-09-03 09:37:02 -0700193/// An interface allowing implementors to process and filter event tags.
194class 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 Tillerf9e6adf2015-05-06 11:45:59 -0700206class ServerCompletionQueue : public CompletionQueue {
207 private:
208 friend class ServerBuilder;
209 ServerCompletionQueue() {}
210};
211
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800212} // namespace grpc
213
Nicolas "Pixel" Noble1ff52d52015-03-01 05:24:36 +0100214#endif // GRPCXX_COMPLETION_QUEUE_H