2 // windows/basic_random_access_handle.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2011 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 ASIO_WINDOWS_BASIC_RANDOM_ACCESS_HANDLE_HPP
12 #define 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 "asio/detail/config.hpp"
20 #if defined(ASIO_HAS_WINDOWS_RANDOM_ACCESS_HANDLE) \
21 || defined(GENERATING_DOCUMENTATION)
24 #include "asio/detail/throw_error.hpp"
25 #include "asio/error.hpp"
26 #include "asio/windows/basic_handle.hpp"
27 #include "asio/windows/random_access_handle_service.hpp"
29 #include "asio/detail/push_options.hpp"
34 /// Provides random-access handle functionality.
36 * The windows::basic_random_access_handle class template provides asynchronous
37 * and blocking random-access handle functionality.
40 * @e Distinct @e objects: Safe.@n
41 * @e Shared @e objects: Unsafe.
43 template <typename RandomAccessHandleService = random_access_handle_service>
44 class basic_random_access_handle
45 : public basic_handle<RandomAccessHandleService>
48 /// The native representation of a handle.
49 typedef typename RandomAccessHandleService::native_type native_type;
51 /// Construct a basic_random_access_handle without opening it.
53 * This constructor creates a random-access handle without opening it. The
54 * handle needs to be opened before data can be written to or or read from it.
56 * @param io_service The io_service object that the random-access handle will
57 * use to dispatch handlers for any asynchronous operations performed on the
60 explicit basic_random_access_handle(asio::io_service& io_service)
61 : basic_handle<RandomAccessHandleService>(io_service)
65 /// Construct a basic_random_access_handle on an existing native handle.
67 * This constructor creates a random-access handle object to hold an existing
70 * @param io_service The io_service object that the random-access handle will
71 * use to dispatch handlers for any asynchronous operations performed on the
74 * @param native_handle The new underlying handle implementation.
76 * @throws asio::system_error Thrown on failure.
78 basic_random_access_handle(asio::io_service& io_service,
79 const native_type& native_handle)
80 : basic_handle<RandomAccessHandleService>(io_service, native_handle)
84 /// Write some data to the handle at the specified offset.
86 * This function is used to write data to the random-access handle. The
87 * function call will block until one or more bytes of the data has been
88 * written successfully, or until an error occurs.
90 * @param offset The offset at which the data will be written.
92 * @param buffers One or more data buffers to be written to the handle.
94 * @returns The number of bytes written.
96 * @throws asio::system_error Thrown on failure. An error code of
97 * asio::error::eof indicates that the connection was closed by the
100 * @note The write_some_at operation may not write all of the data. Consider
101 * using the @ref write_at function if you need to ensure that all data is
102 * written before the blocking operation completes.
105 * To write a single data buffer use the @ref buffer function as follows:
107 * handle.write_some_at(42, asio::buffer(data, size));
109 * See the @ref buffer documentation for information on writing multiple
110 * buffers in one go, and how to use it with arrays, boost::array or
113 template <typename ConstBufferSequence>
114 std::size_t write_some_at(boost::uint64_t offset,
115 const ConstBufferSequence& buffers)
118 std::size_t s = this->service.write_some_at(
119 this->implementation, offset, buffers, ec);
120 asio::detail::throw_error(ec);
124 /// Write some data to the handle at the specified offset.
126 * This function is used to write data to the random-access handle. The
127 * function call will block until one or more bytes of the data has been
128 * written successfully, or until an error occurs.
130 * @param offset The offset at which the data will be written.
132 * @param buffers One or more data buffers to be written to the handle.
134 * @param ec Set to indicate what error occurred, if any.
136 * @returns The number of bytes written. Returns 0 if an error occurred.
138 * @note The write_some operation may not transmit all of the data to the
139 * peer. Consider using the @ref write_at function if you need to ensure that
140 * all data is written before the blocking operation completes.
142 template <typename ConstBufferSequence>
143 std::size_t write_some_at(boost::uint64_t offset,
144 const ConstBufferSequence& buffers, asio::error_code& ec)
146 return this->service.write_some_at(
147 this->implementation, offset, buffers, ec);
150 /// Start an asynchronous write at the specified offset.
152 * This function is used to asynchronously write data to the random-access
153 * handle. The function call always returns immediately.
155 * @param offset The offset at which the data will be written.
157 * @param buffers One or more data buffers to be written to the handle.
158 * Although the buffers object may be copied as necessary, ownership of the
159 * underlying memory blocks is retained by the caller, which must guarantee
160 * that they remain valid until the handler is called.
162 * @param handler The handler to be called when the write operation completes.
163 * Copies will be made of the handler as required. The function signature of
164 * the handler must be:
165 * @code void handler(
166 * const asio::error_code& error, // Result of operation.
167 * std::size_t bytes_transferred // Number of bytes written.
169 * Regardless of whether the asynchronous operation completes immediately or
170 * not, the handler will not be invoked from within this function. Invocation
171 * of the handler will be performed in a manner equivalent to using
172 * asio::io_service::post().
174 * @note The write operation may not transmit all of the data to the peer.
175 * Consider using the @ref async_write_at function if you need to ensure that
176 * all data is written before the asynchronous operation completes.
179 * To write a single data buffer use the @ref buffer function as follows:
181 * handle.async_write_some_at(42, asio::buffer(data, size), handler);
183 * See the @ref buffer documentation for information on writing multiple
184 * buffers in one go, and how to use it with arrays, boost::array or
187 template <typename ConstBufferSequence, typename WriteHandler>
188 void async_write_some_at(boost::uint64_t offset,
189 const ConstBufferSequence& buffers, WriteHandler handler)
191 this->service.async_write_some_at(
192 this->implementation, offset, buffers, handler);
195 /// Read some data from the handle at the specified offset.
197 * This function is used to read data from the random-access handle. The
198 * function call will block until one or more bytes of data has been read
199 * successfully, or until an error occurs.
201 * @param offset The offset at which the data will be read.
203 * @param buffers One or more buffers into which the data will be read.
205 * @returns The number of bytes read.
207 * @throws asio::system_error Thrown on failure. An error code of
208 * asio::error::eof indicates that the connection was closed by the
211 * @note The read_some operation may not read all of the requested number of
212 * bytes. Consider using the @ref read_at function if you need to ensure that
213 * the requested amount of data is read before the blocking operation
217 * To read into a single data buffer use the @ref buffer function as follows:
219 * handle.read_some_at(42, asio::buffer(data, size));
221 * See the @ref buffer documentation for information on reading into multiple
222 * buffers in one go, and how to use it with arrays, boost::array or
225 template <typename MutableBufferSequence>
226 std::size_t read_some_at(boost::uint64_t offset,
227 const MutableBufferSequence& buffers)
230 std::size_t s = this->service.read_some_at(
231 this->implementation, offset, buffers, ec);
232 asio::detail::throw_error(ec);
236 /// Read some data from the handle at the specified offset.
238 * This function is used to read data from the random-access handle. The
239 * function call will block until one or more bytes of data has been read
240 * successfully, or until an error occurs.
242 * @param offset The offset at which the data will be read.
244 * @param buffers One or more buffers into which the data will be read.
246 * @param ec Set to indicate what error occurred, if any.
248 * @returns The number of bytes read. Returns 0 if an error occurred.
250 * @note The read_some operation may not read all of the requested number of
251 * bytes. Consider using the @ref read_at function if you need to ensure that
252 * the requested amount of data is read before the blocking operation
255 template <typename MutableBufferSequence>
256 std::size_t read_some_at(boost::uint64_t offset,
257 const MutableBufferSequence& buffers, asio::error_code& ec)
259 return this->service.read_some_at(
260 this->implementation, offset, buffers, ec);
263 /// Start an asynchronous read at the specified offset.
265 * This function is used to asynchronously read data from the random-access
266 * handle. The function call always returns immediately.
268 * @param offset The offset at which the data will be read.
270 * @param buffers One or more buffers into which the data will be read.
271 * Although the buffers object may be copied as necessary, ownership of the
272 * underlying memory blocks is retained by the caller, which must guarantee
273 * that they remain valid until the handler is called.
275 * @param handler The handler to be called when the read operation completes.
276 * Copies will be made of the handler as required. The function signature of
277 * the handler must be:
278 * @code void handler(
279 * const asio::error_code& error, // Result of operation.
280 * std::size_t bytes_transferred // Number of bytes read.
282 * Regardless of whether the asynchronous operation completes immediately or
283 * not, the handler will not be invoked from within this function. Invocation
284 * of the handler will be performed in a manner equivalent to using
285 * asio::io_service::post().
287 * @note The read operation may not read all of the requested number of bytes.
288 * Consider using the @ref async_read_at function if you need to ensure that
289 * the requested amount of data is read before the asynchronous operation
293 * To read into a single data buffer use the @ref buffer function as follows:
295 * handle.async_read_some_at(42, asio::buffer(data, size), handler);
297 * See the @ref buffer documentation for information on reading into multiple
298 * buffers in one go, and how to use it with arrays, boost::array or
301 template <typename MutableBufferSequence, typename ReadHandler>
302 void async_read_some_at(boost::uint64_t offset,
303 const MutableBufferSequence& buffers, ReadHandler handler)
305 this->service.async_read_some_at(
306 this->implementation, offset, buffers, handler);
310 } // namespace windows
313 #include "asio/detail/pop_options.hpp"
315 #endif // defined(ASIO_HAS_WINDOWS_RANDOM_ACCESS_HANDLE)
316 // || defined(GENERATING_DOCUMENTATION)
318 #endif // ASIO_WINDOWS_BASIC_RANDOM_ACCESS_HANDLE_HPP