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.
|
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;
|
}
|
||||||
};
|
};
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -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);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user