9 releases

0.1.0-alpha.8 Oct 5, 2023
0.1.0-alpha.7 Sep 5, 2023
0.1.0-alpha.6 Aug 11, 2023
0.1.0-alpha.5 Jun 6, 2023
0.1.0-alpha.3 Mar 25, 2023

#795 in Network programming

Download history 80/week @ 2024-07-27 1/week @ 2024-08-03 4/week @ 2024-09-21 3/week @ 2024-09-28

949 downloads per month

MIT license

175KB
4K SLoC

utp

A Rust library for the uTorrent transport protocol (uTP).

🚧 WARNING: UNDER CONSTRUCTION 🚧

This library is currently unstable, with known issues. Use at your own discretion.

Usage

use std::net::SocketAddr;

use utp_rs::conn::ConnectionConfig;
use utp_rs::socket::UtpSocket;
use utp_rs::udp::AsyncUdpSocket;

#[tokio::main]
fn main() {
	// bind a standard UDP socket. (transport is over a `tokio::net::UdpSocket`.)
	let socket_addr = SocketAddr::from(([127, 0, 0, 1], 3400));
	let udp_based_socket = UtpSocket::bind(socket_addr).await.unwrap();

	// bind a custom UDP socket. here we assume `CustomSocket` implements `AsyncUdpSocket`.
	let async_udp_socket = CustomSocket::new(..);
	let custom_socket = UtpSocket::with_socket(async_udp_socket).await.unwrap();

	// connect to a remote peer over uTP.
	let remote = SocketAddr::from(..);
	let config = ConnectionConfig::default();
	let mut stream = udp_socket::connect(remote, config).await.unwrap();

	// write data to the remote peer over the stream.
	let data = vec![0xef; 2048];
	let n = stream.write(data.as_slice()).await.unwrap();

	// accept a connection from a remote peer.
	let config = ConnectionConfig::default();
	let stream = udp_socket.accept(config).await;

	// read data from the remote peer until the peer indicates there is no data left to write.
	let mut data = vec![];
	let n = stream.read_to_eof(&mut data).await.unwrap();
}

Dependencies

~4–13MB
~143K SLoC