Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:06

0001 //
0002 // basic_socket_iostream.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_BASIC_SOCKET_IOSTREAM_HPP
0012 #define BOOST_ASIO_BASIC_SOCKET_IOSTREAM_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 
0020 #if !defined(BOOST_ASIO_NO_IOSTREAM)
0021 
0022 #include <istream>
0023 #include <ostream>
0024 #include <boost/asio/basic_socket_streambuf.hpp>
0025 
0026 #include <boost/asio/detail/push_options.hpp>
0027 
0028 namespace boost {
0029 namespace asio {
0030 namespace detail {
0031 
0032 // A separate base class is used to ensure that the streambuf is initialised
0033 // prior to the basic_socket_iostream's basic_iostream base class.
0034 template <typename Protocol, typename Clock, typename WaitTraits>
0035 class socket_iostream_base
0036 {
0037 protected:
0038   socket_iostream_base()
0039   {
0040   }
0041 
0042   socket_iostream_base(socket_iostream_base&& other)
0043     : streambuf_(std::move(other.streambuf_))
0044   {
0045   }
0046 
0047   socket_iostream_base(basic_stream_socket<Protocol> s)
0048     : streambuf_(std::move(s))
0049   {
0050   }
0051 
0052   socket_iostream_base& operator=(socket_iostream_base&& other)
0053   {
0054     streambuf_ = std::move(other.streambuf_);
0055     return *this;
0056   }
0057 
0058   basic_socket_streambuf<Protocol, Clock, WaitTraits> streambuf_;
0059 };
0060 
0061 } // namespace detail
0062 
0063 #if !defined(BOOST_ASIO_BASIC_SOCKET_IOSTREAM_FWD_DECL)
0064 #define BOOST_ASIO_BASIC_SOCKET_IOSTREAM_FWD_DECL
0065 
0066 // Forward declaration with defaulted arguments.
0067 template <typename Protocol,
0068 #if defined(BOOST_ASIO_HAS_BOOST_DATE_TIME) \
0069   && defined(BOOST_ASIO_USE_BOOST_DATE_TIME_FOR_SOCKET_IOSTREAM)
0070     typename Clock = boost::posix_time::ptime,
0071     typename WaitTraits = time_traits<Clock>>
0072 #else // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
0073       // && defined(BOOST_ASIO_USE_BOOST_DATE_TIME_FOR_SOCKET_IOSTREAM)
0074     typename Clock = chrono::steady_clock,
0075     typename WaitTraits = wait_traits<Clock>>
0076 #endif // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
0077        // && defined(BOOST_ASIO_USE_BOOST_DATE_TIME_FOR_SOCKET_IOSTREAM)
0078 class basic_socket_iostream;
0079 
0080 #endif // !defined(BOOST_ASIO_BASIC_SOCKET_IOSTREAM_FWD_DECL)
0081 
0082 /// Iostream interface for a socket.
0083 #if defined(GENERATING_DOCUMENTATION)
0084 template <typename Protocol,
0085     typename Clock = chrono::steady_clock,
0086     typename WaitTraits = wait_traits<Clock>>
0087 #else // defined(GENERATING_DOCUMENTATION)
0088 template <typename Protocol, typename Clock, typename WaitTraits>
0089 #endif // defined(GENERATING_DOCUMENTATION)
0090 class basic_socket_iostream
0091   : private detail::socket_iostream_base<Protocol, Clock, WaitTraits>,
0092     public std::basic_iostream<char>
0093 {
0094 private:
0095   // These typedefs are intended keep this class's implementation independent
0096   // of whether it's using Boost.DateClock, Boost.Chrono or std::chrono.
0097 #if defined(BOOST_ASIO_HAS_BOOST_DATE_TIME) \
0098   && defined(BOOST_ASIO_USE_BOOST_DATE_TIME_FOR_SOCKET_IOSTREAM)
0099   typedef WaitTraits traits_helper;
0100 #else // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
0101       // && defined(BOOST_ASIO_USE_BOOST_DATE_TIME_FOR_SOCKET_IOSTREAM)
0102   typedef detail::chrono_time_traits<Clock, WaitTraits> traits_helper;
0103 #endif // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
0104        // && defined(BOOST_ASIO_USE_BOOST_DATE_TIME_FOR_SOCKET_IOSTREAM)
0105 
0106 public:
0107   /// The protocol type.
0108   typedef Protocol protocol_type;
0109 
0110   /// The endpoint type.
0111   typedef typename Protocol::endpoint endpoint_type;
0112 
0113   /// The clock type.
0114   typedef Clock clock_type;
0115 
0116 #if defined(GENERATING_DOCUMENTATION)
0117   /// (Deprecated: Use time_point.) The time type.
0118   typedef typename WaitTraits::time_type time_type;
0119 
0120   /// The time type.
0121   typedef typename WaitTraits::time_point time_point;
0122 
0123   /// (Deprecated: Use duration.) The duration type.
0124   typedef typename WaitTraits::duration_type duration_type;
0125 
0126   /// The duration type.
0127   typedef typename WaitTraits::duration duration;
0128 #else
0129 # if !defined(BOOST_ASIO_NO_DEPRECATED)
0130   typedef typename traits_helper::time_type time_type;
0131   typedef typename traits_helper::duration_type duration_type;
0132 # endif // !defined(BOOST_ASIO_NO_DEPRECATED)
0133   typedef typename traits_helper::time_type time_point;
0134   typedef typename traits_helper::duration_type duration;
0135 #endif
0136 
0137   /// Construct a basic_socket_iostream without establishing a connection.
0138   basic_socket_iostream()
0139     : std::basic_iostream<char>(
0140         &this->detail::socket_iostream_base<
0141           Protocol, Clock, WaitTraits>::streambuf_)
0142   {
0143     this->setf(std::ios_base::unitbuf);
0144   }
0145 
0146   /// Construct a basic_socket_iostream from the supplied socket.
0147   explicit basic_socket_iostream(basic_stream_socket<protocol_type> s)
0148     : detail::socket_iostream_base<
0149         Protocol, Clock, WaitTraits>(std::move(s)),
0150       std::basic_iostream<char>(
0151         &this->detail::socket_iostream_base<
0152           Protocol, Clock, WaitTraits>::streambuf_)
0153   {
0154     this->setf(std::ios_base::unitbuf);
0155   }
0156 
0157   /// Move-construct a basic_socket_iostream from another.
0158   basic_socket_iostream(basic_socket_iostream&& other)
0159     : detail::socket_iostream_base<
0160         Protocol, Clock, WaitTraits>(std::move(other)),
0161       std::basic_iostream<char>(std::move(other))
0162   {
0163     this->set_rdbuf(&this->detail::socket_iostream_base<
0164           Protocol, Clock, WaitTraits>::streambuf_);
0165   }
0166 
0167   /// Move-assign a basic_socket_iostream from another.
0168   basic_socket_iostream& operator=(basic_socket_iostream&& other)
0169   {
0170     std::basic_iostream<char>::operator=(std::move(other));
0171     detail::socket_iostream_base<
0172         Protocol, Clock, WaitTraits>::operator=(std::move(other));
0173     return *this;
0174   }
0175 
0176   /// Establish a connection to an endpoint corresponding to a resolver query.
0177   /**
0178    * This constructor automatically establishes a connection based on the
0179    * supplied resolver query parameters. The arguments are used to construct
0180    * a resolver query object.
0181    */
0182   template <typename... T>
0183   explicit basic_socket_iostream(T... x)
0184     : std::basic_iostream<char>(
0185         &this->detail::socket_iostream_base<
0186           Protocol, Clock, WaitTraits>::streambuf_)
0187   {
0188     this->setf(std::ios_base::unitbuf);
0189     if (rdbuf()->connect(x...) == 0)
0190       this->setstate(std::ios_base::failbit);
0191   }
0192 
0193   /// Establish a connection to an endpoint corresponding to a resolver query.
0194   /**
0195    * This function automatically establishes a connection based on the supplied
0196    * resolver query parameters. The arguments are used to construct a resolver
0197    * query object.
0198    */
0199   template <typename... T>
0200   void connect(T... x)
0201   {
0202     if (rdbuf()->connect(x...) == 0)
0203       this->setstate(std::ios_base::failbit);
0204   }
0205 
0206   /// Close the connection.
0207   void close()
0208   {
0209     if (rdbuf()->close() == 0)
0210       this->setstate(std::ios_base::failbit);
0211   }
0212 
0213   /// Return a pointer to the underlying streambuf.
0214   basic_socket_streambuf<Protocol, Clock, WaitTraits>* rdbuf() const
0215   {
0216     return const_cast<basic_socket_streambuf<Protocol, Clock, WaitTraits>*>(
0217         &this->detail::socket_iostream_base<
0218           Protocol, Clock, WaitTraits>::streambuf_);
0219   }
0220 
0221   /// Get a reference to the underlying socket.
0222   basic_socket<Protocol>& socket()
0223   {
0224     return rdbuf()->socket();
0225   }
0226 
0227   /// Get the last error associated with the stream.
0228   /**
0229    * @return An \c error_code corresponding to the last error from the stream.
0230    *
0231    * @par Example
0232    * To print the error associated with a failure to establish a connection:
0233    * @code tcp::iostream s("www.boost.org", "http");
0234    * if (!s)
0235    * {
0236    *   std::cout << "Error: " << s.error().message() << std::endl;
0237    * } @endcode
0238    */
0239   const boost::system::error_code& error() const
0240   {
0241     return rdbuf()->error();
0242   }
0243 
0244 #if !defined(BOOST_ASIO_NO_DEPRECATED)
0245   /// (Deprecated: Use expiry().) Get the stream's expiry time as an absolute
0246   /// time.
0247   /**
0248    * @return An absolute time value representing the stream's expiry time.
0249    */
0250   time_point expires_at() const
0251   {
0252     return rdbuf()->expires_at();
0253   }
0254 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
0255 
0256   /// Get the stream's expiry time as an absolute time.
0257   /**
0258    * @return An absolute time value representing the stream's expiry time.
0259    */
0260   time_point expiry() const
0261   {
0262     return rdbuf()->expiry();
0263   }
0264 
0265   /// Set the stream's expiry time as an absolute time.
0266   /**
0267    * This function sets the expiry time associated with the stream. Stream
0268    * operations performed after this time (where the operations cannot be
0269    * completed using the internal buffers) will fail with the error
0270    * boost::asio::error::operation_aborted.
0271    *
0272    * @param expiry_time The expiry time to be used for the stream.
0273    */
0274   void expires_at(const time_point& expiry_time)
0275   {
0276     rdbuf()->expires_at(expiry_time);
0277   }
0278 
0279   /// Set the stream's expiry time relative to now.
0280   /**
0281    * This function sets the expiry time associated with the stream. Stream
0282    * operations performed after this time (where the operations cannot be
0283    * completed using the internal buffers) will fail with the error
0284    * boost::asio::error::operation_aborted.
0285    *
0286    * @param expiry_time The expiry time to be used for the timer.
0287    */
0288   void expires_after(const duration& expiry_time)
0289   {
0290     rdbuf()->expires_after(expiry_time);
0291   }
0292 
0293 #if !defined(BOOST_ASIO_NO_DEPRECATED)
0294   /// (Deprecated: Use expiry().) Get the stream's expiry time relative to now.
0295   /**
0296    * @return A relative time value representing the stream's expiry time.
0297    */
0298   duration expires_from_now() const
0299   {
0300     return rdbuf()->expires_from_now();
0301   }
0302 
0303   /// (Deprecated: Use expires_after().) Set the stream's expiry time relative
0304   /// to now.
0305   /**
0306    * This function sets the expiry time associated with the stream. Stream
0307    * operations performed after this time (where the operations cannot be
0308    * completed using the internal buffers) will fail with the error
0309    * boost::asio::error::operation_aborted.
0310    *
0311    * @param expiry_time The expiry time to be used for the timer.
0312    */
0313   void expires_from_now(const duration& expiry_time)
0314   {
0315     rdbuf()->expires_from_now(expiry_time);
0316   }
0317 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
0318 
0319 private:
0320   // Disallow copying and assignment.
0321   basic_socket_iostream(const basic_socket_iostream&) = delete;
0322   basic_socket_iostream& operator=(
0323       const basic_socket_iostream&) = delete;
0324 };
0325 
0326 } // namespace asio
0327 } // namespace boost
0328 
0329 #include <boost/asio/detail/pop_options.hpp>
0330 
0331 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
0332 
0333 #endif // BOOST_ASIO_BASIC_SOCKET_IOSTREAM_HPP