Skip to content

Commit

Permalink
Update readme example
Browse files Browse the repository at this point in the history
  • Loading branch information
uchenily committed May 17, 2024
1 parent b04a3be commit ff15c6b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,28 @@ C++20 coroutines + libuv

tcp echo server

```C++
```c++
#include "uvio/core.hpp"
#include "uvio/net.hpp"

using namespace uvio;
using namespace uvio::net;

// Ignore errors
auto process(TcpStream stream) -> Task<> {
while (true) {
std::array<char, 1024> buf{};

auto nread = co_await stream.read(buf);
if (nread < 0) {
auto rret = co_await stream.read(buf);
if (!rret) {
break;
}

console.info("Received: `{}`", buf.data());
co_await stream.write(buf.data());
auto wret = co_await stream.write(buf);
if (!wret) {
break;
}
}
co_return;
}
Expand All @@ -44,15 +49,14 @@ auto server() -> Task<> {
listener.bind(host, port);
console.info("Listening on {}:{} ...", host, port);
while (true) {
auto stream = co_await listener.accept();
auto stream = (co_await listener.accept()).value();
spawn(process(std::move(stream)));
}
}

auto main() -> int {
block_on(server());
}

```

```bash
Expand Down
1 change: 1 addition & 0 deletions examples/meson.build
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
all_examples_sources = [
'echo_server.cpp',
]

foreach source: all_examples_sources
Expand Down

0 comments on commit ff15c6b

Please sign in to comment.