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

Function test_async_operations

test/buffered_read_stream.cpp:241–321  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 12

bufferFunction · 0.85
restartMethod · 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
closeMethod · 0.45

Tested by

no test coverage detected