descriptor_write_op.hpp 5.9 KB

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