tokio-raknet
tokio-raknet is a high-performance, asynchronous implementation of the RakNet protocol written in pure Rust. Built on top of the Tokio runtime, it is designed to provide a modern, ergonomic API for building robust UDP-based networked applications, games (including Minecraft: Bedrock Edition), and services.
Features
- 🚀 Fully Asynchronous: Built from the ground up with
async/awaitfor high concurrency. - 🛡️ Reliability Layers: Full support for all RakNet reliability types (Reliable, Unreliable, Ordered, Sequenced, etc.).
- 📦 Fragmentation: Automatic splitting and reassembly of large packets transparent to the user.
- 🔒 Security & Safety: Bounded buffers and queues to prevent memory exhaustion attacks (gap flooding, ACK withholding).
- ⚙️ Highly Configurable: Fine-tune MTU, timeouts, buffer limits, and protocol constraints via
RaknetListenerConfigandRaknetStreamConfig. - 🔧 Simple API: A high-level abstraction that feels like working with a TCP stream, but with the control of UDP.
- 🔍 Tracing Support: Deep integration with
tracingfor low-overhead debugging and performance profiling.
Installation
Add this to your Cargo.toml:
[]
= { = "1", = ["full"] }
= "0.1"
= "1"
Usage
Creating a Client
Connecting to a server is straightforward. The client handles the offline handshake, MTU negotiation, and session setup automatically.
use RaknetStream;
use Bytes;
async
Creating a Server
The RaknetListener works similarly to a TcpListener, providing a stream of incoming connections.
use RaknetListener;
async
Configuration
You can customize the behavior of clients and listeners using RaknetStreamConfig and RaknetListenerConfig. This allows tuning for specific network conditions or security requirements.
Client with Custom MTU and Timeout:
use ;
use Duration;
async
Server with Connection Limits:
use ;
async
Advanced Sending (Reliability & Channels)
For games and real-time applications, you often need fine-grained control over how packets are delivered. The Message struct allows you to configure reliability, ordering channels, and priority.
use RaknetStream;
use ;
use Message;
async
Examples
We provide several fully runnable examples in the examples/ directory:
basic_ping: A minimal server/client setup exchanging simple text payloads.ping_pong: Shows back-and-forth communication latency.minecraft_start: Demonstrates connecting to a real Minecraft: Bedrock Edition server (verifies handshake and MTU negotiation).
To run an example:
Contributing
Contributions are welcome! Please ensure that any changes pass existing tests and include new tests where appropriate. This project uses standard cargo fmt and cargo clippy settings.
License
This project is licensed under the MIT License.