Skip to content

Parsnip113/rio

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rio

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

Quick Start

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

Build

mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
cmake --build . -j$(nproc)
ctest --output-on-failure

Requires Linux, C++20 (GCC 11+ / Clang 14+), CMake 3.15+. Dependencies are fetched automatically.

Documentation

  • Getting Started — build, examples, error handling, thread pool usage
  • Architecture — component design, data flow, design decisions

Components

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

Examples

See examples/ for runnable demos:

  • echo_server — echo back received data
  • discard_server — receive and discard data
  • connect_client — TCP client with timeout
  • timer_demo — periodic timer

License

MIT

About

A C++20 coroutine-based network library for Linux

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors