2 // detail/win_iocp_handle_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 // Copyright (c) 2008 Rep Invariant Systems, Inc. (info@repinvariant.com)
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)
12 #ifndef BOOST_ASIO_DETAIL_WIN_IOCP_HANDLE_SERVICE_HPP
13 #define BOOST_ASIO_DETAIL_WIN_IOCP_HANDLE_SERVICE_HPP
15 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
17 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
19 #include <boost/asio/detail/config.hpp>
21 #if defined(BOOST_ASIO_HAS_IOCP)
23 #include <boost/asio/error.hpp>
24 #include <boost/asio/io_context.hpp>
25 #include <boost/asio/detail/buffer_sequence_adapter.hpp>
26 #include <boost/asio/detail/cstdint.hpp>
27 #include <boost/asio/detail/handler_alloc_helpers.hpp>
28 #include <boost/asio/detail/memory.hpp>
29 #include <boost/asio/detail/mutex.hpp>
30 #include <boost/asio/detail/operation.hpp>
31 #include <boost/asio/detail/win_iocp_handle_read_op.hpp>
32 #include <boost/asio/detail/win_iocp_handle_write_op.hpp>
33 #include <boost/asio/detail/win_iocp_io_context.hpp>
35 #include <boost/asio/detail/push_options.hpp>
41 class win_iocp_handle_service :
42 public service_base<win_iocp_handle_service>
45 // The native type of a stream handle.
46 typedef HANDLE native_handle_type;
48 // The implementation type of the stream handle.
49 class implementation_type
52 // Default constructor.
54 : handle_(INVALID_HANDLE_VALUE),
55 safe_cancellation_thread_id_(0),
62 // Only this service will have access to the internal values.
63 friend class win_iocp_handle_service;
65 // The native stream handle representation.
66 native_handle_type handle_;
68 // The ID of the thread from which it is safe to cancel asynchronous
69 // operations. 0 means no asynchronous operations have been started yet.
70 // ~0 means asynchronous operations have been started from more than one
71 // thread, and cancellation is not supported for the handle.
72 DWORD safe_cancellation_thread_id_;
74 // Pointers to adjacent handle implementations in linked list.
75 implementation_type* next_;
76 implementation_type* prev_;
79 BOOST_ASIO_DECL win_iocp_handle_service(boost::asio::io_context& io_context);
81 // Destroy all user-defined handler objects owned by the service.
82 BOOST_ASIO_DECL void shutdown();
84 // Construct a new handle implementation.
85 BOOST_ASIO_DECL void construct(implementation_type& impl);
87 // Move-construct a new handle implementation.
88 BOOST_ASIO_DECL void move_construct(implementation_type& impl,
89 implementation_type& other_impl);
91 // Move-assign from another handle implementation.
92 BOOST_ASIO_DECL void move_assign(implementation_type& impl,
93 win_iocp_handle_service& other_service,
94 implementation_type& other_impl);
96 // Destroy a handle implementation.
97 BOOST_ASIO_DECL void destroy(implementation_type& impl);
99 // Assign a native handle to a handle implementation.
100 BOOST_ASIO_DECL boost::system::error_code assign(implementation_type& impl,
101 const native_handle_type& handle, boost::system::error_code& ec);
103 // Determine whether the handle is open.
104 bool is_open(const implementation_type& impl) const
106 return impl.handle_ != INVALID_HANDLE_VALUE;
109 // Destroy a handle implementation.
110 BOOST_ASIO_DECL boost::system::error_code close(implementation_type& impl,
111 boost::system::error_code& ec);
113 // Get the native handle representation.
114 native_handle_type native_handle(const implementation_type& impl) const
119 // Cancel all operations associated with the handle.
120 BOOST_ASIO_DECL boost::system::error_code cancel(implementation_type& impl,
121 boost::system::error_code& ec);
123 // Write the given data. Returns the number of bytes written.
124 template <typename ConstBufferSequence>
125 size_t write_some(implementation_type& impl,
126 const ConstBufferSequence& buffers, boost::system::error_code& ec)
128 return write_some_at(impl, 0, buffers, ec);
131 // Write the given data at the specified offset. Returns the number of bytes
133 template <typename ConstBufferSequence>
134 size_t write_some_at(implementation_type& impl, uint64_t offset,
135 const ConstBufferSequence& buffers, boost::system::error_code& ec)
137 boost::asio::const_buffer buffer =
138 buffer_sequence_adapter<boost::asio::const_buffer,
139 ConstBufferSequence>::first(buffers);
141 return do_write(impl, offset, buffer, ec);
144 // Start an asynchronous write. The data being written must be valid for the
145 // lifetime of the asynchronous operation.
146 template <typename ConstBufferSequence, typename Handler>
147 void async_write_some(implementation_type& impl,
148 const ConstBufferSequence& buffers, Handler& handler)
150 // Allocate and construct an operation to wrap the handler.
151 typedef win_iocp_handle_write_op<ConstBufferSequence, Handler> op;
152 typename op::ptr p = { boost::asio::detail::addressof(handler),
153 op::ptr::allocate(handler), 0 };
154 p.p = new (p.v) op(buffers, handler);
156 BOOST_ASIO_HANDLER_CREATION((iocp_service_.context(), *p.p, "handle", &impl,
157 reinterpret_cast<uintmax_t>(impl.handle_), "async_write_some"));
159 start_write_op(impl, 0,
160 buffer_sequence_adapter<boost::asio::const_buffer,
161 ConstBufferSequence>::first(buffers), p.p);
165 // Start an asynchronous write at a specified offset. The data being written
166 // must be valid for the lifetime of the asynchronous operation.
167 template <typename ConstBufferSequence, typename Handler>
168 void async_write_some_at(implementation_type& impl, uint64_t offset,
169 const ConstBufferSequence& buffers, Handler& handler)
171 // Allocate and construct an operation to wrap the handler.
172 typedef win_iocp_handle_write_op<ConstBufferSequence, Handler> op;
173 typename op::ptr p = { boost::asio::detail::addressof(handler),
174 op::ptr::allocate(handler), 0 };
175 p.p = new (p.v) op(buffers, handler);
177 BOOST_ASIO_HANDLER_CREATION((iocp_service_.context(), *p.p, "handle", &impl,
178 reinterpret_cast<uintmax_t>(impl.handle_), "async_write_some_at"));
180 start_write_op(impl, offset,
181 buffer_sequence_adapter<boost::asio::const_buffer,
182 ConstBufferSequence>::first(buffers), p.p);
186 // Read some data. Returns the number of bytes received.
187 template <typename MutableBufferSequence>
188 size_t read_some(implementation_type& impl,
189 const MutableBufferSequence& buffers, boost::system::error_code& ec)
191 return read_some_at(impl, 0, buffers, ec);
194 // Read some data at a specified offset. Returns the number of bytes received.
195 template <typename MutableBufferSequence>
196 size_t read_some_at(implementation_type& impl, uint64_t offset,
197 const MutableBufferSequence& buffers, boost::system::error_code& ec)
199 boost::asio::mutable_buffer buffer =
200 buffer_sequence_adapter<boost::asio::mutable_buffer,
201 MutableBufferSequence>::first(buffers);
203 return do_read(impl, offset, buffer, ec);
206 // Start an asynchronous read. The buffer for the data being received must be
207 // valid for the lifetime of the asynchronous operation.
208 template <typename MutableBufferSequence, typename Handler>
209 void async_read_some(implementation_type& impl,
210 const MutableBufferSequence& buffers, Handler& handler)
212 // Allocate and construct an operation to wrap the handler.
213 typedef win_iocp_handle_read_op<MutableBufferSequence, Handler> op;
214 typename op::ptr p = { boost::asio::detail::addressof(handler),
215 op::ptr::allocate(handler), 0 };
216 p.p = new (p.v) op(buffers, handler);
218 BOOST_ASIO_HANDLER_CREATION((iocp_service_.context(), *p.p, "handle", &impl,
219 reinterpret_cast<uintmax_t>(impl.handle_), "async_read_some"));
221 start_read_op(impl, 0,
222 buffer_sequence_adapter<boost::asio::mutable_buffer,
223 MutableBufferSequence>::first(buffers), p.p);
227 // Start an asynchronous read at a specified offset. The buffer for the data
228 // being received must be valid for the lifetime of the asynchronous
230 template <typename MutableBufferSequence, typename Handler>
231 void async_read_some_at(implementation_type& impl, uint64_t offset,
232 const MutableBufferSequence& buffers, Handler& handler)
234 // Allocate and construct an operation to wrap the handler.
235 typedef win_iocp_handle_read_op<MutableBufferSequence, Handler> op;
236 typename op::ptr p = { boost::asio::detail::addressof(handler),
237 op::ptr::allocate(handler), 0 };
238 p.p = new (p.v) op(buffers, handler);
240 BOOST_ASIO_HANDLER_CREATION((iocp_service_.context(), *p.p, "handle", &impl,
241 reinterpret_cast<uintmax_t>(impl.handle_), "async_read_some_at"));
243 start_read_op(impl, offset,
244 buffer_sequence_adapter<boost::asio::mutable_buffer,
245 MutableBufferSequence>::first(buffers), p.p);
250 // Prevent the use of the null_buffers type with this service.
251 size_t write_some(implementation_type& impl,
252 const null_buffers& buffers, boost::system::error_code& ec);
253 size_t write_some_at(implementation_type& impl, uint64_t offset,
254 const null_buffers& buffers, boost::system::error_code& ec);
255 template <typename Handler>
256 void async_write_some(implementation_type& impl,
257 const null_buffers& buffers, Handler& handler);
258 template <typename Handler>
259 void async_write_some_at(implementation_type& impl, uint64_t offset,
260 const null_buffers& buffers, Handler& handler);
261 size_t read_some(implementation_type& impl,
262 const null_buffers& buffers, boost::system::error_code& ec);
263 size_t read_some_at(implementation_type& impl, uint64_t offset,
264 const null_buffers& buffers, boost::system::error_code& ec);
265 template <typename Handler>
266 void async_read_some(implementation_type& impl,
267 const null_buffers& buffers, Handler& handler);
268 template <typename Handler>
269 void async_read_some_at(implementation_type& impl, uint64_t offset,
270 const null_buffers& buffers, Handler& handler);
272 // Helper class for waiting for synchronous operations to complete.
273 class overlapped_wrapper;
275 // Helper function to perform a synchronous write operation.
276 BOOST_ASIO_DECL size_t do_write(implementation_type& impl,
277 uint64_t offset, const boost::asio::const_buffer& buffer,
278 boost::system::error_code& ec);
280 // Helper function to start a write operation.
281 BOOST_ASIO_DECL void start_write_op(implementation_type& impl,
282 uint64_t offset, const boost::asio::const_buffer& buffer,
285 // Helper function to perform a synchronous write operation.
286 BOOST_ASIO_DECL size_t do_read(implementation_type& impl,
287 uint64_t offset, const boost::asio::mutable_buffer& buffer,
288 boost::system::error_code& ec);
290 // Helper function to start a read operation.
291 BOOST_ASIO_DECL void start_read_op(implementation_type& impl,
292 uint64_t offset, const boost::asio::mutable_buffer& buffer,
295 // Update the ID of the thread from which cancellation is safe.
296 BOOST_ASIO_DECL void update_cancellation_thread_id(implementation_type& impl);
298 // Helper function to close a handle when the associated object is being
300 BOOST_ASIO_DECL void close_for_destruction(implementation_type& impl);
302 // The IOCP service used for running asynchronous operations and dispatching
304 win_iocp_io_context& iocp_service_;
306 // Mutex to protect access to the linked list of implementations.
309 // The head of a linked list of all implementations.
310 implementation_type* impl_list_;
313 } // namespace detail
317 #include <boost/asio/detail/pop_options.hpp>
319 #if defined(BOOST_ASIO_HEADER_ONLY)
320 # include <boost/asio/detail/impl/win_iocp_handle_service.ipp>
321 #endif // defined(BOOST_ASIO_HEADER_ONLY)
323 #endif // defined(BOOST_ASIO_HAS_IOCP)
325 #endif // BOOST_ASIO_DETAIL_WIN_IOCP_HANDLE_SERVICE_HPP