pub struct UdpSocket { /* private fields */ }udp only.Expand description
A UDP socket with the guard in front of it.
let mut socket = net_io::UdpSocket::bind("0.0.0.0:5353")?;
let mut buf = [0u8; 1500];
loop {
let datagram = socket.recv_from(&mut buf)?;
if datagram.truncated {
continue; // larger than we accept
}
// Replies are limited to the size of what the peer sent until it is verified.
socket.send_to(&buf[..datagram.len], datagram.from)?;
}Implementations§
Source§impl UdpSocket
impl UdpSocket
Sourcepub fn bind<A: ToSocketAddrs>(addr: A) -> Result<UdpSocket, NetError>
pub fn bind<A: ToSocketAddrs>(addr: A) -> Result<UdpSocket, NetError>
Binds with UdpOptions::new. The first resolved address that binds wins.
§Errors
AddrInUse, AddrNotAvailable, PermissionDenied, NoAddresses, Bind.
Sourcepub fn bind_with<A: ToSocketAddrs>(
addr: A,
options: UdpOptions,
) -> Result<UdpSocket, NetError>
pub fn bind_with<A: ToSocketAddrs>( addr: A, options: UdpOptions, ) -> Result<UdpSocket, NetError>
Sourcepub fn recv_from(&mut self, buf: &mut [u8]) -> Result<Datagram, NetError>
pub fn recv_from(&mut self, buf: &mut [u8]) -> Result<Datagram, NetError>
Receives one datagram within the budgets. Datagrams over budget are read, dropped, and counted; ICMP error reports are counted and never end the socket. Under a flood the call returns within twice the read timeout.
§Errors
TimedOut { kind: Read } (only with a read timeout), Os.
Sourcepub fn recv_batch(
&mut self,
slots: &mut [DatagramBuffer<'_>],
) -> Result<usize, NetError>
pub fn recv_batch( &mut self, slots: &mut [DatagramBuffer<'_>], ) -> Result<usize, NetError>
Waits for a datagram, then takes what else is ready, up to slots.len()
(64 at most). Each datagram passes the budgets as in UdpSocket::recv_from:
one that does not leaves its slot’s datagram at None, so walk every slot.
One system call on Linux (recvmmsg), a loop elsewhere. Returns how many
slots hold a datagram. Packet info is not reported here.
§Errors
Sourcepub fn send_batch(
&mut self,
datagrams: &[OutgoingDatagram<'_>],
) -> Result<usize, NetError>
pub fn send_batch( &mut self, datagrams: &[OutgoingDatagram<'_>], ) -> Result<usize, NetError>
Sends from the front of datagrams, each under the guard of
UdpSocket::send_to, until one is refused or the OS takes no more. One system
call per 64 on Linux (sendmmsg), a loop elsewhere. Returns how many went.
§Errors
Limit { which: Amplification } when the first is refused, Os.
Sourcepub fn join_multicast(
&self,
group: IpAddr,
interface: MulticastInterface,
) -> Result<(), NetError>
pub fn join_multicast( &self, group: IpAddr, interface: MulticastInterface, ) -> Result<(), NetError>
Joins a multicast group. Datagrams to the group then pass the same budgets as any other; a reply to a group member is still under the amplification guard.
§Errors
InvalidConfig when the interface’s family is not the group’s, Os.
Sourcepub fn send_to(&mut self, buf: &[u8], to: SocketAddr) -> Result<usize, NetError>
pub fn send_to(&mut self, buf: &[u8], to: SocketAddr) -> Result<usize, NetError>
Sends one datagram. To a source not marked verified, the bytes sent within
the window never exceed factor times the bytes received from it; a
source never heard from gets nothing (an empty datagram aside).
§Errors
Limit { which: Amplification } (nothing was sent), Os.
Sourcepub fn send(&mut self, buf: &[u8]) -> Result<usize, NetError>
pub fn send(&mut self, buf: &[u8]) -> Result<usize, NetError>
Sends to the connected peer, under the same guard as UdpSocket::send_to.
§Errors
Closed when not connected; as UdpSocket::send_to.
Sourcepub fn connect(&mut self, to: SocketAddr) -> Result<(), NetError>
pub fn connect(&mut self, to: SocketAddr) -> Result<(), NetError>
Fixes the peer: the kernel drops datagrams from other sources, and
UdpSocket::send goes to it.
§Errors
Os.
Sourcepub fn mark_verified(&mut self, peer: SocketAddr)
pub fn mark_verified(&mut self, peer: SocketAddr)
The protocol proved peer can receive (a cookie, a handshake): the guard
no longer limits sends to it, and its table entry is kept.
Sourcepub fn set_read_timeout(
&mut self,
timeout: Option<Duration>,
) -> Result<(), NetError>
pub fn set_read_timeout( &mut self, timeout: Option<Duration>, ) -> Result<(), NetError>
How long a receive waits (None: forever, the default).
§Errors
InvalidConfig for a zero duration, Os.