3
* Copyright 2015, Google Inc.
6
* Redistribution and use in source and binary forms, with or without
7
* modification, are permitted provided that the following conditions are
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
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.
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.
34
#ifndef GRPCXX_COMPLETION_QUEUE_H
35
#define GRPCXX_COMPLETION_QUEUE_H
37
#include <grpc/support/time.h>
38
#include <grpc++/impl/grpc_library.h>
39
#include <grpc++/status.h>
40
#include <grpc++/time.h>
42
struct grpc_completion_queue;
50
template <class R, class W>
51
class ClientReaderWriter;
56
template <class R, class W>
57
class ServerReaderWriter;
58
template <class ServiceType, class RequestType, class ResponseType>
59
class RpcMethodHandler;
60
template <class ServiceType, class RequestType, class ResponseType>
61
class ClientStreamingHandler;
62
template <class ServiceType, class RequestType, class ResponseType>
63
class ServerStreamingHandler;
64
template <class ServiceType, class RequestType, class ResponseType>
65
class BidiStreamingHandler;
67
class ChannelInterface;
69
class CompletionQueue;
75
class CompletionQueueTag {
77
virtual ~CompletionQueueTag() {}
78
// Called prior to returning from Next(), return value
79
// is the status of the operation (return status is the default thing
81
// If this function returns false, the tag is dropped and not returned
82
// from the completion queue
83
virtual bool FinalizeResult(void** tag, bool* status) = 0;
86
// grpc_completion_queue wrapper class
87
class CompletionQueue : public GrpcLibrary {
90
explicit CompletionQueue(grpc_completion_queue* take);
91
~CompletionQueue() GRPC_OVERRIDE;
93
// Tri-state return for AsyncNext: SHUTDOWN, GOT_EVENT, TIMEOUT
94
enum NextStatus { SHUTDOWN, GOT_EVENT, TIMEOUT };
96
// Nonblocking (until deadline) read from queue.
97
// Cannot rely on result of tag or ok if return is TIMEOUT
99
NextStatus AsyncNext(void** tag, bool* ok, const T& deadline) {
100
TimePoint<T> deadline_tp(deadline);
101
return AsyncNextInternal(tag, ok, deadline_tp.raw_time());
104
// Blocking read from queue.
105
// Returns false if the queue is ready for destruction, true if event
107
bool Next(void** tag, bool* ok) {
108
return (AsyncNextInternal(tag, ok, gpr_inf_future) != SHUTDOWN);
111
// Shutdown has to be called, and the CompletionQueue can only be
112
// destructed when false is returned from Next().
115
grpc_completion_queue* cq() { return cq_; }
118
// Friend synchronous wrappers so that they can access Pluck(), which is
119
// a semi-private API geared towards the synchronous implementation.
121
friend class ::grpc::ClientReader;
123
friend class ::grpc::ClientWriter;
124
template <class R, class W>
125
friend class ::grpc::ClientReaderWriter;
127
friend class ::grpc::ServerReader;
129
friend class ::grpc::ServerWriter;
130
template <class R, class W>
131
friend class ::grpc::ServerReaderWriter;
132
template <class ServiceType, class RequestType, class ResponseType>
133
friend class RpcMethodHandler;
134
template <class ServiceType, class RequestType, class ResponseType>
135
friend class ClientStreamingHandler;
136
template <class ServiceType, class RequestType, class ResponseType>
137
friend class ServerStreamingHandler;
138
template <class ServiceType, class RequestType, class ResponseType>
139
friend class BidiStreamingHandler;
140
friend class ::grpc::Server;
141
friend class ::grpc::ServerContext;
142
template <class InputMessage, class OutputMessage>
143
friend Status BlockingUnaryCall(ChannelInterface* channel,
144
const RpcMethod& method,
145
ClientContext* context,
146
const InputMessage& request,
147
OutputMessage* result);
149
NextStatus AsyncNextInternal(void** tag, bool* ok, gpr_timespec deadline);
151
// Wraps grpc_completion_queue_pluck.
152
// Cannot be mixed with calls to Next().
153
bool Pluck(CompletionQueueTag* tag);
155
// Does a single polling pluck on tag
156
void TryPluck(CompletionQueueTag* tag);
158
grpc_completion_queue* cq_; // owned
161
class ServerCompletionQueue : public CompletionQueue {
163
friend class ServerBuilder;
164
ServerCompletionQueue() {}
169
#endif // GRPCXX_COMPLETION_QUEUE_H