descriptor_read_op.hpp 6.2 KB

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