Chris@16: // Chris@16: // buffered_read_stream.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_BUFFERED_READ_STREAM_HPP Chris@16: #define BOOST_ASIO_BUFFERED_READ_STREAM_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: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include 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: Chris@16: /// Adds buffering to the read-related operations of a stream. Chris@16: /** Chris@16: * The buffered_read_stream class template can be used to add buffering to the Chris@16: * synchronous and asynchronous read operations of a stream. 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: * @par Concepts: Chris@16: * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream. Chris@16: */ Chris@16: template Chris@16: class buffered_read_stream Chris@16: : private noncopyable Chris@16: { Chris@16: public: Chris@16: /// The type of the next layer. Chris@16: typedef typename remove_reference::type next_layer_type; Chris@16: Chris@16: /// The type of the lowest layer. Chris@16: typedef typename next_layer_type::lowest_layer_type lowest_layer_type; Chris@16: Chris@16: #if defined(GENERATING_DOCUMENTATION) Chris@16: /// The default buffer size. Chris@16: static const std::size_t default_buffer_size = implementation_defined; Chris@16: #else Chris@16: BOOST_ASIO_STATIC_CONSTANT(std::size_t, default_buffer_size = 1024); Chris@16: #endif Chris@16: Chris@16: /// Construct, passing the specified argument to initialise the next layer. Chris@16: template Chris@16: explicit buffered_read_stream(Arg& a) Chris@16: : next_layer_(a), Chris@16: storage_(default_buffer_size) Chris@16: { Chris@16: } Chris@16: Chris@16: /// Construct, passing the specified argument to initialise the next layer. Chris@16: template Chris@16: buffered_read_stream(Arg& a, std::size_t buffer_size) Chris@16: : next_layer_(a), Chris@16: storage_(buffer_size) Chris@16: { Chris@16: } Chris@16: Chris@16: /// Get a reference to the next layer. Chris@16: next_layer_type& next_layer() Chris@16: { Chris@16: return next_layer_; Chris@16: } Chris@16: Chris@16: /// Get a reference to the lowest layer. Chris@16: lowest_layer_type& lowest_layer() Chris@16: { Chris@16: return next_layer_.lowest_layer(); Chris@16: } Chris@16: Chris@16: /// Get a const reference to the lowest layer. Chris@16: const lowest_layer_type& lowest_layer() const Chris@16: { Chris@16: return next_layer_.lowest_layer(); Chris@16: } Chris@16: Chris@16: /// Get the io_service associated with the object. Chris@16: boost::asio::io_service& get_io_service() Chris@16: { Chris@16: return next_layer_.get_io_service(); Chris@16: } Chris@16: Chris@16: /// Close the stream. Chris@16: void close() Chris@16: { Chris@16: next_layer_.close(); Chris@16: } Chris@16: Chris@16: /// Close the stream. Chris@16: boost::system::error_code close(boost::system::error_code& ec) Chris@16: { Chris@16: return next_layer_.close(ec); Chris@16: } Chris@16: Chris@16: /// Write the given data to the stream. Returns the number of bytes written. Chris@16: /// Throws an exception on failure. Chris@16: template Chris@16: std::size_t write_some(const ConstBufferSequence& buffers) Chris@16: { Chris@16: return next_layer_.write_some(buffers); Chris@16: } Chris@16: Chris@16: /// Write the given data to the stream. Returns the number of bytes written, Chris@16: /// or 0 if an error occurred. Chris@16: template Chris@16: std::size_t write_some(const ConstBufferSequence& buffers, Chris@16: boost::system::error_code& ec) Chris@16: { Chris@16: return next_layer_.write_some(buffers, ec); Chris@16: } Chris@16: Chris@16: /// Start an asynchronous write. The data being written must be valid for the Chris@16: /// lifetime of the asynchronous operation. 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(const ConstBufferSequence& buffers, Chris@16: BOOST_ASIO_MOVE_ARG(WriteHandler) handler) Chris@16: { Chris@16: detail::async_result_init< Chris@16: WriteHandler, void (boost::system::error_code, std::size_t)> init( Chris@16: BOOST_ASIO_MOVE_CAST(WriteHandler)(handler)); Chris@16: Chris@16: next_layer_.async_write_some(buffers, Chris@16: BOOST_ASIO_MOVE_CAST(BOOST_ASIO_HANDLER_TYPE(WriteHandler, Chris@16: void (boost::system::error_code, std::size_t)))(init.handler)); Chris@16: Chris@16: return init.result.get(); Chris@16: } Chris@16: Chris@16: /// Fill the buffer with some data. Returns the number of bytes placed in the Chris@16: /// buffer as a result of the operation. Throws an exception on failure. Chris@16: std::size_t fill(); Chris@16: Chris@16: /// Fill the buffer with some data. Returns the number of bytes placed in the Chris@16: /// buffer as a result of the operation, or 0 if an error occurred. Chris@16: std::size_t fill(boost::system::error_code& ec); Chris@16: Chris@16: /// Start an asynchronous fill. Chris@16: template Chris@16: BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler, Chris@16: void (boost::system::error_code, std::size_t)) Chris@16: async_fill(BOOST_ASIO_MOVE_ARG(ReadHandler) handler); Chris@16: Chris@16: /// Read some data from the stream. Returns the number of bytes read. Throws Chris@16: /// an exception on failure. Chris@16: template Chris@16: std::size_t read_some(const MutableBufferSequence& buffers); Chris@16: Chris@16: /// Read some data from the stream. Returns the number of bytes read or 0 if Chris@16: /// an error occurred. Chris@16: template Chris@16: std::size_t read_some(const MutableBufferSequence& buffers, Chris@16: boost::system::error_code& ec); Chris@16: Chris@16: /// Start an asynchronous read. The buffer into which the data will be read Chris@16: /// must be valid for the lifetime of the asynchronous operation. 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(const MutableBufferSequence& buffers, Chris@16: BOOST_ASIO_MOVE_ARG(ReadHandler) handler); Chris@16: Chris@16: /// Peek at the incoming data on the stream. Returns the number of bytes read. Chris@16: /// Throws an exception on failure. Chris@16: template Chris@16: std::size_t peek(const MutableBufferSequence& buffers); Chris@16: Chris@16: /// Peek at the incoming data on the stream. Returns the number of bytes read, Chris@16: /// or 0 if an error occurred. Chris@16: template Chris@16: std::size_t peek(const MutableBufferSequence& buffers, Chris@16: boost::system::error_code& ec); Chris@16: Chris@16: /// Determine the amount of data that may be read without blocking. Chris@16: std::size_t in_avail() Chris@16: { Chris@16: return storage_.size(); Chris@16: } Chris@16: Chris@16: /// Determine the amount of data that may be read without blocking. Chris@16: std::size_t in_avail(boost::system::error_code& ec) Chris@16: { Chris@16: ec = boost::system::error_code(); Chris@16: return storage_.size(); Chris@16: } Chris@16: Chris@16: private: Chris@16: /// Copy data out of the internal buffer to the specified target buffer. Chris@16: /// Returns the number of bytes copied. Chris@16: template Chris@16: std::size_t copy(const MutableBufferSequence& buffers) Chris@16: { Chris@16: std::size_t bytes_copied = boost::asio::buffer_copy( Chris@16: buffers, storage_.data(), storage_.size()); Chris@16: storage_.consume(bytes_copied); Chris@16: return bytes_copied; Chris@16: } Chris@16: Chris@16: /// Copy data from the internal buffer to the specified target buffer, without Chris@16: /// removing the data from the internal buffer. Returns the number of bytes Chris@16: /// copied. Chris@16: template Chris@16: std::size_t peek_copy(const MutableBufferSequence& buffers) Chris@16: { Chris@16: return boost::asio::buffer_copy(buffers, storage_.data(), storage_.size()); Chris@16: } Chris@16: Chris@16: /// The next layer. Chris@16: Stream next_layer_; Chris@16: Chris@16: // The data in the buffer. Chris@16: detail::buffered_stream_storage storage_; Chris@16: }; Chris@16: Chris@16: } // namespace asio Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_ASIO_BUFFERED_READ_STREAM_HPP