win_iocp_overlapped_ptr.hpp 4.1 KB

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