Bitfold
A modern, high-performance reliable UDP networking library for Rust, inspired by ENet.
Bitfold provides flexible delivery guarantees, automatic fragmentation, congestion control, and multi-channel communication—ideal for games and real-time applications.
Features
- Multiple delivery modes - Reliable, unreliable, ordered, sequenced, and unsequenced
- Multi-channel support - Up to 255 independent channels per connection
- Automatic fragmentation - Handles large packets transparently with timeout-based cleanup
- PMTU discovery - Adaptive MTU detection (enabled by default)
- Congestion control - RTT-based adaptive throttling
- Bandwidth limiting - Per-peer bandwidth throttling (optional)
- Compression - Optional LZ4 or Zlib compression
- Data integrity - Optional CRC32 checksums
- Zero-copy design - Efficient buffer management with Arc-based sharing
- Command batching - Multiple operations packed into single UDP packets
Quick Start
Add to your Cargo.toml:
[]
= "0.1"
Basic Example
use ;
use Instant;
Delivery Guarantees
use Packet;
// Unreliable (fire-and-forget, lowest latency)
let pkt = unreliable;
// Reliable (guaranteed delivery, unordered)
let pkt = reliable_unordered;
// Reliable + Ordered (TCP-like)
let pkt = reliable_ordered;
// Sequenced (only latest packet delivered)
let pkt = reliable_sequenced;
// Unsequenced (prevents duplicates, allows reordering)
let pkt = unsequenced;
Multi-Channel Communication
Use channels to prioritize different traffic types independently:
use ;
// Configure channel count
let mut config = default;
config.channel_count = 4;
let mut host = bind_with_config?;
// Send on different channels
host.send?; // High priority
host.send?; // State sync
host.send?; // Chat
Configuration
use ;
use Duration;
let mut config = default;
// Connection settings
config.idle_connection_timeout = from_secs;
config.heartbeat_interval = Some;
// Channels
config.channel_count = 8;
// Fragmentation (PMTU discovery enabled by default)
config.max_packet_size = 32 * 1024; // 32 KB
config.use_pmtu_discovery = true; // Adaptive MTU (default: true)
config.pmtu_min = 576; // Minimum MTU
config.pmtu_max = 1400; // Maximum MTU
// Bandwidth limiting (0 = unlimited)
config.outgoing_bandwidth_limit = 0;
config.incoming_bandwidth_limit = 0;
// Compression (optional)
config.compression = None; // None, Lz4, or Zlib
config.compression_threshold = 128;
// Checksums (optional)
config.use_checksums = false;
let host = bind_with_config?;
Examples
Run the included examples:
# Server
# Client
Event Loop Integration
Manual Polling (Game Loop)
use ;
let mut host = bind_any?;
loop
Automatic Polling (Background Thread)
use thread;
let mut host = bind_any?;
let event_rx = host.get_event_receiver;
// Start background polling
spawn;
// Main thread handles events
for event in event_rx.iter
Architecture
Bitfold is organized into focused modules:
bitfold
├── host - Socket I/O and session management
├── peer - Per-peer state and command batching
├── protocol - Protocol logic, ACKs, fragmentation, congestion
├── core - Configuration and shared types
└── utilities - Helper functions
The protocol layer is purely functional (no I/O), making it easy to test and reason about.
Best Practices
- Use unreliable packets for high-frequency position updates
- Use reliable ordered for critical game events
- Use channels to separate different traffic types
- Enable PMTU discovery for optimal bandwidth (default)
- Poll regularly - call
manual_poll()at least once per frame - Clean up fragments - call
cleanup_stale_fragments()periodically in long-running apps
// In your main loop (recommended once per second)
let now = now;
for in host.peers_mut
Testing
Contributing
We code with AI. All implementation is done through AI-assisted development to maintain consistency and quality.
We Welcome
- Bug reports with reproduction steps
- Performance proposals with benchmarks
- Feature requests with use cases
- PRs for config files (
clippy.toml,.rustfmt.toml, CI/CD, documentation)
We Don't Accept
PRs that modify source code - all implementation is done via our AI workflow.
How to Contribute
File an issue → We implement → You get credited
License
MIT