This crate is aimed to be rust p2p communication framework.
When a socket has been "connected" to the other client, the "connection" is directly to the client, not the server. The server is not used to pass through packages, only to pair the two clients together. As you should know, since it's the whole purpose of UDP hole punching.
To implement a p2p framework, the crate provide 2 sub function:
- the server: Use this function if you want to run a standard server which waits for client-callee to send the identification for registry, then client-caller send "Ask" command with client-callee id. The server will sends the each clients' IP address and external port number to each other. It also has some basic protection against receive overtime etc. If you want something more customisable, take a look at make_match
,
- the client(includes the caller and callee): The single udp transfer packet size in this crate is defined by "pub const Conf.size = 1024;". However,the crate will split data beyond size automatically when sending, assembly to integrate automatically when receiving, ask resending when packets is not complete.
## Quick Start
main.rs ``` use asyncstd::task::blockon; pub mod server; pub mod client;
extern crate anyhow; use client::test::*; use server::process::testswapserver; fn main() {
// block_on(test_callee_listen());
// block_on(test_caller_api());
// block_on(test_swap_server());
}
``` - testswapserver
pub async fn test_swap_server() {
let host = "0.0.0.0:xxxx";
let res= make_match(host).await;
match res{
Ok(())=>dbg!("everything ok"),
Err(e)=>dbg!(&e.to_string()),
};
}
- testcallee/callerlisten
```
fn readfileasu8(inputfile: &str) -> anyhow::Result
fn writefileasu8(pathstr: &str, binary: &Vec
pub fn testcalleelisten() -> anyhow::Result<()> { let mut conf = Conf::default(); conf.swapserver = "x.x.x.x:xxxx".tostring(); conf.id = "xx".tostring(); conf.dbpath="./data/callee".to_string(); conf.set();
init_udp();
std::thread::spawn(|| {
listen();
});
loop {
std::thread::sleep(Duration::from_secs(10));
let (addr, v) = rec_from();
if &v.len() > &0 {
dbg!("callee rec res");
write_file_as_u8("/home/b.exe", &v)?;
let back = "callee got you".as_bytes().to_vec();
send(&back, addr);
}
};
Ok(())
}
pub fn testcallerapi() -> anyhow::Result<()> { let mut conf = Conf::default(); conf.swapserver = "x.x.x.x:xxxx".tostring(); conf.dbpath="./data/caller".tostring(); conf.set(); initudp(); std::thread::spawn(|| { listen(); }); askpeeraddress("xx"); std::thread::sleep(Duration::fromsecs(9)); let addr = readpeeraddress()?; dbg!(&addr); dbg!("begin");
let msg = read_file_as_u8("D://a.exe")?;
let sess=send(&msg, addr);
loop {
let v = rec_one(addr,sess);
if v.len() > 0 {
let s = String::from_utf8_lossy(&v);
dbg!("caller rec res");
}
std::thread::sleep(Duration::from_secs(4));
}
Ok(())
}
```