]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // detail/win_iocp_overlapped_ptr.hpp | |
3 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
4 | // | |
f67539c2 | 5 | // Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
7c673cae FG |
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 | ||
b32b8144 | 22 | #include <boost/asio/io_context.hpp> |
7c673cae | 23 | #include <boost/asio/detail/handler_alloc_helpers.hpp> |
92f5a8d4 | 24 | #include <boost/asio/detail/io_object_executor.hpp> |
b32b8144 | 25 | #include <boost/asio/detail/memory.hpp> |
7c673cae FG |
26 | #include <boost/asio/detail/noncopyable.hpp> |
27 | #include <boost/asio/detail/win_iocp_overlapped_op.hpp> | |
b32b8144 | 28 | #include <boost/asio/detail/win_iocp_io_context.hpp> |
7c673cae FG |
29 | |
30 | #include <boost/asio/detail/push_options.hpp> | |
31 | ||
32 | namespace boost { | |
33 | namespace asio { | |
34 | namespace detail { | |
35 | ||
36 | // Wraps a handler to create an OVERLAPPED object for use with overlapped I/O. | |
37 | class win_iocp_overlapped_ptr | |
38 | : private noncopyable | |
39 | { | |
40 | public: | |
41 | // Construct an empty win_iocp_overlapped_ptr. | |
42 | win_iocp_overlapped_ptr() | |
43 | : ptr_(0), | |
44 | iocp_service_(0) | |
45 | { | |
46 | } | |
47 | ||
48 | // Construct an win_iocp_overlapped_ptr to contain the specified handler. | |
92f5a8d4 TL |
49 | template <typename Executor, typename Handler> |
50 | explicit win_iocp_overlapped_ptr(const Executor& ex, | |
51 | BOOST_ASIO_MOVE_ARG(Handler) handler) | |
7c673cae FG |
52 | : ptr_(0), |
53 | iocp_service_(0) | |
54 | { | |
92f5a8d4 | 55 | this->reset(ex, BOOST_ASIO_MOVE_CAST(Handler)(handler)); |
7c673cae FG |
56 | } |
57 | ||
58 | // Destructor automatically frees the OVERLAPPED object unless released. | |
59 | ~win_iocp_overlapped_ptr() | |
60 | { | |
61 | reset(); | |
62 | } | |
63 | ||
64 | // Reset to empty. | |
65 | void reset() | |
66 | { | |
67 | if (ptr_) | |
68 | { | |
69 | ptr_->destroy(); | |
70 | ptr_ = 0; | |
71 | iocp_service_->work_finished(); | |
72 | iocp_service_ = 0; | |
73 | } | |
74 | } | |
75 | ||
76 | // Reset to contain the specified handler, freeing any current OVERLAPPED | |
77 | // object. | |
92f5a8d4 TL |
78 | template <typename Executor, typename Handler> |
79 | void reset(const Executor& ex, Handler handler) | |
7c673cae | 80 | { |
92f5a8d4 TL |
81 | const bool native = is_same<Executor, io_context::executor_type>::value; |
82 | win_iocp_io_context* iocp_service = this->get_iocp_service(ex); | |
83 | ||
84 | typedef win_iocp_overlapped_op<Handler, io_object_executor<Executor> > op; | |
7c673cae | 85 | typename op::ptr p = { boost::asio::detail::addressof(handler), |
b32b8144 | 86 | op::ptr::allocate(handler), 0 }; |
92f5a8d4 | 87 | p.p = new (p.v) op(handler, io_object_executor<Executor>(ex, native)); |
7c673cae | 88 | |
92f5a8d4 TL |
89 | BOOST_ASIO_HANDLER_CREATION((ex.context(), *p.p, |
90 | "iocp_service", iocp_service, 0, "overlapped")); | |
7c673cae | 91 | |
92f5a8d4 | 92 | iocp_service->work_started(); |
7c673cae FG |
93 | reset(); |
94 | ptr_ = p.p; | |
95 | p.v = p.p = 0; | |
92f5a8d4 | 96 | iocp_service_ = iocp_service; |
7c673cae FG |
97 | } |
98 | ||
99 | // Get the contained OVERLAPPED object. | |
100 | OVERLAPPED* get() | |
101 | { | |
102 | return ptr_; | |
103 | } | |
104 | ||
105 | // Get the contained OVERLAPPED object. | |
106 | const OVERLAPPED* get() const | |
107 | { | |
108 | return ptr_; | |
109 | } | |
110 | ||
111 | // Release ownership of the OVERLAPPED object. | |
112 | OVERLAPPED* release() | |
113 | { | |
114 | if (ptr_) | |
115 | iocp_service_->on_pending(ptr_); | |
116 | ||
117 | OVERLAPPED* tmp = ptr_; | |
118 | ptr_ = 0; | |
119 | iocp_service_ = 0; | |
120 | return tmp; | |
121 | } | |
122 | ||
123 | // Post completion notification for overlapped operation. Releases ownership. | |
124 | void complete(const boost::system::error_code& ec, | |
125 | std::size_t bytes_transferred) | |
126 | { | |
127 | if (ptr_) | |
128 | { | |
129 | iocp_service_->on_completion(ptr_, ec, | |
130 | static_cast<DWORD>(bytes_transferred)); | |
131 | ptr_ = 0; | |
132 | iocp_service_ = 0; | |
133 | } | |
134 | } | |
135 | ||
136 | private: | |
92f5a8d4 TL |
137 | template <typename Executor> |
138 | static win_iocp_io_context* get_iocp_service(const Executor& ex) | |
139 | { | |
140 | return &use_service<win_iocp_io_context>(ex.context()); | |
141 | } | |
142 | ||
143 | static win_iocp_io_context* get_iocp_service( | |
144 | const io_context::executor_type& ex) | |
145 | { | |
146 | return &ex.context().impl_; | |
147 | } | |
148 | ||
7c673cae | 149 | win_iocp_operation* ptr_; |
b32b8144 | 150 | win_iocp_io_context* iocp_service_; |
7c673cae FG |
151 | }; |
152 | ||
153 | } // namespace detail | |
154 | } // namespace asio | |
155 | } // namespace boost | |
156 | ||
157 | #include <boost/asio/detail/pop_options.hpp> | |
158 | ||
159 | #endif // defined(BOOST_ASIO_HAS_IOCP) | |
160 | ||
161 | #endif // BOOST_ASIO_DETAIL_WIN_IOCP_OVERLAPPED_PTR_HPP |