win_iocp_overlapped_ptr.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // detail/win_iocp_overlapped_ptr.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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/detail/handler_alloc_helpers.hpp>
  19. #include <boost/asio/detail/io_object_executor.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. BOOST_ASIO_MOVE_ARG(Handler) handler)
  43. : ptr_(0),
  44. iocp_service_(0)
  45. {
  46. this->reset(ex, BOOST_ASIO_MOVE_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. const bool native = is_same<Executor, io_context::executor_type>::value;
  70. win_iocp_io_context* iocp_service = this->get_iocp_service(ex);
  71. typedef win_iocp_overlapped_op<Handler, io_object_executor<Executor> > op;
  72. typename op::ptr p = { boost::asio::detail::addressof(handler),
  73. op::ptr::allocate(handler), 0 };
  74. p.p = new (p.v) op(handler, io_object_executor<Executor>(ex, native));
  75. BOOST_ASIO_HANDLER_CREATION((ex.context(), *p.p,
  76. "iocp_service", iocp_service, 0, "overlapped"));
  77. iocp_service->work_started();
  78. reset();
  79. ptr_ = p.p;
  80. p.v = p.p = 0;
  81. iocp_service_ = iocp_service;
  82. }
  83. // Get the contained OVERLAPPED object.
  84. OVERLAPPED* get()
  85. {
  86. return ptr_;
  87. }
  88. // Get the contained OVERLAPPED object.
  89. const OVERLAPPED* get() const
  90. {
  91. return ptr_;
  92. }
  93. // Release ownership of the OVERLAPPED object.
  94. OVERLAPPED* release()
  95. {
  96. if (ptr_)
  97. iocp_service_->on_pending(ptr_);
  98. OVERLAPPED* tmp = ptr_;
  99. ptr_ = 0;
  100. iocp_service_ = 0;
  101. return tmp;
  102. }
  103. // Post completion notification for overlapped operation. Releases ownership.
  104. void complete(const boost::system::error_code& ec,
  105. std::size_t bytes_transferred)
  106. {
  107. if (ptr_)
  108. {
  109. iocp_service_->on_completion(ptr_, ec,
  110. static_cast<DWORD>(bytes_transferred));
  111. ptr_ = 0;
  112. iocp_service_ = 0;
  113. }
  114. }
  115. private:
  116. template <typename Executor>
  117. static win_iocp_io_context* get_iocp_service(const Executor& ex)
  118. {
  119. return &use_service<win_iocp_io_context>(ex.context());
  120. }
  121. static win_iocp_io_context* get_iocp_service(
  122. const io_context::executor_type& ex)
  123. {
  124. return &ex.context().impl_;
  125. }
  126. win_iocp_operation* ptr_;
  127. win_iocp_io_context* iocp_service_;
  128. };
  129. } // namespace detail
  130. } // namespace asio
  131. } // namespace boost
  132. #include <boost/asio/detail/pop_options.hpp>
  133. #endif // defined(BOOST_ASIO_HAS_IOCP)
  134. #endif // BOOST_ASIO_DETAIL_WIN_IOCP_OVERLAPPED_PTR_HPP