channel_send_op.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // experimental/detail/channel_send_op.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_DETAIL_CHANNEL_SEND_OP_HPP
  11. #define BOOST_ASIO_EXPERIMENTAL_DETAIL_CHANNEL_SEND_OP_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/bind_handler.hpp>
  17. #include <boost/asio/detail/handler_alloc_helpers.hpp>
  18. #include <boost/asio/error.hpp>
  19. #include <boost/asio/experimental/channel_error.hpp>
  20. #include <boost/asio/experimental/detail/channel_operation.hpp>
  21. #include <boost/asio/experimental/detail/channel_payload.hpp>
  22. #include <boost/asio/detail/push_options.hpp>
  23. namespace boost {
  24. namespace asio {
  25. namespace experimental {
  26. namespace detail {
  27. template <typename Payload>
  28. class channel_send : public channel_operation
  29. {
  30. public:
  31. Payload get_payload()
  32. {
  33. return static_cast<Payload&&>(payload_);
  34. }
  35. void immediate()
  36. {
  37. func_(this, immediate_op, 0);
  38. }
  39. void post()
  40. {
  41. func_(this, post_op, 0);
  42. }
  43. void cancel()
  44. {
  45. func_(this, cancel_op, 0);
  46. }
  47. void close()
  48. {
  49. func_(this, close_op, 0);
  50. }
  51. protected:
  52. channel_send(func_type func, Payload&& payload)
  53. : channel_operation(func),
  54. payload_(static_cast<Payload&&>(payload))
  55. {
  56. }
  57. private:
  58. Payload payload_;
  59. };
  60. template <typename Payload, typename Handler, typename IoExecutor>
  61. class channel_send_op : public channel_send<Payload>
  62. {
  63. public:
  64. BOOST_ASIO_DEFINE_HANDLER_PTR(channel_send_op);
  65. channel_send_op(Payload&& payload,
  66. Handler& handler, const IoExecutor& io_ex)
  67. : channel_send<Payload>(&channel_send_op::do_action,
  68. static_cast<Payload&&>(payload)),
  69. handler_(static_cast<Handler&&>(handler)),
  70. work_(handler_, io_ex)
  71. {
  72. }
  73. static void do_action(channel_operation* base,
  74. channel_operation::action a, void*)
  75. {
  76. // Take ownership of the operation object.
  77. channel_send_op* o(static_cast<channel_send_op*>(base));
  78. ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
  79. BOOST_ASIO_HANDLER_COMPLETION((*o));
  80. // Take ownership of the operation's outstanding work.
  81. channel_operation::handler_work<Handler, IoExecutor> w(
  82. static_cast<channel_operation::handler_work<Handler, IoExecutor>&&>(
  83. o->work_));
  84. boost::system::error_code ec;
  85. switch (a)
  86. {
  87. case channel_operation::cancel_op:
  88. ec = error::channel_cancelled;
  89. break;
  90. case channel_operation::close_op:
  91. ec = error::channel_closed;
  92. break;
  93. default:
  94. break;
  95. }
  96. // Make a copy of the handler so that the memory can be deallocated before
  97. // the handler is posted. Even if we're not about to post the handler, a
  98. // sub-object of the handler may be the true owner of the memory associated
  99. // with the handler. Consequently, a local copy of the handler is required
  100. // to ensure that any owning sub-object remains valid until after we have
  101. // deallocated the memory here.
  102. boost::asio::detail::binder1<Handler, boost::system::error_code>
  103. handler(o->handler_, ec);
  104. p.h = boost::asio::detail::addressof(handler.handler_);
  105. p.reset();
  106. // Post the completion if required.
  107. if (a != channel_operation::destroy_op)
  108. {
  109. BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_));
  110. if (a == channel_operation::immediate_op)
  111. w.immediate(handler, handler.handler_, 0);
  112. else
  113. w.post(handler, handler.handler_);
  114. BOOST_ASIO_HANDLER_INVOCATION_END;
  115. }
  116. }
  117. private:
  118. Handler handler_;
  119. channel_operation::handler_work<Handler, IoExecutor> work_;
  120. };
  121. } // namespace detail
  122. } // namespace experimental
  123. } // namespace asio
  124. } // namespace boost
  125. #include <boost/asio/detail/pop_options.hpp>
  126. #endif // BOOST_ASIO_EXPERIMENTAL_DETAIL_CHANNEL_SEND_OP_HPP