win_iocp_overlapped_ptr.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //
  2. // detail/win_iocp_overlapped_ptr.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2013 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_service.hpp>
  18. #include <boost/asio/detail/addressof.hpp>
  19. #include <boost/asio/detail/handler_alloc_helpers.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_service.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_service& io_service, BOOST_ASIO_MOVE_ARG(Handler) handler)
  42. : ptr_(0),
  43. iocp_service_(0)
  44. {
  45. this->reset(io_service, 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_service& io_service, Handler handler)
  67. {
  68. typedef win_iocp_overlapped_op<Handler> op;
  69. typename op::ptr p = { boost::asio::detail::addressof(handler),
  70. boost_asio_handler_alloc_helpers::allocate(
  71. sizeof(op), handler), 0 };
  72. p.p = new (p.v) op(handler);
  73. BOOST_ASIO_HANDLER_CREATION((p.p, "io_service",
  74. &io_service.impl_, "overlapped"));
  75. io_service.impl_.work_started();
  76. reset();
  77. ptr_ = p.p;
  78. p.v = p.p = 0;
  79. iocp_service_ = &io_service.impl_;
  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 boost::system::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. win_iocp_operation* ptr_;
  115. win_iocp_io_service* iocp_service_;
  116. };
  117. } // namespace detail
  118. } // namespace asio
  119. } // namespace boost
  120. #include <boost/asio/detail/pop_options.hpp>
  121. #endif // defined(BOOST_ASIO_HAS_IOCP)
  122. #endif // BOOST_ASIO_DETAIL_WIN_IOCP_OVERLAPPED_PTR_HPP