A C++20 coroutine-based network library for Linux.
- Coroutine-native:
co_await sock.AsyncRead(buf)— no callbacks, no futures - Symmetric transfer: O(1) stack depth regardless of coroutine chain depth
- Edge-triggered epoll: high performance with non-blocking I/O
- Timeout support: all async operations accept optional timeouts
- Thread pool integration: offload CPU work without blocking the event loop
#include "rio/buffer.hpp"
#include "rio/io_context.hpp"
#include "rio/net/listener.hpp"
#include "rio/net/protocol.hpp"
#include "rio/net/socket.hpp"
#include "rio/task.hpp"
using namespace rio;
using namespace rio::net;
Task<void> HandleConnection(IoContext& ctx, Socket<Tcp> conn) {
Buffer buf;
while (true) {
auto n = co_await conn.AsyncRead(buf);
if (!n) co_return;
while (buf.readableBytes() > 0) {
auto w = co_await conn.AsyncWrite(buf);
if (!w) co_return;
}
}
}
Task<void> AcceptLoop(IoContext& ctx, Listener<Tcp>& listener) {
while (true) {
auto result = co_await listener.AsyncAccept();
if (!result) continue;
Spawn(ctx, HandleConnection(ctx, std::move(*result)));
}
}
int main() {
auto ctx = std::move(*IoContext::Create());
auto listener = std::move(
*Listener<Tcp>::Create(ctx, Endpoint<Tcp>(AddressV4::Any(), 8080)));
Spawn(ctx, AcceptLoop(ctx, listener));
ctx.Run();
}mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
cmake --build . -j$(nproc)
ctest --output-on-failureRequires Linux, C++20 (GCC 11+ / Clang 14+), CMake 3.15+. Dependencies are fetched automatically.
- Getting Started — build, examples, error handling, thread pool usage
- Architecture — component design, data flow, design decisions
| Component | Description |
|---|---|
Task<T> |
Lazy coroutine with symmetric transfer |
IoContext |
Event loop (epoll + timer + cross-thread post) |
EpollPoller |
Edge-triggered epoll wrapper with deferred deletion |
TimingWheel |
O(1) timer wheel (100ms tick, 1024 slots) |
Buffer |
Auto-expanding linear buffer for network I/O |
Socket<Tcp> |
Async read/write/connect with timeout |
Listener<Tcp> |
Async accept |
ThreadPool |
CPU offload with runInPool() awaiter |
See examples/ for runnable demos:
echo_server— echo back received datadiscard_server— receive and discard dataconnect_client— TCP client with timeouttimer_demo— periodic timer
MIT