Coder Social home page Coder Social logo

utp's Introduction

utp

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

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();
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.