buffered_write_stream.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. //
  2. // buffered_write_stream.cpp
  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. // Disable autolinking for unit tests.
  11. #if !defined(BOOST_ALL_NO_LIB)
  12. #define BOOST_ALL_NO_LIB 1
  13. #endif // !defined(BOOST_ALL_NO_LIB)
  14. // Test that header file is self-contained.
  15. #include <boost/asio/buffered_write_stream.hpp>
  16. #include <cstring>
  17. #include "archetypes/async_result.hpp"
  18. #include <boost/asio/buffer.hpp>
  19. #include <boost/asio/io_context.hpp>
  20. #include <boost/asio/ip/tcp.hpp>
  21. #include <boost/system/system_error.hpp>
  22. #include "unit_test.hpp"
  23. #if defined(BOOST_ASIO_HAS_BOOST_ARRAY)
  24. # include <boost/array.hpp>
  25. #else // defined(BOOST_ASIO_HAS_BOOST_ARRAY)
  26. # include <array>
  27. #endif // defined(BOOST_ASIO_HAS_BOOST_ARRAY)
  28. #if defined(BOOST_ASIO_HAS_BOOST_BIND)
  29. # include <boost/bind.hpp>
  30. #else // defined(BOOST_ASIO_HAS_BOOST_BIND)
  31. # include <functional>
  32. #endif // defined(BOOST_ASIO_HAS_BOOST_BIND)
  33. typedef boost::asio::buffered_write_stream<
  34. boost::asio::ip::tcp::socket> stream_type;
  35. void write_some_handler(const boost::system::error_code&, std::size_t)
  36. {
  37. }
  38. void flush_handler(const boost::system::error_code&, std::size_t)
  39. {
  40. }
  41. void read_some_handler(const boost::system::error_code&, std::size_t)
  42. {
  43. }
  44. void test_compile()
  45. {
  46. #if defined(BOOST_ASIO_HAS_BOOST_ARRAY)
  47. using boost::array;
  48. #else // defined(BOOST_ASIO_HAS_BOOST_ARRAY)
  49. using std::array;
  50. #endif // defined(BOOST_ASIO_HAS_BOOST_ARRAY)
  51. using namespace boost::asio;
  52. try
  53. {
  54. io_context ioc;
  55. char mutable_char_buffer[128] = "";
  56. const char const_char_buffer[128] = "";
  57. array<boost::asio::mutable_buffer, 2> mutable_buffers = {{
  58. boost::asio::buffer(mutable_char_buffer, 10),
  59. boost::asio::buffer(mutable_char_buffer + 10, 10) }};
  60. array<boost::asio::const_buffer, 2> const_buffers = {{
  61. boost::asio::buffer(const_char_buffer, 10),
  62. boost::asio::buffer(const_char_buffer + 10, 10) }};
  63. archetypes::lazy_handler lazy;
  64. boost::system::error_code ec;
  65. stream_type stream1(ioc);
  66. stream_type stream2(ioc, 1024);
  67. stream_type::executor_type ex = stream1.get_executor();
  68. (void)ex;
  69. stream_type::lowest_layer_type& lowest_layer = stream1.lowest_layer();
  70. (void)lowest_layer;
  71. stream1.write_some(buffer(mutable_char_buffer));
  72. stream1.write_some(buffer(const_char_buffer));
  73. stream1.write_some(mutable_buffers);
  74. stream1.write_some(const_buffers);
  75. stream1.write_some(null_buffers());
  76. stream1.write_some(buffer(mutable_char_buffer), ec);
  77. stream1.write_some(buffer(const_char_buffer), ec);
  78. stream1.write_some(mutable_buffers, ec);
  79. stream1.write_some(const_buffers, ec);
  80. stream1.write_some(null_buffers(), ec);
  81. stream1.async_write_some(buffer(mutable_char_buffer), &write_some_handler);
  82. stream1.async_write_some(buffer(const_char_buffer), &write_some_handler);
  83. stream1.async_write_some(mutable_buffers, &write_some_handler);
  84. stream1.async_write_some(const_buffers, &write_some_handler);
  85. stream1.async_write_some(null_buffers(), &write_some_handler);
  86. int i1 = stream1.async_write_some(buffer(mutable_char_buffer), lazy);
  87. (void)i1;
  88. int i2 = stream1.async_write_some(buffer(const_char_buffer), lazy);
  89. (void)i2;
  90. int i3 = stream1.async_write_some(mutable_buffers, lazy);
  91. (void)i3;
  92. int i4 = stream1.async_write_some(const_buffers, lazy);
  93. (void)i4;
  94. int i5 = stream1.async_write_some(null_buffers(), lazy);
  95. (void)i5;
  96. stream1.flush();
  97. stream1.flush(ec);
  98. stream1.async_flush(&flush_handler);
  99. int i6 = stream1.async_flush(lazy);
  100. (void)i6;
  101. stream1.read_some(buffer(mutable_char_buffer));
  102. stream1.read_some(mutable_buffers);
  103. stream1.read_some(null_buffers());
  104. stream1.read_some(buffer(mutable_char_buffer), ec);
  105. stream1.read_some(mutable_buffers, ec);
  106. stream1.read_some(null_buffers(), ec);
  107. stream1.async_read_some(buffer(mutable_char_buffer), &read_some_handler);
  108. stream1.async_read_some(mutable_buffers, &read_some_handler);
  109. stream1.async_read_some(null_buffers(), &read_some_handler);
  110. int i7 = stream1.async_read_some(buffer(mutable_char_buffer), lazy);
  111. (void)i7;
  112. int i8 = stream1.async_read_some(mutable_buffers, lazy);
  113. (void)i8;
  114. int i9 = stream1.async_read_some(null_buffers(), lazy);
  115. (void)i9;
  116. }
  117. catch (std::exception&)
  118. {
  119. }
  120. }
  121. void test_sync_operations()
  122. {
  123. using namespace std; // For memcmp.
  124. boost::asio::io_context io_context;
  125. boost::asio::ip::tcp::acceptor acceptor(io_context,
  126. boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 0));
  127. boost::asio::ip::tcp::endpoint server_endpoint = acceptor.local_endpoint();
  128. server_endpoint.address(boost::asio::ip::address_v4::loopback());
  129. stream_type client_socket(io_context);
  130. client_socket.lowest_layer().connect(server_endpoint);
  131. stream_type server_socket(io_context);
  132. acceptor.accept(server_socket.lowest_layer());
  133. const char write_data[]
  134. = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  135. const boost::asio::const_buffer write_buf = boost::asio::buffer(write_data);
  136. std::size_t bytes_written = 0;
  137. while (bytes_written < sizeof(write_data))
  138. {
  139. bytes_written += client_socket.write_some(
  140. boost::asio::buffer(write_buf + bytes_written));
  141. client_socket.flush();
  142. }
  143. char read_data[sizeof(write_data)];
  144. const boost::asio::mutable_buffer read_buf = boost::asio::buffer(read_data);
  145. std::size_t bytes_read = 0;
  146. while (bytes_read < sizeof(read_data))
  147. {
  148. bytes_read += server_socket.read_some(
  149. boost::asio::buffer(read_buf + bytes_read));
  150. }
  151. BOOST_ASIO_CHECK(bytes_written == sizeof(write_data));
  152. BOOST_ASIO_CHECK(bytes_read == sizeof(read_data));
  153. BOOST_ASIO_CHECK(memcmp(write_data, read_data, sizeof(write_data)) == 0);
  154. bytes_written = 0;
  155. while (bytes_written < sizeof(write_data))
  156. {
  157. bytes_written += server_socket.write_some(
  158. boost::asio::buffer(write_buf + bytes_written));
  159. server_socket.flush();
  160. }
  161. bytes_read = 0;
  162. while (bytes_read < sizeof(read_data))
  163. {
  164. bytes_read += client_socket.read_some(
  165. boost::asio::buffer(read_buf + bytes_read));
  166. }
  167. BOOST_ASIO_CHECK(bytes_written == sizeof(write_data));
  168. BOOST_ASIO_CHECK(bytes_read == sizeof(read_data));
  169. BOOST_ASIO_CHECK(memcmp(write_data, read_data, sizeof(write_data)) == 0);
  170. server_socket.close();
  171. boost::system::error_code error;
  172. bytes_read = client_socket.read_some(
  173. boost::asio::buffer(read_buf), error);
  174. BOOST_ASIO_CHECK(bytes_read == 0);
  175. BOOST_ASIO_CHECK(error == boost::asio::error::eof);
  176. client_socket.close(error);
  177. }
  178. void handle_accept(const boost::system::error_code& e)
  179. {
  180. BOOST_ASIO_CHECK(!e);
  181. }
  182. void handle_write(const boost::system::error_code& e,
  183. std::size_t bytes_transferred,
  184. std::size_t* total_bytes_written)
  185. {
  186. BOOST_ASIO_CHECK(!e);
  187. if (e)
  188. throw boost::system::system_error(e); // Terminate test.
  189. *total_bytes_written += bytes_transferred;
  190. }
  191. void handle_flush(const boost::system::error_code& e)
  192. {
  193. BOOST_ASIO_CHECK(!e);
  194. }
  195. void handle_read(const boost::system::error_code& e,
  196. std::size_t bytes_transferred,
  197. std::size_t* total_bytes_read)
  198. {
  199. BOOST_ASIO_CHECK(!e);
  200. if (e)
  201. throw boost::system::system_error(e); // Terminate test.
  202. *total_bytes_read += bytes_transferred;
  203. }
  204. void handle_read_eof(const boost::system::error_code& e,
  205. std::size_t bytes_transferred)
  206. {
  207. BOOST_ASIO_CHECK(e == boost::asio::error::eof);
  208. BOOST_ASIO_CHECK(bytes_transferred == 0);
  209. }
  210. void test_async_operations()
  211. {
  212. using namespace std; // For memcmp.
  213. #if defined(BOOST_ASIO_HAS_BOOST_BIND)
  214. namespace bindns = boost;
  215. #else // defined(BOOST_ASIO_HAS_BOOST_BIND)
  216. namespace bindns = std;
  217. using std::placeholders::_1;
  218. using std::placeholders::_2;
  219. #endif // defined(BOOST_ASIO_HAS_BOOST_BIND)
  220. boost::asio::io_context io_context;
  221. boost::asio::ip::tcp::acceptor acceptor(io_context,
  222. boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 0));
  223. boost::asio::ip::tcp::endpoint server_endpoint = acceptor.local_endpoint();
  224. server_endpoint.address(boost::asio::ip::address_v4::loopback());
  225. stream_type client_socket(io_context);
  226. client_socket.lowest_layer().connect(server_endpoint);
  227. stream_type server_socket(io_context);
  228. acceptor.async_accept(server_socket.lowest_layer(), &handle_accept);
  229. io_context.run();
  230. io_context.restart();
  231. const char write_data[]
  232. = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  233. const boost::asio::const_buffer write_buf = boost::asio::buffer(write_data);
  234. std::size_t bytes_written = 0;
  235. while (bytes_written < sizeof(write_data))
  236. {
  237. client_socket.async_write_some(
  238. boost::asio::buffer(write_buf + bytes_written),
  239. bindns::bind(handle_write, _1, _2, &bytes_written));
  240. io_context.run();
  241. io_context.restart();
  242. client_socket.async_flush(
  243. bindns::bind(handle_flush, _1));
  244. io_context.run();
  245. io_context.restart();
  246. }
  247. char read_data[sizeof(write_data)];
  248. const boost::asio::mutable_buffer read_buf = boost::asio::buffer(read_data);
  249. std::size_t bytes_read = 0;
  250. while (bytes_read < sizeof(read_data))
  251. {
  252. server_socket.async_read_some(
  253. boost::asio::buffer(read_buf + bytes_read),
  254. bindns::bind(handle_read, _1, _2, &bytes_read));
  255. io_context.run();
  256. io_context.restart();
  257. }
  258. BOOST_ASIO_CHECK(bytes_written == sizeof(write_data));
  259. BOOST_ASIO_CHECK(bytes_read == sizeof(read_data));
  260. BOOST_ASIO_CHECK(memcmp(write_data, read_data, sizeof(write_data)) == 0);
  261. bytes_written = 0;
  262. while (bytes_written < sizeof(write_data))
  263. {
  264. server_socket.async_write_some(
  265. boost::asio::buffer(write_buf + bytes_written),
  266. bindns::bind(handle_write, _1, _2, &bytes_written));
  267. io_context.run();
  268. io_context.restart();
  269. server_socket.async_flush(
  270. bindns::bind(handle_flush, _1));
  271. io_context.run();
  272. io_context.restart();
  273. }
  274. bytes_read = 0;
  275. while (bytes_read < sizeof(read_data))
  276. {
  277. client_socket.async_read_some(
  278. boost::asio::buffer(read_buf + bytes_read),
  279. bindns::bind(handle_read, _1, _2, &bytes_read));
  280. io_context.run();
  281. io_context.restart();
  282. }
  283. BOOST_ASIO_CHECK(bytes_written == sizeof(write_data));
  284. BOOST_ASIO_CHECK(bytes_read == sizeof(read_data));
  285. BOOST_ASIO_CHECK(memcmp(write_data, read_data, sizeof(write_data)) == 0);
  286. server_socket.close();
  287. client_socket.async_read_some(boost::asio::buffer(read_buf), handle_read_eof);
  288. }
  289. BOOST_ASIO_TEST_SUITE
  290. (
  291. "buffered_write_stream",
  292. BOOST_ASIO_TEST_CASE(test_compile)
  293. BOOST_ASIO_TEST_CASE(test_sync_operations)
  294. BOOST_ASIO_TEST_CASE(test_async_operations)
  295. )