channel_send_op.hpp 3.8 KB

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