basic_socket_streambuf.hpp 8.1 KB

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