async_unary_call.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. //
  2. //
  3. // Copyright 2015 gRPC authors.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. //
  18. #ifndef GRPCPP_SUPPORT_ASYNC_UNARY_CALL_H
  19. #define GRPCPP_SUPPORT_ASYNC_UNARY_CALL_H
  20. #include <grpc/grpc.h>
  21. #include <grpc/support/log.h>
  22. #include <grpcpp/client_context.h>
  23. #include <grpcpp/impl/call.h>
  24. #include <grpcpp/impl/call_op_set.h>
  25. #include <grpcpp/impl/call_op_set_interface.h>
  26. #include <grpcpp/impl/channel_interface.h>
  27. #include <grpcpp/impl/service_type.h>
  28. #include <grpcpp/server_context.h>
  29. #include <grpcpp/support/status.h>
  30. namespace grpc {
  31. // Forward declaration for use in Helper class
  32. template <class R>
  33. class ClientAsyncResponseReader;
  34. /// An interface relevant for async client side unary RPCs (which send
  35. /// one request message to a server and receive one response message).
  36. template <class R>
  37. class ClientAsyncResponseReaderInterface {
  38. public:
  39. virtual ~ClientAsyncResponseReaderInterface() {}
  40. /// Start the call that was set up by the constructor, but only if the
  41. /// constructor was invoked through the "Prepare" API which doesn't actually
  42. /// start the call
  43. virtual void StartCall() = 0;
  44. /// Request notification of the reading of initial metadata. Completion
  45. /// will be notified by \a tag on the associated completion queue.
  46. /// This call is optional, but if it is used, it cannot be used concurrently
  47. /// with or after the \a Finish method.
  48. ///
  49. /// \param[in] tag Tag identifying this request.
  50. virtual void ReadInitialMetadata(void* tag) = 0;
  51. /// Request to receive the server's response \a msg and final \a status for
  52. /// the call, and to notify \a tag on this call's completion queue when
  53. /// finished.
  54. ///
  55. /// This function will return when either:
  56. /// - when the server's response message and status have been received.
  57. /// - when the server has returned a non-OK status (no message expected in
  58. /// this case).
  59. /// - when the call failed for some reason and the library generated a
  60. /// non-OK status.
  61. ///
  62. /// \param[in] tag Tag identifying this request.
  63. /// \param[out] status To be updated with the operation status.
  64. /// \param[out] msg To be filled in with the server's response message.
  65. virtual void Finish(R* msg, grpc::Status* status, void* tag) = 0;
  66. };
  67. namespace internal {
  68. class ClientAsyncResponseReaderHelper {
  69. public:
  70. /// Start a call and write the request out if \a start is set.
  71. /// \a tag will be notified on \a cq when the call has been started (i.e.
  72. /// initial metadata sent) and \a request has been written out.
  73. /// If \a start is not set, the actual call must be initiated by StartCall
  74. /// Note that \a context will be used to fill in custom initial metadata
  75. /// used to send to the server when starting the call.
  76. ///
  77. /// Optionally pass in a base class for request and response types so that the
  78. /// internal functions and structs can be templated based on that, allowing
  79. /// reuse across RPCs (e.g., MessageLite for protobuf). Since constructors
  80. /// can't have an explicit template parameter, the last argument is an
  81. /// extraneous parameter just to provide the needed type information.
  82. template <class R, class W, class BaseR = R, class BaseW = W>
  83. static ClientAsyncResponseReader<R>* Create(
  84. grpc::ChannelInterface* channel, grpc::CompletionQueue* cq,
  85. const grpc::internal::RpcMethod& method, grpc::ClientContext* context,
  86. const W& request) /* __attribute__((noinline)) */ {
  87. grpc::internal::Call call = channel->CreateCall(method, context, cq);
  88. ClientAsyncResponseReader<R>* result = new (grpc_call_arena_alloc(
  89. call.call(), sizeof(ClientAsyncResponseReader<R>)))
  90. ClientAsyncResponseReader<R>(call, context);
  91. SetupRequest<BaseR, BaseW>(
  92. call.call(), &result->single_buf_, &result->read_initial_metadata_,
  93. &result->finish_, static_cast<const BaseW&>(request));
  94. return result;
  95. }
  96. // Various helper functions to reduce templating use
  97. template <class R, class W>
  98. static void SetupRequest(
  99. grpc_call* call,
  100. grpc::internal::CallOpSendInitialMetadata** single_buf_ptr,
  101. std::function<void(ClientContext*, internal::Call*,
  102. internal::CallOpSendInitialMetadata*, void*)>*
  103. read_initial_metadata,
  104. std::function<
  105. void(ClientContext*, internal::Call*, bool initial_metadata_read,
  106. internal::CallOpSendInitialMetadata*,
  107. internal::CallOpSetInterface**, void*, Status*, void*)>* finish,
  108. const W& request) {
  109. using SingleBufType =
  110. grpc::internal::CallOpSet<grpc::internal::CallOpSendInitialMetadata,
  111. grpc::internal::CallOpSendMessage,
  112. grpc::internal::CallOpClientSendClose,
  113. grpc::internal::CallOpRecvInitialMetadata,
  114. grpc::internal::CallOpRecvMessage<R>,
  115. grpc::internal::CallOpClientRecvStatus>;
  116. SingleBufType* single_buf =
  117. new (grpc_call_arena_alloc(call, sizeof(SingleBufType))) SingleBufType;
  118. *single_buf_ptr = single_buf;
  119. // TODO(ctiller): don't assert
  120. GPR_ASSERT(single_buf->SendMessage(request).ok());
  121. single_buf->ClientSendClose();
  122. // The purpose of the following functions is to type-erase the actual
  123. // templated type of the CallOpSet being used by hiding that type inside the
  124. // function definition rather than specifying it as an argument of the
  125. // function or a member of the class. The type-erased CallOpSet will get
  126. // static_cast'ed back to the real type so that it can be used properly.
  127. *read_initial_metadata =
  128. [](ClientContext* context, internal::Call* call,
  129. internal::CallOpSendInitialMetadata* single_buf_view, void* tag) {
  130. auto* single_buf = static_cast<SingleBufType*>(single_buf_view);
  131. single_buf->set_output_tag(tag);
  132. single_buf->RecvInitialMetadata(context);
  133. call->PerformOps(single_buf);
  134. };
  135. // Note that this function goes one step further than the previous one
  136. // because it type-erases the message being written down to a void*. This
  137. // will be static-cast'ed back to the class specified here by hiding that
  138. // class information inside the function definition. Note that this feature
  139. // expects the class being specified here for R to be a base-class of the
  140. // "real" R without any multiple-inheritance (as applies in protobuf wrt
  141. // MessageLite)
  142. *finish = [](ClientContext* context, internal::Call* call,
  143. bool initial_metadata_read,
  144. internal::CallOpSendInitialMetadata* single_buf_view,
  145. internal::CallOpSetInterface** finish_buf_ptr, void* msg,
  146. Status* status, void* tag) {
  147. if (initial_metadata_read) {
  148. using FinishBufType =
  149. grpc::internal::CallOpSet<grpc::internal::CallOpRecvMessage<R>,
  150. grpc::internal::CallOpClientRecvStatus>;
  151. FinishBufType* finish_buf =
  152. new (grpc_call_arena_alloc(call->call(), sizeof(FinishBufType)))
  153. FinishBufType;
  154. *finish_buf_ptr = finish_buf;
  155. finish_buf->set_output_tag(tag);
  156. finish_buf->RecvMessage(static_cast<R*>(msg));
  157. finish_buf->AllowNoMessage();
  158. finish_buf->ClientRecvStatus(context, status);
  159. call->PerformOps(finish_buf);
  160. } else {
  161. auto* single_buf = static_cast<SingleBufType*>(single_buf_view);
  162. single_buf->set_output_tag(tag);
  163. single_buf->RecvInitialMetadata(context);
  164. single_buf->RecvMessage(static_cast<R*>(msg));
  165. single_buf->AllowNoMessage();
  166. single_buf->ClientRecvStatus(context, status);
  167. call->PerformOps(single_buf);
  168. }
  169. };
  170. }
  171. static void StartCall(grpc::ClientContext* context,
  172. grpc::internal::CallOpSendInitialMetadata* single_buf) {
  173. single_buf->SendInitialMetadata(&context->send_initial_metadata_,
  174. context->initial_metadata_flags());
  175. }
  176. };
  177. // TODO(vjpai): This templated factory is deprecated and will be replaced by
  178. //. the non-templated helper as soon as possible.
  179. template <class R>
  180. class ClientAsyncResponseReaderFactory {
  181. public:
  182. template <class W>
  183. static ClientAsyncResponseReader<R>* Create(
  184. grpc::ChannelInterface* channel, grpc::CompletionQueue* cq,
  185. const grpc::internal::RpcMethod& method, grpc::ClientContext* context,
  186. const W& request, bool start) {
  187. auto* result = ClientAsyncResponseReaderHelper::Create<R>(
  188. channel, cq, method, context, request);
  189. if (start) {
  190. result->StartCall();
  191. }
  192. return result;
  193. }
  194. };
  195. } // namespace internal
  196. /// Async API for client-side unary RPCs, where the message response
  197. /// received from the server is of type \a R.
  198. template <class R>
  199. class ClientAsyncResponseReader final
  200. : public ClientAsyncResponseReaderInterface<R> {
  201. public:
  202. // always allocated against a call arena, no memory free required
  203. static void operator delete(void* /*ptr*/, std::size_t size) {
  204. GPR_ASSERT(size == sizeof(ClientAsyncResponseReader));
  205. }
  206. // This operator should never be called as the memory should be freed as part
  207. // of the arena destruction. It only exists to provide a matching operator
  208. // delete to the operator new so that some compilers will not complain (see
  209. // https://github.com/grpc/grpc/issues/11301) Note at the time of adding this
  210. // there are no tests catching the compiler warning.
  211. static void operator delete(void*, void*) { GPR_ASSERT(false); }
  212. void StartCall() override {
  213. GPR_DEBUG_ASSERT(!started_);
  214. started_ = true;
  215. internal::ClientAsyncResponseReaderHelper::StartCall(context_, single_buf_);
  216. }
  217. /// See \a ClientAsyncResponseReaderInterface::ReadInitialMetadata for
  218. /// semantics.
  219. ///
  220. /// Side effect:
  221. /// - the \a ClientContext associated with this call is updated with
  222. /// possible initial and trailing metadata sent from the server.
  223. void ReadInitialMetadata(void* tag) override {
  224. GPR_DEBUG_ASSERT(started_);
  225. GPR_DEBUG_ASSERT(!context_->initial_metadata_received_);
  226. read_initial_metadata_(context_, &call_, single_buf_, tag);
  227. initial_metadata_read_ = true;
  228. }
  229. /// See \a ClientAsyncResponseReaderInterface::Finish for semantics.
  230. ///
  231. /// Side effect:
  232. /// - the \a ClientContext associated with this call is updated with
  233. /// possible initial and trailing metadata sent from the server.
  234. void Finish(R* msg, grpc::Status* status, void* tag) override {
  235. GPR_DEBUG_ASSERT(started_);
  236. finish_(context_, &call_, initial_metadata_read_, single_buf_, &finish_buf_,
  237. static_cast<void*>(msg), status, tag);
  238. }
  239. private:
  240. friend class internal::ClientAsyncResponseReaderHelper;
  241. grpc::ClientContext* const context_;
  242. grpc::internal::Call call_;
  243. bool started_ = false;
  244. bool initial_metadata_read_ = false;
  245. ClientAsyncResponseReader(grpc::internal::Call call,
  246. grpc::ClientContext* context)
  247. : context_(context), call_(call) {}
  248. // disable operator new
  249. static void* operator new(std::size_t size);
  250. static void* operator new(std::size_t /*size*/, void* p) { return p; }
  251. internal::CallOpSendInitialMetadata* single_buf_;
  252. internal::CallOpSetInterface* finish_buf_ = nullptr;
  253. std::function<void(ClientContext*, internal::Call*,
  254. internal::CallOpSendInitialMetadata*, void*)>
  255. read_initial_metadata_;
  256. std::function<void(ClientContext*, internal::Call*,
  257. bool initial_metadata_read,
  258. internal::CallOpSendInitialMetadata*,
  259. internal::CallOpSetInterface**, void*, Status*, void*)>
  260. finish_;
  261. };
  262. /// Async server-side API for handling unary calls, where the single
  263. /// response message sent to the client is of type \a W.
  264. template <class W>
  265. class ServerAsyncResponseWriter final
  266. : public grpc::internal::ServerAsyncStreamingInterface {
  267. public:
  268. explicit ServerAsyncResponseWriter(grpc::ServerContext* ctx)
  269. : call_(nullptr, nullptr, nullptr), ctx_(ctx) {}
  270. /// See \a ServerAsyncStreamingInterface::SendInitialMetadata for semantics.
  271. ///
  272. /// Side effect:
  273. /// The initial metadata that will be sent to the client from this op will
  274. /// be taken from the \a ServerContext associated with the call.
  275. ///
  276. /// \param[in] tag Tag identifying this request.
  277. void SendInitialMetadata(void* tag) override {
  278. GPR_ASSERT(!ctx_->sent_initial_metadata_);
  279. meta_buf_.set_output_tag(tag);
  280. meta_buf_.SendInitialMetadata(&ctx_->initial_metadata_,
  281. ctx_->initial_metadata_flags());
  282. if (ctx_->compression_level_set()) {
  283. meta_buf_.set_compression_level(ctx_->compression_level());
  284. }
  285. ctx_->sent_initial_metadata_ = true;
  286. call_.PerformOps(&meta_buf_);
  287. }
  288. /// Indicate that the stream is to be finished and request notification
  289. /// when the server has sent the appropriate signals to the client to
  290. /// end the call. Should not be used concurrently with other operations.
  291. ///
  292. /// \param[in] tag Tag identifying this request.
  293. /// \param[in] status To be sent to the client as the result of the call.
  294. /// \param[in] msg Message to be sent to the client.
  295. ///
  296. /// Side effect:
  297. /// - also sends initial metadata if not already sent (using the
  298. /// \a ServerContext associated with this call).
  299. ///
  300. /// Note: if \a status has a non-OK code, then \a msg will not be sent,
  301. /// and the client will receive only the status with possible trailing
  302. /// metadata.
  303. ///
  304. /// gRPC doesn't take ownership or a reference to msg and status, so it is
  305. /// safe to deallocate them once the Finish operation is complete (i.e. a
  306. /// result arrives in the completion queue).
  307. void Finish(const W& msg, const grpc::Status& status, void* tag) {
  308. finish_buf_.set_output_tag(tag);
  309. finish_buf_.set_core_cq_tag(&finish_buf_);
  310. if (!ctx_->sent_initial_metadata_) {
  311. finish_buf_.SendInitialMetadata(&ctx_->initial_metadata_,
  312. ctx_->initial_metadata_flags());
  313. if (ctx_->compression_level_set()) {
  314. finish_buf_.set_compression_level(ctx_->compression_level());
  315. }
  316. ctx_->sent_initial_metadata_ = true;
  317. }
  318. // The response is dropped if the status is not OK.
  319. if (status.ok()) {
  320. finish_buf_.ServerSendStatus(&ctx_->trailing_metadata_,
  321. finish_buf_.SendMessage(msg));
  322. } else {
  323. finish_buf_.ServerSendStatus(&ctx_->trailing_metadata_, status);
  324. }
  325. call_.PerformOps(&finish_buf_);
  326. }
  327. /// Indicate that the stream is to be finished with a non-OK status,
  328. /// and request notification for when the server has finished sending the
  329. /// appropriate signals to the client to end the call.
  330. /// Should not be used concurrently with other operations.
  331. ///
  332. /// \param[in] tag Tag identifying this request.
  333. /// \param[in] status To be sent to the client as the result of the call.
  334. /// - Note: \a status must have a non-OK code.
  335. ///
  336. /// Side effect:
  337. /// - also sends initial metadata if not already sent (using the
  338. /// \a ServerContext associated with this call).
  339. ///
  340. /// gRPC doesn't take ownership or a reference to status, so it is safe to
  341. /// deallocate them once the Finish operation is complete (i.e. a result
  342. /// arrives in the completion queue).
  343. void FinishWithError(const grpc::Status& status, void* tag) {
  344. GPR_ASSERT(!status.ok());
  345. finish_buf_.set_output_tag(tag);
  346. if (!ctx_->sent_initial_metadata_) {
  347. finish_buf_.SendInitialMetadata(&ctx_->initial_metadata_,
  348. ctx_->initial_metadata_flags());
  349. if (ctx_->compression_level_set()) {
  350. finish_buf_.set_compression_level(ctx_->compression_level());
  351. }
  352. ctx_->sent_initial_metadata_ = true;
  353. }
  354. finish_buf_.ServerSendStatus(&ctx_->trailing_metadata_, status);
  355. call_.PerformOps(&finish_buf_);
  356. }
  357. private:
  358. void BindCall(grpc::internal::Call* call) override { call_ = *call; }
  359. grpc::internal::Call call_;
  360. grpc::ServerContext* ctx_;
  361. grpc::internal::CallOpSet<grpc::internal::CallOpSendInitialMetadata>
  362. meta_buf_;
  363. grpc::internal::CallOpSet<grpc::internal::CallOpSendInitialMetadata,
  364. grpc::internal::CallOpSendMessage,
  365. grpc::internal::CallOpServerSendStatus>
  366. finish_buf_;
  367. };
  368. } // namespace grpc
  369. namespace std {
  370. template <class R>
  371. class default_delete<grpc::ClientAsyncResponseReader<R>> {
  372. public:
  373. void operator()(void* /*p*/) {}
  374. };
  375. template <class R>
  376. class default_delete<grpc::ClientAsyncResponseReaderInterface<R>> {
  377. public:
  378. void operator()(void* /*p*/) {}
  379. };
  380. } // namespace std
  381. #endif // GRPCPP_SUPPORT_ASYNC_UNARY_CALL_H