open-humanity
Privacy-first peer assistance network for sovereign autonomous agents
Brought by drakestapleton
open-humanity / crates / beacon-client / src / network.rs
| 1 | //! High-performance UDP datagram transport for Open Humanity nanobeacons. |
| 2 | //! |
| 3 | //! Enforces zero-copy MTU packet limits (<= 1,200 bytes) for 0-RTT broadcast |
| 4 | //! across peer rings. |
| 5 | |
| 6 | use beacon_core::schema::{BeaconError, DistressNanobeacon, ResolutionCapsule, MAX_DATAGRAM_SIZE}; |
| 7 | use std::net::SocketAddr; |
| 8 | use std::sync::Arc; |
| 9 | use tokio::net::UdpSocket; |
| 10 | use thiserror::Error; |
| 11 | |
| 12 | #[derive(Error, Debug)] |
| 13 | pub enum NetworkError { |
| 14 | #[error("IO error: {0}")] |
| 15 | Io(#[from] std::io::Error), |
| 16 | #[error("Beacon protocol error: {0}")] |
| 17 | Protocol(#[from] BeaconError), |
| 18 | #[error("Packet exceeds maximum MTU size: {0} > {1}")] |
| 19 | MtuExceeded(usize, usize), |
| 20 | } |
| 21 | |
| 22 | #[derive(Clone)] |
| 23 | pub struct BeaconTransport { |
| 24 | socket: Arc<UdpSocket>, |
| 25 | } |
| 26 | |
| 27 | impl BeaconTransport { |
| 28 | pub async fn bind<A: tokio::net::ToSocketAddrs>(addr: A) -> Result<Self, NetworkError> { |
| 29 | let socket = UdpSocket::bind(addr).await?; |
| 30 | Ok(Self { |
| 31 | socket: Arc::new(socket), |
| 32 | }) |
| 33 | } |
| 34 | |
| 35 | pub fn local_addr(&self) -> Result<SocketAddr, NetworkError> { |
| 36 | Ok(self.socket.local_addr()?) |
| 37 | } |
| 38 | |
| 39 | pub async fn broadcast_beacon( |
| 40 | &self, |
| 41 | beacon: &DistressNanobeacon, |
| 42 | targets: &[SocketAddr], |
| 43 | ) -> Result<usize, NetworkError> { |
| 44 | let bytes = beacon.to_bytes()?; |
| 45 | if bytes.len() > MAX_DATAGRAM_SIZE { |
| 46 | return Err(NetworkError::MtuExceeded(bytes.len(), MAX_DATAGRAM_SIZE)); |
| 47 | } |
| 48 | |
| 49 | let mut sent = 0; |
| 50 | for target in targets { |
| 51 | if self.socket.send_to(&bytes, target).await.is_ok() { |
| 52 | sent += 1; |
| 53 | } |
| 54 | } |
| 55 | Ok(sent) |
| 56 | } |
| 57 | |
| 58 | pub async fn recv_beacon(&self) -> Result<(DistressNanobeacon, SocketAddr), NetworkError> { |
| 59 | let mut buf = [0u8; MAX_DATAGRAM_SIZE]; |
| 60 | let (len, src) = self.socket.recv_from(&mut buf).await?; |
| 61 | let beacon = DistressNanobeacon::from_bytes(&buf[..len])?; |
| 62 | Ok((beacon, src)) |
| 63 | } |
| 64 | |
| 65 | pub async fn send_capsule( |
| 66 | &self, |
| 67 | capsule: &ResolutionCapsule, |
| 68 | target: SocketAddr, |
| 69 | ) -> Result<usize, NetworkError> { |
| 70 | let bytes = capsule.to_bytes()?; |
| 71 | let len = self.socket.send_to(&bytes, target).await?; |
| 72 | Ok(len) |
| 73 | } |
| 74 | |
| 75 | pub async fn recv_capsule(&self) -> Result<(ResolutionCapsule, SocketAddr), NetworkError> { |
| 76 | let mut buf = [0u8; 65535]; |
| 77 | let (len, src) = self.socket.recv_from(&mut buf).await?; |
| 78 | let capsule = ResolutionCapsule::from_bytes(&buf[..len])?; |
| 79 | Ok((capsule, src)) |
| 80 | } |
| 81 | } |