Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:33:43

0001 //
0002 // buffered_read_stream.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 
0011 #ifndef BOOST_ASIO_BUFFERED_READ_STREAM_HPP
0012 #define BOOST_ASIO_BUFFERED_READ_STREAM_HPP
0013 
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0017 
0018 #include <boost/asio/detail/config.hpp>
0019 #include <cstddef>
0020 #include <boost/asio/async_result.hpp>
0021 #include <boost/asio/buffered_read_stream_fwd.hpp>
0022 #include <boost/asio/buffer.hpp>
0023 #include <boost/asio/detail/bind_handler.hpp>
0024 #include <boost/asio/detail/buffer_resize_guard.hpp>
0025 #include <boost/asio/detail/buffered_stream_storage.hpp>
0026 #include <boost/asio/detail/noncopyable.hpp>
0027 #include <boost/asio/detail/type_traits.hpp>
0028 #include <boost/asio/error.hpp>
0029 
0030 #include <boost/asio/detail/push_options.hpp>
0031 
0032 namespace boost {
0033 namespace asio {
0034 namespace detail {
0035 
0036 template <typename> class initiate_async_buffered_fill;
0037 template <typename> class initiate_async_buffered_read_some;
0038 
0039 } // namespace detail
0040 
0041 /// Adds buffering to the read-related operations of a stream.
0042 /**
0043  * The buffered_read_stream class template can be used to add buffering to the
0044  * synchronous and asynchronous read operations of a stream.
0045  *
0046  * @par Thread Safety
0047  * @e Distinct @e objects: Safe.@n
0048  * @e Shared @e objects: Unsafe.
0049  *
0050  * @par Concepts:
0051  * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream.
0052  */
0053 template <typename Stream>
0054 class buffered_read_stream
0055   : private noncopyable
0056 {
0057 public:
0058   /// The type of the next layer.
0059   typedef remove_reference_t<Stream> next_layer_type;
0060 
0061   /// The type of the lowest layer.
0062   typedef typename next_layer_type::lowest_layer_type lowest_layer_type;
0063 
0064   /// The type of the executor associated with the object.
0065   typedef typename lowest_layer_type::executor_type executor_type;
0066 
0067 #if defined(GENERATING_DOCUMENTATION)
0068   /// The default buffer size.
0069   static const std::size_t default_buffer_size = implementation_defined;
0070 #else
0071   BOOST_ASIO_STATIC_CONSTANT(std::size_t, default_buffer_size = 1024);
0072 #endif
0073 
0074   /// Construct, passing the specified argument to initialise the next layer.
0075   template <typename Arg>
0076   explicit buffered_read_stream(Arg&& a)
0077     : next_layer_(static_cast<Arg&&>(a)),
0078       storage_(default_buffer_size)
0079   {
0080   }
0081 
0082   /// Construct, passing the specified argument to initialise the next layer.
0083   template <typename Arg>
0084   buffered_read_stream(Arg&& a,
0085       std::size_t buffer_size)
0086     : next_layer_(static_cast<Arg&&>(a)),
0087       storage_(buffer_size)
0088   {
0089   }
0090 
0091   /// Get a reference to the next layer.
0092   next_layer_type& next_layer()
0093   {
0094     return next_layer_;
0095   }
0096 
0097   /// Get a reference to the lowest layer.
0098   lowest_layer_type& lowest_layer()
0099   {
0100     return next_layer_.lowest_layer();
0101   }
0102 
0103   /// Get a const reference to the lowest layer.
0104   const lowest_layer_type& lowest_layer() const
0105   {
0106     return next_layer_.lowest_layer();
0107   }
0108 
0109   /// Get the executor associated with the object.
0110   executor_type get_executor() noexcept
0111   {
0112     return next_layer_.lowest_layer().get_executor();
0113   }
0114 
0115   /// Close the stream.
0116   void close()
0117   {
0118     next_layer_.close();
0119   }
0120 
0121   /// Close the stream.
0122   BOOST_ASIO_SYNC_OP_VOID close(boost::system::error_code& ec)
0123   {
0124     next_layer_.close(ec);
0125     BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
0126   }
0127 
0128   /// Write the given data to the stream. Returns the number of bytes written.
0129   /// Throws an exception on failure.
0130   template <typename ConstBufferSequence>
0131   std::size_t write_some(const ConstBufferSequence& buffers)
0132   {
0133     return next_layer_.write_some(buffers);
0134   }
0135 
0136   /// Write the given data to the stream. Returns the number of bytes written,
0137   /// or 0 if an error occurred.
0138   template <typename ConstBufferSequence>
0139   std::size_t write_some(const ConstBufferSequence& buffers,
0140       boost::system::error_code& ec)
0141   {
0142     return next_layer_.write_some(buffers, ec);
0143   }
0144 
0145   /// Start an asynchronous write. The data being written must be valid for the
0146   /// lifetime of the asynchronous operation.
0147   /**
0148    * @par Completion Signature
0149    * @code void(boost::system::error_code, std::size_t) @endcode
0150    */
0151   template <typename ConstBufferSequence,
0152       BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
0153         std::size_t)) WriteHandler = default_completion_token_t<executor_type>>
0154   auto async_write_some(const ConstBufferSequence& buffers,
0155       WriteHandler&& handler = default_completion_token_t<executor_type>())
0156     -> decltype(
0157       declval<conditional_t<true, Stream&, WriteHandler>>().async_write_some(
0158         buffers, static_cast<WriteHandler&&>(handler)))
0159   {
0160     return next_layer_.async_write_some(buffers,
0161         static_cast<WriteHandler&&>(handler));
0162   }
0163 
0164   /// Fill the buffer with some data. Returns the number of bytes placed in the
0165   /// buffer as a result of the operation. Throws an exception on failure.
0166   std::size_t fill();
0167 
0168   /// Fill the buffer with some data. Returns the number of bytes placed in the
0169   /// buffer as a result of the operation, or 0 if an error occurred.
0170   std::size_t fill(boost::system::error_code& ec);
0171 
0172   /// Start an asynchronous fill.
0173   /**
0174    * @par Completion Signature
0175    * @code void(boost::system::error_code, std::size_t) @endcode
0176    */
0177   template <
0178       BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
0179         std::size_t)) ReadHandler = default_completion_token_t<executor_type>>
0180   auto async_fill(
0181       ReadHandler&& handler = default_completion_token_t<executor_type>())
0182     -> decltype(
0183       async_initiate<ReadHandler,
0184         void (boost::system::error_code, std::size_t)>(
0185           declval<detail::initiate_async_buffered_fill<Stream>>(),
0186           handler, declval<detail::buffered_stream_storage*>()));
0187 
0188   /// Read some data from the stream. Returns the number of bytes read. Throws
0189   /// an exception on failure.
0190   template <typename MutableBufferSequence>
0191   std::size_t read_some(const MutableBufferSequence& buffers);
0192 
0193   /// Read some data from the stream. Returns the number of bytes read or 0 if
0194   /// an error occurred.
0195   template <typename MutableBufferSequence>
0196   std::size_t read_some(const MutableBufferSequence& buffers,
0197       boost::system::error_code& ec);
0198 
0199   /// Start an asynchronous read. The buffer into which the data will be read
0200   /// must be valid for the lifetime of the asynchronous operation.
0201   /**
0202    * @par Completion Signature
0203    * @code void(boost::system::error_code, std::size_t) @endcode
0204    */
0205   template <typename MutableBufferSequence,
0206       BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
0207         std::size_t)) ReadHandler = default_completion_token_t<executor_type>>
0208   auto async_read_some(const MutableBufferSequence& buffers,
0209       ReadHandler&& handler = default_completion_token_t<executor_type>())
0210     -> decltype(
0211       async_initiate<ReadHandler,
0212         void (boost::system::error_code, std::size_t)>(
0213           declval<detail::initiate_async_buffered_read_some<Stream>>(),
0214           handler, declval<detail::buffered_stream_storage*>(), buffers));
0215 
0216   /// Peek at the incoming data on the stream. Returns the number of bytes read.
0217   /// Throws an exception on failure.
0218   template <typename MutableBufferSequence>
0219   std::size_t peek(const MutableBufferSequence& buffers);
0220 
0221   /// Peek at the incoming data on the stream. Returns the number of bytes read,
0222   /// or 0 if an error occurred.
0223   template <typename MutableBufferSequence>
0224   std::size_t peek(const MutableBufferSequence& buffers,
0225       boost::system::error_code& ec);
0226 
0227   /// Determine the amount of data that may be read without blocking.
0228   std::size_t in_avail()
0229   {
0230     return storage_.size();
0231   }
0232 
0233   /// Determine the amount of data that may be read without blocking.
0234   std::size_t in_avail(boost::system::error_code& ec)
0235   {
0236     ec = boost::system::error_code();
0237     return storage_.size();
0238   }
0239 
0240 private:
0241   /// Copy data out of the internal buffer to the specified target buffer.
0242   /// Returns the number of bytes copied.
0243   template <typename MutableBufferSequence>
0244   std::size_t copy(const MutableBufferSequence& buffers)
0245   {
0246     std::size_t bytes_copied = boost::asio::buffer_copy(
0247         buffers, storage_.data(), storage_.size());
0248     storage_.consume(bytes_copied);
0249     return bytes_copied;
0250   }
0251 
0252   /// Copy data from the internal buffer to the specified target buffer, without
0253   /// removing the data from the internal buffer. Returns the number of bytes
0254   /// copied.
0255   template <typename MutableBufferSequence>
0256   std::size_t peek_copy(const MutableBufferSequence& buffers)
0257   {
0258     return boost::asio::buffer_copy(buffers, storage_.data(), storage_.size());
0259   }
0260 
0261   /// The next layer.
0262   Stream next_layer_;
0263 
0264   // The data in the buffer.
0265   detail::buffered_stream_storage storage_;
0266 };
0267 
0268 } // namespace asio
0269 } // namespace boost
0270 
0271 #include <boost/asio/detail/pop_options.hpp>
0272 
0273 #include <boost/asio/impl/buffered_read_stream.hpp>
0274 
0275 #endif // BOOST_ASIO_BUFFERED_READ_STREAM_HPP