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

Function session

demo_example/async_echo_server.cpp:23–48  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

21using asio::ip::tcp;
22
23async_simple::coro::Lazy<void> session(tcp::socket sock) {
24 int msg_index = 0;
25 for (;;) {
26 const size_t max_length = 1024;
27 char data[max_length];
28 auto [error, length] =
29 co_await async_read_some(sock, asio::buffer(data, max_length));
30 msg_index++;
31 if (error == asio::error::eof) {
32 std::cout << "Remote client closed at message index: "
33 << msg_index - 1 << ".\n";
34 break;
35 } else if (error) {
36 std::cout << error.message() << '\n';
37 throw asio::system_error(error);
38 }
39
40 co_await async_write(sock, asio::buffer(data, length));
41 }
42
43 std::error_code ec;
44 sock.shutdown(asio::ip::tcp::socket::shutdown_both, ec);
45 sock.close(ec);
46
47 std::cout << "Finished echo message, total: " << msg_index - 1 << ".\n";
48}
49
50async_simple::coro::Lazy<void> start_server(asio::io_context& io_context,
51 unsigned short port,

Callers 1

start_serverFunction · 0.70

Calls 7

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

Tested by

no test coverage detected