2 // windows/basic_random_access_handle.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_WINDOWS_BASIC_RANDOM_ACCESS_HANDLE_HPP
12 #define BOOST_ASIO_WINDOWS_BASIC_RANDOM_ACCESS_HANDLE_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
22 #if defined(BOOST_ASIO_HAS_WINDOWS_RANDOM_ACCESS_HANDLE) \
23 || defined(GENERATING_DOCUMENTATION)
26 #include <boost/asio/detail/handler_type_requirements.hpp>
27 #include <boost/asio/detail/throw_error.hpp>
28 #include <boost/asio/error.hpp>
29 #include <boost/asio/windows/basic_handle.hpp>
30 #include <boost/asio/windows/random_access_handle_service.hpp>
32 #include <boost/asio/detail/push_options.hpp>
38 /// Provides random-access handle functionality.
40 * The windows::basic_random_access_handle class template provides asynchronous
41 * and blocking random-access handle functionality.
44 * @e Distinct @e objects: Safe.@n
45 * @e Shared @e objects: Unsafe.
47 template <typename RandomAccessHandleService = random_access_handle_service>
48 class basic_random_access_handle
49 : public basic_handle<RandomAccessHandleService>
52 /// The native representation of a handle.
53 typedef typename RandomAccessHandleService::native_handle_type
56 /// Construct a basic_random_access_handle without opening it.
58 * This constructor creates a random-access handle without opening it. The
59 * handle needs to be opened before data can be written to or read from it.
61 * @param io_context The io_context object that the random-access handle will
62 * use to dispatch handlers for any asynchronous operations performed on the
65 explicit basic_random_access_handle(boost::asio::io_context& io_context)
66 : basic_handle<RandomAccessHandleService>(io_context)
70 /// Construct a basic_random_access_handle on an existing native handle.
72 * This constructor creates a random-access handle object to hold an existing
75 * @param io_context The io_context object that the random-access handle will
76 * use to dispatch handlers for any asynchronous operations performed on the
79 * @param handle The new underlying handle implementation.
81 * @throws boost::system::system_error Thrown on failure.
83 basic_random_access_handle(boost::asio::io_context& io_context,
84 const native_handle_type& handle)
85 : basic_handle<RandomAccessHandleService>(io_context, handle)
89 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
90 /// Move-construct a basic_random_access_handle from another.
92 * This constructor moves a random-access handle from one object to another.
94 * @param other The other basic_random_access_handle object from which the
97 * @note Following the move, the moved-from object is in the same state as if
98 * constructed using the @c basic_random_access_handle(io_context&)
101 basic_random_access_handle(basic_random_access_handle&& other)
102 : basic_handle<RandomAccessHandleService>(
103 BOOST_ASIO_MOVE_CAST(basic_random_access_handle)(other))
107 /// Move-assign a basic_random_access_handle from another.
109 * This assignment operator moves a random-access handle from one object to
112 * @param other The other basic_random_access_handle object from which the
115 * @note Following the move, the moved-from object is in the same state as if
116 * constructed using the @c basic_random_access_handle(io_context&)
119 basic_random_access_handle& operator=(basic_random_access_handle&& other)
121 basic_handle<RandomAccessHandleService>::operator=(
122 BOOST_ASIO_MOVE_CAST(basic_random_access_handle)(other));
125 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
127 /// Write some data to the handle at the specified offset.
129 * This function is used to write data to the random-access handle. The
130 * function call will block until one or more bytes of the data has been
131 * written successfully, or until an error occurs.
133 * @param offset The offset at which the data will be written.
135 * @param buffers One or more data buffers to be written to the handle.
137 * @returns The number of bytes written.
139 * @throws boost::system::system_error Thrown on failure. An error code of
140 * boost::asio::error::eof indicates that the connection was closed by the
143 * @note The write_some_at operation may not write all of the data. Consider
144 * using the @ref write_at function if you need to ensure that all data is
145 * written before the blocking operation completes.
148 * To write a single data buffer use the @ref buffer function as follows:
150 * handle.write_some_at(42, boost::asio::buffer(data, size));
152 * See the @ref buffer documentation for information on writing multiple
153 * buffers in one go, and how to use it with arrays, boost::array or
156 template <typename ConstBufferSequence>
157 std::size_t write_some_at(uint64_t offset,
158 const ConstBufferSequence& buffers)
160 boost::system::error_code ec;
161 std::size_t s = this->get_service().write_some_at(
162 this->get_implementation(), offset, buffers, ec);
163 boost::asio::detail::throw_error(ec, "write_some_at");
167 /// Write some data to the handle at the specified offset.
169 * This function is used to write data to the random-access handle. The
170 * function call will block until one or more bytes of the data has been
171 * written successfully, or until an error occurs.
173 * @param offset The offset at which the data will be written.
175 * @param buffers One or more data buffers to be written to the handle.
177 * @param ec Set to indicate what error occurred, if any.
179 * @returns The number of bytes written. Returns 0 if an error occurred.
181 * @note The write_some operation may not transmit all of the data to the
182 * peer. Consider using the @ref write_at function if you need to ensure that
183 * all data is written before the blocking operation completes.
185 template <typename ConstBufferSequence>
186 std::size_t write_some_at(uint64_t offset,
187 const ConstBufferSequence& buffers, boost::system::error_code& ec)
189 return this->get_service().write_some_at(
190 this->get_implementation(), offset, buffers, ec);
193 /// Start an asynchronous write at the specified offset.
195 * This function is used to asynchronously write data to the random-access
196 * handle. The function call always returns immediately.
198 * @param offset The offset at which the data will be written.
200 * @param buffers One or more data buffers to be written to the handle.
201 * Although the buffers object may be copied as necessary, ownership of the
202 * underlying memory blocks is retained by the caller, which must guarantee
203 * that they remain valid until the handler is called.
205 * @param handler The handler to be called when the write operation completes.
206 * Copies will be made of the handler as required. The function signature of
207 * the handler must be:
208 * @code void handler(
209 * const boost::system::error_code& error, // Result of operation.
210 * std::size_t bytes_transferred // Number of bytes written.
212 * Regardless of whether the asynchronous operation completes immediately or
213 * not, the handler will not be invoked from within this function. Invocation
214 * of the handler will be performed in a manner equivalent to using
215 * boost::asio::io_context::post().
217 * @note The write operation may not transmit all of the data to the peer.
218 * Consider using the @ref async_write_at function if you need to ensure that
219 * all data is written before the asynchronous operation completes.
222 * To write a single data buffer use the @ref buffer function as follows:
224 * handle.async_write_some_at(42, boost::asio::buffer(data, size), handler);
226 * See the @ref buffer documentation for information on writing multiple
227 * buffers in one go, and how to use it with arrays, boost::array or
230 template <typename ConstBufferSequence, typename WriteHandler>
231 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
232 void (boost::system::error_code, std::size_t))
233 async_write_some_at(uint64_t offset,
234 const ConstBufferSequence& buffers,
235 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
237 // If you get an error on the following line it means that your handler does
238 // not meet the documented type requirements for a WriteHandler.
239 BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
241 return this->get_service().async_write_some_at(this->get_implementation(),
242 offset, buffers, BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
245 /// Read some data from the handle at the specified offset.
247 * This function is used to read data from the random-access handle. The
248 * function call will block until one or more bytes of data has been read
249 * successfully, or until an error occurs.
251 * @param offset The offset at which the data will be read.
253 * @param buffers One or more buffers into which the data will be read.
255 * @returns The number of bytes read.
257 * @throws boost::system::system_error Thrown on failure. An error code of
258 * boost::asio::error::eof indicates that the connection was closed by the
261 * @note The read_some operation may not read all of the requested number of
262 * bytes. Consider using the @ref read_at function if you need to ensure that
263 * the requested amount of data is read before the blocking operation
267 * To read into a single data buffer use the @ref buffer function as follows:
269 * handle.read_some_at(42, boost::asio::buffer(data, size));
271 * See the @ref buffer documentation for information on reading into multiple
272 * buffers in one go, and how to use it with arrays, boost::array or
275 template <typename MutableBufferSequence>
276 std::size_t read_some_at(uint64_t offset,
277 const MutableBufferSequence& buffers)
279 boost::system::error_code ec;
280 std::size_t s = this->get_service().read_some_at(
281 this->get_implementation(), offset, buffers, ec);
282 boost::asio::detail::throw_error(ec, "read_some_at");
286 /// Read some data from the handle at the specified offset.
288 * This function is used to read data from the random-access handle. The
289 * function call will block until one or more bytes of data has been read
290 * successfully, or until an error occurs.
292 * @param offset The offset at which the data will be read.
294 * @param buffers One or more buffers into which the data will be read.
296 * @param ec Set to indicate what error occurred, if any.
298 * @returns The number of bytes read. Returns 0 if an error occurred.
300 * @note The read_some operation may not read all of the requested number of
301 * bytes. Consider using the @ref read_at function if you need to ensure that
302 * the requested amount of data is read before the blocking operation
305 template <typename MutableBufferSequence>
306 std::size_t read_some_at(uint64_t offset,
307 const MutableBufferSequence& buffers, boost::system::error_code& ec)
309 return this->get_service().read_some_at(
310 this->get_implementation(), offset, buffers, ec);
313 /// Start an asynchronous read at the specified offset.
315 * This function is used to asynchronously read data from the random-access
316 * handle. The function call always returns immediately.
318 * @param offset The offset at which the data will be read.
320 * @param buffers One or more buffers into which the data will be read.
321 * Although the buffers object may be copied as necessary, ownership of the
322 * underlying memory blocks is retained by the caller, which must guarantee
323 * that they remain valid until the handler is called.
325 * @param handler The handler to be called when the read operation completes.
326 * Copies will be made of the handler as required. The function signature of
327 * the handler must be:
328 * @code void handler(
329 * const boost::system::error_code& error, // Result of operation.
330 * std::size_t bytes_transferred // Number of bytes read.
332 * Regardless of whether the asynchronous operation completes immediately or
333 * not, the handler will not be invoked from within this function. Invocation
334 * of the handler will be performed in a manner equivalent to using
335 * boost::asio::io_context::post().
337 * @note The read operation may not read all of the requested number of bytes.
338 * Consider using the @ref async_read_at function if you need to ensure that
339 * the requested amount of data is read before the asynchronous operation
343 * To read into a single data buffer use the @ref buffer function as follows:
345 * handle.async_read_some_at(42, boost::asio::buffer(data, size), handler);
347 * See the @ref buffer documentation for information on reading into multiple
348 * buffers in one go, and how to use it with arrays, boost::array or
351 template <typename MutableBufferSequence, typename ReadHandler>
352 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
353 void (boost::system::error_code, std::size_t))
354 async_read_some_at(uint64_t offset,
355 const MutableBufferSequence& buffers,
356 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
358 // If you get an error on the following line it means that your handler does
359 // not meet the documented type requirements for a ReadHandler.
360 BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
362 return this->get_service().async_read_some_at(this->get_implementation(),
363 offset, buffers, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
367 } // namespace windows
371 #include <boost/asio/detail/pop_options.hpp>
373 #endif // defined(BOOST_ASIO_HAS_WINDOWS_RANDOM_ACCESS_HANDLE)
374 // || defined(GENERATING_DOCUMENTATION)
376 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
378 #endif // BOOST_ASIO_WINDOWS_BASIC_RANDOM_ACCESS_HANDLE_HPP