Chris@16: // Chris@16: // windows/basic_random_access_handle.hpp Chris@16: // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Chris@16: // Chris@101: // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com) Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: Chris@16: #ifndef BOOST_ASIO_WINDOWS_BASIC_RANDOM_ACCESS_HANDLE_HPP Chris@16: #define BOOST_ASIO_WINDOWS_BASIC_RANDOM_ACCESS_HANDLE_HPP Chris@16: Chris@16: #if defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: # pragma once Chris@16: #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: Chris@16: #include Chris@16: Chris@16: #if defined(BOOST_ASIO_HAS_WINDOWS_RANDOM_ACCESS_HANDLE) \ Chris@16: || defined(GENERATING_DOCUMENTATION) Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace asio { Chris@16: namespace windows { Chris@16: Chris@16: /// Provides random-access handle functionality. Chris@16: /** Chris@16: * The windows::basic_random_access_handle class template provides asynchronous Chris@16: * and blocking random-access handle functionality. Chris@16: * Chris@16: * @par Thread Safety Chris@16: * @e Distinct @e objects: Safe.@n Chris@16: * @e Shared @e objects: Unsafe. Chris@16: */ Chris@16: template Chris@16: class basic_random_access_handle Chris@16: : public basic_handle Chris@16: { Chris@16: public: Chris@16: /// (Deprecated: Use native_handle_type.) The native representation of a Chris@16: /// handle. Chris@16: typedef typename RandomAccessHandleService::native_handle_type native_type; Chris@16: Chris@16: /// The native representation of a handle. Chris@16: typedef typename RandomAccessHandleService::native_handle_type Chris@16: native_handle_type; Chris@16: Chris@16: /// Construct a basic_random_access_handle without opening it. Chris@16: /** Chris@16: * This constructor creates a random-access handle without opening it. The Chris@16: * handle needs to be opened before data can be written to or read from it. Chris@16: * Chris@16: * @param io_service The io_service object that the random-access handle will Chris@16: * use to dispatch handlers for any asynchronous operations performed on the Chris@16: * handle. Chris@16: */ Chris@16: explicit basic_random_access_handle(boost::asio::io_service& io_service) Chris@16: : basic_handle(io_service) Chris@16: { Chris@16: } Chris@16: Chris@16: /// Construct a basic_random_access_handle on an existing native handle. Chris@16: /** Chris@16: * This constructor creates a random-access handle object to hold an existing Chris@16: * native handle. Chris@16: * Chris@16: * @param io_service The io_service object that the random-access handle will Chris@16: * use to dispatch handlers for any asynchronous operations performed on the Chris@16: * handle. Chris@16: * Chris@16: * @param handle The new underlying handle implementation. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: */ Chris@16: basic_random_access_handle(boost::asio::io_service& io_service, Chris@16: const native_handle_type& handle) Chris@16: : basic_handle(io_service, handle) Chris@16: { Chris@16: } Chris@16: Chris@16: #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) Chris@16: /// Move-construct a basic_random_access_handle from another. Chris@16: /** Chris@16: * This constructor moves a random-access handle from one object to another. Chris@16: * Chris@16: * @param other The other basic_random_access_handle object from which the Chris@16: * move will occur. Chris@16: * Chris@16: * @note Following the move, the moved-from object is in the same state as if Chris@16: * constructed using the @c basic_random_access_handle(io_service&) Chris@16: * constructor. Chris@16: */ Chris@16: basic_random_access_handle(basic_random_access_handle&& other) Chris@16: : basic_handle( Chris@16: BOOST_ASIO_MOVE_CAST(basic_random_access_handle)(other)) Chris@16: { Chris@16: } Chris@16: Chris@16: /// Move-assign a basic_random_access_handle from another. Chris@16: /** Chris@16: * This assignment operator moves a random-access handle from one object to Chris@16: * another. Chris@16: * Chris@16: * @param other The other basic_random_access_handle object from which the Chris@16: * move will occur. Chris@16: * Chris@16: * @note Following the move, the moved-from object is in the same state as if Chris@16: * constructed using the @c basic_random_access_handle(io_service&) Chris@16: * constructor. Chris@16: */ Chris@16: basic_random_access_handle& operator=(basic_random_access_handle&& other) Chris@16: { Chris@16: basic_handle::operator=( Chris@16: BOOST_ASIO_MOVE_CAST(basic_random_access_handle)(other)); Chris@16: return *this; Chris@16: } Chris@16: #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) Chris@16: Chris@16: /// Write some data to the handle at the specified offset. Chris@16: /** Chris@16: * This function is used to write data to the random-access handle. The Chris@16: * function call will block until one or more bytes of the data has been Chris@16: * written successfully, or until an error occurs. Chris@16: * Chris@16: * @param offset The offset at which the data will be written. Chris@16: * Chris@16: * @param buffers One or more data buffers to be written to the handle. Chris@16: * Chris@16: * @returns The number of bytes written. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. An error code of Chris@16: * boost::asio::error::eof indicates that the connection was closed by the Chris@16: * peer. Chris@16: * Chris@16: * @note The write_some_at operation may not write all of the data. Consider Chris@16: * using the @ref write_at function if you need to ensure that all data is Chris@16: * written before the blocking operation completes. Chris@16: * Chris@16: * @par Example Chris@16: * To write a single data buffer use the @ref buffer function as follows: Chris@16: * @code Chris@16: * handle.write_some_at(42, boost::asio::buffer(data, size)); Chris@16: * @endcode Chris@16: * See the @ref buffer documentation for information on writing multiple Chris@16: * buffers in one go, and how to use it with arrays, boost::array or Chris@16: * std::vector. Chris@16: */ Chris@16: template Chris@16: std::size_t write_some_at(uint64_t offset, Chris@16: const ConstBufferSequence& buffers) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: std::size_t s = this->get_service().write_some_at( Chris@16: this->get_implementation(), offset, buffers, ec); Chris@16: boost::asio::detail::throw_error(ec, "write_some_at"); Chris@16: return s; Chris@16: } Chris@16: Chris@16: /// Write some data to the handle at the specified offset. Chris@16: /** Chris@16: * This function is used to write data to the random-access handle. The Chris@16: * function call will block until one or more bytes of the data has been Chris@16: * written successfully, or until an error occurs. Chris@16: * Chris@16: * @param offset The offset at which the data will be written. Chris@16: * Chris@16: * @param buffers One or more data buffers to be written to the handle. Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: * Chris@16: * @returns The number of bytes written. Returns 0 if an error occurred. Chris@16: * Chris@16: * @note The write_some operation may not transmit all of the data to the Chris@16: * peer. Consider using the @ref write_at function if you need to ensure that Chris@16: * all data is written before the blocking operation completes. Chris@16: */ Chris@16: template Chris@16: std::size_t write_some_at(uint64_t offset, Chris@16: const ConstBufferSequence& buffers, boost::system::error_code& ec) Chris@16: { Chris@16: return this->get_service().write_some_at( Chris@16: this->get_implementation(), offset, buffers, ec); Chris@16: } Chris@16: Chris@16: /// Start an asynchronous write at the specified offset. Chris@16: /** Chris@16: * This function is used to asynchronously write data to the random-access Chris@16: * handle. The function call always returns immediately. Chris@16: * Chris@16: * @param offset The offset at which the data will be written. Chris@16: * Chris@16: * @param buffers One or more data buffers to be written to the handle. Chris@16: * Although the buffers object may be copied as necessary, ownership of the Chris@16: * underlying memory blocks is retained by the caller, which must guarantee Chris@16: * that they remain valid until the handler is called. Chris@16: * Chris@16: * @param handler The handler to be called when the write operation completes. Chris@16: * Copies will be made of the handler as required. The function signature of Chris@16: * the handler must be: Chris@16: * @code void handler( Chris@16: * const boost::system::error_code& error, // Result of operation. Chris@16: * std::size_t bytes_transferred // Number of bytes written. Chris@16: * ); @endcode Chris@16: * Regardless of whether the asynchronous operation completes immediately or Chris@16: * not, the handler will not be invoked from within this function. Invocation Chris@16: * of the handler will be performed in a manner equivalent to using Chris@16: * boost::asio::io_service::post(). Chris@16: * Chris@16: * @note The write operation may not transmit all of the data to the peer. Chris@16: * Consider using the @ref async_write_at function if you need to ensure that Chris@16: * all data is written before the asynchronous operation completes. Chris@16: * Chris@16: * @par Example Chris@16: * To write a single data buffer use the @ref buffer function as follows: Chris@16: * @code Chris@16: * handle.async_write_some_at(42, boost::asio::buffer(data, size), handler); Chris@16: * @endcode Chris@16: * See the @ref buffer documentation for information on writing multiple Chris@16: * buffers in one go, and how to use it with arrays, boost::array or Chris@16: * std::vector. Chris@16: */ Chris@16: template Chris@16: BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler, Chris@16: void (boost::system::error_code, std::size_t)) Chris@16: async_write_some_at(uint64_t offset, Chris@16: const ConstBufferSequence& buffers, Chris@16: BOOST_ASIO_MOVE_ARG(WriteHandler) handler) Chris@16: { Chris@16: // If you get an error on the following line it means that your handler does Chris@16: // not meet the documented type requirements for a WriteHandler. Chris@16: BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check; Chris@16: Chris@16: return this->get_service().async_write_some_at(this->get_implementation(), Chris@16: offset, buffers, BOOST_ASIO_MOVE_CAST(WriteHandler)(handler)); Chris@16: } Chris@16: Chris@16: /// Read some data from the handle at the specified offset. Chris@16: /** Chris@16: * This function is used to read data from the random-access handle. The Chris@16: * function call will block until one or more bytes of data has been read Chris@16: * successfully, or until an error occurs. Chris@16: * Chris@16: * @param offset The offset at which the data will be read. Chris@16: * Chris@16: * @param buffers One or more buffers into which the data will be read. Chris@16: * Chris@16: * @returns The number of bytes read. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. An error code of Chris@16: * boost::asio::error::eof indicates that the connection was closed by the Chris@16: * peer. Chris@16: * Chris@16: * @note The read_some operation may not read all of the requested number of Chris@16: * bytes. Consider using the @ref read_at function if you need to ensure that Chris@16: * the requested amount of data is read before the blocking operation Chris@16: * completes. Chris@16: * Chris@16: * @par Example Chris@16: * To read into a single data buffer use the @ref buffer function as follows: Chris@16: * @code Chris@16: * handle.read_some_at(42, boost::asio::buffer(data, size)); Chris@16: * @endcode Chris@16: * See the @ref buffer documentation for information on reading into multiple Chris@16: * buffers in one go, and how to use it with arrays, boost::array or Chris@16: * std::vector. Chris@16: */ Chris@16: template Chris@16: std::size_t read_some_at(uint64_t offset, Chris@16: const MutableBufferSequence& buffers) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: std::size_t s = this->get_service().read_some_at( Chris@16: this->get_implementation(), offset, buffers, ec); Chris@16: boost::asio::detail::throw_error(ec, "read_some_at"); Chris@16: return s; Chris@16: } Chris@16: Chris@16: /// Read some data from the handle at the specified offset. Chris@16: /** Chris@16: * This function is used to read data from the random-access handle. The Chris@16: * function call will block until one or more bytes of data has been read Chris@16: * successfully, or until an error occurs. Chris@16: * Chris@16: * @param offset The offset at which the data will be read. Chris@16: * Chris@16: * @param buffers One or more buffers into which the data will be read. Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: * Chris@16: * @returns The number of bytes read. Returns 0 if an error occurred. Chris@16: * Chris@16: * @note The read_some operation may not read all of the requested number of Chris@16: * bytes. Consider using the @ref read_at function if you need to ensure that Chris@16: * the requested amount of data is read before the blocking operation Chris@16: * completes. Chris@16: */ Chris@16: template Chris@16: std::size_t read_some_at(uint64_t offset, Chris@16: const MutableBufferSequence& buffers, boost::system::error_code& ec) Chris@16: { Chris@16: return this->get_service().read_some_at( Chris@16: this->get_implementation(), offset, buffers, ec); Chris@16: } Chris@16: Chris@16: /// Start an asynchronous read at the specified offset. Chris@16: /** Chris@16: * This function is used to asynchronously read data from the random-access Chris@16: * handle. The function call always returns immediately. Chris@16: * Chris@16: * @param offset The offset at which the data will be read. Chris@16: * Chris@16: * @param buffers One or more buffers into which the data will be read. Chris@16: * Although the buffers object may be copied as necessary, ownership of the Chris@16: * underlying memory blocks is retained by the caller, which must guarantee Chris@16: * that they remain valid until the handler is called. Chris@16: * Chris@16: * @param handler The handler to be called when the read operation completes. Chris@16: * Copies will be made of the handler as required. The function signature of Chris@16: * the handler must be: Chris@16: * @code void handler( Chris@16: * const boost::system::error_code& error, // Result of operation. Chris@16: * std::size_t bytes_transferred // Number of bytes read. Chris@16: * ); @endcode Chris@16: * Regardless of whether the asynchronous operation completes immediately or Chris@16: * not, the handler will not be invoked from within this function. Invocation Chris@16: * of the handler will be performed in a manner equivalent to using Chris@16: * boost::asio::io_service::post(). Chris@16: * Chris@16: * @note The read operation may not read all of the requested number of bytes. Chris@16: * Consider using the @ref async_read_at function if you need to ensure that Chris@16: * the requested amount of data is read before the asynchronous operation Chris@16: * completes. Chris@16: * Chris@16: * @par Example Chris@16: * To read into a single data buffer use the @ref buffer function as follows: Chris@16: * @code Chris@16: * handle.async_read_some_at(42, boost::asio::buffer(data, size), handler); Chris@16: * @endcode Chris@16: * See the @ref buffer documentation for information on reading into multiple Chris@16: * buffers in one go, and how to use it with arrays, boost::array or Chris@16: * std::vector. Chris@16: */ Chris@16: template Chris@16: BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler, Chris@16: void (boost::system::error_code, std::size_t)) Chris@16: async_read_some_at(uint64_t offset, Chris@16: const MutableBufferSequence& buffers, Chris@16: BOOST_ASIO_MOVE_ARG(ReadHandler) handler) Chris@16: { Chris@16: // If you get an error on the following line it means that your handler does Chris@16: // not meet the documented type requirements for a ReadHandler. Chris@16: BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check; Chris@16: Chris@16: return this->get_service().async_read_some_at(this->get_implementation(), Chris@16: offset, buffers, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler)); Chris@16: } Chris@16: }; Chris@16: Chris@16: } // namespace windows Chris@16: } // namespace asio Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // defined(BOOST_ASIO_HAS_WINDOWS_RANDOM_ACCESS_HANDLE) Chris@16: // || defined(GENERATING_DOCUMENTATION) Chris@16: Chris@16: #endif // BOOST_ASIO_WINDOWS_BASIC_RANDOM_ACCESS_HANDLE_HPP