https://github.com/dvc94ch/peernet
easy and reliable p2p networking
https://github.com/dvc94ch/peernet
Last synced: 2 months ago
JSON representation
easy and reliable p2p networking
- Host: GitHub
- URL: https://github.com/dvc94ch/peernet
- Owner: dvc94ch
- Created: 2023-10-03T11:49:05.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2025-03-22T09:18:12.000Z (3 months ago)
- Last Synced: 2025-04-02T04:08:42.870Z (3 months ago)
- Language: Rust
- Homepage:
- Size: 50.8 KB
- Stars: 38
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-iroh - Peernet - Easy & reliable p2p networking. (Frameworks)
- awesome-iroh - Peernet - Easy & reliable p2p networking. (Frameworks)
README
# P2P
A p2p networking library- transport: [quinn](https://github.com/quinn-rs/quinn)
- hole punching and relaying: [iroh-net](https://github.com/n0-computer/iroh)
- dht: [pkarr](https://github.com/nuhvi/pkarr)
- mdns: [simple-mdns](https://github.com/balliegojr/simple-dns)## Example
```rust
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 for PingPong {
fn request(
&self,
_peer_id: PeerId,
request: ::Request,
response: oneshot::Sender<::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::(&peer, &Ping(42)).await?;
assert_eq!(pong.0, 42);
```## License
Apache-2.0 + MIT