mirror of
https://gitee.com/acl-dev/acl.git
synced 2024-11-29 18:37:41 +08:00
test fiber demo.
This commit is contained in:
parent
346780f330
commit
faf8a24027
19
README.md
19
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;
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -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);
|
||||
};
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user