descriptor_read_op.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // detail/descriptor_read_op.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2022 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/detail/push_options.hpp"
  25. namespace asio {
  26. namespace detail {
  27. template <typename MutableBufferSequence>
  28. class descriptor_read_op_base : public reactor_op
  29. {
  30. public:
  31. descriptor_read_op_base(const asio::error_code& success_ec,
  32. int descriptor, const MutableBufferSequence& buffers,
  33. func_type complete_func)
  34. : reactor_op(success_ec,
  35. &descriptor_read_op_base::do_perform, complete_func),
  36. descriptor_(descriptor),
  37. buffers_(buffers)
  38. {
  39. }
  40. static status do_perform(reactor_op* base)
  41. {
  42. descriptor_read_op_base* o(static_cast<descriptor_read_op_base*>(base));
  43. typedef buffer_sequence_adapter<asio::mutable_buffer,
  44. MutableBufferSequence> bufs_type;
  45. status result;
  46. if (bufs_type::is_single_buffer)
  47. {
  48. result = descriptor_ops::non_blocking_read1(o->descriptor_,
  49. bufs_type::first(o->buffers_).data(),
  50. bufs_type::first(o->buffers_).size(),
  51. o->ec_, o->bytes_transferred_) ? done : not_done;
  52. }
  53. else
  54. {
  55. bufs_type bufs(o->buffers_);
  56. result = descriptor_ops::non_blocking_read(o->descriptor_,
  57. bufs.buffers(), bufs.count(), o->ec_, o->bytes_transferred_)
  58. ? done : not_done;
  59. }
  60. ASIO_HANDLER_REACTOR_OPERATION((*o, "non_blocking_read",
  61. o->ec_, o->bytes_transferred_));
  62. return result;
  63. }
  64. private:
  65. int descriptor_;
  66. MutableBufferSequence buffers_;
  67. };
  68. template <typename MutableBufferSequence, typename Handler, typename IoExecutor>
  69. class descriptor_read_op
  70. : public descriptor_read_op_base<MutableBufferSequence>
  71. {
  72. public:
  73. ASIO_DEFINE_HANDLER_PTR(descriptor_read_op);
  74. descriptor_read_op(const asio::error_code& success_ec,
  75. int descriptor, const MutableBufferSequence& buffers,
  76. Handler& handler, const IoExecutor& io_ex)
  77. : descriptor_read_op_base<MutableBufferSequence>(success_ec,
  78. descriptor, buffers, &descriptor_read_op::do_complete),
  79. handler_(ASIO_MOVE_CAST(Handler)(handler)),
  80. work_(handler_, io_ex)
  81. {
  82. }
  83. static void do_complete(void* owner, operation* base,
  84. const asio::error_code& /*ec*/,
  85. std::size_t /*bytes_transferred*/)
  86. {
  87. // Take ownership of the handler object.
  88. descriptor_read_op* o(static_cast<descriptor_read_op*>(base));
  89. ptr p = { asio::detail::addressof(o->handler_), o, o };
  90. ASIO_HANDLER_COMPLETION((*o));
  91. // Take ownership of the operation's outstanding work.
  92. handler_work<Handler, IoExecutor> w(
  93. ASIO_MOVE_CAST2(handler_work<Handler, IoExecutor>)(
  94. o->work_));
  95. // Make a copy of the handler so that the memory can be deallocated before
  96. // the upcall is made. Even if we're not about to make an upcall, 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. detail::binder2<Handler, asio::error_code, std::size_t>
  102. handler(o->handler_, o->ec_, o->bytes_transferred_);
  103. p.h = asio::detail::addressof(handler.handler_);
  104. p.reset();
  105. // Make the upcall if required.
  106. if (owner)
  107. {
  108. fenced_block b(fenced_block::half);
  109. ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_));
  110. w.complete(handler, handler.handler_);
  111. ASIO_HANDLER_INVOCATION_END;
  112. }
  113. }
  114. private:
  115. Handler handler_;
  116. handler_work<Handler, IoExecutor> work_;
  117. };
  118. } // namespace detail
  119. } // namespace asio
  120. #include "asio/detail/pop_options.hpp"
  121. #endif // !defined(ASIO_WINDOWS) && !defined(__CYGWIN__)
  122. #endif // ASIO_DETAIL_DESCRIPTOR_READ_OP_HPP