win_iocp_overlapped_ptr.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // detail/win_iocp_overlapped_ptr.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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/memory.hpp>
  20. #include <boost/asio/detail/noncopyable.hpp>
  21. #include <boost/asio/detail/win_iocp_overlapped_op.hpp>
  22. #include <boost/asio/detail/win_iocp_io_context.hpp>
  23. #include <boost/asio/detail/push_options.hpp>
  24. namespace boost {
  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 Handler>
  40. explicit win_iocp_overlapped_ptr(
  41. boost::asio::io_context& io_context, BOOST_ASIO_MOVE_ARG(Handler) handler)
  42. : ptr_(0),
  43. iocp_service_(0)
  44. {
  45. this->reset(io_context, BOOST_ASIO_MOVE_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 Handler>
  66. void reset(boost::asio::io_context& io_context, Handler handler)
  67. {
  68. typedef win_iocp_overlapped_op<Handler> op;
  69. typename op::ptr p = { boost::asio::detail::addressof(handler),
  70. op::ptr::allocate(handler), 0 };
  71. p.p = new (p.v) op(handler);
  72. BOOST_ASIO_HANDLER_CREATION((io_context, *p.p,
  73. "io_context", &io_context.impl_, 0, "overlapped"));
  74. io_context.impl_.work_started();
  75. reset();
  76. ptr_ = p.p;
  77. p.v = p.p = 0;
  78. iocp_service_ = &io_context.impl_;
  79. }
  80. // Get the contained OVERLAPPED object.
  81. OVERLAPPED* get()
  82. {
  83. return ptr_;
  84. }
  85. // Get the contained OVERLAPPED object.
  86. const OVERLAPPED* get() const
  87. {
  88. return ptr_;
  89. }
  90. // Release ownership of the OVERLAPPED object.
  91. OVERLAPPED* release()
  92. {
  93. if (ptr_)
  94. iocp_service_->on_pending(ptr_);
  95. OVERLAPPED* tmp = ptr_;
  96. ptr_ = 0;
  97. iocp_service_ = 0;
  98. return tmp;
  99. }
  100. // Post completion notification for overlapped operation. Releases ownership.
  101. void complete(const boost::system::error_code& ec,
  102. std::size_t bytes_transferred)
  103. {
  104. if (ptr_)
  105. {
  106. iocp_service_->on_completion(ptr_, ec,
  107. static_cast<DWORD>(bytes_transferred));
  108. ptr_ = 0;
  109. iocp_service_ = 0;
  110. }
  111. }
  112. private:
  113. win_iocp_operation* ptr_;
  114. win_iocp_io_context* iocp_service_;
  115. };
  116. } // namespace detail
  117. } // namespace asio
  118. } // namespace boost
  119. #include <boost/asio/detail/pop_options.hpp>
  120. #endif // defined(BOOST_ASIO_HAS_IOCP)
  121. #endif // BOOST_ASIO_DETAIL_WIN_IOCP_OVERLAPPED_PTR_HPP