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.
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;
};
}
}
};
}
};

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;
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);
};
}