descriptor_write_op.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //
  2. // detail/descriptor_write_op.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_DETAIL_DESCRIPTOR_WRITE_OP_HPP
  11. #define BOOST_ASIO_DETAIL_DESCRIPTOR_WRITE_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. #if !defined(BOOST_ASIO_WINDOWS) && !defined(__CYGWIN__)
  17. #include <boost/asio/detail/bind_handler.hpp>
  18. #include <boost/asio/detail/buffer_sequence_adapter.hpp>
  19. #include <boost/asio/detail/descriptor_ops.hpp>
  20. #include <boost/asio/detail/fenced_block.hpp>
  21. #include <boost/asio/detail/handler_work.hpp>
  22. #include <boost/asio/detail/memory.hpp>
  23. #include <boost/asio/detail/reactor_op.hpp>
  24. #include <boost/asio/detail/push_options.hpp>
  25. namespace boost {
  26. namespace asio {
  27. namespace detail {
  28. template <typename ConstBufferSequence>
  29. class descriptor_write_op_base : public reactor_op
  30. {
  31. public:
  32. descriptor_write_op_base(const boost::system::error_code& success_ec,
  33. int descriptor, const ConstBufferSequence& buffers,
  34. func_type complete_func)
  35. : reactor_op(success_ec,
  36. &descriptor_write_op_base::do_perform, complete_func),
  37. descriptor_(descriptor),
  38. buffers_(buffers)
  39. {
  40. }
  41. static status do_perform(reactor_op* base)
  42. {
  43. BOOST_ASIO_ASSUME(base != 0);
  44. descriptor_write_op_base* o(static_cast<descriptor_write_op_base*>(base));
  45. typedef buffer_sequence_adapter<boost::asio::const_buffer,
  46. ConstBufferSequence> bufs_type;
  47. status result;
  48. if (bufs_type::is_single_buffer)
  49. {
  50. result = descriptor_ops::non_blocking_write1(o->descriptor_,
  51. bufs_type::first(o->buffers_).data(),
  52. bufs_type::first(o->buffers_).size(),
  53. o->ec_, o->bytes_transferred_) ? done : not_done;
  54. }
  55. else
  56. {
  57. bufs_type bufs(o->buffers_);
  58. result = descriptor_ops::non_blocking_write(o->descriptor_,
  59. bufs.buffers(), bufs.count(), o->ec_, o->bytes_transferred_)
  60. ? done : not_done;
  61. }
  62. BOOST_ASIO_HANDLER_REACTOR_OPERATION((*o, "non_blocking_write",
  63. o->ec_, o->bytes_transferred_));
  64. return result;
  65. }
  66. private:
  67. int descriptor_;
  68. ConstBufferSequence buffers_;
  69. };
  70. template <typename ConstBufferSequence, typename Handler, typename IoExecutor>
  71. class descriptor_write_op
  72. : public descriptor_write_op_base<ConstBufferSequence>
  73. {
  74. public:
  75. typedef Handler handler_type;
  76. typedef IoExecutor io_executor_type;
  77. BOOST_ASIO_DEFINE_HANDLER_PTR(descriptor_write_op);
  78. descriptor_write_op(const boost::system::error_code& success_ec,
  79. int descriptor, const ConstBufferSequence& buffers,
  80. Handler& handler, const IoExecutor& io_ex)
  81. : descriptor_write_op_base<ConstBufferSequence>(success_ec,
  82. descriptor, buffers, &descriptor_write_op::do_complete),
  83. handler_(static_cast<Handler&&>(handler)),
  84. work_(handler_, io_ex)
  85. {
  86. }
  87. static void do_complete(void* owner, operation* base,
  88. const boost::system::error_code& /*ec*/,
  89. std::size_t /*bytes_transferred*/)
  90. {
  91. // Take ownership of the handler object.
  92. BOOST_ASIO_ASSUME(base != 0);
  93. descriptor_write_op* o(static_cast<descriptor_write_op*>(base));
  94. ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
  95. BOOST_ASIO_HANDLER_COMPLETION((*o));
  96. // Take ownership of the operation's outstanding work.
  97. handler_work<Handler, IoExecutor> w(
  98. static_cast<handler_work<Handler, IoExecutor>&&>(
  99. o->work_));
  100. BOOST_ASIO_ERROR_LOCATION(o->ec_);
  101. // Make a copy of the handler so that the memory can be deallocated before
  102. // the upcall is made. Even if we're not about to make an upcall, a
  103. // sub-object of the handler may be the true owner of the memory associated
  104. // with the handler. Consequently, a local copy of the handler is required
  105. // to ensure that any owning sub-object remains valid until after we have
  106. // deallocated the memory here.
  107. detail::binder2<Handler, boost::system::error_code, std::size_t>
  108. handler(o->handler_, o->ec_, o->bytes_transferred_);
  109. p.h = boost::asio::detail::addressof(handler.handler_);
  110. p.reset();
  111. // Make the upcall if required.
  112. if (owner)
  113. {
  114. fenced_block b(fenced_block::half);
  115. BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_));
  116. w.complete(handler, handler.handler_);
  117. BOOST_ASIO_HANDLER_INVOCATION_END;
  118. }
  119. }
  120. static void do_immediate(operation* base, bool, const void* io_ex)
  121. {
  122. // Take ownership of the handler object.
  123. BOOST_ASIO_ASSUME(base != 0);
  124. descriptor_write_op* o(static_cast<descriptor_write_op*>(base));
  125. ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
  126. BOOST_ASIO_HANDLER_COMPLETION((*o));
  127. // Take ownership of the operation's outstanding work.
  128. immediate_handler_work<Handler, IoExecutor> w(
  129. static_cast<handler_work<Handler, IoExecutor>&&>(
  130. o->work_));
  131. BOOST_ASIO_ERROR_LOCATION(o->ec_);
  132. // Make a copy of the handler so that the memory can be deallocated before
  133. // the upcall is made. Even if we're not about to make an upcall, a
  134. // sub-object of the handler may be the true owner of the memory associated
  135. // with the handler. Consequently, a local copy of the handler is required
  136. // to ensure that any owning sub-object remains valid until after we have
  137. // deallocated the memory here.
  138. detail::binder2<Handler, boost::system::error_code, std::size_t>
  139. handler(o->handler_, o->ec_, o->bytes_transferred_);
  140. p.h = boost::asio::detail::addressof(handler.handler_);
  141. p.reset();
  142. BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_));
  143. w.complete(handler, handler.handler_, io_ex);
  144. BOOST_ASIO_HANDLER_INVOCATION_END;
  145. }
  146. private:
  147. Handler handler_;
  148. handler_work<Handler, IoExecutor> work_;
  149. };
  150. } // namespace detail
  151. } // namespace asio
  152. } // namespace boost
  153. #include <boost/asio/detail/pop_options.hpp>
  154. #endif // !defined(BOOST_ASIO_WINDOWS) && !defined(__CYGWIN__)
  155. #endif // BOOST_ASIO_DETAIL_DESCRIPTOR_WRITE_OP_HPP