Chris@16: // Chris@16: // detail/win_iocp_overlapped_ptr.hpp Chris@16: // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Chris@16: // Chris@101: // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com) Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: Chris@16: #ifndef BOOST_ASIO_DETAIL_WIN_IOCP_OVERLAPPED_PTR_HPP Chris@16: #define BOOST_ASIO_DETAIL_WIN_IOCP_OVERLAPPED_PTR_HPP Chris@16: Chris@16: #if defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: # pragma once Chris@16: #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: Chris@16: #include Chris@16: Chris@16: #if defined(BOOST_ASIO_HAS_IOCP) Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace asio { Chris@16: namespace detail { Chris@16: Chris@16: // Wraps a handler to create an OVERLAPPED object for use with overlapped I/O. Chris@16: class win_iocp_overlapped_ptr Chris@16: : private noncopyable Chris@16: { Chris@16: public: Chris@16: // Construct an empty win_iocp_overlapped_ptr. Chris@16: win_iocp_overlapped_ptr() Chris@16: : ptr_(0), Chris@16: iocp_service_(0) Chris@16: { Chris@16: } Chris@16: Chris@16: // Construct an win_iocp_overlapped_ptr to contain the specified handler. Chris@16: template Chris@16: explicit win_iocp_overlapped_ptr( Chris@16: boost::asio::io_service& io_service, BOOST_ASIO_MOVE_ARG(Handler) handler) Chris@16: : ptr_(0), Chris@16: iocp_service_(0) Chris@16: { Chris@16: this->reset(io_service, BOOST_ASIO_MOVE_CAST(Handler)(handler)); Chris@16: } Chris@16: Chris@16: // Destructor automatically frees the OVERLAPPED object unless released. Chris@16: ~win_iocp_overlapped_ptr() Chris@16: { Chris@16: reset(); Chris@16: } Chris@16: Chris@16: // Reset to empty. Chris@16: void reset() Chris@16: { Chris@16: if (ptr_) Chris@16: { Chris@16: ptr_->destroy(); Chris@16: ptr_ = 0; Chris@16: iocp_service_->work_finished(); Chris@16: iocp_service_ = 0; Chris@16: } Chris@16: } Chris@16: Chris@16: // Reset to contain the specified handler, freeing any current OVERLAPPED Chris@16: // object. Chris@16: template Chris@16: void reset(boost::asio::io_service& io_service, Handler handler) Chris@16: { Chris@16: typedef win_iocp_overlapped_op op; Chris@16: typename op::ptr p = { boost::asio::detail::addressof(handler), Chris@16: boost_asio_handler_alloc_helpers::allocate( Chris@16: sizeof(op), handler), 0 }; Chris@16: p.p = new (p.v) op(handler); Chris@16: Chris@16: BOOST_ASIO_HANDLER_CREATION((p.p, "io_service", Chris@16: &io_service.impl_, "overlapped")); Chris@16: Chris@16: io_service.impl_.work_started(); Chris@16: reset(); Chris@16: ptr_ = p.p; Chris@16: p.v = p.p = 0; Chris@16: iocp_service_ = &io_service.impl_; Chris@16: } Chris@16: Chris@16: // Get the contained OVERLAPPED object. Chris@16: OVERLAPPED* get() Chris@16: { Chris@16: return ptr_; Chris@16: } Chris@16: Chris@16: // Get the contained OVERLAPPED object. Chris@16: const OVERLAPPED* get() const Chris@16: { Chris@16: return ptr_; Chris@16: } Chris@16: Chris@16: // Release ownership of the OVERLAPPED object. Chris@16: OVERLAPPED* release() Chris@16: { Chris@16: if (ptr_) Chris@16: iocp_service_->on_pending(ptr_); Chris@16: Chris@16: OVERLAPPED* tmp = ptr_; Chris@16: ptr_ = 0; Chris@16: iocp_service_ = 0; Chris@16: return tmp; Chris@16: } Chris@16: Chris@16: // Post completion notification for overlapped operation. Releases ownership. Chris@16: void complete(const boost::system::error_code& ec, Chris@16: std::size_t bytes_transferred) Chris@16: { Chris@16: if (ptr_) Chris@16: { Chris@16: iocp_service_->on_completion(ptr_, ec, Chris@16: static_cast(bytes_transferred)); Chris@16: ptr_ = 0; Chris@16: iocp_service_ = 0; Chris@16: } Chris@16: } Chris@16: Chris@16: private: Chris@16: win_iocp_operation* ptr_; Chris@16: win_iocp_io_service* iocp_service_; Chris@16: }; Chris@16: Chris@16: } // namespace detail Chris@16: } // namespace asio Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // defined(BOOST_ASIO_HAS_IOCP) Chris@16: Chris@16: #endif // BOOST_ASIO_DETAIL_WIN_IOCP_OVERLAPPED_PTR_HPP