async_unary_call.h 17 KB

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