GRPC C++ 0.10.0.0

- Main Page
- Namespaces
- Data Structures
- Files

- File List
- Globals

All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
completion_queue.h
Go to the documentation of this file.
1 /*
2 *
3 * Copyright 2015, Google Inc.
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
34 #ifndef GRPCXX_COMPLETION_QUEUE_H
35 #define GRPCXX_COMPLETION_QUEUE_H
36
37 #include <grpc/support/time.h>
38 #include <grpc++/impl/grpc_library.h>
39 #include <grpc++/status.h>
40 #include <grpc++/time.h>
41
42 struct grpc_completion_queue;
43
44 namespace grpc {
45
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 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;
66
67 class ChannelInterface;
68 class ClientContext;
69 class CompletionQueue;
70 class RpcMethod;
71 class Server;
72 class ServerBuilder;
73 class ServerContext;
74
75 class CompletionQueueTag {
76 public:
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
80 // to do)
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;
84 };
85
86 // grpc_completion_queue wrapper class
87 class CompletionQueue : public GrpcLibrary {
88 public:
89 CompletionQueue();
90 explicit CompletionQueue(grpc_completion_queue* take);
91 ~CompletionQueue() GRPC_OVERRIDE;
92
93 // Tri-state return for AsyncNext: SHUTDOWN, GOT_EVENT, TIMEOUT
94 enum NextStatus { SHUTDOWN, GOT_EVENT, TIMEOUT };
95
96 // Nonblocking (until deadline) read from queue.
97 // Cannot rely on result of tag or ok if return is TIMEOUT
98 template <typename T>
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());
102 }
103
104 // Blocking read from queue.
105 // Returns false if the queue is ready for destruction, true if event
106
107 bool Next(void** tag, bool* ok) {
108 return (AsyncNextInternal(tag, ok, gpr_inf_future(GPR_CLOCK_REALTIME)) !=
109 SHUTDOWN);
110 }
111
112 // Shutdown has to be called, and the CompletionQueue can only be
113 // destructed when false is returned from Next().
114 void Shutdown();
115
116 grpc_completion_queue* cq() { return cq_; }
117
118 private:
119 // Friend synchronous wrappers so that they can access Pluck(), which is
120 // a semi-private API geared towards the synchronous implementation.
121 template <class R>
122 friend class ::grpc::ClientReader;
123 template <class W>
124 friend class ::grpc::ClientWriter;
125 template <class R, class W>
126 friend class ::grpc::ClientReaderWriter;
127 template <class R>
128 friend class ::grpc::ServerReader;
129 template <class W>
130 friend class ::grpc::ServerWriter;
131 template <class R, class W>
132 friend class ::grpc::ServerReaderWriter;
133 template <class ServiceType, class RequestType, class ResponseType>
134 friend class RpcMethodHandler;
135 template <class ServiceType, class RequestType, class ResponseType>
136 friend class ClientStreamingHandler;
137 template <class ServiceType, class RequestType, class ResponseType>
138 friend class ServerStreamingHandler;
139 template <class ServiceType, class RequestType, class ResponseType>
140 friend class BidiStreamingHandler;
141 friend class ::grpc::Server;
142 friend class ::grpc::ServerContext;
143 template <class InputMessage, class OutputMessage>
144 friend Status BlockingUnaryCall(ChannelInterface* channel,
145 const RpcMethod& method,
146 ClientContext* context,
147 const InputMessage& request,
148 OutputMessage* result);
149
150 NextStatus AsyncNextInternal(void** tag, bool* ok, gpr_timespec deadline);
151
152 // Wraps grpc_completion_queue_pluck.
153 // Cannot be mixed with calls to Next().
154 bool Pluck(CompletionQueueTag* tag);
155
156 // Does a single polling pluck on tag
157 void TryPluck(CompletionQueueTag* tag);
158
159 grpc_completion_queue* cq_; // owned
160 };
161
162 class ServerCompletionQueue : public CompletionQueue {
163 private:
164 friend class ServerBuilder;
165 ServerCompletionQueue() {}
166 };
167
168 } // namespace grpc
169
170 #endif // GRPCXX_COMPLETION_QUEUE_H
grpc::CompletionQueueTag
Definition: completion_queue.h:75
grpc::CompletionQueue::BlockingUnaryCall
friend Status BlockingUnaryCall(ChannelInterface *channel, const RpcMethod &method, ClientContext *context, const InputMessage &request, OutputMessage *result)
Definition: client_unary_call.h:51
time.h
grpc::CompletionQueueTag::FinalizeResult
virtual bool FinalizeResult(void **tag, bool *status)=0
grpc::CompletionQueue::GOT_EVENT
Definition: completion_queue.h:94
grpc_library.h
grpc::CompletionQueue::SHUTDOWN
Definition: completion_queue.h:94
grpc::TimePoint::raw_time
gpr_timespec raw_time()
Definition: time.h:56
grpc::TimePoint
Definition: time.h:53
grpc::CompletionQueue::cq
grpc_completion_queue * cq()
Definition: completion_queue.h:116
grpc::ClientContext
Definition: client_context.h:74
grpc::ServerReader
Definition: completion_queue.h:53
grpc::CompletionQueue::~CompletionQueue
~CompletionQueue() GRPC_OVERRIDE
grpc::CompletionQueue::Next
bool Next(void **tag, bool *ok)
Definition: completion_queue.h:107
grpc::GrpcLibrary
Definition: grpc_library.h:41
grpc::CompletionQueue::AsyncNext
NextStatus AsyncNext(void **tag, bool *ok, const T &deadline)
Definition: completion_queue.h:99
grpc::CompletionQueueTag::~CompletionQueueTag
virtual ~CompletionQueueTag()
Definition: completion_queue.h:77
grpc::Server
Definition: server.h:61
grpc::RpcMethodHandler
Definition: completion_queue.h:59
grpc::CompletionQueue::Shutdown
void Shutdown()
grpc::ChannelInterface
Definition: channel_interface.h:52
grpc::CompletionQueue::NextStatus
NextStatus
Definition: completion_queue.h:94
grpc::CompletionQueue::CompletionQueue
CompletionQueue()
grpc::ServerContext
Definition: server_context.h:86
grpc::ServerWriter
Definition: completion_queue.h:55
grpc::CompletionQueue
Definition: completion_queue.h:87
grpc::ClientStreamingHandler
Definition: completion_queue.h:61
status.h
grpc::RpcMethod
Definition: rpc_method.h:39
grpc::ServerReaderWriter
Definition: completion_queue.h:57
grpc::ServerStreamingHandler
Definition: completion_queue.h:63
grpc::Status
Definition: status.h:42
grpc::ServerCompletionQueue
Definition: completion_queue.h:162
GRPC_OVERRIDE
#define GRPC_OVERRIDE
Definition: config.h:77
grpc::BidiStreamingHandler
Definition: completion_queue.h:65
grpc::ServerBuilder
Definition: server_builder.h:54
grpc::CompletionQueue::TIMEOUT
Definition: completion_queue.h:94