descriptor_read_op.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //
  2. // detail/descriptor_read_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_DETAIL_DESCRIPTOR_READ_OP_HPP
  11. #define 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 "asio/detail/config.hpp"
  16. #if !defined(ASIO_WINDOWS) && !defined(__CYGWIN__)
  17. #include "asio/detail/bind_handler.hpp"
  18. #include "asio/detail/buffer_sequence_adapter.hpp"
  19. #include "asio/detail/descriptor_ops.hpp"
  20. #include "asio/detail/fenced_block.hpp"
  21. #include "asio/detail/handler_work.hpp"
  22. #include "asio/detail/memory.hpp"
  23. #include "asio/detail/reactor_op.hpp"
  24. #include "asio/dispatch.hpp"
  25. #include "asio/detail/push_options.hpp"
  26. namespace asio {
  27. namespace detail {
  28. template <typename MutableBufferSequence>
  29. class descriptor_read_op_base : public reactor_op
  30. {
  31. public:
  32. descriptor_read_op_base(const asio::error_code& success_ec,
  33. int descriptor, const MutableBufferSequence& buffers,
  34. func_type complete_func)
  35. : reactor_op(success_ec,
  36. &descriptor_read_op_base::do_perform, complete_func),
  37. descriptor_(descriptor),
  38. buffers_(buffers)
  39. {
  40. }
  41. static status do_perform(reactor_op* base)
  42. {
  43. ASIO_ASSUME(base != 0);
  44. descriptor_read_op_base* o(static_cast<descriptor_read_op_base*>(base));
  45. typedef buffer_sequence_adapter<asio::mutable_buffer,
  46. MutableBufferSequence> bufs_type;
  47. status result;
  48. if (bufs_type::is_single_buffer)
  49. {
  50. result = descriptor_ops::non_blocking_read1(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_read(o->descriptor_,
  59. bufs.buffers(), bufs.count(), o->ec_, o->bytes_transferred_)
  60. ? done : not_done;
  61. }
  62. ASIO_HANDLER_REACTOR_OPERATION((*o, "non_blocking_read",
  63. o->ec_, o->bytes_transferred_));
  64. return result;
  65. }
  66. private:
  67. int descriptor_;
  68. MutableBufferSequence buffers_;
  69. };
  70. template <typename MutableBufferSequence, typename Handler, typename IoExecutor>
  71. class descriptor_read_op
  72. : public descriptor_read_op_base<MutableBufferSequence>
  73. {
  74. public:
  75. typedef Handler handler_type;
  76. typedef IoExecutor io_executor_type;
  77. ASIO_DEFINE_HANDLER_PTR(descriptor_read_op);
  78. descriptor_read_op(const asio::error_code& success_ec,
  79. int descriptor, const MutableBufferSequence& buffers,
  80. Handler& handler, const IoExecutor& io_ex)
  81. : descriptor_read_op_base<MutableBufferSequence>(success_ec,
  82. descriptor, buffers, &descriptor_read_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 asio::error_code& /*ec*/,
  89. std::size_t /*bytes_transferred*/)
  90. {
  91. // Take ownership of the handler object.
  92. ASIO_ASSUME(base != 0);
  93. descriptor_read_op* o(static_cast<descriptor_read_op*>(base));
  94. ptr p = { asio::detail::addressof(o->handler_), o, o };
  95. 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. 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, asio::error_code, std::size_t>
  108. handler(o->handler_, o->ec_, o->bytes_transferred_);
  109. p.h = 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. ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_));
  116. w.complete(handler, handler.handler_);
  117. 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. ASIO_ASSUME(base != 0);
  124. descriptor_read_op* o(static_cast<descriptor_read_op*>(base));
  125. ptr p = { asio::detail::addressof(o->handler_), o, o };
  126. 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. 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, asio::error_code, std::size_t>
  139. handler(o->handler_, o->ec_, o->bytes_transferred_);
  140. p.h = asio::detail::addressof(handler.handler_);
  141. p.reset();
  142. ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_));
  143. w.complete(handler, handler.handler_, io_ex);
  144. ASIO_HANDLER_INVOCATION_END;
  145. }
  146. private:
  147. Handler handler_;
  148. handler_work<Handler, IoExecutor> work_;
  149. };
  150. } // namespace detail
  151. } // namespace asio
  152. #include "asio/detail/pop_options.hpp"
  153. #endif // !defined(ASIO_WINDOWS) && !defined(__CYGWIN__)
  154. #endif // ASIO_DETAIL_DESCRIPTOR_READ_OP_HPP