Crates.io | rustp2p-transport |
lib.rs | rustp2p-transport |
version | 0.1.0 |
source | src |
created_at | 2025-02-21 06:41:20.808555+00 |
updated_at | 2025-02-21 06:41:20.808555+00 |
description | A Rust library for building a decentralized logical network, allowing nodes to communicate using IP, TCP, UDP, and ICMP protocols. |
homepage | |
repository | https://github.com/rustp2p/rustp2p-transport |
max_upload_size | |
id | 1563663 |
size | 75,587 |
rustp2p-transport
creates a decentralized communication platform.
It organizes network nodes into a virtual LAN,
allowing users to communicate through TCP/UDP APIs similar to those in the standard library.
The nodes in the rustp2p-transport
are decentralized, and all nodes are equal.
This demonstrates a simple implementation where Node B accesses Node A.
Cargo.toml
:
[dependencies]
rustp2p-transport = {version = "0.1", features = ["global"]}
Node-A(192.168.0.2):
use tokio::io::AsyncReadExt;
use rustp2p_transport::TransportBuilder;
#[tokio::main]
async fn main() -> std::io::Result<()> {
let ip = std::net::Ipv4Addr::new(10, 0, 0, 2);
let port = 12345;
TransportBuilder::default()
.endpoint(ip)
.listen_port(port)
.build_context()
.await?;
let mut tcp_listener = tcp_ip::tcp::TcpListener::bind("0.0.0.0:8080").await?;
println!("***** tcp_listener accept *****");
loop {
let (mut stream, addr) = tcp_listener.accept().await?;
println!("***** accept stream {addr} *****");
tokio::spawn(async move {
let mut buf = vec![0; 65536];
loop {
match stream.read(&mut buf).await {
Ok(n) => {
println!("{addr}: {:?}", String::from_utf8(buf[..n].to_vec()));
}
Err(e) => {
println!("error addr={addr} {e:?}");
return;
}
}
}
});
}
}
Node-B:
use rustp2p_transport::TransportBuilder;
#[tokio::main]
async fn main() -> std::io::Result<()> {
let node_a = std::net::Ipv4Addr::new(10, 0, 0, 2);
let node_b = std::net::Ipv4Addr::new(10, 0, 0, 3);
let port = 12345;
TransportBuilder::default()
.endpoint(node_b)
.listen_port(port)
.peers(vec!["tcp://192.168.0.2:12345".parse().unwrap()])
.build_context()
.await?;
let tcp_stream = tcp_ip::tcp::TcpStream::connect(format!("{node_a}:8080")).await?;
// Use `tcp_stream` to communicate with Node A.
Ok(())
}