Crates.io | peernet |
lib.rs | peernet |
version | 0.4.0 |
source | src |
created_at | 2023-12-04 18:23:23.820093 |
updated_at | 2024-10-05 15:37:25.043016 |
description | easy and reliable p2p networking |
homepage | |
repository | https://github.com/dvc94ch/peernet |
max_upload_size | |
id | 1057904 |
size | 34,227 |
A p2p networking library
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