win_iocp_handle_write_op.hpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // detail/win_iocp_handle_write_op.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2011 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. // Copyright (c) 2008 Rep Invariant Systems, Inc. (info@repinvariant.com)
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. #ifndef ASIO_DETAIL_WIN_IOCP_HANDLE_WRITE_OP_HPP
  12. #define ASIO_DETAIL_WIN_IOCP_HANDLE_WRITE_OP_HPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include "asio/detail/config.hpp"
  17. #if defined(ASIO_HAS_IOCP)
  18. #include "asio/error.hpp"
  19. #include <boost/utility/addressof.hpp>
  20. #include "asio/detail/bind_handler.hpp"
  21. #include "asio/detail/buffer_sequence_adapter.hpp"
  22. #include "asio/detail/fenced_block.hpp"
  23. #include "asio/detail/handler_alloc_helpers.hpp"
  24. #include "asio/detail/handler_invoke_helpers.hpp"
  25. #include "asio/detail/operation.hpp"
  26. #include "asio/detail/push_options.hpp"
  27. namespace asio {
  28. namespace detail {
  29. template <typename ConstBufferSequence, typename Handler>
  30. class win_iocp_handle_write_op : public operation
  31. {
  32. public:
  33. ASIO_DEFINE_HANDLER_PTR(win_iocp_handle_write_op);
  34. win_iocp_handle_write_op(const ConstBufferSequence& buffers, Handler handler)
  35. : operation(&win_iocp_handle_write_op::do_complete),
  36. buffers_(buffers),
  37. handler_(handler)
  38. {
  39. }
  40. static void do_complete(io_service_impl* owner, operation* base,
  41. asio::error_code ec, std::size_t bytes_transferred)
  42. {
  43. // Take ownership of the operation object.
  44. win_iocp_handle_write_op* o(static_cast<win_iocp_handle_write_op*>(base));
  45. ptr p = { boost::addressof(o->handler_), o, o };
  46. #if defined(ASIO_ENABLE_BUFFER_DEBUGGING)
  47. if (owner)
  48. {
  49. // Check whether buffers are still valid.
  50. buffer_sequence_adapter<asio::const_buffer,
  51. ConstBufferSequence>::validate(o->buffers_);
  52. }
  53. #endif // defined(ASIO_ENABLE_BUFFER_DEBUGGING)
  54. // Make a copy of the handler so that the memory can be deallocated before
  55. // the upcall is made. Even if we're not about to make an upcall, a
  56. // sub-object of the handler may be the true owner of the memory associated
  57. // with the handler. Consequently, a local copy of the handler is required
  58. // to ensure that any owning sub-object remains valid until after we have
  59. // deallocated the memory here.
  60. detail::binder2<Handler, asio::error_code, std::size_t>
  61. handler(o->handler_, ec, bytes_transferred);
  62. p.h = boost::addressof(handler.handler_);
  63. p.reset();
  64. // Make the upcall if required.
  65. if (owner)
  66. {
  67. asio::detail::fenced_block b;
  68. asio_handler_invoke_helpers::invoke(handler, handler.handler_);
  69. }
  70. }
  71. private:
  72. ConstBufferSequence buffers_;
  73. Handler handler_;
  74. };
  75. } // namespace detail
  76. } // namespace asio
  77. #include "asio/detail/pop_options.hpp"
  78. #endif // defined(ASIO_HAS_IOCP)
  79. #endif // ASIO_DETAIL_WIN_IOCP_HANDLE_WRITE_OP_HPP