win_iocp_overlapped_ptr.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //
  2. // detail/win_iocp_overlapped_ptr.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_PTR_HPP
  11. #define BOOST_ASIO_DETAIL_WIN_IOCP_OVERLAPPED_PTR_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/io_context.hpp>
  18. #include <boost/asio/query.hpp>
  19. #include <boost/asio/detail/handler_alloc_helpers.hpp>
  20. #include <boost/asio/detail/memory.hpp>
  21. #include <boost/asio/detail/noncopyable.hpp>
  22. #include <boost/asio/detail/win_iocp_overlapped_op.hpp>
  23. #include <boost/asio/detail/win_iocp_io_context.hpp>
  24. #include <boost/asio/detail/push_options.hpp>
  25. namespace boost {
  26. namespace asio {
  27. namespace detail {
  28. // Wraps a handler to create an OVERLAPPED object for use with overlapped I/O.
  29. class win_iocp_overlapped_ptr
  30. : private noncopyable
  31. {
  32. public:
  33. // Construct an empty win_iocp_overlapped_ptr.
  34. win_iocp_overlapped_ptr()
  35. : ptr_(0),
  36. iocp_service_(0)
  37. {
  38. }
  39. // Construct an win_iocp_overlapped_ptr to contain the specified handler.
  40. template <typename Executor, typename Handler>
  41. explicit win_iocp_overlapped_ptr(const Executor& ex,
  42. Handler&& handler)
  43. : ptr_(0),
  44. iocp_service_(0)
  45. {
  46. this->reset(ex, static_cast<Handler&&>(handler));
  47. }
  48. // Destructor automatically frees the OVERLAPPED object unless released.
  49. ~win_iocp_overlapped_ptr()
  50. {
  51. reset();
  52. }
  53. // Reset to empty.
  54. void reset()
  55. {
  56. if (ptr_)
  57. {
  58. ptr_->destroy();
  59. ptr_ = 0;
  60. iocp_service_->work_finished();
  61. iocp_service_ = 0;
  62. }
  63. }
  64. // Reset to contain the specified handler, freeing any current OVERLAPPED
  65. // object.
  66. template <typename Executor, typename Handler>
  67. void reset(const Executor& ex, Handler handler)
  68. {
  69. win_iocp_io_context* iocp_service = this->get_iocp_service(ex);
  70. typedef win_iocp_overlapped_op<Handler, Executor> op;
  71. typename op::ptr p = { boost::asio::detail::addressof(handler),
  72. op::ptr::allocate(handler), 0 };
  73. p.p = new (p.v) op(handler, ex);
  74. BOOST_ASIO_HANDLER_CREATION((ex.context(), *p.p,
  75. "iocp_service", iocp_service, 0, "overlapped"));
  76. iocp_service->work_started();
  77. reset();
  78. ptr_ = p.p;
  79. p.v = p.p = 0;
  80. iocp_service_ = iocp_service;
  81. }
  82. // Get the contained OVERLAPPED object.
  83. OVERLAPPED* get()
  84. {
  85. return ptr_;
  86. }
  87. // Get the contained OVERLAPPED object.
  88. const OVERLAPPED* get() const
  89. {
  90. return ptr_;
  91. }
  92. // Release ownership of the OVERLAPPED object.
  93. OVERLAPPED* release()
  94. {
  95. if (ptr_)
  96. iocp_service_->on_pending(ptr_);
  97. OVERLAPPED* tmp = ptr_;
  98. ptr_ = 0;
  99. iocp_service_ = 0;
  100. return tmp;
  101. }
  102. // Post completion notification for overlapped operation. Releases ownership.
  103. void complete(const boost::system::error_code& ec,
  104. std::size_t bytes_transferred)
  105. {
  106. if (ptr_)
  107. {
  108. iocp_service_->on_completion(ptr_, ec,
  109. static_cast<DWORD>(bytes_transferred));
  110. ptr_ = 0;
  111. iocp_service_ = 0;
  112. }
  113. }
  114. private:
  115. template <typename Executor>
  116. static win_iocp_io_context* get_iocp_service(const Executor& ex,
  117. enable_if_t<
  118. can_query<const Executor&, execution::context_t>::value
  119. >* = 0)
  120. {
  121. return &use_service<win_iocp_io_context>(
  122. boost::asio::query(ex, execution::context));
  123. }
  124. template <typename Executor>
  125. static win_iocp_io_context* get_iocp_service(const Executor& ex,
  126. enable_if_t<
  127. !can_query<const Executor&, execution::context_t>::value
  128. >* = 0)
  129. {
  130. return &use_service<win_iocp_io_context>(ex.context());
  131. }
  132. static win_iocp_io_context* get_iocp_service(
  133. const io_context::executor_type& ex)
  134. {
  135. return &boost::asio::query(ex, execution::context).impl_;
  136. }
  137. win_iocp_operation* ptr_;
  138. win_iocp_io_context* iocp_service_;
  139. };
  140. } // namespace detail
  141. } // namespace asio
  142. } // namespace boost
  143. #include <boost/asio/detail/pop_options.hpp>
  144. #endif // defined(BOOST_ASIO_HAS_IOCP)
  145. #endif // BOOST_ASIO_DETAIL_WIN_IOCP_OVERLAPPED_PTR_HPP