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

Function test_sync_operations

test/buffered_read_stream.cpp:138–207  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

136}
137
138void test_sync_operations()
139{
140 using namespace std; // For memcmp.
141
142 boost::asio::io_context io_context;
143
144 boost::asio::ip::tcp::acceptor acceptor(io_context,
145 boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 0));
146 boost::asio::ip::tcp::endpoint server_endpoint = acceptor.local_endpoint();
147 server_endpoint.address(boost::asio::ip::address_v4::loopback());
148
149 stream_type client_socket(io_context);
150 client_socket.lowest_layer().connect(server_endpoint);
151
152 stream_type server_socket(io_context);
153 acceptor.accept(server_socket.lowest_layer());
154
155 const char write_data[]
156 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
157 const boost::asio::const_buffer write_buf = boost::asio::buffer(write_data);
158
159 std::size_t bytes_written = 0;
160 while (bytes_written < sizeof(write_data))
161 {
162 bytes_written += client_socket.write_some(
163 boost::asio::buffer(write_buf + bytes_written));
164 }
165
166 char read_data[sizeof(write_data)];
167 const boost::asio::mutable_buffer read_buf = boost::asio::buffer(read_data);
168
169 std::size_t bytes_read = 0;
170 while (bytes_read < sizeof(read_data))
171 {
172 bytes_read += server_socket.read_some(
173 boost::asio::buffer(read_buf + bytes_read));
174 }
175
176 BOOST_ASIO_CHECK(bytes_written == sizeof(write_data));
177 BOOST_ASIO_CHECK(bytes_read == sizeof(read_data));
178 BOOST_ASIO_CHECK(memcmp(write_data, read_data, sizeof(write_data)) == 0);
179
180 bytes_written = 0;
181 while (bytes_written < sizeof(write_data))
182 {
183 bytes_written += server_socket.write_some(
184 boost::asio::buffer(write_buf + bytes_written));
185 }
186
187 bytes_read = 0;
188 while (bytes_read < sizeof(read_data))
189 {
190 bytes_read += client_socket.read_some(
191 boost::asio::buffer(read_buf + bytes_read));
192 }
193
194 BOOST_ASIO_CHECK(bytes_written == sizeof(write_data));
195 BOOST_ASIO_CHECK(bytes_read == sizeof(read_data));

Callers

nothing calls this directly

Calls 9

bufferFunction · 0.85
endpointClass · 0.50
local_endpointMethod · 0.45
addressMethod · 0.45
connectMethod · 0.45
acceptMethod · 0.45
write_someMethod · 0.45
read_someMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected