smux
A Rust implementation of the smux protocol - a stream multiplexing library that enables multiple logical streams over a single connection.
Features
- Async/await support: Built on tokio for high-performance async I/O
- Multiple streams: Create many logical streams over one connection
- Flow control: Built-in sliding window flow control
- Keep-alive: Configurable keep-alive mechanism
- Interoperability: Compatible with the Go smux protocol
- Zero-copy: Efficient data handling with minimal allocations
Quick Start
Add this to your Cargo.toml:
[]
= "0.1"
= { = "1.0", = ["full"] }
Server Example
use ;
use TcpListener;
use ;
async
Client Example
use ;
use TcpStream;
use ;
async
Configuration
Customize the session behavior with ConfigBuilder:
use ;
use Duration;
let config = new
.version
.keep_alive_interval
.keep_alive_timeout
.max_frame_size // 64KB frames
.max_receive_buffer // 8MB buffer
.enable_keep_alive
.build
.expect;
Configuration Options
| Option | Default | Description |
|---|---|---|
version |
1 | Protocol version |
keep_alive_interval |
10s | Interval between keep-alive probes |
keep_alive_timeout |
30s | Timeout before considering connection dead |
max_frame_size |
32KB | Maximum size of a single frame |
max_receive_buffer |
4MB | Maximum receive buffer size |
max_stream_buffer |
64KB | Per-stream buffer size |
enable_keep_alive |
true | Enable/disable keep-alive mechanism |
Protocol Compatibility
This implementation is compatible with the Go smux library. You can use Rust clients with Go servers and vice versa.
Performance
The library is designed for high performance with:
- Zero-copy frame processing where possible
- Efficient async I/O with tokio
- Lock-free stream management using atomic operations
- Batched frame sending to reduce syscalls
Error Handling
The library uses the SmuxError enum for comprehensive error handling:
use ;
match session.open_stream.await
License
This project is distributed under the terms of MIT.
See LICENSE for details.