GRPC C++  0.10.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 
34 #ifndef GRPCXX_COMPLETION_QUEUE_H
35 #define GRPCXX_COMPLETION_QUEUE_H
36 
37 #include <grpc/support/time.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>
54 template <class W>
56 template <class R, class W>
58 template <class ServiceType, class RequestType, class ResponseType>
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 
67 class ChannelInterface;
68 class ClientContext;
69 class CompletionQueue;
70 class RpcMethod;
71 class Server;
72 class ServerBuilder;
73 class ServerContext;
74 
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:
90  explicit CompletionQueue(grpc_completion_queue* take);
92 
93  // Tri-state return for AsyncNext: 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>
137  template <class ServiceType, class RequestType, class ResponseType>
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 
163  private:
164  friend class ServerBuilder;
166 };
167 
168 } // namespace grpc
169 
170 #endif // GRPCXX_COMPLETION_QUEUE_H
Definition: completion_queue.h:75
friend Status BlockingUnaryCall(ChannelInterface *channel, const RpcMethod &method, ClientContext *context, const InputMessage &request, OutputMessage *result)
Definition: client_unary_call.h:51
virtual bool FinalizeResult(void **tag, bool *status)=0
Definition: completion_queue.h:94
Definition: completion_queue.h:94
gpr_timespec raw_time()
Definition: time.h:56
Definition: time.h:53
grpc_completion_queue * cq()
Definition: completion_queue.h:116
Definition: client_context.h:74
Definition: completion_queue.h:53
~CompletionQueue() GRPC_OVERRIDE
Definition: completion_queue.cc:47
bool Next(void **tag, bool *ok)
Definition: completion_queue.h:107
Definition: grpc_library.h:41
NextStatus AsyncNext(void **tag, bool *ok, const T &deadline)
Definition: completion_queue.h:99
virtual ~CompletionQueueTag()
Definition: completion_queue.h:77
Definition: server.h:61
Definition: completion_queue.h:59
void Shutdown()
Definition: completion_queue.cc:49
Definition: channel_interface.h:52
NextStatus
Definition: completion_queue.h:94
CompletionQueue()
Definition: completion_queue.cc:43
Definition: server_context.h:86
Definition: completion_queue.h:55
Definition: completion_queue.h:87
Definition: completion_queue.h:61
Definition: rpc_method.h:39
Definition: completion_queue.h:57
Definition: completion_queue.h:63
Definition: status.h:42
Definition: completion_queue.h:162
#define GRPC_OVERRIDE
Definition: config.h:77
Definition: completion_queue.h:65
Definition: server_builder.h:54
Definition: completion_queue.h:94