gRPC  0.6.0
 All Classes Namespaces Functions Variables Enumerations Properties Pages
completion_queue.h
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/client_unary_call.h>
39 #include <grpc++/impl/grpc_library.h>
40 #include <grpc++/time.h>
41 
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 
59 class CompletionQueue;
60 class Server;
61 class ServerBuilder;
62 class ServerContext;
63 
65  public:
66  virtual ~CompletionQueueTag() {}
67  // Called prior to returning from Next(), return value
68  // is the status of the operation (return status is the default thing
69  // to do)
70  // If this function returns false, the tag is dropped and not returned
71  // from the completion queue
72  virtual bool FinalizeResult(void** tag, bool* status) = 0;
73 };
74 
75 // grpc_completion_queue wrapper class
76 class CompletionQueue : public GrpcLibrary {
77  public:
79  explicit CompletionQueue(grpc_completion_queue* take);
80  ~CompletionQueue() GRPC_OVERRIDE;
81 
82  // Tri-state return for AsyncNext: SHUTDOWN, GOT_EVENT, TIMEOUT
83  enum NextStatus { SHUTDOWN, GOT_EVENT, TIMEOUT };
84 
85  // Nonblocking (until deadline) read from queue.
86  // Cannot rely on result of tag or ok if return is TIMEOUT
87  template<typename T>
88  NextStatus AsyncNext(void** tag, bool* ok, const T& deadline) {
89  TimePoint<T> deadline_tp(deadline);
90  return AsyncNextInternal(tag, ok, deadline_tp.raw_time());
91  }
92 
93  // Blocking read from queue.
94  // Returns false if the queue is ready for destruction, true if event
95 
96  bool Next(void** tag, bool* ok) {
97  return (AsyncNextInternal(tag, ok, gpr_inf_future) != SHUTDOWN);
98  }
99 
100  // Shutdown has to be called, and the CompletionQueue can only be
101  // destructed when false is returned from Next().
102  void Shutdown();
103 
104  grpc_completion_queue* cq() { return cq_; }
105 
106  private:
107  // Friend synchronous wrappers so that they can access Pluck(), which is
108  // a semi-private API geared towards the synchronous implementation.
109  template <class R>
110  friend class ::grpc::ClientReader;
111  template <class W>
112  friend class ::grpc::ClientWriter;
113  template <class R, class W>
114  friend class ::grpc::ClientReaderWriter;
115  template <class R>
116  friend class ::grpc::ServerReader;
117  template <class W>
118  friend class ::grpc::ServerWriter;
119  template <class R, class W>
120  friend class ::grpc::ServerReaderWriter;
121  friend class ::grpc::Server;
122  friend class ::grpc::ServerContext;
123  friend Status BlockingUnaryCall(ChannelInterface* channel,
124  const RpcMethod& method,
125  ClientContext* context,
126  const grpc::protobuf::Message& request,
127  grpc::protobuf::Message* result);
128 
129  NextStatus AsyncNextInternal(void** tag, bool* ok, gpr_timespec deadline);
130 
131  // Wraps grpc_completion_queue_pluck.
132  // Cannot be mixed with calls to Next().
133  bool Pluck(CompletionQueueTag* tag);
134 
135  // Does a single polling pluck on tag
136  void TryPluck(CompletionQueueTag* tag);
137 
138  grpc_completion_queue* cq_; // owned
139 };
140 
142  private:
143  friend class ServerBuilder;
145 };
146 
147 } // namespace grpc
148 
149 #endif // GRPCXX_COMPLETION_QUEUE_H
Definition: completion_queue.h:76
Definition: channel_interface.h:52
Definition: completion_queue.h:53
Definition: status.h:42
Definition: completion_queue.h:64
Definition: completion_queue.h:141
Definition: completion_queue.h:55
Definition: client_context.h:72
Definition: completion_queue.c:61
Definition: completion_queue.h:57
Definition: rpc_method.h:39
Definition: server_builder.h:54
Definition: server_context.h:70
Definition: time.h:48
Definition: channel_create.c:62
Definition: grpc_library.h:41
Definition: time.h:53
Definition: _server.h:42