Chris@16
|
1 //
|
Chris@16
|
2 // windows/basic_random_access_handle.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 //
|
Chris@16
|
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
9 //
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_ASIO_WINDOWS_BASIC_RANDOM_ACCESS_HANDLE_HPP
|
Chris@16
|
12 #define BOOST_ASIO_WINDOWS_BASIC_RANDOM_ACCESS_HANDLE_HPP
|
Chris@16
|
13
|
Chris@16
|
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
15 # pragma once
|
Chris@16
|
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
17
|
Chris@16
|
18 #include <boost/asio/detail/config.hpp>
|
Chris@16
|
19
|
Chris@16
|
20 #if defined(BOOST_ASIO_HAS_WINDOWS_RANDOM_ACCESS_HANDLE) \
|
Chris@16
|
21 || defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
22
|
Chris@16
|
23 #include <cstddef>
|
Chris@16
|
24 #include <boost/asio/detail/handler_type_requirements.hpp>
|
Chris@16
|
25 #include <boost/asio/detail/throw_error.hpp>
|
Chris@16
|
26 #include <boost/asio/error.hpp>
|
Chris@16
|
27 #include <boost/asio/windows/basic_handle.hpp>
|
Chris@16
|
28 #include <boost/asio/windows/random_access_handle_service.hpp>
|
Chris@16
|
29
|
Chris@16
|
30 #include <boost/asio/detail/push_options.hpp>
|
Chris@16
|
31
|
Chris@16
|
32 namespace boost {
|
Chris@16
|
33 namespace asio {
|
Chris@16
|
34 namespace windows {
|
Chris@16
|
35
|
Chris@16
|
36 /// Provides random-access handle functionality.
|
Chris@16
|
37 /**
|
Chris@16
|
38 * The windows::basic_random_access_handle class template provides asynchronous
|
Chris@16
|
39 * and blocking random-access handle functionality.
|
Chris@16
|
40 *
|
Chris@16
|
41 * @par Thread Safety
|
Chris@16
|
42 * @e Distinct @e objects: Safe.@n
|
Chris@16
|
43 * @e Shared @e objects: Unsafe.
|
Chris@16
|
44 */
|
Chris@16
|
45 template <typename RandomAccessHandleService = random_access_handle_service>
|
Chris@16
|
46 class basic_random_access_handle
|
Chris@16
|
47 : public basic_handle<RandomAccessHandleService>
|
Chris@16
|
48 {
|
Chris@16
|
49 public:
|
Chris@16
|
50 /// (Deprecated: Use native_handle_type.) The native representation of a
|
Chris@16
|
51 /// handle.
|
Chris@16
|
52 typedef typename RandomAccessHandleService::native_handle_type native_type;
|
Chris@16
|
53
|
Chris@16
|
54 /// The native representation of a handle.
|
Chris@16
|
55 typedef typename RandomAccessHandleService::native_handle_type
|
Chris@16
|
56 native_handle_type;
|
Chris@16
|
57
|
Chris@16
|
58 /// Construct a basic_random_access_handle without opening it.
|
Chris@16
|
59 /**
|
Chris@16
|
60 * This constructor creates a random-access handle without opening it. The
|
Chris@16
|
61 * handle needs to be opened before data can be written to or read from it.
|
Chris@16
|
62 *
|
Chris@16
|
63 * @param io_service The io_service object that the random-access handle will
|
Chris@16
|
64 * use to dispatch handlers for any asynchronous operations performed on the
|
Chris@16
|
65 * handle.
|
Chris@16
|
66 */
|
Chris@16
|
67 explicit basic_random_access_handle(boost::asio::io_service& io_service)
|
Chris@16
|
68 : basic_handle<RandomAccessHandleService>(io_service)
|
Chris@16
|
69 {
|
Chris@16
|
70 }
|
Chris@16
|
71
|
Chris@16
|
72 /// Construct a basic_random_access_handle on an existing native handle.
|
Chris@16
|
73 /**
|
Chris@16
|
74 * This constructor creates a random-access handle object to hold an existing
|
Chris@16
|
75 * native handle.
|
Chris@16
|
76 *
|
Chris@16
|
77 * @param io_service The io_service object that the random-access handle will
|
Chris@16
|
78 * use to dispatch handlers for any asynchronous operations performed on the
|
Chris@16
|
79 * handle.
|
Chris@16
|
80 *
|
Chris@16
|
81 * @param handle The new underlying handle implementation.
|
Chris@16
|
82 *
|
Chris@16
|
83 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
84 */
|
Chris@16
|
85 basic_random_access_handle(boost::asio::io_service& io_service,
|
Chris@16
|
86 const native_handle_type& handle)
|
Chris@16
|
87 : basic_handle<RandomAccessHandleService>(io_service, handle)
|
Chris@16
|
88 {
|
Chris@16
|
89 }
|
Chris@16
|
90
|
Chris@16
|
91 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
92 /// Move-construct a basic_random_access_handle from another.
|
Chris@16
|
93 /**
|
Chris@16
|
94 * This constructor moves a random-access handle from one object to another.
|
Chris@16
|
95 *
|
Chris@16
|
96 * @param other The other basic_random_access_handle object from which the
|
Chris@16
|
97 * move will occur.
|
Chris@16
|
98 *
|
Chris@16
|
99 * @note Following the move, the moved-from object is in the same state as if
|
Chris@16
|
100 * constructed using the @c basic_random_access_handle(io_service&)
|
Chris@16
|
101 * constructor.
|
Chris@16
|
102 */
|
Chris@16
|
103 basic_random_access_handle(basic_random_access_handle&& other)
|
Chris@16
|
104 : basic_handle<RandomAccessHandleService>(
|
Chris@16
|
105 BOOST_ASIO_MOVE_CAST(basic_random_access_handle)(other))
|
Chris@16
|
106 {
|
Chris@16
|
107 }
|
Chris@16
|
108
|
Chris@16
|
109 /// Move-assign a basic_random_access_handle from another.
|
Chris@16
|
110 /**
|
Chris@16
|
111 * This assignment operator moves a random-access handle from one object to
|
Chris@16
|
112 * another.
|
Chris@16
|
113 *
|
Chris@16
|
114 * @param other The other basic_random_access_handle object from which the
|
Chris@16
|
115 * move will occur.
|
Chris@16
|
116 *
|
Chris@16
|
117 * @note Following the move, the moved-from object is in the same state as if
|
Chris@16
|
118 * constructed using the @c basic_random_access_handle(io_service&)
|
Chris@16
|
119 * constructor.
|
Chris@16
|
120 */
|
Chris@16
|
121 basic_random_access_handle& operator=(basic_random_access_handle&& other)
|
Chris@16
|
122 {
|
Chris@16
|
123 basic_handle<RandomAccessHandleService>::operator=(
|
Chris@16
|
124 BOOST_ASIO_MOVE_CAST(basic_random_access_handle)(other));
|
Chris@16
|
125 return *this;
|
Chris@16
|
126 }
|
Chris@16
|
127 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
128
|
Chris@16
|
129 /// Write some data to the handle at the specified offset.
|
Chris@16
|
130 /**
|
Chris@16
|
131 * This function is used to write data to the random-access handle. The
|
Chris@16
|
132 * function call will block until one or more bytes of the data has been
|
Chris@16
|
133 * written successfully, or until an error occurs.
|
Chris@16
|
134 *
|
Chris@16
|
135 * @param offset The offset at which the data will be written.
|
Chris@16
|
136 *
|
Chris@16
|
137 * @param buffers One or more data buffers to be written to the handle.
|
Chris@16
|
138 *
|
Chris@16
|
139 * @returns The number of bytes written.
|
Chris@16
|
140 *
|
Chris@16
|
141 * @throws boost::system::system_error Thrown on failure. An error code of
|
Chris@16
|
142 * boost::asio::error::eof indicates that the connection was closed by the
|
Chris@16
|
143 * peer.
|
Chris@16
|
144 *
|
Chris@16
|
145 * @note The write_some_at operation may not write all of the data. Consider
|
Chris@16
|
146 * using the @ref write_at function if you need to ensure that all data is
|
Chris@16
|
147 * written before the blocking operation completes.
|
Chris@16
|
148 *
|
Chris@16
|
149 * @par Example
|
Chris@16
|
150 * To write a single data buffer use the @ref buffer function as follows:
|
Chris@16
|
151 * @code
|
Chris@16
|
152 * handle.write_some_at(42, boost::asio::buffer(data, size));
|
Chris@16
|
153 * @endcode
|
Chris@16
|
154 * See the @ref buffer documentation for information on writing multiple
|
Chris@16
|
155 * buffers in one go, and how to use it with arrays, boost::array or
|
Chris@16
|
156 * std::vector.
|
Chris@16
|
157 */
|
Chris@16
|
158 template <typename ConstBufferSequence>
|
Chris@16
|
159 std::size_t write_some_at(uint64_t offset,
|
Chris@16
|
160 const ConstBufferSequence& buffers)
|
Chris@16
|
161 {
|
Chris@16
|
162 boost::system::error_code ec;
|
Chris@16
|
163 std::size_t s = this->get_service().write_some_at(
|
Chris@16
|
164 this->get_implementation(), offset, buffers, ec);
|
Chris@16
|
165 boost::asio::detail::throw_error(ec, "write_some_at");
|
Chris@16
|
166 return s;
|
Chris@16
|
167 }
|
Chris@16
|
168
|
Chris@16
|
169 /// Write some data to the handle at the specified offset.
|
Chris@16
|
170 /**
|
Chris@16
|
171 * This function is used to write data to the random-access handle. The
|
Chris@16
|
172 * function call will block until one or more bytes of the data has been
|
Chris@16
|
173 * written successfully, or until an error occurs.
|
Chris@16
|
174 *
|
Chris@16
|
175 * @param offset The offset at which the data will be written.
|
Chris@16
|
176 *
|
Chris@16
|
177 * @param buffers One or more data buffers to be written to the handle.
|
Chris@16
|
178 *
|
Chris@16
|
179 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
180 *
|
Chris@16
|
181 * @returns The number of bytes written. Returns 0 if an error occurred.
|
Chris@16
|
182 *
|
Chris@16
|
183 * @note The write_some operation may not transmit all of the data to the
|
Chris@16
|
184 * peer. Consider using the @ref write_at function if you need to ensure that
|
Chris@16
|
185 * all data is written before the blocking operation completes.
|
Chris@16
|
186 */
|
Chris@16
|
187 template <typename ConstBufferSequence>
|
Chris@16
|
188 std::size_t write_some_at(uint64_t offset,
|
Chris@16
|
189 const ConstBufferSequence& buffers, boost::system::error_code& ec)
|
Chris@16
|
190 {
|
Chris@16
|
191 return this->get_service().write_some_at(
|
Chris@16
|
192 this->get_implementation(), offset, buffers, ec);
|
Chris@16
|
193 }
|
Chris@16
|
194
|
Chris@16
|
195 /// Start an asynchronous write at the specified offset.
|
Chris@16
|
196 /**
|
Chris@16
|
197 * This function is used to asynchronously write data to the random-access
|
Chris@16
|
198 * handle. The function call always returns immediately.
|
Chris@16
|
199 *
|
Chris@16
|
200 * @param offset The offset at which the data will be written.
|
Chris@16
|
201 *
|
Chris@16
|
202 * @param buffers One or more data buffers to be written to the handle.
|
Chris@16
|
203 * Although the buffers object may be copied as necessary, ownership of the
|
Chris@16
|
204 * underlying memory blocks is retained by the caller, which must guarantee
|
Chris@16
|
205 * that they remain valid until the handler is called.
|
Chris@16
|
206 *
|
Chris@16
|
207 * @param handler The handler to be called when the write operation completes.
|
Chris@16
|
208 * Copies will be made of the handler as required. The function signature of
|
Chris@16
|
209 * the handler must be:
|
Chris@16
|
210 * @code void handler(
|
Chris@16
|
211 * const boost::system::error_code& error, // Result of operation.
|
Chris@16
|
212 * std::size_t bytes_transferred // Number of bytes written.
|
Chris@16
|
213 * ); @endcode
|
Chris@16
|
214 * Regardless of whether the asynchronous operation completes immediately or
|
Chris@16
|
215 * not, the handler will not be invoked from within this function. Invocation
|
Chris@16
|
216 * of the handler will be performed in a manner equivalent to using
|
Chris@16
|
217 * boost::asio::io_service::post().
|
Chris@16
|
218 *
|
Chris@16
|
219 * @note The write operation may not transmit all of the data to the peer.
|
Chris@16
|
220 * Consider using the @ref async_write_at function if you need to ensure that
|
Chris@16
|
221 * all data is written before the asynchronous operation completes.
|
Chris@16
|
222 *
|
Chris@16
|
223 * @par Example
|
Chris@16
|
224 * To write a single data buffer use the @ref buffer function as follows:
|
Chris@16
|
225 * @code
|
Chris@16
|
226 * handle.async_write_some_at(42, boost::asio::buffer(data, size), handler);
|
Chris@16
|
227 * @endcode
|
Chris@16
|
228 * See the @ref buffer documentation for information on writing multiple
|
Chris@16
|
229 * buffers in one go, and how to use it with arrays, boost::array or
|
Chris@16
|
230 * std::vector.
|
Chris@16
|
231 */
|
Chris@16
|
232 template <typename ConstBufferSequence, typename WriteHandler>
|
Chris@16
|
233 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
|
Chris@16
|
234 void (boost::system::error_code, std::size_t))
|
Chris@16
|
235 async_write_some_at(uint64_t offset,
|
Chris@16
|
236 const ConstBufferSequence& buffers,
|
Chris@16
|
237 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
|
Chris@16
|
238 {
|
Chris@16
|
239 // If you get an error on the following line it means that your handler does
|
Chris@16
|
240 // not meet the documented type requirements for a WriteHandler.
|
Chris@16
|
241 BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
|
Chris@16
|
242
|
Chris@16
|
243 return this->get_service().async_write_some_at(this->get_implementation(),
|
Chris@16
|
244 offset, buffers, BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
|
Chris@16
|
245 }
|
Chris@16
|
246
|
Chris@16
|
247 /// Read some data from the handle at the specified offset.
|
Chris@16
|
248 /**
|
Chris@16
|
249 * This function is used to read data from the random-access handle. The
|
Chris@16
|
250 * function call will block until one or more bytes of data has been read
|
Chris@16
|
251 * successfully, or until an error occurs.
|
Chris@16
|
252 *
|
Chris@16
|
253 * @param offset The offset at which the data will be read.
|
Chris@16
|
254 *
|
Chris@16
|
255 * @param buffers One or more buffers into which the data will be read.
|
Chris@16
|
256 *
|
Chris@16
|
257 * @returns The number of bytes read.
|
Chris@16
|
258 *
|
Chris@16
|
259 * @throws boost::system::system_error Thrown on failure. An error code of
|
Chris@16
|
260 * boost::asio::error::eof indicates that the connection was closed by the
|
Chris@16
|
261 * peer.
|
Chris@16
|
262 *
|
Chris@16
|
263 * @note The read_some operation may not read all of the requested number of
|
Chris@16
|
264 * bytes. Consider using the @ref read_at function if you need to ensure that
|
Chris@16
|
265 * the requested amount of data is read before the blocking operation
|
Chris@16
|
266 * completes.
|
Chris@16
|
267 *
|
Chris@16
|
268 * @par Example
|
Chris@16
|
269 * To read into a single data buffer use the @ref buffer function as follows:
|
Chris@16
|
270 * @code
|
Chris@16
|
271 * handle.read_some_at(42, boost::asio::buffer(data, size));
|
Chris@16
|
272 * @endcode
|
Chris@16
|
273 * See the @ref buffer documentation for information on reading into multiple
|
Chris@16
|
274 * buffers in one go, and how to use it with arrays, boost::array or
|
Chris@16
|
275 * std::vector.
|
Chris@16
|
276 */
|
Chris@16
|
277 template <typename MutableBufferSequence>
|
Chris@16
|
278 std::size_t read_some_at(uint64_t offset,
|
Chris@16
|
279 const MutableBufferSequence& buffers)
|
Chris@16
|
280 {
|
Chris@16
|
281 boost::system::error_code ec;
|
Chris@16
|
282 std::size_t s = this->get_service().read_some_at(
|
Chris@16
|
283 this->get_implementation(), offset, buffers, ec);
|
Chris@16
|
284 boost::asio::detail::throw_error(ec, "read_some_at");
|
Chris@16
|
285 return s;
|
Chris@16
|
286 }
|
Chris@16
|
287
|
Chris@16
|
288 /// Read some data from the handle at the specified offset.
|
Chris@16
|
289 /**
|
Chris@16
|
290 * This function is used to read data from the random-access handle. The
|
Chris@16
|
291 * function call will block until one or more bytes of data has been read
|
Chris@16
|
292 * successfully, or until an error occurs.
|
Chris@16
|
293 *
|
Chris@16
|
294 * @param offset The offset at which the data will be read.
|
Chris@16
|
295 *
|
Chris@16
|
296 * @param buffers One or more buffers into which the data will be read.
|
Chris@16
|
297 *
|
Chris@16
|
298 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
299 *
|
Chris@16
|
300 * @returns The number of bytes read. Returns 0 if an error occurred.
|
Chris@16
|
301 *
|
Chris@16
|
302 * @note The read_some operation may not read all of the requested number of
|
Chris@16
|
303 * bytes. Consider using the @ref read_at function if you need to ensure that
|
Chris@16
|
304 * the requested amount of data is read before the blocking operation
|
Chris@16
|
305 * completes.
|
Chris@16
|
306 */
|
Chris@16
|
307 template <typename MutableBufferSequence>
|
Chris@16
|
308 std::size_t read_some_at(uint64_t offset,
|
Chris@16
|
309 const MutableBufferSequence& buffers, boost::system::error_code& ec)
|
Chris@16
|
310 {
|
Chris@16
|
311 return this->get_service().read_some_at(
|
Chris@16
|
312 this->get_implementation(), offset, buffers, ec);
|
Chris@16
|
313 }
|
Chris@16
|
314
|
Chris@16
|
315 /// Start an asynchronous read at the specified offset.
|
Chris@16
|
316 /**
|
Chris@16
|
317 * This function is used to asynchronously read data from the random-access
|
Chris@16
|
318 * handle. The function call always returns immediately.
|
Chris@16
|
319 *
|
Chris@16
|
320 * @param offset The offset at which the data will be read.
|
Chris@16
|
321 *
|
Chris@16
|
322 * @param buffers One or more buffers into which the data will be read.
|
Chris@16
|
323 * Although the buffers object may be copied as necessary, ownership of the
|
Chris@16
|
324 * underlying memory blocks is retained by the caller, which must guarantee
|
Chris@16
|
325 * that they remain valid until the handler is called.
|
Chris@16
|
326 *
|
Chris@16
|
327 * @param handler The handler to be called when the read operation completes.
|
Chris@16
|
328 * Copies will be made of the handler as required. The function signature of
|
Chris@16
|
329 * the handler must be:
|
Chris@16
|
330 * @code void handler(
|
Chris@16
|
331 * const boost::system::error_code& error, // Result of operation.
|
Chris@16
|
332 * std::size_t bytes_transferred // Number of bytes read.
|
Chris@16
|
333 * ); @endcode
|
Chris@16
|
334 * Regardless of whether the asynchronous operation completes immediately or
|
Chris@16
|
335 * not, the handler will not be invoked from within this function. Invocation
|
Chris@16
|
336 * of the handler will be performed in a manner equivalent to using
|
Chris@16
|
337 * boost::asio::io_service::post().
|
Chris@16
|
338 *
|
Chris@16
|
339 * @note The read operation may not read all of the requested number of bytes.
|
Chris@16
|
340 * Consider using the @ref async_read_at function if you need to ensure that
|
Chris@16
|
341 * the requested amount of data is read before the asynchronous operation
|
Chris@16
|
342 * completes.
|
Chris@16
|
343 *
|
Chris@16
|
344 * @par Example
|
Chris@16
|
345 * To read into a single data buffer use the @ref buffer function as follows:
|
Chris@16
|
346 * @code
|
Chris@16
|
347 * handle.async_read_some_at(42, boost::asio::buffer(data, size), handler);
|
Chris@16
|
348 * @endcode
|
Chris@16
|
349 * See the @ref buffer documentation for information on reading into multiple
|
Chris@16
|
350 * buffers in one go, and how to use it with arrays, boost::array or
|
Chris@16
|
351 * std::vector.
|
Chris@16
|
352 */
|
Chris@16
|
353 template <typename MutableBufferSequence, typename ReadHandler>
|
Chris@16
|
354 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
|
Chris@16
|
355 void (boost::system::error_code, std::size_t))
|
Chris@16
|
356 async_read_some_at(uint64_t offset,
|
Chris@16
|
357 const MutableBufferSequence& buffers,
|
Chris@16
|
358 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
|
Chris@16
|
359 {
|
Chris@16
|
360 // If you get an error on the following line it means that your handler does
|
Chris@16
|
361 // not meet the documented type requirements for a ReadHandler.
|
Chris@16
|
362 BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
|
Chris@16
|
363
|
Chris@16
|
364 return this->get_service().async_read_some_at(this->get_implementation(),
|
Chris@16
|
365 offset, buffers, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
|
Chris@16
|
366 }
|
Chris@16
|
367 };
|
Chris@16
|
368
|
Chris@16
|
369 } // namespace windows
|
Chris@16
|
370 } // namespace asio
|
Chris@16
|
371 } // namespace boost
|
Chris@16
|
372
|
Chris@16
|
373 #include <boost/asio/detail/pop_options.hpp>
|
Chris@16
|
374
|
Chris@16
|
375 #endif // defined(BOOST_ASIO_HAS_WINDOWS_RANDOM_ACCESS_HANDLE)
|
Chris@16
|
376 // || defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
377
|
Chris@16
|
378 #endif // BOOST_ASIO_WINDOWS_BASIC_RANDOM_ACCESS_HANDLE_HPP
|