pub struct UdpSocketBuilder { /* private fields */ }Expand description
A UDP socket to set up before it binds: what only a socket not bound yet takes
(SO_REUSEPORT among sockets that share a port, IPV6_V6ONLY, a device), and the rest
before the first datagram.
use nuclei::net::UdpSocketBuilder;
let builder = UdpSocketBuilder::new_v4()?;
builder.set_reuseaddr(true)?;
builder.set_recv_buffer_size(256 * 1024)?;
let socket = builder.bind("127.0.0.1:0".parse().unwrap())?;
let addr = socket.local_addr()?;
socket.send_to(b"hi", addr).await?;
let mut buf = [0u8; 2];
assert_eq!(socket.recv_from(&mut buf).await?, (2, addr));Implementations§
Source§impl UdpSocketBuilder
impl UdpSocketBuilder
Sourcepub fn set_reuseaddr(&self, reuseaddr: bool) -> Result<()>
pub fn set_reuseaddr(&self, reuseaddr: bool) -> Result<()>
Sets SO_REUSEADDR: the socket may bind an address still held by a
connection that is closing.
Sourcepub fn set_reuseport(&self, reuseport: bool) -> Result<()>
pub fn set_reuseport(&self, reuseport: bool) -> Result<()>
Sets SO_REUSEPORT: several sockets may bind the same address and
port, and the system shares what comes among them.
Sourcepub fn set_send_buffer_size(&self, size: u32) -> Result<()>
pub fn set_send_buffer_size(&self, size: u32) -> Result<()>
Sets the size of the system’s send buffer (SO_SNDBUF); the system
may round it (Linux doubles it).
Sourcepub fn send_buffer_size(&self) -> Result<u32>
pub fn send_buffer_size(&self) -> Result<u32>
The size of the system’s send buffer (SO_SNDBUF).
Sourcepub fn set_recv_buffer_size(&self, size: u32) -> Result<()>
pub fn set_recv_buffer_size(&self, size: u32) -> Result<()>
Sets the size of the system’s receive buffer (SO_RCVBUF); the
system may round it (Linux doubles it).
Sourcepub fn recv_buffer_size(&self) -> Result<u32>
pub fn recv_buffer_size(&self) -> Result<u32>
The size of the system’s receive buffer (SO_RCVBUF).
Sourcepub fn set_tos(&self, tos: u32) -> Result<()>
pub fn set_tos(&self, tos: u32) -> Result<()>
Sets the type of service of the socket’s IPv4 packets (IP_TOS).
Sourcepub fn set_tclass_v6(&self, tclass: u32) -> Result<()>
pub fn set_tclass_v6(&self, tclass: u32) -> Result<()>
Sets the traffic class of the socket’s IPv6 packets (IPV6_TCLASS).
Sourcepub fn tclass_v6(&self) -> Result<u32>
pub fn tclass_v6(&self) -> Result<u32>
The traffic class of the socket’s IPv6 packets (IPV6_TCLASS).
Sourcepub fn set_only_v6(&self, only_v6: bool) -> Result<()>
pub fn set_only_v6(&self, only_v6: bool) -> Result<()>
Sets IPV6_V6ONLY on an IPv6 socket: with true it takes IPv6 alone,
else IPv4 too, through addresses mapped into IPv6.
§Errors
As setsockopt(2): an IPv4 socket has no such option, and a bound one
may no longer change it.
Sourcepub fn bind_device(&self, interface: Option<&[u8]>) -> Result<()>
pub fn bind_device(&self, interface: Option<&[u8]>) -> Result<()>
Binds the socket to the network interface named interface
(SO_BINDTODEVICE), or, with None, to none.
Sourcepub fn device(&self) -> Result<Option<DeviceName>>
pub fn device(&self) -> Result<Option<DeviceName>>
The network interface the socket is bound to, if any
(SO_BINDTODEVICE).
Source§impl UdpSocketBuilder
impl UdpSocketBuilder
Sourcepub fn new_v4() -> Result<UdpSocketBuilder>
pub fn new_v4() -> Result<UdpSocketBuilder>
Sourcepub fn new_v6() -> Result<UdpSocketBuilder>
pub fn new_v6() -> Result<UdpSocketBuilder>
Sourcepub fn local_addr(&self) -> Result<SocketAddr>
pub fn local_addr(&self) -> Result<SocketAddr>
The address the socket is bound to; before it binds, the unspecified one on unix, and an
error (InvalidInput) on Windows, as std’s local_addr answers there.
Sourcepub fn take_error(&self) -> Result<Option<Error>>
pub fn take_error(&self) -> Result<Option<Error>>
The socket’s pending error (SO_ERROR), taken.