win_iocp_overlapped_ptr.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // detail/win_iocp_overlapped_ptr.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 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_service.hpp"
  18. #include "asio/detail/addressof.hpp"
  19. #include "asio/detail/handler_alloc_helpers.hpp"
  20. #include "asio/detail/noncopyable.hpp"
  21. #include "asio/detail/win_iocp_overlapped_op.hpp"
  22. #include "asio/detail/win_iocp_io_service.hpp"
  23. #include "asio/detail/push_options.hpp"
  24. namespace asio {
  25. namespace detail {
  26. // Wraps a handler to create an OVERLAPPED object for use with overlapped I/O.
  27. class win_iocp_overlapped_ptr
  28. : private noncopyable
  29. {
  30. public:
  31. // Construct an empty win_iocp_overlapped_ptr.
  32. win_iocp_overlapped_ptr()
  33. : ptr_(0),
  34. iocp_service_(0)
  35. {
  36. }
  37. // Construct an win_iocp_overlapped_ptr to contain the specified handler.
  38. template <typename Handler>
  39. explicit win_iocp_overlapped_ptr(
  40. asio::io_service& io_service, ASIO_MOVE_ARG(Handler) handler)
  41. : ptr_(0),
  42. iocp_service_(0)
  43. {
  44. this->reset(io_service, ASIO_MOVE_CAST(Handler)(handler));
  45. }
  46. // Destructor automatically frees the OVERLAPPED object unless released.
  47. ~win_iocp_overlapped_ptr()
  48. {
  49. reset();
  50. }
  51. // Reset to empty.
  52. void reset()
  53. {
  54. if (ptr_)
  55. {
  56. ptr_->destroy();
  57. ptr_ = 0;
  58. iocp_service_->work_finished();
  59. iocp_service_ = 0;
  60. }
  61. }
  62. // Reset to contain the specified handler, freeing any current OVERLAPPED
  63. // object.
  64. template <typename Handler>
  65. void reset(asio::io_service& io_service, Handler handler)
  66. {
  67. typedef win_iocp_overlapped_op<Handler> op;
  68. typename op::ptr p = { asio::detail::addressof(handler),
  69. asio_handler_alloc_helpers::allocate(
  70. sizeof(op), handler), 0 };
  71. p.p = new (p.v) op(handler);
  72. ASIO_HANDLER_CREATION((p.p, "io_service",
  73. &io_service.impl_, "overlapped"));
  74. io_service.impl_.work_started();
  75. reset();
  76. ptr_ = p.p;
  77. p.v = p.p = 0;
  78. iocp_service_ = &io_service.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 asio::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_service* iocp_service_;
  115. };
  116. } // namespace detail
  117. } // namespace asio
  118. #include "asio/detail/pop_options.hpp"
  119. #endif // defined(ASIO_HAS_IOCP)
  120. #endif // ASIO_DETAIL_WIN_IOCP_OVERLAPPED_PTR_HPP