blob: 0a2a7687b75eb8e49fd33b13f3dfa2de20361613 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, 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
Nicolas "Pixel" Noble1ff52d52015-03-01 05:24:36 +010034#ifndef GRPCXX_COMPLETION_QUEUE_H
35#define GRPCXX_COMPLETION_QUEUE_H
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080036
vjpai7aadf462015-03-16 23:58:44 -070037#include <chrono>
Craig Tiller80e00a82015-02-09 20:54:25 -080038#include <grpc++/impl/client_unary_call.h>
Yang Gaoc4b6ffb2015-04-23 16:35:24 -070039#include <grpc++/impl/grpc_library.h>
Yang Gaob7b965c2015-03-20 15:27:51 -070040#include <grpc/support/time.h>
Craig Tiller80e00a82015-02-09 20:54:25 -080041
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080042struct grpc_completion_queue;
43
44namespace grpc {
45
Craig Tillerde917062015-02-09 17:15:03 -080046template <class R>
47class ClientReader;
48template <class W>
49class ClientWriter;
50template <class R, class W>
51class ClientReaderWriter;
52template <class R>
53class ServerReader;
54template <class W>
55class ServerWriter;
56template <class R, class W>
57class ServerReaderWriter;
58
Craig Tiller50950712015-02-09 10:38:38 -080059class CompletionQueue;
Craig Tillerc4165772015-02-11 10:51:04 -080060class Server;
Craig Tiller645466e2015-02-18 09:18:33 -080061class ServerContext;
Craig Tiller50950712015-02-09 10:38:38 -080062
63class CompletionQueueTag {
64 public:
Yang Gao31150f02015-02-12 16:44:00 -080065 virtual ~CompletionQueueTag() {}
Craig Tillerde917062015-02-09 17:15:03 -080066 // 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 Tiller645466e2015-02-18 09:18:33 -080069 // If this function returns false, the tag is dropped and not returned
70 // from the completion queue
Yang Gao6baa9b62015-03-17 10:49:39 -070071 virtual bool FinalizeResult(void** tag, bool* status) = 0;
Craig Tiller50950712015-02-09 10:38:38 -080072};
73
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080074// grpc_completion_queue wrapper class
Yang Gaoc4b6ffb2015-04-23 16:35:24 -070075class CompletionQueue : public GrpcLibrary {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080076 public:
77 CompletionQueue();
Yang Gao6baa9b62015-03-17 10:49:39 -070078 explicit CompletionQueue(grpc_completion_queue* take);
Yang Gaoc4b6ffb2015-04-23 16:35:24 -070079 ~CompletionQueue() GRPC_OVERRIDE;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080080
Vijay Paic41bf3c2015-03-12 05:19:34 -070081 // Tri-state return for AsyncNext: SHUTDOWN, GOT_EVENT, TIMEOUT
Yang Gao757afae2015-03-17 15:49:26 -070082 enum NextStatus { SHUTDOWN, GOT_EVENT, TIMEOUT };
Vijay Pai3e0a46a2015-03-12 05:16:31 -070083
Vijay Pai3e0a46a2015-03-12 05:16:31 -070084 // Nonblocking (until deadline) read from queue.
85 // Cannot rely on result of tag or ok if return is TIMEOUT
Yang Gao757afae2015-03-17 15:49:26 -070086 NextStatus AsyncNext(void** tag, bool* ok,
87 std::chrono::system_clock::time_point deadline);
Craig Tillerc4965752015-02-09 09:51:00 -080088
Vijay Paic41bf3c2015-03-12 05:19:34 -070089 // Blocking (until deadline) read from queue.
90 // Returns false if the queue is ready for destruction, true if event
Yang Gao050e37a2015-03-19 07:27:28 -070091
Yang Gao757afae2015-03-17 15:49:26 -070092 bool Next(void** tag, bool* ok) {
Yang Gaob7b965c2015-03-20 15:27:51 -070093 return (AsyncNextInternal(tag, ok, gpr_inf_future) != SHUTDOWN);
Yang Gao050e37a2015-03-19 07:27:28 -070094 }
Craig Tillerc4965752015-02-09 09:51:00 -080095
Yang Gao050e37a2015-03-19 07:27:28 -070096 // Shutdown has to be called, and the CompletionQueue can only be
97 // destructed when false is returned from Next().
98 void Shutdown();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080099
Yang Gao050e37a2015-03-19 07:27:28 -0700100 grpc_completion_queue* cq() { return cq_; }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800101
Yang Gao050e37a2015-03-19 07:27:28 -0700102 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 Tillerde917062015-02-09 17:15:03 -0800124
Yang Gaob7b965c2015-03-20 15:27:51 -0700125 NextStatus AsyncNextInternal(void** tag, bool* ok, gpr_timespec deadline);
126
Yang Gao050e37a2015-03-19 07:27:28 -0700127 // Wraps grpc_completion_queue_pluck.
128 // Cannot be mixed with calls to Next().
129 bool Pluck(CompletionQueueTag* tag);
Craig Tillerde917062015-02-09 17:15:03 -0800130
Yang Gao050e37a2015-03-19 07:27:28 -0700131 // Does a single polling pluck on tag
132 void TryPluck(CompletionQueueTag* tag);
Craig Tiller645466e2015-02-18 09:18:33 -0800133
Yang Gao050e37a2015-03-19 07:27:28 -0700134 grpc_completion_queue* cq_; // owned
135};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800136
137} // namespace grpc
138
Nicolas "Pixel" Noble1ff52d52015-03-01 05:24:36 +0100139#endif // GRPCXX_COMPLETION_QUEUE_H