basic_socket_streambuf.hpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. //
  2. // basic_socket_streambuf.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2008 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 BOOST_ASIO_BASIC_SOCKET_STREAMBUF_HPP
  11. #define BOOST_ASIO_BASIC_SOCKET_STREAMBUF_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/push_options.hpp>
  16. #include <boost/asio/detail/push_options.hpp>
  17. #include <streambuf>
  18. #include <boost/array.hpp>
  19. #include <boost/preprocessor/arithmetic/inc.hpp>
  20. #include <boost/preprocessor/repetition/enum_binary_params.hpp>
  21. #include <boost/preprocessor/repetition/enum_params.hpp>
  22. #include <boost/preprocessor/repetition/repeat_from_to.hpp>
  23. #include <boost/utility/base_from_member.hpp>
  24. #include <boost/asio/detail/pop_options.hpp>
  25. #include <boost/asio/basic_socket.hpp>
  26. #include <boost/asio/io_service.hpp>
  27. #include <boost/asio/stream_socket_service.hpp>
  28. #include <boost/asio/detail/throw_error.hpp>
  29. #if !defined(BOOST_ASIO_SOCKET_STREAMBUF_MAX_ARITY)
  30. #define BOOST_ASIO_SOCKET_STREAMBUF_MAX_ARITY 5
  31. #endif // !defined(BOOST_ASIO_SOCKET_STREAMBUF_MAX_ARITY)
  32. // A macro that should expand to:
  33. // template <typename T1, ..., typename Tn>
  34. // basic_socket_streambuf<Protocol, StreamSocketService>* connect(
  35. // T1 x1, ..., Tn xn)
  36. // {
  37. // init_buffers();
  38. // boost::system::error_code ec;
  39. // this->basic_socket<Protocol, StreamSocketService>::close(ec);
  40. // typedef typename Protocol::resolver_query resolver_query;
  41. // resolver_query query(x1, ..., xn);
  42. // resolve_and_connect(query, ec);
  43. // return !ec ? this : 0;
  44. // }
  45. // This macro should only persist within this file.
  46. #define BOOST_ASIO_PRIVATE_CONNECT_DEF( z, n, data ) \
  47. template <BOOST_PP_ENUM_PARAMS(n, typename T)> \
  48. basic_socket_streambuf<Protocol, StreamSocketService>* connect( \
  49. BOOST_PP_ENUM_BINARY_PARAMS(n, T, x)) \
  50. { \
  51. init_buffers(); \
  52. boost::system::error_code ec; \
  53. this->basic_socket<Protocol, StreamSocketService>::close(ec); \
  54. typedef typename Protocol::resolver_query resolver_query; \
  55. resolver_query query(BOOST_PP_ENUM_PARAMS(n, x)); \
  56. resolve_and_connect(query, ec); \
  57. return !ec ? this : 0; \
  58. } \
  59. /**/
  60. namespace boost {
  61. namespace asio {
  62. /// Iostream streambuf for a socket.
  63. template <typename Protocol,
  64. typename StreamSocketService = stream_socket_service<Protocol> >
  65. class basic_socket_streambuf
  66. : public std::streambuf,
  67. private boost::base_from_member<io_service>,
  68. public basic_socket<Protocol, StreamSocketService>
  69. {
  70. public:
  71. /// The endpoint type.
  72. typedef typename Protocol::endpoint endpoint_type;
  73. /// Construct a basic_socket_streambuf without establishing a connection.
  74. basic_socket_streambuf()
  75. : basic_socket<Protocol, StreamSocketService>(
  76. boost::base_from_member<boost::asio::io_service>::member),
  77. unbuffered_(false)
  78. {
  79. init_buffers();
  80. }
  81. /// Destructor flushes buffered data.
  82. virtual ~basic_socket_streambuf()
  83. {
  84. if (pptr() != pbase())
  85. overflow(traits_type::eof());
  86. }
  87. /// Establish a connection.
  88. /**
  89. * This function establishes a connection to the specified endpoint.
  90. *
  91. * @return \c this if a connection was successfully established, a null
  92. * pointer otherwise.
  93. */
  94. basic_socket_streambuf<Protocol, StreamSocketService>* connect(
  95. const endpoint_type& endpoint)
  96. {
  97. init_buffers();
  98. boost::system::error_code ec;
  99. this->basic_socket<Protocol, StreamSocketService>::close(ec);
  100. this->basic_socket<Protocol, StreamSocketService>::connect(endpoint, ec);
  101. return !ec ? this : 0;
  102. }
  103. #if defined(GENERATING_DOCUMENTATION)
  104. /// Establish a connection.
  105. /**
  106. * This function automatically establishes a connection based on the supplied
  107. * resolver query parameters. The arguments are used to construct a resolver
  108. * query object.
  109. *
  110. * @return \c this if a connection was successfully established, a null
  111. * pointer otherwise.
  112. */
  113. template <typename T1, ..., typename TN>
  114. basic_socket_streambuf<Protocol, StreamSocketService>* connect(
  115. T1 t1, ..., TN tn);
  116. #else
  117. BOOST_PP_REPEAT_FROM_TO(
  118. 1, BOOST_PP_INC(BOOST_ASIO_SOCKET_STREAMBUF_MAX_ARITY),
  119. BOOST_ASIO_PRIVATE_CONNECT_DEF, _ )
  120. #endif
  121. /// Close the connection.
  122. /**
  123. * @return \c this if a connection was successfully established, a null
  124. * pointer otherwise.
  125. */
  126. basic_socket_streambuf<Protocol, StreamSocketService>* close()
  127. {
  128. boost::system::error_code ec;
  129. sync();
  130. this->basic_socket<Protocol, StreamSocketService>::close(ec);
  131. if (!ec)
  132. init_buffers();
  133. return !ec ? this : 0;
  134. }
  135. protected:
  136. int_type underflow()
  137. {
  138. if (gptr() == egptr())
  139. {
  140. boost::system::error_code ec;
  141. std::size_t bytes_transferred = this->service.receive(
  142. this->implementation,
  143. boost::asio::buffer(boost::asio::buffer(get_buffer_) + putback_max),
  144. 0, ec);
  145. if (ec)
  146. return traits_type::eof();
  147. setg(get_buffer_.begin(), get_buffer_.begin() + putback_max,
  148. get_buffer_.begin() + putback_max + bytes_transferred);
  149. return traits_type::to_int_type(*gptr());
  150. }
  151. else
  152. {
  153. return traits_type::eof();
  154. }
  155. }
  156. int_type overflow(int_type c)
  157. {
  158. if (unbuffered_)
  159. {
  160. if (traits_type::eq_int_type(c, traits_type::eof()))
  161. {
  162. // Nothing to do.
  163. return traits_type::not_eof(c);
  164. }
  165. else
  166. {
  167. // Send the single character immediately.
  168. boost::system::error_code ec;
  169. char_type ch = traits_type::to_char_type(c);
  170. this->service.send(this->implementation,
  171. boost::asio::buffer(&ch, sizeof(char_type)), 0, ec);
  172. if (ec)
  173. return traits_type::eof();
  174. return c;
  175. }
  176. }
  177. else
  178. {
  179. // Send all data in the output buffer.
  180. boost::asio::const_buffer buffer =
  181. boost::asio::buffer(pbase(), pptr() - pbase());
  182. while (boost::asio::buffer_size(buffer) > 0)
  183. {
  184. boost::system::error_code ec;
  185. std::size_t bytes_transferred = this->service.send(
  186. this->implementation, boost::asio::buffer(buffer),
  187. 0, ec);
  188. if (ec)
  189. return traits_type::eof();
  190. buffer = buffer + bytes_transferred;
  191. }
  192. setp(put_buffer_.begin(), put_buffer_.end());
  193. // If the new character is eof then our work here is done.
  194. if (traits_type::eq_int_type(c, traits_type::eof()))
  195. return traits_type::not_eof(c);
  196. // Add the new character to the output buffer.
  197. *pptr() = traits_type::to_char_type(c);
  198. pbump(1);
  199. return c;
  200. }
  201. }
  202. int sync()
  203. {
  204. return overflow(traits_type::eof());
  205. }
  206. std::streambuf* setbuf(char_type* s, std::streamsize n)
  207. {
  208. if (pptr() == pbase() && s == 0 && n == 0)
  209. {
  210. unbuffered_ = true;
  211. setp(0, 0);
  212. return this;
  213. }
  214. return 0;
  215. }
  216. private:
  217. void init_buffers()
  218. {
  219. setg(get_buffer_.begin(),
  220. get_buffer_.begin() + putback_max,
  221. get_buffer_.begin() + putback_max);
  222. if (unbuffered_)
  223. setp(0, 0);
  224. else
  225. setp(put_buffer_.begin(), put_buffer_.end());
  226. }
  227. template <typename ResolverQuery>
  228. void resolve_and_connect(const ResolverQuery& query,
  229. boost::system::error_code& ec)
  230. {
  231. typedef typename Protocol::resolver resolver_type;
  232. typedef typename Protocol::resolver_iterator iterator_type;
  233. resolver_type resolver(
  234. boost::base_from_member<boost::asio::io_service>::member);
  235. iterator_type i = resolver.resolve(query, ec);
  236. if (!ec)
  237. {
  238. iterator_type end;
  239. ec = boost::asio::error::host_not_found;
  240. while (ec && i != end)
  241. {
  242. this->basic_socket<Protocol, StreamSocketService>::close();
  243. this->basic_socket<Protocol, StreamSocketService>::connect(*i, ec);
  244. ++i;
  245. }
  246. }
  247. }
  248. enum { putback_max = 8 };
  249. enum { buffer_size = 512 };
  250. boost::array<char, buffer_size> get_buffer_;
  251. boost::array<char, buffer_size> put_buffer_;
  252. bool unbuffered_;
  253. };
  254. } // namespace asio
  255. } // namespace boost
  256. #undef BOOST_ASIO_PRIVATE_CONNECT_DEF
  257. #include <boost/asio/detail/pop_options.hpp>
  258. #endif // BOOST_ASIO_BASIC_SOCKET_STREAMBUF_HPP