win_iocp_overlapped_op.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //
  2. // detail/win_iocp_overlapped_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_WIN_IOCP_OVERLAPPED_OP_HPP
  11. #define BOOST_ASIO_DETAIL_WIN_IOCP_OVERLAPPED_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_HAS_IOCP)
  17. #include <boost/asio/detail/bind_handler.hpp>
  18. #include <boost/asio/detail/fenced_block.hpp>
  19. #include <boost/asio/detail/handler_alloc_helpers.hpp>
  20. #include <boost/asio/detail/handler_work.hpp>
  21. #include <boost/asio/detail/memory.hpp>
  22. #include <boost/asio/detail/operation.hpp>
  23. #include <boost/asio/error.hpp>
  24. #include <boost/asio/detail/push_options.hpp>
  25. namespace boost {
  26. namespace asio {
  27. namespace detail {
  28. template <typename Handler, typename IoExecutor>
  29. class win_iocp_overlapped_op : public operation
  30. {
  31. public:
  32. BOOST_ASIO_DEFINE_HANDLER_PTR(win_iocp_overlapped_op);
  33. win_iocp_overlapped_op(Handler& handler, const IoExecutor& io_ex)
  34. : operation(&win_iocp_overlapped_op::do_complete),
  35. handler_(static_cast<Handler&&>(handler)),
  36. work_(handler_, io_ex)
  37. {
  38. }
  39. static void do_complete(void* owner, operation* base,
  40. const boost::system::error_code& result_ec, std::size_t bytes_transferred)
  41. {
  42. boost::system::error_code ec(result_ec);
  43. // Take ownership of the operation object.
  44. BOOST_ASIO_ASSUME(base != 0);
  45. win_iocp_overlapped_op* o(static_cast<win_iocp_overlapped_op*>(base));
  46. ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
  47. BOOST_ASIO_HANDLER_COMPLETION((*o));
  48. // Take ownership of the operation's outstanding work.
  49. handler_work<Handler, IoExecutor> w(
  50. static_cast<handler_work<Handler, IoExecutor>&&>(
  51. o->work_));
  52. BOOST_ASIO_ERROR_LOCATION(ec);
  53. // Make a copy of the handler so that the memory can be deallocated before
  54. // the upcall is made. Even if we're not about to make an upcall, a
  55. // sub-object of the handler may be the true owner of the memory associated
  56. // with the handler. Consequently, a local copy of the handler is required
  57. // to ensure that any owning sub-object remains valid until after we have
  58. // deallocated the memory here.
  59. detail::binder2<Handler, boost::system::error_code, std::size_t>
  60. handler(o->handler_, ec, bytes_transferred);
  61. p.h = boost::asio::detail::addressof(handler.handler_);
  62. p.reset();
  63. // Make the upcall if required.
  64. if (owner)
  65. {
  66. fenced_block b(fenced_block::half);
  67. BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_));
  68. w.complete(handler, handler.handler_);
  69. BOOST_ASIO_HANDLER_INVOCATION_END;
  70. }
  71. }
  72. private:
  73. Handler handler_;
  74. handler_work<Handler, IoExecutor> work_;
  75. };
  76. } // namespace detail
  77. } // namespace asio
  78. } // namespace boost
  79. #include <boost/asio/detail/pop_options.hpp>
  80. #endif // defined(BOOST_ASIO_HAS_IOCP)
  81. #endif // BOOST_ASIO_DETAIL_WIN_IOCP_OVERLAPPED_OP_HPP