Chris@16
|
1 //
|
Chris@16
|
2 // detail/win_iocp_handle_service.hpp
|
Chris@16
|
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
Chris@16
|
4 //
|
Chris@101
|
5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
Chris@16
|
6 // Copyright (c) 2008 Rep Invariant Systems, Inc. (info@repinvariant.com)
|
Chris@16
|
7 //
|
Chris@16
|
8 // Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
9 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
10 //
|
Chris@16
|
11
|
Chris@16
|
12 #ifndef BOOST_ASIO_DETAIL_WIN_IOCP_HANDLE_SERVICE_HPP
|
Chris@16
|
13 #define BOOST_ASIO_DETAIL_WIN_IOCP_HANDLE_SERVICE_HPP
|
Chris@16
|
14
|
Chris@16
|
15 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
16 # pragma once
|
Chris@16
|
17 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
18
|
Chris@16
|
19 #include <boost/asio/detail/config.hpp>
|
Chris@16
|
20
|
Chris@16
|
21 #if defined(BOOST_ASIO_HAS_IOCP)
|
Chris@16
|
22
|
Chris@16
|
23 #include <boost/asio/error.hpp>
|
Chris@16
|
24 #include <boost/asio/io_service.hpp>
|
Chris@16
|
25 #include <boost/asio/detail/addressof.hpp>
|
Chris@16
|
26 #include <boost/asio/detail/buffer_sequence_adapter.hpp>
|
Chris@16
|
27 #include <boost/asio/detail/cstdint.hpp>
|
Chris@16
|
28 #include <boost/asio/detail/handler_alloc_helpers.hpp>
|
Chris@16
|
29 #include <boost/asio/detail/mutex.hpp>
|
Chris@16
|
30 #include <boost/asio/detail/operation.hpp>
|
Chris@16
|
31 #include <boost/asio/detail/win_iocp_handle_read_op.hpp>
|
Chris@16
|
32 #include <boost/asio/detail/win_iocp_handle_write_op.hpp>
|
Chris@16
|
33 #include <boost/asio/detail/win_iocp_io_service.hpp>
|
Chris@16
|
34
|
Chris@16
|
35 #include <boost/asio/detail/push_options.hpp>
|
Chris@16
|
36
|
Chris@16
|
37 namespace boost {
|
Chris@16
|
38 namespace asio {
|
Chris@16
|
39 namespace detail {
|
Chris@16
|
40
|
Chris@16
|
41 class win_iocp_handle_service
|
Chris@16
|
42 {
|
Chris@16
|
43 public:
|
Chris@16
|
44 // The native type of a stream handle.
|
Chris@16
|
45 typedef HANDLE native_handle_type;
|
Chris@16
|
46
|
Chris@16
|
47 // The implementation type of the stream handle.
|
Chris@16
|
48 class implementation_type
|
Chris@16
|
49 {
|
Chris@16
|
50 public:
|
Chris@16
|
51 // Default constructor.
|
Chris@16
|
52 implementation_type()
|
Chris@16
|
53 : handle_(INVALID_HANDLE_VALUE),
|
Chris@16
|
54 safe_cancellation_thread_id_(0),
|
Chris@16
|
55 next_(0),
|
Chris@16
|
56 prev_(0)
|
Chris@16
|
57 {
|
Chris@16
|
58 }
|
Chris@16
|
59
|
Chris@16
|
60 private:
|
Chris@16
|
61 // Only this service will have access to the internal values.
|
Chris@16
|
62 friend class win_iocp_handle_service;
|
Chris@16
|
63
|
Chris@16
|
64 // The native stream handle representation.
|
Chris@16
|
65 native_handle_type handle_;
|
Chris@16
|
66
|
Chris@16
|
67 // The ID of the thread from which it is safe to cancel asynchronous
|
Chris@16
|
68 // operations. 0 means no asynchronous operations have been started yet.
|
Chris@16
|
69 // ~0 means asynchronous operations have been started from more than one
|
Chris@16
|
70 // thread, and cancellation is not supported for the handle.
|
Chris@16
|
71 DWORD safe_cancellation_thread_id_;
|
Chris@16
|
72
|
Chris@16
|
73 // Pointers to adjacent handle implementations in linked list.
|
Chris@16
|
74 implementation_type* next_;
|
Chris@16
|
75 implementation_type* prev_;
|
Chris@16
|
76 };
|
Chris@16
|
77
|
Chris@16
|
78 BOOST_ASIO_DECL win_iocp_handle_service(boost::asio::io_service& io_service);
|
Chris@16
|
79
|
Chris@16
|
80 // Destroy all user-defined handler objects owned by the service.
|
Chris@16
|
81 BOOST_ASIO_DECL void shutdown_service();
|
Chris@16
|
82
|
Chris@16
|
83 // Construct a new handle implementation.
|
Chris@16
|
84 BOOST_ASIO_DECL void construct(implementation_type& impl);
|
Chris@16
|
85
|
Chris@16
|
86 // Move-construct a new handle implementation.
|
Chris@16
|
87 BOOST_ASIO_DECL void move_construct(implementation_type& impl,
|
Chris@16
|
88 implementation_type& other_impl);
|
Chris@16
|
89
|
Chris@16
|
90 // Move-assign from another handle implementation.
|
Chris@16
|
91 BOOST_ASIO_DECL void move_assign(implementation_type& impl,
|
Chris@16
|
92 win_iocp_handle_service& other_service,
|
Chris@16
|
93 implementation_type& other_impl);
|
Chris@16
|
94
|
Chris@16
|
95 // Destroy a handle implementation.
|
Chris@16
|
96 BOOST_ASIO_DECL void destroy(implementation_type& impl);
|
Chris@16
|
97
|
Chris@16
|
98 // Assign a native handle to a handle implementation.
|
Chris@16
|
99 BOOST_ASIO_DECL boost::system::error_code assign(implementation_type& impl,
|
Chris@16
|
100 const native_handle_type& handle, boost::system::error_code& ec);
|
Chris@16
|
101
|
Chris@16
|
102 // Determine whether the handle is open.
|
Chris@16
|
103 bool is_open(const implementation_type& impl) const
|
Chris@16
|
104 {
|
Chris@16
|
105 return impl.handle_ != INVALID_HANDLE_VALUE;
|
Chris@16
|
106 }
|
Chris@16
|
107
|
Chris@16
|
108 // Destroy a handle implementation.
|
Chris@16
|
109 BOOST_ASIO_DECL boost::system::error_code close(implementation_type& impl,
|
Chris@16
|
110 boost::system::error_code& ec);
|
Chris@16
|
111
|
Chris@16
|
112 // Get the native handle representation.
|
Chris@16
|
113 native_handle_type native_handle(const implementation_type& impl) const
|
Chris@16
|
114 {
|
Chris@16
|
115 return impl.handle_;
|
Chris@16
|
116 }
|
Chris@16
|
117
|
Chris@16
|
118 // Cancel all operations associated with the handle.
|
Chris@16
|
119 BOOST_ASIO_DECL boost::system::error_code cancel(implementation_type& impl,
|
Chris@16
|
120 boost::system::error_code& ec);
|
Chris@16
|
121
|
Chris@16
|
122 // Write the given data. Returns the number of bytes written.
|
Chris@16
|
123 template <typename ConstBufferSequence>
|
Chris@16
|
124 size_t write_some(implementation_type& impl,
|
Chris@16
|
125 const ConstBufferSequence& buffers, boost::system::error_code& ec)
|
Chris@16
|
126 {
|
Chris@16
|
127 return write_some_at(impl, 0, buffers, ec);
|
Chris@16
|
128 }
|
Chris@16
|
129
|
Chris@16
|
130 // Write the given data at the specified offset. Returns the number of bytes
|
Chris@16
|
131 // written.
|
Chris@16
|
132 template <typename ConstBufferSequence>
|
Chris@16
|
133 size_t write_some_at(implementation_type& impl, uint64_t offset,
|
Chris@16
|
134 const ConstBufferSequence& buffers, boost::system::error_code& ec)
|
Chris@16
|
135 {
|
Chris@16
|
136 boost::asio::const_buffer buffer =
|
Chris@16
|
137 buffer_sequence_adapter<boost::asio::const_buffer,
|
Chris@16
|
138 ConstBufferSequence>::first(buffers);
|
Chris@16
|
139
|
Chris@16
|
140 return do_write(impl, offset, buffer, ec);
|
Chris@16
|
141 }
|
Chris@16
|
142
|
Chris@16
|
143 // Start an asynchronous write. The data being written must be valid for the
|
Chris@16
|
144 // lifetime of the asynchronous operation.
|
Chris@16
|
145 template <typename ConstBufferSequence, typename Handler>
|
Chris@16
|
146 void async_write_some(implementation_type& impl,
|
Chris@16
|
147 const ConstBufferSequence& buffers, Handler& handler)
|
Chris@16
|
148 {
|
Chris@16
|
149 // Allocate and construct an operation to wrap the handler.
|
Chris@16
|
150 typedef win_iocp_handle_write_op<ConstBufferSequence, Handler> op;
|
Chris@16
|
151 typename op::ptr p = { boost::asio::detail::addressof(handler),
|
Chris@16
|
152 boost_asio_handler_alloc_helpers::allocate(
|
Chris@16
|
153 sizeof(op), handler), 0 };
|
Chris@16
|
154 p.p = new (p.v) op(buffers, handler);
|
Chris@16
|
155
|
Chris@16
|
156 BOOST_ASIO_HANDLER_CREATION((p.p, "handle", &impl, "async_write_some"));
|
Chris@16
|
157
|
Chris@16
|
158 start_write_op(impl, 0,
|
Chris@16
|
159 buffer_sequence_adapter<boost::asio::const_buffer,
|
Chris@16
|
160 ConstBufferSequence>::first(buffers), p.p);
|
Chris@16
|
161 p.v = p.p = 0;
|
Chris@16
|
162 }
|
Chris@16
|
163
|
Chris@16
|
164 // Start an asynchronous write at a specified offset. The data being written
|
Chris@16
|
165 // must be valid for the lifetime of the asynchronous operation.
|
Chris@16
|
166 template <typename ConstBufferSequence, typename Handler>
|
Chris@16
|
167 void async_write_some_at(implementation_type& impl, uint64_t offset,
|
Chris@16
|
168 const ConstBufferSequence& buffers, Handler& handler)
|
Chris@16
|
169 {
|
Chris@16
|
170 // Allocate and construct an operation to wrap the handler.
|
Chris@16
|
171 typedef win_iocp_handle_write_op<ConstBufferSequence, Handler> op;
|
Chris@16
|
172 typename op::ptr p = { boost::asio::detail::addressof(handler),
|
Chris@16
|
173 boost_asio_handler_alloc_helpers::allocate(
|
Chris@16
|
174 sizeof(op), handler), 0 };
|
Chris@16
|
175 p.p = new (p.v) op(buffers, handler);
|
Chris@16
|
176
|
Chris@16
|
177 BOOST_ASIO_HANDLER_CREATION((p.p, "handle", &impl, "async_write_some_at"));
|
Chris@16
|
178
|
Chris@16
|
179 start_write_op(impl, offset,
|
Chris@16
|
180 buffer_sequence_adapter<boost::asio::const_buffer,
|
Chris@16
|
181 ConstBufferSequence>::first(buffers), p.p);
|
Chris@16
|
182 p.v = p.p = 0;
|
Chris@16
|
183 }
|
Chris@16
|
184
|
Chris@16
|
185 // Read some data. Returns the number of bytes received.
|
Chris@16
|
186 template <typename MutableBufferSequence>
|
Chris@16
|
187 size_t read_some(implementation_type& impl,
|
Chris@16
|
188 const MutableBufferSequence& buffers, boost::system::error_code& ec)
|
Chris@16
|
189 {
|
Chris@16
|
190 return read_some_at(impl, 0, buffers, ec);
|
Chris@16
|
191 }
|
Chris@16
|
192
|
Chris@16
|
193 // Read some data at a specified offset. Returns the number of bytes received.
|
Chris@16
|
194 template <typename MutableBufferSequence>
|
Chris@16
|
195 size_t read_some_at(implementation_type& impl, uint64_t offset,
|
Chris@16
|
196 const MutableBufferSequence& buffers, boost::system::error_code& ec)
|
Chris@16
|
197 {
|
Chris@16
|
198 boost::asio::mutable_buffer buffer =
|
Chris@16
|
199 buffer_sequence_adapter<boost::asio::mutable_buffer,
|
Chris@16
|
200 MutableBufferSequence>::first(buffers);
|
Chris@16
|
201
|
Chris@16
|
202 return do_read(impl, offset, buffer, ec);
|
Chris@16
|
203 }
|
Chris@16
|
204
|
Chris@16
|
205 // Start an asynchronous read. The buffer for the data being received must be
|
Chris@16
|
206 // valid for the lifetime of the asynchronous operation.
|
Chris@16
|
207 template <typename MutableBufferSequence, typename Handler>
|
Chris@16
|
208 void async_read_some(implementation_type& impl,
|
Chris@16
|
209 const MutableBufferSequence& buffers, Handler& handler)
|
Chris@16
|
210 {
|
Chris@16
|
211 // Allocate and construct an operation to wrap the handler.
|
Chris@16
|
212 typedef win_iocp_handle_read_op<MutableBufferSequence, Handler> op;
|
Chris@16
|
213 typename op::ptr p = { boost::asio::detail::addressof(handler),
|
Chris@16
|
214 boost_asio_handler_alloc_helpers::allocate(
|
Chris@16
|
215 sizeof(op), handler), 0 };
|
Chris@16
|
216 p.p = new (p.v) op(buffers, handler);
|
Chris@16
|
217
|
Chris@16
|
218 BOOST_ASIO_HANDLER_CREATION((p.p, "handle", &impl, "async_read_some"));
|
Chris@16
|
219
|
Chris@16
|
220 start_read_op(impl, 0,
|
Chris@16
|
221 buffer_sequence_adapter<boost::asio::mutable_buffer,
|
Chris@16
|
222 MutableBufferSequence>::first(buffers), p.p);
|
Chris@16
|
223 p.v = p.p = 0;
|
Chris@16
|
224 }
|
Chris@16
|
225
|
Chris@16
|
226 // Start an asynchronous read at a specified offset. The buffer for the data
|
Chris@16
|
227 // being received must be valid for the lifetime of the asynchronous
|
Chris@16
|
228 // operation.
|
Chris@16
|
229 template <typename MutableBufferSequence, typename Handler>
|
Chris@16
|
230 void async_read_some_at(implementation_type& impl, uint64_t offset,
|
Chris@16
|
231 const MutableBufferSequence& buffers, Handler& handler)
|
Chris@16
|
232 {
|
Chris@16
|
233 // Allocate and construct an operation to wrap the handler.
|
Chris@16
|
234 typedef win_iocp_handle_read_op<MutableBufferSequence, Handler> op;
|
Chris@16
|
235 typename op::ptr p = { boost::asio::detail::addressof(handler),
|
Chris@16
|
236 boost_asio_handler_alloc_helpers::allocate(
|
Chris@16
|
237 sizeof(op), handler), 0 };
|
Chris@16
|
238 p.p = new (p.v) op(buffers, handler);
|
Chris@16
|
239
|
Chris@16
|
240 BOOST_ASIO_HANDLER_CREATION((p.p, "handle", &impl, "async_read_some_at"));
|
Chris@16
|
241
|
Chris@16
|
242 start_read_op(impl, offset,
|
Chris@16
|
243 buffer_sequence_adapter<boost::asio::mutable_buffer,
|
Chris@16
|
244 MutableBufferSequence>::first(buffers), p.p);
|
Chris@16
|
245 p.v = p.p = 0;
|
Chris@16
|
246 }
|
Chris@16
|
247
|
Chris@16
|
248 private:
|
Chris@16
|
249 // Prevent the use of the null_buffers type with this service.
|
Chris@16
|
250 size_t write_some(implementation_type& impl,
|
Chris@16
|
251 const null_buffers& buffers, boost::system::error_code& ec);
|
Chris@16
|
252 size_t write_some_at(implementation_type& impl, uint64_t offset,
|
Chris@16
|
253 const null_buffers& buffers, boost::system::error_code& ec);
|
Chris@16
|
254 template <typename Handler>
|
Chris@16
|
255 void async_write_some(implementation_type& impl,
|
Chris@16
|
256 const null_buffers& buffers, Handler& handler);
|
Chris@16
|
257 template <typename Handler>
|
Chris@16
|
258 void async_write_some_at(implementation_type& impl, uint64_t offset,
|
Chris@16
|
259 const null_buffers& buffers, Handler& handler);
|
Chris@16
|
260 size_t read_some(implementation_type& impl,
|
Chris@16
|
261 const null_buffers& buffers, boost::system::error_code& ec);
|
Chris@16
|
262 size_t read_some_at(implementation_type& impl, uint64_t offset,
|
Chris@16
|
263 const null_buffers& buffers, boost::system::error_code& ec);
|
Chris@16
|
264 template <typename Handler>
|
Chris@16
|
265 void async_read_some(implementation_type& impl,
|
Chris@16
|
266 const null_buffers& buffers, Handler& handler);
|
Chris@16
|
267 template <typename Handler>
|
Chris@16
|
268 void async_read_some_at(implementation_type& impl, uint64_t offset,
|
Chris@16
|
269 const null_buffers& buffers, Handler& handler);
|
Chris@16
|
270
|
Chris@16
|
271 // Helper class for waiting for synchronous operations to complete.
|
Chris@16
|
272 class overlapped_wrapper;
|
Chris@16
|
273
|
Chris@16
|
274 // Helper function to perform a synchronous write operation.
|
Chris@16
|
275 BOOST_ASIO_DECL size_t do_write(implementation_type& impl,
|
Chris@16
|
276 uint64_t offset, const boost::asio::const_buffer& buffer,
|
Chris@16
|
277 boost::system::error_code& ec);
|
Chris@16
|
278
|
Chris@16
|
279 // Helper function to start a write operation.
|
Chris@16
|
280 BOOST_ASIO_DECL void start_write_op(implementation_type& impl,
|
Chris@16
|
281 uint64_t offset, const boost::asio::const_buffer& buffer,
|
Chris@16
|
282 operation* op);
|
Chris@16
|
283
|
Chris@16
|
284 // Helper function to perform a synchronous write operation.
|
Chris@16
|
285 BOOST_ASIO_DECL size_t do_read(implementation_type& impl,
|
Chris@16
|
286 uint64_t offset, const boost::asio::mutable_buffer& buffer,
|
Chris@16
|
287 boost::system::error_code& ec);
|
Chris@16
|
288
|
Chris@16
|
289 // Helper function to start a read operation.
|
Chris@16
|
290 BOOST_ASIO_DECL void start_read_op(implementation_type& impl,
|
Chris@16
|
291 uint64_t offset, const boost::asio::mutable_buffer& buffer,
|
Chris@16
|
292 operation* op);
|
Chris@16
|
293
|
Chris@16
|
294 // Update the ID of the thread from which cancellation is safe.
|
Chris@16
|
295 BOOST_ASIO_DECL void update_cancellation_thread_id(implementation_type& impl);
|
Chris@16
|
296
|
Chris@16
|
297 // Helper function to close a handle when the associated object is being
|
Chris@16
|
298 // destroyed.
|
Chris@16
|
299 BOOST_ASIO_DECL void close_for_destruction(implementation_type& impl);
|
Chris@16
|
300
|
Chris@16
|
301 // The IOCP service used for running asynchronous operations and dispatching
|
Chris@16
|
302 // handlers.
|
Chris@16
|
303 win_iocp_io_service& iocp_service_;
|
Chris@16
|
304
|
Chris@16
|
305 // Mutex to protect access to the linked list of implementations.
|
Chris@16
|
306 mutex mutex_;
|
Chris@16
|
307
|
Chris@16
|
308 // The head of a linked list of all implementations.
|
Chris@16
|
309 implementation_type* impl_list_;
|
Chris@16
|
310 };
|
Chris@16
|
311
|
Chris@16
|
312 } // namespace detail
|
Chris@16
|
313 } // namespace asio
|
Chris@16
|
314 } // namespace boost
|
Chris@16
|
315
|
Chris@16
|
316 #include <boost/asio/detail/pop_options.hpp>
|
Chris@16
|
317
|
Chris@16
|
318 #if defined(BOOST_ASIO_HEADER_ONLY)
|
Chris@16
|
319 # include <boost/asio/detail/impl/win_iocp_handle_service.ipp>
|
Chris@16
|
320 #endif // defined(BOOST_ASIO_HEADER_ONLY)
|
Chris@16
|
321
|
Chris@16
|
322 #endif // defined(BOOST_ASIO_HAS_IOCP)
|
Chris@16
|
323
|
Chris@16
|
324 #endif // BOOST_ASIO_DETAIL_WIN_IOCP_HANDLE_SERVICE_HPP
|