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.

326 lines
10.0KB

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