Chris@16
|
1 //
|
Chris@16
|
2 // basic_socket_iostream.hpp
|
Chris@16
|
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~
|
Chris@16
|
4 //
|
Chris@101
|
5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
Chris@16
|
6 //
|
Chris@16
|
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
9 //
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_ASIO_BASIC_SOCKET_IOSTREAM_HPP
|
Chris@16
|
12 #define BOOST_ASIO_BASIC_SOCKET_IOSTREAM_HPP
|
Chris@16
|
13
|
Chris@16
|
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
15 # pragma once
|
Chris@16
|
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
17
|
Chris@16
|
18 #include <boost/asio/detail/config.hpp>
|
Chris@16
|
19
|
Chris@16
|
20 #if !defined(BOOST_ASIO_NO_IOSTREAM)
|
Chris@16
|
21
|
Chris@16
|
22 #include <istream>
|
Chris@16
|
23 #include <ostream>
|
Chris@16
|
24 #include <boost/asio/basic_socket_streambuf.hpp>
|
Chris@16
|
25 #include <boost/asio/stream_socket_service.hpp>
|
Chris@16
|
26
|
Chris@16
|
27 #if !defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
|
Chris@16
|
28
|
Chris@16
|
29 # include <boost/asio/detail/variadic_templates.hpp>
|
Chris@16
|
30
|
Chris@16
|
31 // A macro that should expand to:
|
Chris@16
|
32 // template <typename T1, ..., typename Tn>
|
Chris@16
|
33 // explicit basic_socket_iostream(T1 x1, ..., Tn xn)
|
Chris@16
|
34 // : std::basic_iostream<char>(
|
Chris@16
|
35 // &this->detail::socket_iostream_base<
|
Chris@16
|
36 // Protocol, StreamSocketService, Time,
|
Chris@16
|
37 // TimeTraits, TimerService>::streambuf_)
|
Chris@16
|
38 // {
|
Chris@16
|
39 // if (rdbuf()->connect(x1, ..., xn) == 0)
|
Chris@16
|
40 // this->setstate(std::ios_base::failbit);
|
Chris@16
|
41 // }
|
Chris@16
|
42 // This macro should only persist within this file.
|
Chris@16
|
43
|
Chris@16
|
44 # define BOOST_ASIO_PRIVATE_CTR_DEF(n) \
|
Chris@16
|
45 template <BOOST_ASIO_VARIADIC_TPARAMS(n)> \
|
Chris@16
|
46 explicit basic_socket_iostream(BOOST_ASIO_VARIADIC_PARAMS(n)) \
|
Chris@16
|
47 : std::basic_iostream<char>( \
|
Chris@16
|
48 &this->detail::socket_iostream_base< \
|
Chris@16
|
49 Protocol, StreamSocketService, Time, \
|
Chris@16
|
50 TimeTraits, TimerService>::streambuf_) \
|
Chris@16
|
51 { \
|
Chris@16
|
52 this->setf(std::ios_base::unitbuf); \
|
Chris@16
|
53 if (rdbuf()->connect(BOOST_ASIO_VARIADIC_ARGS(n)) == 0) \
|
Chris@16
|
54 this->setstate(std::ios_base::failbit); \
|
Chris@16
|
55 } \
|
Chris@16
|
56 /**/
|
Chris@16
|
57
|
Chris@16
|
58 // A macro that should expand to:
|
Chris@16
|
59 // template <typename T1, ..., typename Tn>
|
Chris@16
|
60 // void connect(T1 x1, ..., Tn xn)
|
Chris@16
|
61 // {
|
Chris@16
|
62 // if (rdbuf()->connect(x1, ..., xn) == 0)
|
Chris@16
|
63 // this->setstate(std::ios_base::failbit);
|
Chris@16
|
64 // }
|
Chris@16
|
65 // This macro should only persist within this file.
|
Chris@16
|
66
|
Chris@16
|
67 # define BOOST_ASIO_PRIVATE_CONNECT_DEF(n) \
|
Chris@16
|
68 template <BOOST_ASIO_VARIADIC_TPARAMS(n)> \
|
Chris@16
|
69 void connect(BOOST_ASIO_VARIADIC_PARAMS(n)) \
|
Chris@16
|
70 { \
|
Chris@16
|
71 if (rdbuf()->connect(BOOST_ASIO_VARIADIC_ARGS(n)) == 0) \
|
Chris@16
|
72 this->setstate(std::ios_base::failbit); \
|
Chris@16
|
73 } \
|
Chris@16
|
74 /**/
|
Chris@16
|
75
|
Chris@16
|
76 #endif // !defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
|
Chris@16
|
77
|
Chris@16
|
78 #include <boost/asio/detail/push_options.hpp>
|
Chris@16
|
79
|
Chris@16
|
80 namespace boost {
|
Chris@16
|
81 namespace asio {
|
Chris@16
|
82 namespace detail {
|
Chris@16
|
83
|
Chris@16
|
84 // A separate base class is used to ensure that the streambuf is initialised
|
Chris@16
|
85 // prior to the basic_socket_iostream's basic_iostream base class.
|
Chris@16
|
86 template <typename Protocol, typename StreamSocketService,
|
Chris@16
|
87 typename Time, typename TimeTraits, typename TimerService>
|
Chris@16
|
88 class socket_iostream_base
|
Chris@16
|
89 {
|
Chris@16
|
90 protected:
|
Chris@16
|
91 basic_socket_streambuf<Protocol, StreamSocketService,
|
Chris@16
|
92 Time, TimeTraits, TimerService> streambuf_;
|
Chris@16
|
93 };
|
Chris@16
|
94
|
Chris@16
|
95 }
|
Chris@16
|
96
|
Chris@16
|
97 /// Iostream interface for a socket.
|
Chris@16
|
98 template <typename Protocol,
|
Chris@16
|
99 typename StreamSocketService = stream_socket_service<Protocol>,
|
Chris@16
|
100 #if defined(BOOST_ASIO_HAS_BOOST_DATE_TIME) \
|
Chris@16
|
101 || defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
102 typename Time = boost::posix_time::ptime,
|
Chris@16
|
103 typename TimeTraits = boost::asio::time_traits<Time>,
|
Chris@16
|
104 typename TimerService = deadline_timer_service<Time, TimeTraits> >
|
Chris@16
|
105 #else
|
Chris@16
|
106 typename Time = steady_timer::clock_type,
|
Chris@16
|
107 typename TimeTraits = steady_timer::traits_type,
|
Chris@16
|
108 typename TimerService = steady_timer::service_type>
|
Chris@16
|
109 #endif
|
Chris@16
|
110 class basic_socket_iostream
|
Chris@16
|
111 : private detail::socket_iostream_base<Protocol,
|
Chris@16
|
112 StreamSocketService, Time, TimeTraits, TimerService>,
|
Chris@16
|
113 public std::basic_iostream<char>
|
Chris@16
|
114 {
|
Chris@16
|
115 private:
|
Chris@16
|
116 // These typedefs are intended keep this class's implementation independent
|
Chris@16
|
117 // of whether it's using Boost.DateTime, Boost.Chrono or std::chrono.
|
Chris@16
|
118 #if defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
|
Chris@16
|
119 typedef TimeTraits traits_helper;
|
Chris@16
|
120 #else
|
Chris@16
|
121 typedef detail::chrono_time_traits<Time, TimeTraits> traits_helper;
|
Chris@16
|
122 #endif
|
Chris@16
|
123
|
Chris@16
|
124 public:
|
Chris@16
|
125 /// The endpoint type.
|
Chris@16
|
126 typedef typename Protocol::endpoint endpoint_type;
|
Chris@16
|
127
|
Chris@16
|
128 #if defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
129 /// The time type.
|
Chris@16
|
130 typedef typename TimeTraits::time_type time_type;
|
Chris@16
|
131
|
Chris@16
|
132 /// The duration type.
|
Chris@16
|
133 typedef typename TimeTraits::duration_type duration_type;
|
Chris@16
|
134 #else
|
Chris@16
|
135 typedef typename traits_helper::time_type time_type;
|
Chris@16
|
136 typedef typename traits_helper::duration_type duration_type;
|
Chris@16
|
137 #endif
|
Chris@16
|
138
|
Chris@16
|
139 /// Construct a basic_socket_iostream without establishing a connection.
|
Chris@16
|
140 basic_socket_iostream()
|
Chris@16
|
141 : std::basic_iostream<char>(
|
Chris@16
|
142 &this->detail::socket_iostream_base<
|
Chris@16
|
143 Protocol, StreamSocketService, Time,
|
Chris@16
|
144 TimeTraits, TimerService>::streambuf_)
|
Chris@16
|
145 {
|
Chris@16
|
146 this->setf(std::ios_base::unitbuf);
|
Chris@16
|
147 }
|
Chris@16
|
148
|
Chris@16
|
149 #if defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
150 /// Establish a connection to an endpoint corresponding to a resolver query.
|
Chris@16
|
151 /**
|
Chris@16
|
152 * This constructor automatically establishes a connection based on the
|
Chris@16
|
153 * supplied resolver query parameters. The arguments are used to construct
|
Chris@16
|
154 * a resolver query object.
|
Chris@16
|
155 */
|
Chris@16
|
156 template <typename T1, ..., typename TN>
|
Chris@16
|
157 explicit basic_socket_iostream(T1 t1, ..., TN tn);
|
Chris@16
|
158 #elif defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
|
Chris@16
|
159 template <typename... T>
|
Chris@16
|
160 explicit basic_socket_iostream(T... x)
|
Chris@16
|
161 : std::basic_iostream<char>(
|
Chris@16
|
162 &this->detail::socket_iostream_base<
|
Chris@16
|
163 Protocol, StreamSocketService, Time,
|
Chris@16
|
164 TimeTraits, TimerService>::streambuf_)
|
Chris@16
|
165 {
|
Chris@16
|
166 this->setf(std::ios_base::unitbuf);
|
Chris@16
|
167 if (rdbuf()->connect(x...) == 0)
|
Chris@16
|
168 this->setstate(std::ios_base::failbit);
|
Chris@16
|
169 }
|
Chris@16
|
170 #else
|
Chris@16
|
171 BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_CTR_DEF)
|
Chris@16
|
172 #endif
|
Chris@16
|
173
|
Chris@16
|
174 #if defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
175 /// Establish a connection to an endpoint corresponding to a resolver query.
|
Chris@16
|
176 /**
|
Chris@16
|
177 * This function automatically establishes a connection based on the supplied
|
Chris@16
|
178 * resolver query parameters. The arguments are used to construct a resolver
|
Chris@16
|
179 * query object.
|
Chris@16
|
180 */
|
Chris@16
|
181 template <typename T1, ..., typename TN>
|
Chris@16
|
182 void connect(T1 t1, ..., TN tn);
|
Chris@16
|
183 #elif defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
|
Chris@16
|
184 template <typename... T>
|
Chris@16
|
185 void connect(T... x)
|
Chris@16
|
186 {
|
Chris@16
|
187 if (rdbuf()->connect(x...) == 0)
|
Chris@16
|
188 this->setstate(std::ios_base::failbit);
|
Chris@16
|
189 }
|
Chris@16
|
190 #else
|
Chris@16
|
191 BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_CONNECT_DEF)
|
Chris@16
|
192 #endif
|
Chris@16
|
193
|
Chris@16
|
194 /// Close the connection.
|
Chris@16
|
195 void close()
|
Chris@16
|
196 {
|
Chris@16
|
197 if (rdbuf()->close() == 0)
|
Chris@16
|
198 this->setstate(std::ios_base::failbit);
|
Chris@16
|
199 }
|
Chris@16
|
200
|
Chris@16
|
201 /// Return a pointer to the underlying streambuf.
|
Chris@16
|
202 basic_socket_streambuf<Protocol, StreamSocketService,
|
Chris@16
|
203 Time, TimeTraits, TimerService>* rdbuf() const
|
Chris@16
|
204 {
|
Chris@16
|
205 return const_cast<basic_socket_streambuf<Protocol, StreamSocketService,
|
Chris@16
|
206 Time, TimeTraits, TimerService>*>(
|
Chris@16
|
207 &this->detail::socket_iostream_base<
|
Chris@16
|
208 Protocol, StreamSocketService, Time,
|
Chris@16
|
209 TimeTraits, TimerService>::streambuf_);
|
Chris@16
|
210 }
|
Chris@16
|
211
|
Chris@16
|
212 /// Get the last error associated with the stream.
|
Chris@16
|
213 /**
|
Chris@16
|
214 * @return An \c error_code corresponding to the last error from the stream.
|
Chris@16
|
215 *
|
Chris@16
|
216 * @par Example
|
Chris@16
|
217 * To print the error associated with a failure to establish a connection:
|
Chris@16
|
218 * @code tcp::iostream s("www.boost.org", "http");
|
Chris@16
|
219 * if (!s)
|
Chris@16
|
220 * {
|
Chris@16
|
221 * std::cout << "Error: " << s.error().message() << std::endl;
|
Chris@16
|
222 * } @endcode
|
Chris@16
|
223 */
|
Chris@16
|
224 const boost::system::error_code& error() const
|
Chris@16
|
225 {
|
Chris@16
|
226 return rdbuf()->puberror();
|
Chris@16
|
227 }
|
Chris@16
|
228
|
Chris@16
|
229 /// Get the stream's expiry time as an absolute time.
|
Chris@16
|
230 /**
|
Chris@16
|
231 * @return An absolute time value representing the stream's expiry time.
|
Chris@16
|
232 */
|
Chris@16
|
233 time_type expires_at() const
|
Chris@16
|
234 {
|
Chris@16
|
235 return rdbuf()->expires_at();
|
Chris@16
|
236 }
|
Chris@16
|
237
|
Chris@16
|
238 /// Set the stream's expiry time as an absolute time.
|
Chris@16
|
239 /**
|
Chris@16
|
240 * This function sets the expiry time associated with the stream. Stream
|
Chris@16
|
241 * operations performed after this time (where the operations cannot be
|
Chris@16
|
242 * completed using the internal buffers) will fail with the error
|
Chris@16
|
243 * boost::asio::error::operation_aborted.
|
Chris@16
|
244 *
|
Chris@16
|
245 * @param expiry_time The expiry time to be used for the stream.
|
Chris@16
|
246 */
|
Chris@16
|
247 void expires_at(const time_type& expiry_time)
|
Chris@16
|
248 {
|
Chris@16
|
249 rdbuf()->expires_at(expiry_time);
|
Chris@16
|
250 }
|
Chris@16
|
251
|
Chris@16
|
252 /// Get the timer's expiry time relative to now.
|
Chris@16
|
253 /**
|
Chris@16
|
254 * @return A relative time value representing the stream's expiry time.
|
Chris@16
|
255 */
|
Chris@16
|
256 duration_type expires_from_now() const
|
Chris@16
|
257 {
|
Chris@16
|
258 return rdbuf()->expires_from_now();
|
Chris@16
|
259 }
|
Chris@16
|
260
|
Chris@16
|
261 /// Set the stream's expiry time relative to now.
|
Chris@16
|
262 /**
|
Chris@16
|
263 * This function sets the expiry time associated with the stream. Stream
|
Chris@16
|
264 * operations performed after this time (where the operations cannot be
|
Chris@16
|
265 * completed using the internal buffers) will fail with the error
|
Chris@16
|
266 * boost::asio::error::operation_aborted.
|
Chris@16
|
267 *
|
Chris@16
|
268 * @param expiry_time The expiry time to be used for the timer.
|
Chris@16
|
269 */
|
Chris@16
|
270 void expires_from_now(const duration_type& expiry_time)
|
Chris@16
|
271 {
|
Chris@16
|
272 rdbuf()->expires_from_now(expiry_time);
|
Chris@16
|
273 }
|
Chris@16
|
274 };
|
Chris@16
|
275
|
Chris@16
|
276 } // namespace asio
|
Chris@16
|
277 } // namespace boost
|
Chris@16
|
278
|
Chris@16
|
279 #include <boost/asio/detail/pop_options.hpp>
|
Chris@16
|
280
|
Chris@16
|
281 #if !defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
|
Chris@16
|
282 # undef BOOST_ASIO_PRIVATE_CTR_DEF
|
Chris@16
|
283 # undef BOOST_ASIO_PRIVATE_CONNECT_DEF
|
Chris@16
|
284 #endif // !defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
|
Chris@16
|
285
|
Chris@16
|
286 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
|
Chris@16
|
287
|
Chris@16
|
288 #endif // BOOST_ASIO_BASIC_SOCKET_IOSTREAM_HPP
|