From faf8a24027b2928b9bb4d2a2be42f6be49a086e7 Mon Sep 17 00:00:00 2001 From: zhengshuxin Date: Sun, 15 Sep 2024 22:14:18 +0800 Subject: [PATCH] test fiber demo. --- README.md | 19 +++++++++++-------- lib_fiber/samples-c++1x/server/main.cpp | 9 ++++----- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index fe85b6f08..6552fb583 100644 --- a/README.md +++ b/README.md @@ -270,17 +270,20 @@ void run(void) { go[&] { // Create one server coroutine to wait for connection. while (true) { - acl::socket_stream* conn = server.accept(); - if (conn) { - go[=] { // Create one client coroutine to handle the connection. + acl::shared_stream conn = server.shared_accept(); + if (conn == nullptr) { + break; + } + + go[conn] { // Create one client coroutine to handle the connection. + while (true) { char buf[256]; int ret = conn->read(buf, sizeof(buf), false); - if (ret > 0) { - (void) conn->write(buf, ret); + if (ret <= 0 || conn->write(buf, ret) != ret) { + break; } - delete conn; - }; - } + } + }; } }; diff --git a/lib_fiber/samples-c++1x/server/main.cpp b/lib_fiber/samples-c++1x/server/main.cpp index 2a7517340..738efcdf0 100644 --- a/lib_fiber/samples-c++1x/server/main.cpp +++ b/lib_fiber/samples-c++1x/server/main.cpp @@ -4,7 +4,7 @@ ////////////////////////////////////////////////////////////////////////////// -static void client_echo(acl::socket_stream* conn, bool readable) { +static void client_echo(acl::shared_stream conn, bool readable) { acl::string buf; while (true) { if (readable) { @@ -33,18 +33,17 @@ static void client_echo(acl::socket_stream* conn, bool readable) { } //acl::fiber::delay(1000); } - delete conn; } static void server_listen(acl::server_socket& ss, bool readable) { while (true) { - acl::socket_stream* conn = ss.accept(); - if (conn == NULL) { + acl::shared_stream conn = ss.shared_accept(); + if (conn == nullptr) { printf("accept error %s\r\n", acl::last_serror()); break; } - go[=] { + go[conn, readable] { client_echo(conn, readable); }; }