test fiber demo.

This commit is contained in:
zhengshuxin 2024-09-15 22:14:18 +08:00
parent 346780f330
commit faf8a24027
2 changed files with 15 additions and 13 deletions

View File

@ -270,17 +270,20 @@ void run(void) {
go[&] { // Create one server coroutine to wait for connection. go[&] { // Create one server coroutine to wait for connection.
while (true) { while (true) {
acl::socket_stream* conn = server.accept(); acl::shared_stream conn = server.shared_accept();
if (conn) { if (conn == nullptr) {
go[=] { // Create one client coroutine to handle the connection. break;
}
go[conn] { // Create one client coroutine to handle the connection.
while (true) {
char buf[256]; char buf[256];
int ret = conn->read(buf, sizeof(buf), false); int ret = conn->read(buf, sizeof(buf), false);
if (ret > 0) { if (ret <= 0 || conn->write(buf, ret) != ret) {
(void) conn->write(buf, ret); break;
} }
delete conn; }
}; };
}
} }
}; };

View File

@ -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; acl::string buf;
while (true) { while (true) {
if (readable) { if (readable) {
@ -33,18 +33,17 @@ static void client_echo(acl::socket_stream* conn, bool readable) {
} }
//acl::fiber::delay(1000); //acl::fiber::delay(1000);
} }
delete conn;
} }
static void server_listen(acl::server_socket& ss, bool readable) { static void server_listen(acl::server_socket& ss, bool readable) {
while (true) { while (true) {
acl::socket_stream* conn = ss.accept(); acl::shared_stream conn = ss.shared_accept();
if (conn == NULL) { if (conn == nullptr) {
printf("accept error %s\r\n", acl::last_serror()); printf("accept error %s\r\n", acl::last_serror());
break; break;
} }
go[=] { go[conn, readable] {
client_echo(conn, readable); client_echo(conn, readable);
}; };
} }