completion_queue.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. /// A completion queue implements a producer-consumer queue, with two main
  34. /// methods:
  35. ///
  36. /// - Next
  37. /// - AsyncNext XXX
  38. ///
  39. #ifndef GRPCXX_COMPLETION_QUEUE_H
  40. #define GRPCXX_COMPLETION_QUEUE_H
  41. #include <grpc/support/time.h>
  42. #include <grpc++/impl/grpc_library.h>
  43. #include <grpc++/status.h>
  44. #include <grpc++/time.h>
  45. struct grpc_completion_queue;
  46. namespace grpc {
  47. template <class R>
  48. class ClientReader;
  49. template <class W>
  50. class ClientWriter;
  51. template <class R, class W>
  52. class ClientReaderWriter;
  53. template <class R>
  54. class ServerReader;
  55. template <class W>
  56. class ServerWriter;
  57. template <class R, class W>
  58. class ServerReaderWriter;
  59. template <class ServiceType, class RequestType, class ResponseType>
  60. class RpcMethodHandler;
  61. template <class ServiceType, class RequestType, class ResponseType>
  62. class ClientStreamingHandler;
  63. template <class ServiceType, class RequestType, class ResponseType>
  64. class ServerStreamingHandler;
  65. template <class ServiceType, class RequestType, class ResponseType>
  66. class BidiStreamingHandler;
  67. class UnknownMethodHandler;
  68. class ChannelInterface;
  69. class ClientContext;
  70. class CompletionQueueTag;
  71. class CompletionQueue;
  72. class RpcMethod;
  73. class Server;
  74. class ServerBuilder;
  75. class ServerContext;
  76. // grpc_completion_queue wrapper class
  77. class CompletionQueue : public GrpcLibrary {
  78. public:
  79. CompletionQueue();
  80. explicit CompletionQueue(grpc_completion_queue* take);
  81. ~CompletionQueue() GRPC_OVERRIDE;
  82. // Tri-state return for AsyncNext: SHUTDOWN, GOT_EVENT, TIMEOUT
  83. enum NextStatus { SHUTDOWN, GOT_EVENT, TIMEOUT };
  84. // Nonblocking (until deadline) read from queue.
  85. // Cannot rely on result of tag or ok if return is TIMEOUT
  86. template <typename T>
  87. NextStatus AsyncNext(void** tag, bool* ok, const T& deadline) {
  88. TimePoint<T> deadline_tp(deadline);
  89. return AsyncNextInternal(tag, ok, deadline_tp.raw_time());
  90. }
  91. // Blocking read from queue.
  92. // Returns false if the queue is ready for destruction, true if event
  93. bool Next(void** tag, bool* ok) {
  94. return (AsyncNextInternal(tag, ok, gpr_inf_future(GPR_CLOCK_REALTIME)) !=
  95. SHUTDOWN);
  96. }
  97. // Shutdown has to be called, and the CompletionQueue can only be
  98. // destructed when false is returned from Next().
  99. void Shutdown();
  100. grpc_completion_queue* cq() { return cq_; }
  101. private:
  102. // Friend synchronous wrappers so that they can access Pluck(), which is
  103. // a semi-private API geared towards the synchronous implementation.
  104. template <class R>
  105. friend class ::grpc::ClientReader;
  106. template <class W>
  107. friend class ::grpc::ClientWriter;
  108. template <class R, class W>
  109. friend class ::grpc::ClientReaderWriter;
  110. template <class R>
  111. friend class ::grpc::ServerReader;
  112. template <class W>
  113. friend class ::grpc::ServerWriter;
  114. template <class R, class W>
  115. friend class ::grpc::ServerReaderWriter;
  116. template <class ServiceType, class RequestType, class ResponseType>
  117. friend class RpcMethodHandler;
  118. template <class ServiceType, class RequestType, class ResponseType>
  119. friend class ClientStreamingHandler;
  120. template <class ServiceType, class RequestType, class ResponseType>
  121. friend class ServerStreamingHandler;
  122. template <class ServiceType, class RequestType, class ResponseType>
  123. friend class BidiStreamingHandler;
  124. friend class UnknownMethodHandler;
  125. friend class ::grpc::Server;
  126. friend class ::grpc::ServerContext;
  127. template <class InputMessage, class OutputMessage>
  128. friend Status BlockingUnaryCall(ChannelInterface* channel,
  129. const RpcMethod& method,
  130. ClientContext* context,
  131. const InputMessage& request,
  132. OutputMessage* result);
  133. NextStatus AsyncNextInternal(void** tag, bool* ok, gpr_timespec deadline);
  134. // Wraps grpc_completion_queue_pluck.
  135. // Cannot be mixed with calls to Next().
  136. bool Pluck(CompletionQueueTag* tag);
  137. // Does a single polling pluck on tag
  138. void TryPluck(CompletionQueueTag* tag);
  139. grpc_completion_queue* cq_; // owned
  140. };
  141. class CompletionQueueTag {
  142. public:
  143. virtual ~CompletionQueueTag() {}
  144. // Called prior to returning from Next(), return value
  145. // is the status of the operation (return status is the default thing
  146. // to do)
  147. // If this function returns false, the tag is dropped and not returned
  148. // from the completion queue
  149. virtual bool FinalizeResult(void** tag, bool* status) = 0;
  150. };
  151. class ServerCompletionQueue : public CompletionQueue {
  152. private:
  153. friend class ServerBuilder;
  154. ServerCompletionQueue() {}
  155. };
  156. } // namespace grpc
  157. #endif // GRPCXX_COMPLETION_QUEUE_H