basic_channel.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. //
  2. // experimental/basic_channel.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_EXPERIMENTAL_BASIC_CHANNEL_HPP
  11. #define BOOST_ASIO_EXPERIMENTAL_BASIC_CHANNEL_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #include <boost/asio/detail/non_const_lvalue.hpp>
  17. #include <boost/asio/detail/null_mutex.hpp>
  18. #include <boost/asio/execution/executor.hpp>
  19. #include <boost/asio/execution_context.hpp>
  20. #include <boost/asio/experimental/detail/channel_send_functions.hpp>
  21. #include <boost/asio/experimental/detail/channel_service.hpp>
  22. #include <boost/asio/detail/push_options.hpp>
  23. namespace boost {
  24. namespace asio {
  25. namespace experimental {
  26. namespace detail {
  27. } // namespace detail
  28. /// A channel for messages.
  29. /**
  30. * The basic_channel class template is used for sending messages between
  31. * different parts of the same application. A <em>message</em> is defined as a
  32. * collection of arguments to be passed to a completion handler, and the set of
  33. * messages supported by a channel is specified by its @c Traits and
  34. * <tt>Signatures...</tt> template parameters. Messages may be sent and received
  35. * using asynchronous or non-blocking synchronous operations.
  36. *
  37. * Unless customising the traits, applications will typically use the @c
  38. * experimental::channel alias template. For example:
  39. * @code void send_loop(int i, steady_timer& timer,
  40. * channel<void(error_code, int)>& ch)
  41. * {
  42. * if (i < 10)
  43. * {
  44. * timer.expires_after(chrono::seconds(1));
  45. * timer.async_wait(
  46. * [i, &timer, &ch](error_code error)
  47. * {
  48. * if (!error)
  49. * {
  50. * ch.async_send(error_code(), i,
  51. * [i, &timer, &ch](error_code error)
  52. * {
  53. * if (!error)
  54. * {
  55. * send_loop(i + 1, timer, ch);
  56. * }
  57. * });
  58. * }
  59. * });
  60. * }
  61. * else
  62. * {
  63. * ch.close();
  64. * }
  65. * }
  66. *
  67. * void receive_loop(channel<void(error_code, int)>& ch)
  68. * {
  69. * ch.async_receive(
  70. * [&ch](error_code error, int i)
  71. * {
  72. * if (!error)
  73. * {
  74. * std::cout << "Received " << i << "\n";
  75. * receive_loop(ch);
  76. * }
  77. * });
  78. * } @endcode
  79. *
  80. * @par Thread Safety
  81. * @e Distinct @e objects: Safe.@n
  82. * @e Shared @e objects: Unsafe.
  83. *
  84. * The basic_channel class template is not thread-safe, and would typically be
  85. * used for passing messages between application code that runs on the same
  86. * thread or in the same strand. Consider using @ref basic_concurrent_channel,
  87. * and its alias template @c experimental::concurrent_channel, to pass messages
  88. * between code running in different threads.
  89. */
  90. template <typename Executor, typename Traits, typename... Signatures>
  91. class basic_channel
  92. #if !defined(GENERATING_DOCUMENTATION)
  93. : public detail::channel_send_functions<
  94. basic_channel<Executor, Traits, Signatures...>,
  95. Executor, Signatures...>
  96. #endif // !defined(GENERATING_DOCUMENTATION)
  97. {
  98. private:
  99. class initiate_async_send;
  100. class initiate_async_receive;
  101. typedef detail::channel_service<boost::asio::detail::null_mutex> service_type;
  102. typedef typename service_type::template implementation_type<
  103. Traits, Signatures...>::payload_type payload_type;
  104. template <typename... PayloadSignatures,
  105. BOOST_ASIO_COMPLETION_TOKEN_FOR(PayloadSignatures...) CompletionToken>
  106. auto do_async_receive(
  107. boost::asio::detail::completion_payload<PayloadSignatures...>*,
  108. CompletionToken&& token)
  109. -> decltype(
  110. async_initiate<CompletionToken, PayloadSignatures...>(
  111. declval<initiate_async_receive>(), token))
  112. {
  113. return async_initiate<CompletionToken, PayloadSignatures...>(
  114. initiate_async_receive(this), token);
  115. }
  116. public:
  117. /// The type of the executor associated with the channel.
  118. typedef Executor executor_type;
  119. /// Rebinds the channel type to another executor.
  120. template <typename Executor1>
  121. struct rebind_executor
  122. {
  123. /// The channel type when rebound to the specified executor.
  124. typedef basic_channel<Executor1, Traits, Signatures...> other;
  125. };
  126. /// The traits type associated with the channel.
  127. typedef typename Traits::template rebind<Signatures...>::other traits_type;
  128. /// Construct a basic_channel.
  129. /**
  130. * This constructor creates and channel.
  131. *
  132. * @param ex The I/O executor that the channel will use, by default, to
  133. * dispatch handlers for any asynchronous operations performed on the channel.
  134. *
  135. * @param max_buffer_size The maximum number of messages that may be buffered
  136. * in the channel.
  137. */
  138. basic_channel(const executor_type& ex, std::size_t max_buffer_size = 0)
  139. : service_(&boost::asio::use_service<service_type>(
  140. basic_channel::get_context(ex))),
  141. impl_(),
  142. executor_(ex)
  143. {
  144. service_->construct(impl_, max_buffer_size);
  145. }
  146. /// Construct and open a basic_channel.
  147. /**
  148. * This constructor creates and opens a channel.
  149. *
  150. * @param context An execution context which provides the I/O executor that
  151. * the channel will use, by default, to dispatch handlers for any asynchronous
  152. * operations performed on the channel.
  153. *
  154. * @param max_buffer_size The maximum number of messages that may be buffered
  155. * in the channel.
  156. */
  157. template <typename ExecutionContext>
  158. basic_channel(ExecutionContext& context, std::size_t max_buffer_size = 0,
  159. constraint_t<
  160. is_convertible<ExecutionContext&, execution_context&>::value,
  161. defaulted_constraint
  162. > = defaulted_constraint())
  163. : service_(&boost::asio::use_service<service_type>(context)),
  164. impl_(),
  165. executor_(context.get_executor())
  166. {
  167. service_->construct(impl_, max_buffer_size);
  168. }
  169. /// Move-construct a basic_channel from another.
  170. /**
  171. * This constructor moves a channel from one object to another.
  172. *
  173. * @param other The other basic_channel object from which the move will occur.
  174. *
  175. * @note Following the move, the moved-from object is in the same state as if
  176. * constructed using the @c basic_channel(const executor_type&) constructor.
  177. */
  178. basic_channel(basic_channel&& other)
  179. : service_(other.service_),
  180. executor_(other.executor_)
  181. {
  182. service_->move_construct(impl_, other.impl_);
  183. }
  184. /// Move-assign a basic_channel from another.
  185. /**
  186. * This assignment operator moves a channel from one object to another.
  187. * Cancels any outstanding asynchronous operations associated with the target
  188. * object.
  189. *
  190. * @param other The other basic_channel object from which the move will occur.
  191. *
  192. * @note Following the move, the moved-from object is in the same state as if
  193. * constructed using the @c basic_channel(const executor_type&)
  194. * constructor.
  195. */
  196. basic_channel& operator=(basic_channel&& other)
  197. {
  198. if (this != &other)
  199. {
  200. service_->move_assign(impl_, *other.service_, other.impl_);
  201. executor_.~executor_type();
  202. new (&executor_) executor_type(other.executor_);
  203. service_ = other.service_;
  204. }
  205. return *this;
  206. }
  207. // All channels have access to each other's implementations.
  208. template <typename, typename, typename...>
  209. friend class basic_channel;
  210. /// Move-construct a basic_channel from another.
  211. /**
  212. * This constructor moves a channel from one object to another.
  213. *
  214. * @param other The other basic_channel object from which the move will occur.
  215. *
  216. * @note Following the move, the moved-from object is in the same state as if
  217. * constructed using the @c basic_channel(const executor_type&)
  218. * constructor.
  219. */
  220. template <typename Executor1>
  221. basic_channel(
  222. basic_channel<Executor1, Traits, Signatures...>&& other,
  223. constraint_t<
  224. is_convertible<Executor1, Executor>::value
  225. > = 0)
  226. : service_(other.service_),
  227. executor_(other.executor_)
  228. {
  229. service_->move_construct(impl_, other.impl_);
  230. }
  231. /// Move-assign a basic_channel from another.
  232. /**
  233. * This assignment operator moves a channel from one object to another.
  234. * Cancels any outstanding asynchronous operations associated with the target
  235. * object.
  236. *
  237. * @param other The other basic_channel object from which the move will
  238. * occur.
  239. *
  240. * @note Following the move, the moved-from object is in the same state as if
  241. * constructed using the @c basic_channel(const executor_type&)
  242. * constructor.
  243. */
  244. template <typename Executor1>
  245. constraint_t<
  246. is_convertible<Executor1, Executor>::value,
  247. basic_channel&
  248. > operator=(basic_channel<Executor1, Traits, Signatures...>&& other)
  249. {
  250. if (this != &other)
  251. {
  252. service_->move_assign(impl_, *other.service_, other.impl_);
  253. executor_.~executor_type();
  254. new (&executor_) executor_type(other.executor_);
  255. service_ = other.service_;
  256. }
  257. return *this;
  258. }
  259. /// Destructor.
  260. ~basic_channel()
  261. {
  262. service_->destroy(impl_);
  263. }
  264. /// Get the executor associated with the object.
  265. const executor_type& get_executor() noexcept
  266. {
  267. return executor_;
  268. }
  269. /// Get the capacity of the channel's buffer.
  270. std::size_t capacity() noexcept
  271. {
  272. return service_->capacity(impl_);
  273. }
  274. /// Determine whether the channel is open.
  275. bool is_open() const noexcept
  276. {
  277. return service_->is_open(impl_);
  278. }
  279. /// Reset the channel to its initial state.
  280. void reset()
  281. {
  282. service_->reset(impl_);
  283. }
  284. /// Close the channel.
  285. void close()
  286. {
  287. service_->close(impl_);
  288. }
  289. /// Cancel all asynchronous operations waiting on the channel.
  290. /**
  291. * All outstanding send operations will complete with the error
  292. * @c boost::asio::experimental::error::channel_cancelled. Outstanding receive
  293. * operations complete with the result as determined by the channel traits.
  294. */
  295. void cancel()
  296. {
  297. service_->cancel(impl_);
  298. }
  299. /// Determine whether a message can be received without blocking.
  300. bool ready() const noexcept
  301. {
  302. return service_->ready(impl_);
  303. }
  304. #if defined(GENERATING_DOCUMENTATION)
  305. /// Try to send a message without blocking.
  306. /**
  307. * Fails if the buffer is full and there are no waiting receive operations.
  308. *
  309. * @returns @c true on success, @c false on failure.
  310. */
  311. template <typename... Args>
  312. bool try_send(Args&&... args);
  313. /// Try to send a message without blocking, using dispatch semantics to call
  314. /// the receive operation's completion handler.
  315. /**
  316. * Fails if the buffer is full and there are no waiting receive operations.
  317. *
  318. * The receive operation's completion handler may be called from inside this
  319. * function.
  320. *
  321. * @returns @c true on success, @c false on failure.
  322. */
  323. template <typename... Args>
  324. bool try_send_via_dispatch(Args&&... args);
  325. /// Try to send a number of messages without blocking.
  326. /**
  327. * @returns The number of messages that were sent.
  328. */
  329. template <typename... Args>
  330. std::size_t try_send_n(std::size_t count, Args&&... args);
  331. /// Try to send a number of messages without blocking, using dispatch
  332. /// semantics to call the receive operations' completion handlers.
  333. /**
  334. * The receive operations' completion handlers may be called from inside this
  335. * function.
  336. *
  337. * @returns The number of messages that were sent.
  338. */
  339. template <typename... Args>
  340. std::size_t try_send_n_via_dispatch(std::size_t count, Args&&... args);
  341. /// Asynchronously send a message.
  342. /**
  343. * @par Completion Signature
  344. * @code void(boost::system::error_code) @endcode
  345. */
  346. template <typename... Args,
  347. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code))
  348. CompletionToken BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  349. auto async_send(Args&&... args,
  350. CompletionToken&& token);
  351. #endif // defined(GENERATING_DOCUMENTATION)
  352. /// Try to receive a message without blocking.
  353. /**
  354. * Fails if the buffer is full and there are no waiting receive operations.
  355. *
  356. * @returns @c true on success, @c false on failure.
  357. */
  358. template <typename Handler>
  359. bool try_receive(Handler&& handler)
  360. {
  361. return service_->try_receive(impl_, static_cast<Handler&&>(handler));
  362. }
  363. /// Asynchronously receive a message.
  364. /**
  365. * @par Completion Signature
  366. * As determined by the <tt>Signatures...</tt> template parameter and the
  367. * channel traits.
  368. */
  369. template <typename CompletionToken
  370. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  371. auto async_receive(
  372. CompletionToken&& token
  373. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(Executor))
  374. #if !defined(GENERATING_DOCUMENTATION)
  375. -> decltype(
  376. this->do_async_receive(static_cast<payload_type*>(0),
  377. static_cast<CompletionToken&&>(token)))
  378. #endif // !defined(GENERATING_DOCUMENTATION)
  379. {
  380. return this->do_async_receive(static_cast<payload_type*>(0),
  381. static_cast<CompletionToken&&>(token));
  382. }
  383. private:
  384. // Disallow copying and assignment.
  385. basic_channel(const basic_channel&) = delete;
  386. basic_channel& operator=(const basic_channel&) = delete;
  387. template <typename, typename, typename...>
  388. friend class detail::channel_send_functions;
  389. // Helper function to get an executor's context.
  390. template <typename T>
  391. static execution_context& get_context(const T& t,
  392. enable_if_t<execution::is_executor<T>::value>* = 0)
  393. {
  394. return boost::asio::query(t, execution::context);
  395. }
  396. // Helper function to get an executor's context.
  397. template <typename T>
  398. static execution_context& get_context(const T& t,
  399. enable_if_t<!execution::is_executor<T>::value>* = 0)
  400. {
  401. return t.context();
  402. }
  403. class initiate_async_send
  404. {
  405. public:
  406. typedef Executor executor_type;
  407. explicit initiate_async_send(basic_channel* self)
  408. : self_(self)
  409. {
  410. }
  411. const executor_type& get_executor() const noexcept
  412. {
  413. return self_->get_executor();
  414. }
  415. template <typename SendHandler>
  416. void operator()(SendHandler&& handler,
  417. payload_type&& payload) const
  418. {
  419. boost::asio::detail::non_const_lvalue<SendHandler> handler2(handler);
  420. self_->service_->async_send(self_->impl_,
  421. static_cast<payload_type&&>(payload),
  422. handler2.value, self_->get_executor());
  423. }
  424. private:
  425. basic_channel* self_;
  426. };
  427. class initiate_async_receive
  428. {
  429. public:
  430. typedef Executor executor_type;
  431. explicit initiate_async_receive(basic_channel* self)
  432. : self_(self)
  433. {
  434. }
  435. const executor_type& get_executor() const noexcept
  436. {
  437. return self_->get_executor();
  438. }
  439. template <typename ReceiveHandler>
  440. void operator()(ReceiveHandler&& handler) const
  441. {
  442. boost::asio::detail::non_const_lvalue<ReceiveHandler> handler2(handler);
  443. self_->service_->async_receive(self_->impl_,
  444. handler2.value, self_->get_executor());
  445. }
  446. private:
  447. basic_channel* self_;
  448. };
  449. // The service associated with the I/O object.
  450. service_type* service_;
  451. // The underlying implementation of the I/O object.
  452. typename service_type::template implementation_type<
  453. Traits, Signatures...> impl_;
  454. // The associated executor.
  455. Executor executor_;
  456. };
  457. } // namespace experimental
  458. } // namespace asio
  459. } // namespace boost
  460. #include <boost/asio/detail/pop_options.hpp>
  461. #endif // BOOST_ASIO_EXPERIMENTAL_BASIC_CHANNEL_HPP