uni-stream
uni-stream is a Rust library for unified operation of TcpStream and UdpStream, designed to make your service support UDP and TCP (such as proxy services) with a single code implementation. On top of that the library provides the ability to customize dns server resolution.
Features
-
Generic:
uni-streamprovides an abstraction of UDP and TCP streams, which is exposed through some traits, making it easy for users to make secondary abstractions. -
Customizable:
uni-streamprovides functions that allow users to customize the resolution of dns services for TCP or UDP connections. -
Datagram-first UDP API: UDP is message-oriented.
uni-streamexposes explicitrecv_datagram/send_datagramAPIs so callers can preserve UDP packet boundaries. -
Owned split for spawn:
into_split()returns owned read/write halves ('static + Send) for use intokio::spawnor other cross-task workflows.
Usage
To use uni-stream in your Rust project, simply add it as a dependency in your Cargo.toml file:
[]
= "*"
Minimum supported Rust version (MSRV): 1.75.
Then, you can import and use the library in your Rust code.The following is a generic-based implementation of echo_server:
For UDP datagram-preserving forwarding, see examples/udp_datagram_echo.rs.
use AsyncReadExt;
use AsyncWriteExt;
use ListenerProvider;
use StreamAccept;
use TcpListenerProvider;
use UdpListenerProvider;
async
async
Owned split (spawn-friendly)
When you need to move IO halves into a spawned task, use into_split() to get owned halves:
use ;
async
UDP datagram interface (important)
TCP is a byte stream; UDP is message-oriented. If you treat UDP like a byte stream, packet boundaries are lost and higher-level protocols (e.g. QUIC, DNS, RTP, game traffic) can break.
uni-stream exposes explicit datagram APIs on UDP halves:
UdpStreamReadHalf::recv_datagram() -> returns exactly one UDP packet
UdpStreamWriteHalf::send_datagram() -> sends exactly one UDP packet
Simple UDP datagram echo (see examples/udp_datagram_echo.rs):
use Error;
use UdpListener;
async
Why boundaries matter (short version)
- TCP:
read()can return any number of bytes. It can merge or split packets. - UDP: each
recvreturns exactly one datagram.
So a UDP tunnel must preserve packet boundaries; uni-stream provides the APIs to do that safely.
Visual intuition:
TCP stream:
bytes: [A][B][C] -> read() may return [A+B] or [B+C] or [A] then [B+C]
UDP datagrams:
recv() returns exactly [A] then exactly [B] then exactly [C]
Customized dns resolution servers:
use set_custom_dns_server;
// use google and alibaba dns server
set_custom_dns_server.unwrap;
Customize Udp Timeout:
use set_custom_timeout;
use Duration;
let timeout = from_secs;
set_custom_timeout;
Or don't set any timeout on UdpStream
[]
= { = "0.1.0", = false }
For more details on how to use uni-stream, please refer to the examples.
Contributing
Contributions to uni-stream are welcome! If you would like to contribute to the library, please follow the standard Rust community guidelines for contributing, including opening issues, submitting pull requests, and providing feedback.
License
uni-stream is licensed under the MIT License, which allows for free use, modification, and distribution, subject to the terms and conditions outlined in the license.
We hope that uni-stream is useful for your projects! If you have any questions or need further assistance, please don't hesitate to contact us or open an issue in the repository.