basic_channel.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. //
  2. // experimental/basic_channel.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2023 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(detail::channel_payload<PayloadSignatures...>*,
  107. CompletionToken&& token)
  108. -> decltype(
  109. async_initiate<CompletionToken, PayloadSignatures...>(
  110. declval<initiate_async_receive>(), token))
  111. {
  112. return async_initiate<CompletionToken, PayloadSignatures...>(
  113. initiate_async_receive(this), token);
  114. }
  115. public:
  116. /// The type of the executor associated with the channel.
  117. typedef Executor executor_type;
  118. /// Rebinds the channel type to another executor.
  119. template <typename Executor1>
  120. struct rebind_executor
  121. {
  122. /// The channel type when rebound to the specified executor.
  123. typedef basic_channel<Executor1, Traits, Signatures...> other;
  124. };
  125. /// The traits type associated with the channel.
  126. typedef typename Traits::template rebind<Signatures...>::other traits_type;
  127. /// Construct a basic_channel.
  128. /**
  129. * This constructor creates and channel.
  130. *
  131. * @param ex The I/O executor that the channel will use, by default, to
  132. * dispatch handlers for any asynchronous operations performed on the channel.
  133. *
  134. * @param max_buffer_size The maximum number of messages that may be buffered
  135. * in the channel.
  136. */
  137. basic_channel(const executor_type& ex, std::size_t max_buffer_size = 0)
  138. : service_(&boost::asio::use_service<service_type>(
  139. basic_channel::get_context(ex))),
  140. impl_(),
  141. executor_(ex)
  142. {
  143. service_->construct(impl_, max_buffer_size);
  144. }
  145. /// Construct and open a basic_channel.
  146. /**
  147. * This constructor creates and opens a channel.
  148. *
  149. * @param context An execution context which provides the I/O executor that
  150. * the channel will use, by default, to dispatch handlers for any asynchronous
  151. * operations performed on the channel.
  152. *
  153. * @param max_buffer_size The maximum number of messages that may be buffered
  154. * in the channel.
  155. */
  156. template <typename ExecutionContext>
  157. basic_channel(ExecutionContext& context, std::size_t max_buffer_size = 0,
  158. constraint_t<
  159. is_convertible<ExecutionContext&, execution_context&>::value,
  160. defaulted_constraint
  161. > = defaulted_constraint())
  162. : service_(&boost::asio::use_service<service_type>(context)),
  163. impl_(),
  164. executor_(context.get_executor())
  165. {
  166. service_->construct(impl_, max_buffer_size);
  167. }
  168. /// Move-construct a basic_channel from another.
  169. /**
  170. * This constructor moves a channel from one object to another.
  171. *
  172. * @param other The other basic_channel object from which the move will occur.
  173. *
  174. * @note Following the move, the moved-from object is in the same state as if
  175. * constructed using the @c basic_channel(const executor_type&) constructor.
  176. */
  177. basic_channel(basic_channel&& other)
  178. : service_(other.service_),
  179. executor_(other.executor_)
  180. {
  181. service_->move_construct(impl_, other.impl_);
  182. }
  183. /// Move-assign a basic_channel from another.
  184. /**
  185. * This assignment operator moves a channel from one object to another.
  186. * Cancels any outstanding asynchronous operations associated with the target
  187. * object.
  188. *
  189. * @param other The other basic_channel object from which the move will occur.
  190. *
  191. * @note Following the move, the moved-from object is in the same state as if
  192. * constructed using the @c basic_channel(const executor_type&)
  193. * constructor.
  194. */
  195. basic_channel& operator=(basic_channel&& other)
  196. {
  197. if (this != &other)
  198. {
  199. service_->move_assign(impl_, *other.service_, other.impl_);
  200. executor_.~executor_type();
  201. new (&executor_) executor_type(other.executor_);
  202. service_ = other.service_;
  203. }
  204. return *this;
  205. }
  206. // All channels have access to each other's implementations.
  207. template <typename, typename, typename...>
  208. friend class basic_channel;
  209. /// Move-construct a basic_channel from another.
  210. /**
  211. * This constructor moves a channel from one object to another.
  212. *
  213. * @param other The other basic_channel object from which the move will occur.
  214. *
  215. * @note Following the move, the moved-from object is in the same state as if
  216. * constructed using the @c basic_channel(const executor_type&)
  217. * constructor.
  218. */
  219. template <typename Executor1>
  220. basic_channel(
  221. basic_channel<Executor1, Traits, Signatures...>&& other,
  222. constraint_t<
  223. is_convertible<Executor1, Executor>::value
  224. > = 0)
  225. : service_(other.service_),
  226. executor_(other.executor_)
  227. {
  228. service_->move_construct(impl_, other.impl_);
  229. }
  230. /// Move-assign a basic_channel from another.
  231. /**
  232. * This assignment operator moves a channel from one object to another.
  233. * Cancels any outstanding asynchronous operations associated with the target
  234. * object.
  235. *
  236. * @param other The other basic_channel object from which the move will
  237. * occur.
  238. *
  239. * @note Following the move, the moved-from object is in the same state as if
  240. * constructed using the @c basic_channel(const executor_type&)
  241. * constructor.
  242. */
  243. template <typename Executor1>
  244. constraint_t<
  245. is_convertible<Executor1, Executor>::value,
  246. basic_channel&
  247. > operator=(basic_channel<Executor1, Traits, Signatures...>&& other)
  248. {
  249. if (this != &other)
  250. {
  251. service_->move_assign(impl_, *other.service_, other.impl_);
  252. executor_.~executor_type();
  253. new (&executor_) executor_type(other.executor_);
  254. service_ = other.service_;
  255. }
  256. return *this;
  257. }
  258. /// Destructor.
  259. ~basic_channel()
  260. {
  261. service_->destroy(impl_);
  262. }
  263. /// Get the executor associated with the object.
  264. const executor_type& get_executor() noexcept
  265. {
  266. return executor_;
  267. }
  268. /// Get the capacity of the channel's buffer.
  269. std::size_t capacity() noexcept
  270. {
  271. return service_->capacity(impl_);
  272. }
  273. /// Determine whether the channel is open.
  274. bool is_open() const noexcept
  275. {
  276. return service_->is_open(impl_);
  277. }
  278. /// Reset the channel to its initial state.
  279. void reset()
  280. {
  281. service_->reset(impl_);
  282. }
  283. /// Close the channel.
  284. void close()
  285. {
  286. service_->close(impl_);
  287. }
  288. /// Cancel all asynchronous operations waiting on the channel.
  289. /**
  290. * All outstanding send operations will complete with the error
  291. * @c boost::asio::experimental::error::channel_cancelled. Outstanding receive
  292. * operations complete with the result as determined by the channel traits.
  293. */
  294. void cancel()
  295. {
  296. service_->cancel(impl_);
  297. }
  298. /// Determine whether a message can be received without blocking.
  299. bool ready() const noexcept
  300. {
  301. return service_->ready(impl_);
  302. }
  303. #if defined(GENERATING_DOCUMENTATION)
  304. /// Try to send a message without blocking.
  305. /**
  306. * Fails if the buffer is full and there are no waiting receive operations.
  307. *
  308. * @returns @c true on success, @c false on failure.
  309. */
  310. template <typename... Args>
  311. bool try_send(Args&&... args);
  312. /// Try to send a message without blocking, using dispatch semantics to call
  313. /// the receive operation's completion handler.
  314. /**
  315. * Fails if the buffer is full and there are no waiting receive operations.
  316. *
  317. * The receive operation's completion handler may be called from inside this
  318. * function.
  319. *
  320. * @returns @c true on success, @c false on failure.
  321. */
  322. template <typename... Args>
  323. bool try_send_via_dispatch(Args&&... args);
  324. /// Try to send a number of messages without blocking.
  325. /**
  326. * @returns The number of messages that were sent.
  327. */
  328. template <typename... Args>
  329. std::size_t try_send_n(std::size_t count, Args&&... args);
  330. /// Try to send a number of messages without blocking, using dispatch
  331. /// semantics to call the receive operations' completion handlers.
  332. /**
  333. * The receive operations' completion handlers may be called from inside this
  334. * function.
  335. *
  336. * @returns The number of messages that were sent.
  337. */
  338. template <typename... Args>
  339. std::size_t try_send_n_via_dispatch(std::size_t count, Args&&... args);
  340. /// Asynchronously send a message.
  341. /**
  342. * @par Completion Signature
  343. * @code void(boost::system::error_code) @endcode
  344. */
  345. template <typename... Args,
  346. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code))
  347. CompletionToken BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  348. auto async_send(Args&&... args,
  349. CompletionToken&& token);
  350. #endif // defined(GENERATING_DOCUMENTATION)
  351. /// Try to receive a message without blocking.
  352. /**
  353. * Fails if the buffer is full and there are no waiting receive operations.
  354. *
  355. * @returns @c true on success, @c false on failure.
  356. */
  357. template <typename Handler>
  358. bool try_receive(Handler&& handler)
  359. {
  360. return service_->try_receive(impl_, static_cast<Handler&&>(handler));
  361. }
  362. /// Asynchronously receive a message.
  363. /**
  364. * @par Completion Signature
  365. * As determined by the <tt>Signatures...</tt> template parameter and the
  366. * channel traits.
  367. */
  368. template <typename CompletionToken
  369. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  370. auto async_receive(
  371. CompletionToken&& token
  372. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(Executor))
  373. #if !defined(GENERATING_DOCUMENTATION)
  374. -> decltype(
  375. this->do_async_receive(static_cast<payload_type*>(0),
  376. static_cast<CompletionToken&&>(token)))
  377. #endif // !defined(GENERATING_DOCUMENTATION)
  378. {
  379. return this->do_async_receive(static_cast<payload_type*>(0),
  380. static_cast<CompletionToken&&>(token));
  381. }
  382. private:
  383. // Disallow copying and assignment.
  384. basic_channel(const basic_channel&) = delete;
  385. basic_channel& operator=(const basic_channel&) = delete;
  386. template <typename, typename, typename...>
  387. friend class detail::channel_send_functions;
  388. // Helper function to get an executor's context.
  389. template <typename T>
  390. static execution_context& get_context(const T& t,
  391. enable_if_t<execution::is_executor<T>::value>* = 0)
  392. {
  393. return boost::asio::query(t, execution::context);
  394. }
  395. // Helper function to get an executor's context.
  396. template <typename T>
  397. static execution_context& get_context(const T& t,
  398. enable_if_t<!execution::is_executor<T>::value>* = 0)
  399. {
  400. return t.context();
  401. }
  402. class initiate_async_send
  403. {
  404. public:
  405. typedef Executor executor_type;
  406. explicit initiate_async_send(basic_channel* self)
  407. : self_(self)
  408. {
  409. }
  410. const executor_type& get_executor() const noexcept
  411. {
  412. return self_->get_executor();
  413. }
  414. template <typename SendHandler>
  415. void operator()(SendHandler&& handler,
  416. payload_type&& payload) const
  417. {
  418. boost::asio::detail::non_const_lvalue<SendHandler> handler2(handler);
  419. self_->service_->async_send(self_->impl_,
  420. static_cast<payload_type&&>(payload),
  421. handler2.value, self_->get_executor());
  422. }
  423. private:
  424. basic_channel* self_;
  425. };
  426. class initiate_async_receive
  427. {
  428. public:
  429. typedef Executor executor_type;
  430. explicit initiate_async_receive(basic_channel* self)
  431. : self_(self)
  432. {
  433. }
  434. const executor_type& get_executor() const noexcept
  435. {
  436. return self_->get_executor();
  437. }
  438. template <typename ReceiveHandler>
  439. void operator()(ReceiveHandler&& handler) const
  440. {
  441. boost::asio::detail::non_const_lvalue<ReceiveHandler> handler2(handler);
  442. self_->service_->async_receive(self_->impl_,
  443. handler2.value, self_->get_executor());
  444. }
  445. private:
  446. basic_channel* self_;
  447. };
  448. // The service associated with the I/O object.
  449. service_type* service_;
  450. // The underlying implementation of the I/O object.
  451. typename service_type::template implementation_type<
  452. Traits, Signatures...> impl_;
  453. // The associated executor.
  454. Executor executor_;
  455. };
  456. } // namespace experimental
  457. } // namespace asio
  458. } // namespace boost
  459. #include <boost/asio/detail/pop_options.hpp>
  460. #endif // BOOST_ASIO_EXPERIMENTAL_BASIC_CHANNEL_HPP