MCPcopy Create free account
hub / github.com/boostorg/asio / test_async_operations

Function test_async_operations

test/buffered_write_stream.cpp:248–336  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

246}
247
248void test_async_operations()
249{
250 using namespace std; // For memcmp.
251
252 namespace bindns = std;
253 using bindns::placeholders::_1;
254 using bindns::placeholders::_2;
255
256 boost::asio::io_context io_context;
257
258 boost::asio::ip::tcp::acceptor acceptor(io_context,
259 boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 0));
260 boost::asio::ip::tcp::endpoint server_endpoint = acceptor.local_endpoint();
261 server_endpoint.address(boost::asio::ip::address_v4::loopback());
262
263 stream_type client_socket(io_context);
264 client_socket.lowest_layer().connect(server_endpoint);
265
266 stream_type server_socket(io_context);
267 acceptor.async_accept(server_socket.lowest_layer(), &handle_accept);
268 io_context.run();
269 io_context.restart();
270
271 const char write_data[]
272 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
273 const boost::asio::const_buffer write_buf = boost::asio::buffer(write_data);
274
275 std::size_t bytes_written = 0;
276 while (bytes_written < sizeof(write_data))
277 {
278 client_socket.async_write_some(
279 boost::asio::buffer(write_buf + bytes_written),
280 bindns::bind(handle_write, _1, _2, &bytes_written));
281 io_context.run();
282 io_context.restart();
283 client_socket.async_flush(
284 bindns::bind(handle_flush, _1));
285 io_context.run();
286 io_context.restart();
287 }
288
289 char read_data[sizeof(write_data)];
290 const boost::asio::mutable_buffer read_buf = boost::asio::buffer(read_data);
291
292 std::size_t bytes_read = 0;
293 while (bytes_read < sizeof(read_data))
294 {
295 server_socket.async_read_some(
296 boost::asio::buffer(read_buf + bytes_read),
297 bindns::bind(handle_read, _1, _2, &bytes_read));
298 io_context.run();
299 io_context.restart();
300 }
301
302 BOOST_ASIO_CHECK(bytes_written == sizeof(write_data));
303 BOOST_ASIO_CHECK(bytes_read == sizeof(read_data));
304 BOOST_ASIO_CHECK(memcmp(write_data, read_data, sizeof(write_data)) == 0);
305

Callers

nothing calls this directly

Calls 13

bufferFunction · 0.85
restartMethod · 0.80
async_flushMethod · 0.80
endpointClass · 0.50
bindFunction · 0.50
local_endpointMethod · 0.45
addressMethod · 0.45
connectMethod · 0.45
async_acceptMethod · 0.45
runMethod · 0.45
async_write_someMethod · 0.45
async_read_someMethod · 0.45

Tested by

no test coverage detected