2 // basic_socket_iostream.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11 #ifndef BOOST_ASIO_BASIC_SOCKET_IOSTREAM_HPP
12 #define BOOST_ASIO_BASIC_SOCKET_IOSTREAM_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #if !defined(BOOST_ASIO_NO_IOSTREAM)
24 #include <boost/asio/basic_socket_streambuf.hpp>
26 #if !defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
28 # include <boost/asio/detail/variadic_templates.hpp>
30 // A macro that should expand to:
31 // template <typename T1, ..., typename Tn>
32 // explicit basic_socket_iostream(T1 x1, ..., Tn xn)
33 // : std::basic_iostream<char>(
34 // &this->detail::socket_iostream_base<
35 // Protocol, Clock, WaitTraits>::streambuf_)
37 // if (rdbuf()->connect(x1, ..., xn) == 0)
38 // this->setstate(std::ios_base::failbit);
40 // This macro should only persist within this file.
42 # define BOOST_ASIO_PRIVATE_CTR_DEF(n) \
43 template <BOOST_ASIO_VARIADIC_TPARAMS(n)> \
44 explicit basic_socket_iostream(BOOST_ASIO_VARIADIC_BYVAL_PARAMS(n)) \
45 : std::basic_iostream<char>( \
46 &this->detail::socket_iostream_base< \
47 Protocol, Clock, WaitTraits>::streambuf_) \
49 this->setf(std::ios_base::unitbuf); \
50 if (rdbuf()->connect(BOOST_ASIO_VARIADIC_BYVAL_ARGS(n)) == 0) \
51 this->setstate(std::ios_base::failbit); \
55 // A macro that should expand to:
56 // template <typename T1, ..., typename Tn>
57 // void connect(T1 x1, ..., Tn xn)
59 // if (rdbuf()->connect(x1, ..., xn) == 0)
60 // this->setstate(std::ios_base::failbit);
62 // This macro should only persist within this file.
64 # define BOOST_ASIO_PRIVATE_CONNECT_DEF(n) \
65 template <BOOST_ASIO_VARIADIC_TPARAMS(n)> \
66 void connect(BOOST_ASIO_VARIADIC_BYVAL_PARAMS(n)) \
68 if (rdbuf()->connect(BOOST_ASIO_VARIADIC_BYVAL_ARGS(n)) == 0) \
69 this->setstate(std::ios_base::failbit); \
73 #endif // !defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
75 #include <boost/asio/detail/push_options.hpp>
81 // A separate base class is used to ensure that the streambuf is initialised
82 // prior to the basic_socket_iostream's basic_iostream base class.
83 template <typename Protocol, typename Clock, typename WaitTraits>
84 class socket_iostream_base
87 socket_iostream_base()
91 #if defined(BOOST_ASIO_HAS_MOVE)
92 socket_iostream_base(socket_iostream_base&& other)
93 : streambuf_(std::move(other.streambuf_))
97 socket_iostream_base(basic_stream_socket<Protocol> s)
98 : streambuf_(std::move(s))
102 socket_iostream_base& operator=(socket_iostream_base&& other)
104 streambuf_ = std::move(other.streambuf_);
107 #endif // defined(BOOST_ASIO_HAS_MOVE)
109 basic_socket_streambuf<Protocol, Clock, WaitTraits> streambuf_;
112 } // namespace detail
114 #if !defined(BOOST_ASIO_BASIC_SOCKET_IOSTREAM_FWD_DECL)
115 #define BOOST_ASIO_BASIC_SOCKET_IOSTREAM_FWD_DECL
117 // Forward declaration with defaulted arguments.
118 template <typename Protocol,
119 #if defined(BOOST_ASIO_HAS_BOOST_DATE_TIME) \
120 && defined(BOOST_ASIO_USE_BOOST_DATE_TIME_FOR_SOCKET_IOSTREAM)
121 typename Clock = boost::posix_time::ptime,
122 typename WaitTraits = time_traits<Clock> >
123 #else // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
124 // && defined(BOOST_ASIO_USE_BOOST_DATE_TIME_FOR_SOCKET_IOSTREAM)
125 typename Clock = chrono::steady_clock,
126 typename WaitTraits = wait_traits<Clock> >
127 #endif // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
128 // && defined(BOOST_ASIO_USE_BOOST_DATE_TIME_FOR_SOCKET_IOSTREAM)
129 class basic_socket_iostream;
131 #endif // !defined(BOOST_ASIO_BASIC_SOCKET_IOSTREAM_FWD_DECL)
133 /// Iostream interface for a socket.
134 #if defined(GENERATING_DOCUMENTATION)
135 template <typename Protocol,
136 typename Clock = chrono::steady_clock,
137 typename WaitTraits = wait_traits<Clock> >
138 #else // defined(GENERATING_DOCUMENTATION)
139 template <typename Protocol, typename Clock, typename WaitTraits>
140 #endif // defined(GENERATING_DOCUMENTATION)
141 class basic_socket_iostream
142 : private detail::socket_iostream_base<Protocol, Clock, WaitTraits>,
143 public std::basic_iostream<char>
146 // These typedefs are intended keep this class's implementation independent
147 // of whether it's using Boost.DateClock, Boost.Chrono or std::chrono.
148 #if defined(BOOST_ASIO_HAS_BOOST_DATE_TIME) \
149 && defined(BOOST_ASIO_USE_BOOST_DATE_TIME_FOR_SOCKET_IOSTREAM)
150 typedef WaitTraits traits_helper;
151 #else // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
152 // && defined(BOOST_ASIO_USE_BOOST_DATE_TIME_FOR_SOCKET_IOSTREAM)
153 typedef detail::chrono_time_traits<Clock, WaitTraits> traits_helper;
154 #endif // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
155 // && defined(BOOST_ASIO_USE_BOOST_DATE_TIME_FOR_SOCKET_IOSTREAM)
158 /// The protocol type.
159 typedef Protocol protocol_type;
161 /// The endpoint type.
162 typedef typename Protocol::endpoint endpoint_type;
165 typedef Clock clock_type;
167 #if defined(GENERATING_DOCUMENTATION)
168 /// (Deprecated: Use time_point.) The time type.
169 typedef typename WaitTraits::time_type time_type;
172 typedef typename WaitTraits::time_point time_point;
174 /// (Deprecated: Use duration.) The duration type.
175 typedef typename WaitTraits::duration_type duration_type;
177 /// The duration type.
178 typedef typename WaitTraits::duration duration;
180 # if !defined(BOOST_ASIO_NO_DEPRECATED)
181 typedef typename traits_helper::time_type time_type;
182 typedef typename traits_helper::duration_type duration_type;
183 # endif // !defined(BOOST_ASIO_NO_DEPRECATED)
184 typedef typename traits_helper::time_type time_point;
185 typedef typename traits_helper::duration_type duration;
188 /// Construct a basic_socket_iostream without establishing a connection.
189 basic_socket_iostream()
190 : std::basic_iostream<char>(
191 &this->detail::socket_iostream_base<
192 Protocol, Clock, WaitTraits>::streambuf_)
194 this->setf(std::ios_base::unitbuf);
197 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
198 /// Construct a basic_socket_iostream from the supplied socket.
199 explicit basic_socket_iostream(basic_stream_socket<protocol_type> s)
200 : detail::socket_iostream_base<
201 Protocol, Clock, WaitTraits>(std::move(s)),
202 std::basic_iostream<char>(
203 &this->detail::socket_iostream_base<
204 Protocol, Clock, WaitTraits>::streambuf_)
206 this->setf(std::ios_base::unitbuf);
209 #if defined(BOOST_ASIO_HAS_STD_IOSTREAM_MOVE) \
210 || defined(GENERATING_DOCUMENTATION)
211 /// Move-construct a basic_socket_iostream from another.
212 basic_socket_iostream(basic_socket_iostream&& other)
213 : detail::socket_iostream_base<
214 Protocol, Clock, WaitTraits>(std::move(other)),
215 std::basic_iostream<char>(std::move(other))
217 this->set_rdbuf(&this->detail::socket_iostream_base<
218 Protocol, Clock, WaitTraits>::streambuf_);
221 /// Move-assign a basic_socket_iostream from another.
222 basic_socket_iostream& operator=(basic_socket_iostream&& other)
224 std::basic_iostream<char>::operator=(std::move(other));
225 detail::socket_iostream_base<
226 Protocol, Clock, WaitTraits>::operator=(std::move(other));
229 #endif // defined(BOOST_ASIO_HAS_STD_IOSTREAM_MOVE)
230 // || defined(GENERATING_DOCUMENTATION)
231 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
233 #if defined(GENERATING_DOCUMENTATION)
234 /// Establish a connection to an endpoint corresponding to a resolver query.
236 * This constructor automatically establishes a connection based on the
237 * supplied resolver query parameters. The arguments are used to construct
238 * a resolver query object.
240 template <typename T1, ..., typename TN>
241 explicit basic_socket_iostream(T1 t1, ..., TN tn);
242 #elif defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
243 template <typename... T>
244 explicit basic_socket_iostream(T... x)
245 : std::basic_iostream<char>(
246 &this->detail::socket_iostream_base<
247 Protocol, Clock, WaitTraits>::streambuf_)
249 this->setf(std::ios_base::unitbuf);
250 if (rdbuf()->connect(x...) == 0)
251 this->setstate(std::ios_base::failbit);
254 BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_CTR_DEF)
257 #if defined(GENERATING_DOCUMENTATION)
258 /// Establish a connection to an endpoint corresponding to a resolver query.
260 * This function automatically establishes a connection based on the supplied
261 * resolver query parameters. The arguments are used to construct a resolver
264 template <typename T1, ..., typename TN>
265 void connect(T1 t1, ..., TN tn);
266 #elif defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
267 template <typename... T>
270 if (rdbuf()->connect(x...) == 0)
271 this->setstate(std::ios_base::failbit);
274 BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_CONNECT_DEF)
277 /// Close the connection.
280 if (rdbuf()->close() == 0)
281 this->setstate(std::ios_base::failbit);
284 /// Return a pointer to the underlying streambuf.
285 basic_socket_streambuf<Protocol, Clock, WaitTraits>* rdbuf() const
287 return const_cast<basic_socket_streambuf<Protocol, Clock, WaitTraits>*>(
288 &this->detail::socket_iostream_base<
289 Protocol, Clock, WaitTraits>::streambuf_);
292 /// Get a reference to the underlying socket.
293 basic_socket<Protocol>& socket()
295 return rdbuf()->socket();
298 /// Get the last error associated with the stream.
300 * @return An \c error_code corresponding to the last error from the stream.
303 * To print the error associated with a failure to establish a connection:
304 * @code tcp::iostream s("www.boost.org", "http");
307 * std::cout << "Error: " << s.error().message() << std::endl;
310 const boost::system::error_code& error() const
312 return rdbuf()->error();
315 #if !defined(BOOST_ASIO_NO_DEPRECATED)
316 /// (Deprecated: Use expiry().) Get the stream's expiry time as an absolute
319 * @return An absolute time value representing the stream's expiry time.
321 time_point expires_at() const
323 return rdbuf()->expires_at();
325 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
327 /// Get the stream's expiry time as an absolute time.
329 * @return An absolute time value representing the stream's expiry time.
331 time_point expiry() const
333 return rdbuf()->expiry();
336 /// Set the stream's expiry time as an absolute time.
338 * This function sets the expiry time associated with the stream. Stream
339 * operations performed after this time (where the operations cannot be
340 * completed using the internal buffers) will fail with the error
341 * boost::asio::error::operation_aborted.
343 * @param expiry_time The expiry time to be used for the stream.
345 void expires_at(const time_point& expiry_time)
347 rdbuf()->expires_at(expiry_time);
350 /// Set the stream's expiry time relative to now.
352 * This function sets the expiry time associated with the stream. Stream
353 * operations performed after this time (where the operations cannot be
354 * completed using the internal buffers) will fail with the error
355 * boost::asio::error::operation_aborted.
357 * @param expiry_time The expiry time to be used for the timer.
359 void expires_after(const duration& expiry_time)
361 rdbuf()->expires_after(expiry_time);
364 #if !defined(BOOST_ASIO_NO_DEPRECATED)
365 /// (Deprecated: Use expiry().) Get the stream's expiry time relative to now.
367 * @return A relative time value representing the stream's expiry time.
369 duration expires_from_now() const
371 return rdbuf()->expires_from_now();
374 /// (Deprecated: Use expires_after().) Set the stream's expiry time relative
377 * This function sets the expiry time associated with the stream. Stream
378 * operations performed after this time (where the operations cannot be
379 * completed using the internal buffers) will fail with the error
380 * boost::asio::error::operation_aborted.
382 * @param expiry_time The expiry time to be used for the timer.
384 void expires_from_now(const duration& expiry_time)
386 rdbuf()->expires_from_now(expiry_time);
388 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
391 // Disallow copying and assignment.
392 basic_socket_iostream(const basic_socket_iostream&) BOOST_ASIO_DELETED;
393 basic_socket_iostream& operator=(
394 const basic_socket_iostream&) BOOST_ASIO_DELETED;
400 #include <boost/asio/detail/pop_options.hpp>
402 #if !defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
403 # undef BOOST_ASIO_PRIVATE_CTR_DEF
404 # undef BOOST_ASIO_PRIVATE_CONNECT_DEF
405 #endif // !defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
407 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
409 #endif // BOOST_ASIO_BASIC_SOCKET_IOSTREAM_HPP