GRPC C++  0.11.0.0
 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 
36 #ifndef GRPCXX_COMPLETION_QUEUE_H
37 #define GRPCXX_COMPLETION_QUEUE_H
38 
39 #include <grpc/support/time.h>
41 #include <grpc++/support/status.h>
42 #include <grpc++/support/time.h>
43 
44 struct grpc_completion_queue;
45 
46 namespace grpc {
47 
48 template <class R>
49 class ClientReader;
50 template <class W>
51 class ClientWriter;
52 template <class R, class W>
53 class ClientReaderWriter;
54 template <class R>
56 template <class W>
58 template <class R, class W>
60 template <class ServiceType, class RequestType, class ResponseType>
62 template <class ServiceType, class RequestType, class ResponseType>
64 template <class ServiceType, class RequestType, class ResponseType>
66 template <class ServiceType, class RequestType, class ResponseType>
69 
70 class Channel;
71 class ClientContext;
72 class CompletionQueueTag;
73 class CompletionQueue;
74 class RpcMethod;
75 class Server;
76 class ServerBuilder;
77 class ServerContext;
78 
81 class CompletionQueue : public GrpcLibrary {
82  public:
86 
90  explicit CompletionQueue(grpc_completion_queue* take);
91 
94 
96  enum NextStatus {
99  TIMEOUT
101  };
102 
113  template <typename T>
114  NextStatus AsyncNext(void** tag, bool* ok, const T& deadline) {
115  TimePoint<T> deadline_tp(deadline);
116  return AsyncNextInternal(tag, ok, deadline_tp.raw_time());
117  }
118 
126  bool Next(void** tag, bool* ok) {
127  return (AsyncNextInternal(tag, ok, gpr_inf_future(GPR_CLOCK_REALTIME)) !=
128  SHUTDOWN);
129  }
130 
138  void Shutdown();
139 
145  grpc_completion_queue* cq() { return cq_; }
146 
147  private:
148  // Friend synchronous wrappers so that they can access Pluck(), which is
149  // a semi-private API geared towards the synchronous implementation.
150  template <class R>
151  friend class ::grpc::ClientReader;
152  template <class W>
153  friend class ::grpc::ClientWriter;
154  template <class R, class W>
155  friend class ::grpc::ClientReaderWriter;
156  template <class R>
157  friend class ::grpc::ServerReader;
158  template <class W>
159  friend class ::grpc::ServerWriter;
160  template <class R, class W>
161  friend class ::grpc::ServerReaderWriter;
162  template <class ServiceType, class RequestType, class ResponseType>
163  friend class RpcMethodHandler;
164  template <class ServiceType, class RequestType, class ResponseType>
166  template <class ServiceType, class RequestType, class ResponseType>
168  template <class ServiceType, class RequestType, class ResponseType>
169  friend class BidiStreamingHandler;
170  friend class UnknownMethodHandler;
171  friend class ::grpc::Server;
172  friend class ::grpc::ServerContext;
173  template <class InputMessage, class OutputMessage>
174  friend Status BlockingUnaryCall(Channel* channel, const RpcMethod& method,
175  ClientContext* context,
176  const InputMessage& request,
177  OutputMessage* result);
178 
179  NextStatus AsyncNextInternal(void** tag, bool* ok, gpr_timespec deadline);
180 
183  bool Pluck(CompletionQueueTag* tag);
184 
186  void TryPluck(CompletionQueueTag* tag);
187 
188  grpc_completion_queue* cq_; // owned
189 };
190 
193  public:
194  virtual ~CompletionQueueTag() {}
195  // Called prior to returning from Next(), return value is the status of the
196  // operation (return status is the default thing to do). If this function
197  // returns false, the tag is dropped and not returned from the completion
198  // queue
199  virtual bool FinalizeResult(void** tag, bool* status) = 0;
200 };
201 
205  private:
206  friend class ServerBuilder;
208 };
209 
210 } // namespace grpc
211 
212 #endif // GRPCXX_COMPLETION_QUEUE_H
An interface allowing implementors to process and filter event tags.
Definition: completion_queue.h:192
friend Status BlockingUnaryCall(Channel *channel, const RpcMethod &method, ClientContext *context, const InputMessage &request, OutputMessage *result)
Definition: client_unary_call.h:50
virtual bool FinalizeResult(void **tag, bool *status)=0
Got a new event; tag will be filled in with its associated value; ok indicating its success...
Definition: completion_queue.h:98
The completion queue has been shutdown.
Definition: completion_queue.h:97
gpr_timespec raw_time()
Definition: time.h:56
Definition: time.h:53
grpc_completion_queue * cq()
Returns a raw pointer to the underlying grpc_completion_queue instance.
Definition: completion_queue.h:145
Definition: client_context.h:149
Definition: completion_queue.h:55
~CompletionQueue() GRPC_OVERRIDE
Destructor. Destroys the owned wrapped completion queue / instance.
Definition: completion_queue.cc:49
bool Next(void **tag, bool *ok)
Read from the queue, blocking until an event is available or the queue is shutting down...
Definition: completion_queue.h:126
Definition: rpc_service_method.h:212
Definition: grpc_library.h:41
NextStatus AsyncNext(void **tag, bool *ok, const T &deadline)
Read from the queue, blocking up to deadline (or the queue's shutdown).
Definition: completion_queue.h:114
virtual ~CompletionQueueTag()
Definition: completion_queue.h:194
Models a gRPC server.
Definition: server.h:63
Definition: completion_queue.h:61
void Shutdown()
Request the shutdown of the queue.
Definition: completion_queue.cc:51
NextStatus
Tri-state return for AsyncNext: SHUTDOWN, GOT_EVENT, TIMEOUT.
Definition: completion_queue.h:96
CompletionQueue()
Default constructor.
Definition: completion_queue.cc:43
Definition: server_context.h:89
Definition: completion_queue.h:57
A thin wrapper around grpc_completion_queue (see / src/core/surface/completion_queue.h).
Definition: completion_queue.h:81
Definition: completion_queue.h:63
Definition: rpc_method.h:43
Server-side interface for bi-directional streaming.
Definition: completion_queue.h:59
Definition: completion_queue.h:65
Did it work? If it didn't, why?
Definition: status.h:45
A specific type of completion queue used by the processing of notifications by servers.
Definition: completion_queue.h:204
#define GRPC_OVERRIDE
Definition: config.h:77
Definition: completion_queue.h:67
A builder class for the creation and startup of grpc::Server instances.
Definition: server_builder.h:55
deadline was reached.
Definition: completion_queue.h:100
Channels represent a connection to an endpoint. Created by CreateChannel.
Definition: channel.h:69