7 个版本
0.2.0 | 2024 年 6 月 25 日 |
---|---|
0.1.5 | 2024 年 5 月 14 日 |
0.1.4 | 2024 年 3 月 21 日 |
0.1.2 | 2024 年 2 月 5 日 |
0.1.0 | 2023 年 12 月 4 日 |
#1896 在 网络编程
35KB
965 行
P2P
一个 P2P 网络库
- 传输: quinn
- 打洞和转发: iroh-net
- dht: pkarr
- mdns: simple-mdns
示例
const ALPN: &[u8] = b"/p2p/ping/1";
#[derive(Debug, Deserialize, Serialize)]
pub struct Ping(u16);
#[derive(Debug, Deserialize, Serialize)]
pub struct Pong(u16);
pub struct PingPong;
impl Protocol for PingPong {
const ID: u16 = 0;
const REQ_BUF: usize = 1024;
const RES_BUF: usize = 1024;
type Request = Ping;
type Response = Pong;
}
impl RequestHandler<Self> for PingPong {
fn request(
&self,
_peer_id: PeerId,
request: <Self as Protocol>::Request,
response: oneshot::Sender<<Self as Protocol>::Response>,
) -> Result<()> {
response
.send(Pong(request.0))
.map_err(|_| anyhow::anyhow!("response channel closed"))?;
Ok(())
}
}
let mut builder = ProtocolHandler::builder();
builder.register_request_handler(PingPong);
let handler = builder.build();
let mut builder = Endpoint::builder(ALPN.to_vec());
builder.handler(handler);
let endpoint = builder.build().await?;
let pong = endpoint.request::<PingPong>(&peer, &Ping(42)).await?;
assert_eq!(pong.0, 42);
许可证
Apache-2.0 + MIT
依赖项
~42–76MB
~1.5M SLoC