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 
11 #ifndef BOOST_ASIO_DETAIL_WIN_IOCP_OVERLAPPED_PTR_HPP
12 #define BOOST_ASIO_DETAIL_WIN_IOCP_OVERLAPPED_PTR_HPP
13 
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17 
18 #include <boost/asio/detail/config.hpp>
19 
20 #if defined(BOOST_ASIO_HAS_IOCP)
21 
22 #include <boost/asio/io_service.hpp>
23 #include <boost/asio/detail/addressof.hpp>
24 #include <boost/asio/detail/handler_alloc_helpers.hpp>
25 #include <boost/asio/detail/noncopyable.hpp>
26 #include <boost/asio/detail/win_iocp_overlapped_op.hpp>
27 #include <boost/asio/detail/win_iocp_io_service.hpp>
28 
29 #include <boost/asio/detail/push_options.hpp>
30 
31 namespace boost {
32 namespace asio {
33 namespace detail {
34 
35 // Wraps a handler to create an OVERLAPPED object for use with overlapped I/O.
36 class win_iocp_overlapped_ptr
37   : private noncopyable
38 {
39 public:
40   // Construct an empty win_iocp_overlapped_ptr.
win_iocp_overlapped_ptr()41   win_iocp_overlapped_ptr()
42     : ptr_(0),
43       iocp_service_(0)
44   {
45   }
46 
47   // Construct an win_iocp_overlapped_ptr to contain the specified handler.
48   template <typename Handler>
win_iocp_overlapped_ptr(boost::asio::io_service & io_service,BOOST_ASIO_MOVE_ARG (Handler)handler)49   explicit win_iocp_overlapped_ptr(
50       boost::asio::io_service& io_service, BOOST_ASIO_MOVE_ARG(Handler) handler)
51     : ptr_(0),
52       iocp_service_(0)
53   {
54     this->reset(io_service, BOOST_ASIO_MOVE_CAST(Handler)(handler));
55   }
56 
57   // Destructor automatically frees the OVERLAPPED object unless released.
~win_iocp_overlapped_ptr()58   ~win_iocp_overlapped_ptr()
59   {
60     reset();
61   }
62 
63   // Reset to empty.
reset()64   void reset()
65   {
66     if (ptr_)
67     {
68       ptr_->destroy();
69       ptr_ = 0;
70       iocp_service_->work_finished();
71       iocp_service_ = 0;
72     }
73   }
74 
75   // Reset to contain the specified handler, freeing any current OVERLAPPED
76   // object.
77   template <typename Handler>
reset(boost::asio::io_service & io_service,Handler handler)78   void reset(boost::asio::io_service& io_service, Handler handler)
79   {
80     typedef win_iocp_overlapped_op<Handler> op;
81     typename op::ptr p = { boost::asio::detail::addressof(handler),
82       boost_asio_handler_alloc_helpers::allocate(
83         sizeof(op), handler), 0 };
84     p.p = new (p.v) op(handler);
85 
86     BOOST_ASIO_HANDLER_CREATION((p.p, "io_service",
87           &io_service.impl_, "overlapped"));
88 
89     io_service.impl_.work_started();
90     reset();
91     ptr_ = p.p;
92     p.v = p.p = 0;
93     iocp_service_ = &io_service.impl_;
94   }
95 
96   // Get the contained OVERLAPPED object.
get()97   OVERLAPPED* get()
98   {
99     return ptr_;
100   }
101 
102   // Get the contained OVERLAPPED object.
get() const103   const OVERLAPPED* get() const
104   {
105     return ptr_;
106   }
107 
108   // Release ownership of the OVERLAPPED object.
release()109   OVERLAPPED* release()
110   {
111     if (ptr_)
112       iocp_service_->on_pending(ptr_);
113 
114     OVERLAPPED* tmp = ptr_;
115     ptr_ = 0;
116     iocp_service_ = 0;
117     return tmp;
118   }
119 
120   // Post completion notification for overlapped operation. Releases ownership.
complete(const boost::system::error_code & ec,std::size_t bytes_transferred)121   void complete(const boost::system::error_code& ec,
122       std::size_t bytes_transferred)
123   {
124     if (ptr_)
125     {
126       iocp_service_->on_completion(ptr_, ec,
127           static_cast<DWORD>(bytes_transferred));
128       ptr_ = 0;
129       iocp_service_ = 0;
130     }
131   }
132 
133 private:
134   win_iocp_operation* ptr_;
135   win_iocp_io_service* iocp_service_;
136 };
137 
138 } // namespace detail
139 } // namespace asio
140 } // namespace boost
141 
142 #include <boost/asio/detail/pop_options.hpp>
143 
144 #endif // defined(BOOST_ASIO_HAS_IOCP)
145 
146 #endif // BOOST_ASIO_DETAIL_WIN_IOCP_OVERLAPPED_PTR_HPP
147