Chris@16: // Chris@16: // buffered_write_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_WRITE_STREAM_HPP Chris@16: #define BOOST_ASIO_BUFFERED_WRITE_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 write-related operations of a stream. Chris@16: /** Chris@16: * The buffered_write_stream class template can be used to add buffering to the Chris@16: * synchronous and asynchronous 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_write_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_write_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_write_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: /// 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: /// 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: /// 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: /// 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: /// Write the given data to the stream. Returns the number of bytes written, Chris@16: /// or 0 if an error occurred and the error handler did not throw. Chris@16: template Chris@16: std::size_t write_some(const ConstBufferSequence& buffers, Chris@16: boost::system::error_code& ec); 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: /// 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 next_layer_.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 next_layer_.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: detail::async_result_init< Chris@16: ReadHandler, void (boost::system::error_code, std::size_t)> init( Chris@16: BOOST_ASIO_MOVE_CAST(ReadHandler)(handler)); Chris@16: Chris@16: next_layer_.async_read_some(buffers, Chris@16: BOOST_ASIO_MOVE_CAST(BOOST_ASIO_HANDLER_TYPE(ReadHandler, 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: /// 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 next_layer_.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 next_layer_.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 next_layer_.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 next_layer_.in_avail(ec); Chris@16: } Chris@16: Chris@16: private: Chris@16: /// Copy data into the internal buffer from the specified source buffer. Chris@16: /// Returns the number of bytes copied. Chris@16: template Chris@16: std::size_t copy(const ConstBufferSequence& buffers); 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_WRITE_STREAM_HPP