Chris@16: // Chris@16: // buffered_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_STREAM_HPP Chris@16: #define BOOST_ASIO_BUFFERED_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: Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace asio { Chris@16: Chris@16: /// Adds buffering to the read- and write-related operations of a stream. Chris@16: /** Chris@16: * The buffered_stream class template can be used to add buffering to the Chris@16: * synchronous and asynchronous read and write 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_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: /// Construct, passing the specified argument to initialise the next layer. Chris@16: template Chris@16: explicit buffered_stream(Arg& a) Chris@16: : inner_stream_impl_(a), Chris@16: stream_impl_(inner_stream_impl_) Chris@16: { Chris@16: } Chris@16: Chris@16: /// Construct, passing the specified argument to initialise the next layer. Chris@16: template Chris@16: explicit buffered_stream(Arg& a, std::size_t read_buffer_size, Chris@16: std::size_t write_buffer_size) Chris@16: : inner_stream_impl_(a, write_buffer_size), Chris@16: stream_impl_(inner_stream_impl_, read_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 stream_impl_.next_layer().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 stream_impl_.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 stream_impl_.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 stream_impl_.get_io_service(); Chris@16: } Chris@16: Chris@16: /// Close the stream. Chris@16: void close() Chris@16: { Chris@16: stream_impl_.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 stream_impl_.close(ec); Chris@16: } Chris@16: Chris@16: /// Flush all data from the buffer to the next layer. Returns the number of Chris@16: /// bytes written to the next layer on the last write operation. Throws an Chris@16: /// exception on failure. Chris@16: std::size_t flush() Chris@16: { Chris@16: return stream_impl_.next_layer().flush(); Chris@16: } Chris@16: Chris@16: /// Flush all data from the buffer to the next layer. Returns the number of Chris@16: /// bytes written to the next layer on the last write operation, or 0 if an Chris@16: /// error occurred. Chris@16: std::size_t flush(boost::system::error_code& ec) Chris@16: { Chris@16: return stream_impl_.next_layer().flush(ec); Chris@16: } Chris@16: Chris@16: /// Start an asynchronous flush. Chris@16: template Chris@16: BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler, Chris@16: void (boost::system::error_code, std::size_t)) Chris@16: async_flush(BOOST_ASIO_MOVE_ARG(WriteHandler) handler) Chris@16: { Chris@16: return stream_impl_.next_layer().async_flush( Chris@16: BOOST_ASIO_MOVE_CAST(WriteHandler)(handler)); 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 stream_impl_.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 stream_impl_.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: return stream_impl_.async_write_some(buffers, Chris@16: BOOST_ASIO_MOVE_CAST(WriteHandler)(handler)); 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: return stream_impl_.fill(); 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, or 0 if an error occurred. Chris@16: std::size_t fill(boost::system::error_code& ec) Chris@16: { Chris@16: return stream_impl_.fill(ec); Chris@16: } 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: return stream_impl_.async_fill(BOOST_ASIO_MOVE_CAST(ReadHandler)(handler)); Chris@16: } 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: return stream_impl_.read_some(buffers); Chris@16: } 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: return stream_impl_.read_some(buffers, ec); Chris@16: } 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: return stream_impl_.async_read_some(buffers, Chris@16: BOOST_ASIO_MOVE_CAST(ReadHandler)(handler)); Chris@16: } 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: return stream_impl_.peek(buffers); Chris@16: } 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: return stream_impl_.peek(buffers, ec); Chris@16: } 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 stream_impl_.in_avail(); 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: return stream_impl_.in_avail(ec); Chris@16: } Chris@16: Chris@16: private: Chris@16: // The buffered write stream. Chris@16: typedef buffered_write_stream write_stream_type; Chris@16: write_stream_type inner_stream_impl_; Chris@16: Chris@16: // The buffered read stream. Chris@16: typedef buffered_read_stream read_stream_type; Chris@16: read_stream_type stream_impl_; Chris@16: }; Chris@16: Chris@16: } // namespace asio Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_ASIO_BUFFERED_STREAM_HPP