~ubuntu-branches/ubuntu/wily/grpc/wily

« back to all changes in this revision

Viewing changes to include/grpc++/completion_queue.h

  • Committer: Package Import Robot
  • Author(s): Andrew Pollock
  • Date: 2015-05-07 13:28:11 UTC
  • Revision ID: package-import@ubuntu.com-20150507132811-ybm4hfq73tnvvd2e
Tags: upstream-0.10.0
ImportĀ upstreamĀ versionĀ 0.10.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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) != SHUTDOWN);
 
109
  }
 
110
 
 
111
  // Shutdown has to be called, and the CompletionQueue can only be
 
112
  // destructed when false is returned from Next().
 
113
  void Shutdown();
 
114
 
 
115
  grpc_completion_queue* cq() { return cq_; }
 
116
 
 
117
 private:
 
118
  // Friend synchronous wrappers so that they can access Pluck(), which is
 
119
  // a semi-private API geared towards the synchronous implementation.
 
120
  template <class R>
 
121
  friend class ::grpc::ClientReader;
 
122
  template <class W>
 
123
  friend class ::grpc::ClientWriter;
 
124
  template <class R, class W>
 
125
  friend class ::grpc::ClientReaderWriter;
 
126
  template <class R>
 
127
  friend class ::grpc::ServerReader;
 
128
  template <class W>
 
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);
 
148
 
 
149
  NextStatus AsyncNextInternal(void** tag, bool* ok, gpr_timespec deadline);
 
150
 
 
151
  // Wraps grpc_completion_queue_pluck.
 
152
  // Cannot be mixed with calls to Next().
 
153
  bool Pluck(CompletionQueueTag* tag);
 
154
 
 
155
  // Does a single polling pluck on tag
 
156
  void TryPluck(CompletionQueueTag* tag);
 
157
 
 
158
  grpc_completion_queue* cq_;  // owned
 
159
};
 
160
 
 
161
class ServerCompletionQueue : public CompletionQueue {
 
162
 private:
 
163
  friend class ServerBuilder;
 
164
  ServerCompletionQueue() {}
 
165
};
 
166
 
 
167
}  // namespace grpc
 
168
 
 
169
#endif  // GRPCXX_COMPLETION_QUEUE_H