Initial import: ICE-NET research docs and Rust workspace (v0.4)
Some checks failed
rust-ci / checks (push) Has been cancelled
Some checks failed
rust-ci / checks (push) Has been cancelled
- Rust workspace: 7 library crates, icenetd CLI, icenet-city (Slint GUI) - Sim: fix false Delivered after a route cut, derive traffic class from payload size, per-link airtime slots (link capacity, queueing) - Docs: single docs/STATUS.md for all figures, remove v0.2/egui leftovers, drop unimplemented seed claims, uncheck store-and-forward - License: LGPL-3.0-or-later (LICENSE, LICENSES/GPL-3.0.txt)
This commit is contained in:
commit
2e61688eb3
141 changed files with 13993 additions and 0 deletions
68
workspace/crates/icenet-transport/src/udp.rs
Normal file
68
workspace/crates/icenet-transport/src/udp.rs
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
//! Non-blocking UDP adapter for one connected peer.
|
||||
use std::io;
|
||||
use std::net::{SocketAddr, ToSocketAddrs, UdpSocket};
|
||||
|
||||
use crate::{MAX_DATAGRAM_BYTES, Transport, TransportError};
|
||||
|
||||
/// A non-blocking UDP socket connected to one peer address.
|
||||
#[derive(Debug)]
|
||||
pub struct UdpTransport {
|
||||
socket: UdpSocket,
|
||||
}
|
||||
|
||||
impl UdpTransport {
|
||||
/// Binds a local address and makes the socket non-blocking.
|
||||
///
|
||||
/// # Errors
|
||||
/// Returns the underlying socket error when binding or configuration fails.
|
||||
pub fn bind(address: impl ToSocketAddrs) -> io::Result<Self> {
|
||||
let socket = UdpSocket::bind(address)?;
|
||||
socket.set_nonblocking(true)?;
|
||||
Ok(Self { socket })
|
||||
}
|
||||
|
||||
/// Connects this socket to one peer.
|
||||
///
|
||||
/// # Errors
|
||||
/// Returns the underlying socket error when connecting fails.
|
||||
pub fn connect(self, peer: impl ToSocketAddrs) -> io::Result<Self> {
|
||||
self.socket.connect(peer)?;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
/// Returns the local socket address, including an OS-assigned port.
|
||||
///
|
||||
/// # Errors
|
||||
/// Returns the underlying socket error when the address is unavailable.
|
||||
pub fn local_addr(&self) -> io::Result<SocketAddr> {
|
||||
self.socket.local_addr()
|
||||
}
|
||||
}
|
||||
|
||||
impl Transport for UdpTransport {
|
||||
fn send(&mut self, frame: Vec<u8>) -> Result<(), TransportError> {
|
||||
if frame.len() > MAX_DATAGRAM_BYTES {
|
||||
return Err(TransportError::TooLarge);
|
||||
}
|
||||
match self.socket.send(&frame) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(_) => Err(TransportError::Unavailable),
|
||||
}
|
||||
}
|
||||
|
||||
fn recv(&mut self) -> Result<Option<Vec<u8>>, TransportError> {
|
||||
let mut buffer = [0_u8; MAX_DATAGRAM_BYTES];
|
||||
match self.socket.recv(&mut buffer) {
|
||||
Ok(size) => Ok(Some(buffer[..size].to_vec())),
|
||||
Err(error)
|
||||
if matches!(
|
||||
error.kind(),
|
||||
io::ErrorKind::WouldBlock | io::ErrorKind::TimedOut
|
||||
) =>
|
||||
{
|
||||
Ok(None)
|
||||
}
|
||||
Err(_) => Err(TransportError::Unavailable),
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue