MCPcopy Create free account
hub / github.com/alibaba/async_simple / start

Function start

demo_example/async_echo_client.cpp:23–57  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

21using asio::ip::tcp;
22
23async_simple::coro::Lazy<void> start(asio::io_context &io_context,
24 std::string host, std::string port) {
25 asio::ip::tcp::socket socket(io_context);
26 auto ec = co_await async_connect(io_context, socket, host, port);
27 if (ec) {
28 std::cout << "Connect error: " << ec.message() << '\n';
29 throw asio::system_error(ec);
30 }
31 std::cout << "Connect to " << host << ":" << port << " successfully.\n";
32 const int max_length = 1024;
33 char write_buf[max_length] = {"hello async_simple"};
34 char read_buf[max_length];
35 const int count = 100000;
36 for (int i = 0; i < count; ++i) {
37 co_await async_write(socket, asio::buffer(write_buf, max_length));
38 auto [error, reply_length] = co_await async_read_some(
39 socket, asio::buffer(read_buf, max_length));
40 if (error == asio::error::eof) {
41 std::cout << "eof at message index: " << i << '\n';
42 break;
43 } else if (error) {
44 std::cout << "error: " << error.message()
45 << ", message index: " << i << '\n';
46 throw asio::system_error(error);
47 }
48
49 // handle read data as your wish.
50 }
51
52 std::cout << "Finished send and receive " << count
53 << " messages, client will close.\n";
54 std::error_code ignore_ec;
55 socket.shutdown(asio::ip::tcp::socket::shutdown_both, ignore_ec);
56 io_context.stop();
57}
58
59int main(int argc, char *argv[]) {
60 try {

Callers 2

mainFunction · 0.70
directlyStartFunction · 0.50

Calls 8

system_errorClass · 0.85
messageMethod · 0.80
async_connectFunction · 0.70
async_writeFunction · 0.70
async_read_someFunction · 0.70
bufferFunction · 0.50
shutdownMethod · 0.45
stopMethod · 0.45

Tested by

no test coverage detected