| Crates.io | vt-muxer |
| lib.rs | vt-muxer |
| version | 0.1.0 |
| created_at | 2025-04-29 13:36:20.696766+00 |
| updated_at | 2025-04-29 13:36:20.696766+00 |
| description | Mux one TCP stream into many |
| homepage | |
| repository | https://github.com/Vrtgs/vt-muxer.git |
| max_upload_size | |
| id | 1653536 |
| size | 102,016 |
the Vrtgs Tcp Muxer
[dependencies]
vt-muxer = "0.1.0"
use vt_muxer::{MuxListener, MuxPipe, MuxConnection};
use tokio::net::TcpStream;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
// Server side
async fn run_server(tcp_stream: TcpStream) {
let mux = MuxListener::new(tcp_stream);
// Accept new multiplexed connections
while let Ok(mut connection) = mux.accept().await {
tokio::spawn(async move {
let mut buf = vec![0; 1024];
while let Ok(n) = connection.read(&mut buf).await {
if n == 0 { break; }
// Handle data...
}
});
}
}
// Client side
async fn run_client(tcp_stream: TcpStream) {
let mux = MuxPipe::new(tcp_stream);
// Create a new multiplexed connection
let addr = "127.0.0.1:12345".parse().unwrap();
let mut connection = mux.add_connection(addr).await.unwrap();
// Use the connection
connection.write_all(b"Hello world!").await.unwrap();
}
The server-side multiplexer that accepts new logical connections:
new(stream: TcpStream) -> MuxListener - Create a new multiplexer from a TCP streamaccept() -> Future<Result<MuxConnection>> - Accept a new multiplexed connectionadd_connection(addr: SocketAddr) -> Future<Result<MuxConnection>> - Explicitly add a new connectionThe client-side interface for creating new multiplexed connections:
new(stream: TcpStream) -> MuxPipe - Create a new multiplexer from a TCP streamadd_connection(addr: SocketAddr) -> Future<Result<MuxConnection>> - Create a new multiplexed connectionRepresents a single Buffered multiplexed connection:
Implements AsyncRead and AsyncWrite for standard async I/O operations
addr() -> SocketAddr - Get the address associated with this connection
MuxConnection should always be shutdown, otherwise a task will be spawned to shutdown the connection
The library provides optimized address types that merge Ipv4 and Ipv6 addresses into one using Ipv6 mapped/compatible addresses for network operations:
SocketAddr - A compact socket address representationIpAddr - A lightweight IP address typebytemuck