Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

basic_socket_iostream.hpp 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. //
  2. // basic_socket_iostream.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  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)
  9. //
  10. #ifndef ASIO_BASIC_SOCKET_IOSTREAM_HPP
  11. #define ASIO_BASIC_SOCKET_IOSTREAM_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #if !defined(ASIO_NO_IOSTREAM)
  17. #include <istream>
  18. #include <ostream>
  19. #include "asio/basic_socket_streambuf.hpp"
  20. #if !defined(ASIO_HAS_VARIADIC_TEMPLATES)
  21. # include "asio/detail/variadic_templates.hpp"
  22. // A macro that should expand to:
  23. // template <typename T1, ..., typename Tn>
  24. // explicit basic_socket_iostream(T1 x1, ..., Tn xn)
  25. // : std::basic_iostream<char>(
  26. // &this->detail::socket_iostream_base<
  27. // Protocol, Clock, WaitTraits>::streambuf_)
  28. // {
  29. // if (rdbuf()->connect(x1, ..., xn) == 0)
  30. // this->setstate(std::ios_base::failbit);
  31. // }
  32. // This macro should only persist within this file.
  33. # define ASIO_PRIVATE_CTR_DEF(n) \
  34. template <ASIO_VARIADIC_TPARAMS(n)> \
  35. explicit basic_socket_iostream(ASIO_VARIADIC_BYVAL_PARAMS(n)) \
  36. : std::basic_iostream<char>( \
  37. &this->detail::socket_iostream_base< \
  38. Protocol, Clock, WaitTraits>::streambuf_) \
  39. { \
  40. this->setf(std::ios_base::unitbuf); \
  41. if (rdbuf()->connect(ASIO_VARIADIC_BYVAL_ARGS(n)) == 0) \
  42. this->setstate(std::ios_base::failbit); \
  43. } \
  44. /**/
  45. // A macro that should expand to:
  46. // template <typename T1, ..., typename Tn>
  47. // void connect(T1 x1, ..., Tn xn)
  48. // {
  49. // if (rdbuf()->connect(x1, ..., xn) == 0)
  50. // this->setstate(std::ios_base::failbit);
  51. // }
  52. // This macro should only persist within this file.
  53. # define ASIO_PRIVATE_CONNECT_DEF(n) \
  54. template <ASIO_VARIADIC_TPARAMS(n)> \
  55. void connect(ASIO_VARIADIC_BYVAL_PARAMS(n)) \
  56. { \
  57. if (rdbuf()->connect(ASIO_VARIADIC_BYVAL_ARGS(n)) == 0) \
  58. this->setstate(std::ios_base::failbit); \
  59. } \
  60. /**/
  61. #endif // !defined(ASIO_HAS_VARIADIC_TEMPLATES)
  62. #include "asio/detail/push_options.hpp"
  63. namespace asio {
  64. namespace detail {
  65. // A separate base class is used to ensure that the streambuf is initialised
  66. // prior to the basic_socket_iostream's basic_iostream base class.
  67. template <typename Protocol, typename Clock, typename WaitTraits>
  68. class socket_iostream_base
  69. {
  70. protected:
  71. socket_iostream_base()
  72. {
  73. }
  74. #if defined(ASIO_HAS_MOVE)
  75. socket_iostream_base(socket_iostream_base&& other)
  76. : streambuf_(std::move(other.streambuf_))
  77. {
  78. }
  79. socket_iostream_base(basic_stream_socket<Protocol> s)
  80. : streambuf_(std::move(s))
  81. {
  82. }
  83. socket_iostream_base& operator=(socket_iostream_base&& other)
  84. {
  85. streambuf_ = std::move(other.streambuf_);
  86. return *this;
  87. }
  88. #endif // defined(ASIO_HAS_MOVE)
  89. basic_socket_streambuf<Protocol, Clock, WaitTraits> streambuf_;
  90. };
  91. } // namespace detail
  92. #if !defined(ASIO_BASIC_SOCKET_IOSTREAM_FWD_DECL)
  93. #define ASIO_BASIC_SOCKET_IOSTREAM_FWD_DECL
  94. // Forward declaration with defaulted arguments.
  95. template <typename Protocol,
  96. #if defined(ASIO_HAS_BOOST_DATE_TIME) \
  97. && defined(ASIO_USE_BOOST_DATE_TIME_FOR_SOCKET_IOSTREAM)
  98. typename Clock = boost::posix_time::ptime,
  99. typename WaitTraits = time_traits<Clock> >
  100. #else // defined(ASIO_HAS_BOOST_DATE_TIME)
  101. // && defined(ASIO_USE_BOOST_DATE_TIME_FOR_SOCKET_IOSTREAM)
  102. typename Clock = chrono::steady_clock,
  103. typename WaitTraits = wait_traits<Clock> >
  104. #endif // defined(ASIO_HAS_BOOST_DATE_TIME)
  105. // && defined(ASIO_USE_BOOST_DATE_TIME_FOR_SOCKET_IOSTREAM)
  106. class basic_socket_iostream;
  107. #endif // !defined(ASIO_BASIC_SOCKET_IOSTREAM_FWD_DECL)
  108. /// Iostream interface for a socket.
  109. #if defined(GENERATING_DOCUMENTATION)
  110. template <typename Protocol,
  111. typename Clock = chrono::steady_clock,
  112. typename WaitTraits = wait_traits<Clock> >
  113. #else // defined(GENERATING_DOCUMENTATION)
  114. template <typename Protocol, typename Clock, typename WaitTraits>
  115. #endif // defined(GENERATING_DOCUMENTATION)
  116. class basic_socket_iostream
  117. : private detail::socket_iostream_base<Protocol, Clock, WaitTraits>,
  118. public std::basic_iostream<char>
  119. {
  120. private:
  121. // These typedefs are intended keep this class's implementation independent
  122. // of whether it's using Boost.DateClock, Boost.Chrono or std::chrono.
  123. #if defined(ASIO_HAS_BOOST_DATE_TIME) \
  124. && defined(ASIO_USE_BOOST_DATE_TIME_FOR_SOCKET_IOSTREAM)
  125. typedef WaitTraits traits_helper;
  126. #else // defined(ASIO_HAS_BOOST_DATE_TIME)
  127. // && defined(ASIO_USE_BOOST_DATE_TIME_FOR_SOCKET_IOSTREAM)
  128. typedef detail::chrono_time_traits<Clock, WaitTraits> traits_helper;
  129. #endif // defined(ASIO_HAS_BOOST_DATE_TIME)
  130. // && defined(ASIO_USE_BOOST_DATE_TIME_FOR_SOCKET_IOSTREAM)
  131. public:
  132. /// The protocol type.
  133. typedef Protocol protocol_type;
  134. /// The endpoint type.
  135. typedef typename Protocol::endpoint endpoint_type;
  136. /// The clock type.
  137. typedef Clock clock_type;
  138. #if defined(GENERATING_DOCUMENTATION)
  139. /// (Deprecated: Use time_point.) The time type.
  140. typedef typename WaitTraits::time_type time_type;
  141. /// The time type.
  142. typedef typename WaitTraits::time_point time_point;
  143. /// (Deprecated: Use duration.) The duration type.
  144. typedef typename WaitTraits::duration_type duration_type;
  145. /// The duration type.
  146. typedef typename WaitTraits::duration duration;
  147. #else
  148. # if !defined(ASIO_NO_DEPRECATED)
  149. typedef typename traits_helper::time_type time_type;
  150. typedef typename traits_helper::duration_type duration_type;
  151. # endif // !defined(ASIO_NO_DEPRECATED)
  152. typedef typename traits_helper::time_type time_point;
  153. typedef typename traits_helper::duration_type duration;
  154. #endif
  155. /// Construct a basic_socket_iostream without establishing a connection.
  156. basic_socket_iostream()
  157. : std::basic_iostream<char>(
  158. &this->detail::socket_iostream_base<
  159. Protocol, Clock, WaitTraits>::streambuf_)
  160. {
  161. this->setf(std::ios_base::unitbuf);
  162. }
  163. #if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  164. /// Construct a basic_socket_iostream from the supplied socket.
  165. explicit basic_socket_iostream(basic_stream_socket<protocol_type> s)
  166. : detail::socket_iostream_base<
  167. Protocol, Clock, WaitTraits>(std::move(s)),
  168. std::basic_iostream<char>(
  169. &this->detail::socket_iostream_base<
  170. Protocol, Clock, WaitTraits>::streambuf_)
  171. {
  172. this->setf(std::ios_base::unitbuf);
  173. }
  174. #if defined(ASIO_HAS_STD_IOSTREAM_MOVE) \
  175. || defined(GENERATING_DOCUMENTATION)
  176. /// Move-construct a basic_socket_iostream from another.
  177. basic_socket_iostream(basic_socket_iostream&& other)
  178. : detail::socket_iostream_base<
  179. Protocol, Clock, WaitTraits>(std::move(other)),
  180. std::basic_iostream<char>(std::move(other))
  181. {
  182. this->set_rdbuf(&this->detail::socket_iostream_base<
  183. Protocol, Clock, WaitTraits>::streambuf_);
  184. }
  185. /// Move-assign a basic_socket_iostream from another.
  186. basic_socket_iostream& operator=(basic_socket_iostream&& other)
  187. {
  188. std::basic_iostream<char>::operator=(std::move(other));
  189. detail::socket_iostream_base<
  190. Protocol, Clock, WaitTraits>::operator=(std::move(other));
  191. return *this;
  192. }
  193. #endif // defined(ASIO_HAS_STD_IOSTREAM_MOVE)
  194. // || defined(GENERATING_DOCUMENTATION)
  195. #endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  196. #if defined(GENERATING_DOCUMENTATION)
  197. /// Establish a connection to an endpoint corresponding to a resolver query.
  198. /**
  199. * This constructor automatically establishes a connection based on the
  200. * supplied resolver query parameters. The arguments are used to construct
  201. * a resolver query object.
  202. */
  203. template <typename T1, ..., typename TN>
  204. explicit basic_socket_iostream(T1 t1, ..., TN tn);
  205. #elif defined(ASIO_HAS_VARIADIC_TEMPLATES)
  206. template <typename... T>
  207. explicit basic_socket_iostream(T... x)
  208. : std::basic_iostream<char>(
  209. &this->detail::socket_iostream_base<
  210. Protocol, Clock, WaitTraits>::streambuf_)
  211. {
  212. this->setf(std::ios_base::unitbuf);
  213. if (rdbuf()->connect(x...) == 0)
  214. this->setstate(std::ios_base::failbit);
  215. }
  216. #else
  217. ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_CTR_DEF)
  218. #endif
  219. #if defined(GENERATING_DOCUMENTATION)
  220. /// Establish a connection to an endpoint corresponding to a resolver query.
  221. /**
  222. * This function automatically establishes a connection based on the supplied
  223. * resolver query parameters. The arguments are used to construct a resolver
  224. * query object.
  225. */
  226. template <typename T1, ..., typename TN>
  227. void connect(T1 t1, ..., TN tn);
  228. #elif defined(ASIO_HAS_VARIADIC_TEMPLATES)
  229. template <typename... T>
  230. void connect(T... x)
  231. {
  232. if (rdbuf()->connect(x...) == 0)
  233. this->setstate(std::ios_base::failbit);
  234. }
  235. #else
  236. ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_CONNECT_DEF)
  237. #endif
  238. /// Close the connection.
  239. void close()
  240. {
  241. if (rdbuf()->close() == 0)
  242. this->setstate(std::ios_base::failbit);
  243. }
  244. /// Return a pointer to the underlying streambuf.
  245. basic_socket_streambuf<Protocol, Clock, WaitTraits>* rdbuf() const
  246. {
  247. return const_cast<basic_socket_streambuf<Protocol, Clock, WaitTraits>*>(
  248. &this->detail::socket_iostream_base<
  249. Protocol, Clock, WaitTraits>::streambuf_);
  250. }
  251. /// Get a reference to the underlying socket.
  252. basic_socket<Protocol>& socket()
  253. {
  254. return rdbuf()->socket();
  255. }
  256. /// Get the last error associated with the stream.
  257. /**
  258. * @return An \c error_code corresponding to the last error from the stream.
  259. *
  260. * @par Example
  261. * To print the error associated with a failure to establish a connection:
  262. * @code tcp::iostream s("www.boost.org", "http");
  263. * if (!s)
  264. * {
  265. * std::cout << "Error: " << s.error().message() << std::endl;
  266. * } @endcode
  267. */
  268. const asio::error_code& error() const
  269. {
  270. return rdbuf()->error();
  271. }
  272. #if !defined(ASIO_NO_DEPRECATED)
  273. /// (Deprecated: Use expiry().) Get the stream's expiry time as an absolute
  274. /// time.
  275. /**
  276. * @return An absolute time value representing the stream's expiry time.
  277. */
  278. time_point expires_at() const
  279. {
  280. return rdbuf()->expires_at();
  281. }
  282. #endif // !defined(ASIO_NO_DEPRECATED)
  283. /// Get the stream's expiry time as an absolute time.
  284. /**
  285. * @return An absolute time value representing the stream's expiry time.
  286. */
  287. time_point expiry() const
  288. {
  289. return rdbuf()->expiry();
  290. }
  291. /// Set the stream's expiry time as an absolute time.
  292. /**
  293. * This function sets the expiry time associated with the stream. Stream
  294. * operations performed after this time (where the operations cannot be
  295. * completed using the internal buffers) will fail with the error
  296. * asio::error::operation_aborted.
  297. *
  298. * @param expiry_time The expiry time to be used for the stream.
  299. */
  300. void expires_at(const time_point& expiry_time)
  301. {
  302. rdbuf()->expires_at(expiry_time);
  303. }
  304. /// Set the stream's expiry time relative to now.
  305. /**
  306. * This function sets the expiry time associated with the stream. Stream
  307. * operations performed after this time (where the operations cannot be
  308. * completed using the internal buffers) will fail with the error
  309. * asio::error::operation_aborted.
  310. *
  311. * @param expiry_time The expiry time to be used for the timer.
  312. */
  313. void expires_after(const duration& expiry_time)
  314. {
  315. rdbuf()->expires_after(expiry_time);
  316. }
  317. #if !defined(ASIO_NO_DEPRECATED)
  318. /// (Deprecated: Use expiry().) Get the stream's expiry time relative to now.
  319. /**
  320. * @return A relative time value representing the stream's expiry time.
  321. */
  322. duration expires_from_now() const
  323. {
  324. return rdbuf()->expires_from_now();
  325. }
  326. /// (Deprecated: Use expires_after().) Set the stream's expiry time relative
  327. /// to now.
  328. /**
  329. * This function sets the expiry time associated with the stream. Stream
  330. * operations performed after this time (where the operations cannot be
  331. * completed using the internal buffers) will fail with the error
  332. * asio::error::operation_aborted.
  333. *
  334. * @param expiry_time The expiry time to be used for the timer.
  335. */
  336. void expires_from_now(const duration& expiry_time)
  337. {
  338. rdbuf()->expires_from_now(expiry_time);
  339. }
  340. #endif // !defined(ASIO_NO_DEPRECATED)
  341. private:
  342. // Disallow copying and assignment.
  343. basic_socket_iostream(const basic_socket_iostream&) ASIO_DELETED;
  344. basic_socket_iostream& operator=(
  345. const basic_socket_iostream&) ASIO_DELETED;
  346. };
  347. } // namespace asio
  348. #include "asio/detail/pop_options.hpp"
  349. #if !defined(ASIO_HAS_VARIADIC_TEMPLATES)
  350. # undef ASIO_PRIVATE_CTR_DEF
  351. # undef ASIO_PRIVATE_CONNECT_DEF
  352. #endif // !defined(ASIO_HAS_VARIADIC_TEMPLATES)
  353. #endif // !defined(ASIO_NO_IOSTREAM)
  354. #endif // ASIO_BASIC_SOCKET_IOSTREAM_HPP