pub struct Socket<'a> { /* private fields */ }
Expand description
A User Datagram Protocol socket.
A UDP socket is bound to a specific endpoint, and owns transmit and receive packet buffers.
Implementations§
source§impl<'a> Socket<'a>
impl<'a> Socket<'a>
sourcepub fn new(
rx_buffer: PacketBuffer<'a>,
tx_buffer: PacketBuffer<'a>
) -> Socket<'a>
pub fn new(
rx_buffer: PacketBuffer<'a>,
tx_buffer: PacketBuffer<'a>
) -> Socket<'a>
Create an UDP socket with the given buffers.
sourcepub fn register_recv_waker(&mut self, waker: &Waker)
pub fn register_recv_waker(&mut self, waker: &Waker)
Register a waker for receive operations.
The waker is woken on state changes that might affect the return value
of recv
method calls, such as receiving data, or the socket closing.
Notes:
- Only one waker can be registered at a time. If another waker was previously registered, it is overwritten and will no longer be woken.
- The Waker is woken only once. Once woken, you must register it again to receive more wakes.
- “Spurious wakes” are allowed: a wake doesn’t guarantee the result of
recv
has necessarily changed.
sourcepub fn register_send_waker(&mut self, waker: &Waker)
pub fn register_send_waker(&mut self, waker: &Waker)
Register a waker for send operations.
The waker is woken on state changes that might affect the return value
of send
method calls, such as space becoming available in the transmit
buffer, or the socket closing.
Notes:
- Only one waker can be registered at a time. If another waker was previously registered, it is overwritten and will no longer be woken.
- The Waker is woken only once. Once woken, you must register it again to receive more wakes.
- “Spurious wakes” are allowed: a wake doesn’t guarantee the result of
send
has necessarily changed.
sourcepub fn endpoint(&self) -> IpListenEndpoint
pub fn endpoint(&self) -> IpListenEndpoint
Return the bound endpoint.
sourcepub fn hop_limit(&self) -> Option<u8>
pub fn hop_limit(&self) -> Option<u8>
Return the time-to-live (IPv4) or hop limit (IPv6) value used in outgoing packets.
See also the set_hop_limit method
sourcepub fn set_hop_limit(&mut self, hop_limit: Option<u8>)
pub fn set_hop_limit(&mut self, hop_limit: Option<u8>)
Set the time-to-live (IPv4) or hop limit (IPv6) value used in outgoing packets.
A socket without an explicitly set hop limit value uses the default IANA recommended value (64).
Panics
This function panics if a hop limit value of 0 is given. See RFC 1122 § 3.2.1.7.
sourcepub fn bind<T: Into<IpListenEndpoint>>(
&mut self,
endpoint: T
) -> Result<(), BindError>
pub fn bind<T: Into<IpListenEndpoint>>(
&mut self,
endpoint: T
) -> Result<(), BindError>
Bind the socket to the given endpoint.
This function returns Err(Error::Illegal)
if the socket was open
(see is_open), and Err(Error::Unaddressable)
if the port in the given endpoint is zero.
sourcepub fn packet_recv_capacity(&self) -> usize
pub fn packet_recv_capacity(&self) -> usize
Return the maximum number packets the socket can receive.
sourcepub fn packet_send_capacity(&self) -> usize
pub fn packet_send_capacity(&self) -> usize
Return the maximum number packets the socket can transmit.
sourcepub fn payload_recv_capacity(&self) -> usize
pub fn payload_recv_capacity(&self) -> usize
Return the maximum number of bytes inside the recv buffer.
sourcepub fn payload_send_capacity(&self) -> usize
pub fn payload_send_capacity(&self) -> usize
Return the maximum number of bytes inside the transmit buffer.
sourcepub fn send(
&mut self,
size: usize,
remote_endpoint: IpEndpoint
) -> Result<&mut [u8], SendError>
pub fn send(
&mut self,
size: usize,
remote_endpoint: IpEndpoint
) -> Result<&mut [u8], SendError>
Enqueue a packet to be sent to a given remote endpoint, and return a pointer to its payload.
This function returns Err(Error::Exhausted)
if the transmit buffer is full,
Err(Error::Unaddressable)
if local or remote port, or remote address are unspecified,
and Err(Error::Truncated)
if there is not enough transmit buffer capacity
to ever send this packet.
sourcepub fn send_with<F>(
&mut self,
max_size: usize,
remote_endpoint: IpEndpoint,
f: F
) -> Result<usize, SendError>where
F: FnOnce(&mut [u8]) -> usize,
pub fn send_with<F>(
&mut self,
max_size: usize,
remote_endpoint: IpEndpoint,
f: F
) -> Result<usize, SendError>where
F: FnOnce(&mut [u8]) -> usize,
Enqueue a packet to be send to a given remote endpoint and pass the buffer to the provided closure. The closure then returns the size of the data written into the buffer.
Also see send.
sourcepub fn send_slice(
&mut self,
data: &[u8],
remote_endpoint: IpEndpoint
) -> Result<(), SendError>
pub fn send_slice(
&mut self,
data: &[u8],
remote_endpoint: IpEndpoint
) -> Result<(), SendError>
Enqueue a packet to be sent to a given remote endpoint, and fill it from a slice.
See also send.
sourcepub fn recv(&mut self) -> Result<(&[u8], IpEndpoint), RecvError>
pub fn recv(&mut self) -> Result<(&[u8], IpEndpoint), RecvError>
Dequeue a packet received from a remote endpoint, and return the endpoint as well as a pointer to the payload.
This function returns Err(Error::Exhausted)
if the receive buffer is empty.
sourcepub fn recv_slice(
&mut self,
data: &mut [u8]
) -> Result<(usize, IpEndpoint), RecvError>
pub fn recv_slice(
&mut self,
data: &mut [u8]
) -> Result<(usize, IpEndpoint), RecvError>
Dequeue a packet received from a remote endpoint, copy the payload into the given slice, and return the amount of octets copied as well as the endpoint.
See also recv.
sourcepub fn peek(&mut self) -> Result<(&[u8], &IpEndpoint), RecvError>
pub fn peek(&mut self) -> Result<(&[u8], &IpEndpoint), RecvError>
Peek at a packet received from a remote endpoint, and return the endpoint as well as a pointer to the payload without removing the packet from the receive buffer. This function otherwise behaves identically to recv.
It returns Err(Error::Exhausted)
if the receive buffer is empty.
sourcepub fn peek_slice(
&mut self,
data: &mut [u8]
) -> Result<(usize, &IpEndpoint), RecvError>
pub fn peek_slice(
&mut self,
data: &mut [u8]
) -> Result<(usize, &IpEndpoint), RecvError>
Peek at a packet received from a remote endpoint, copy the payload into the given slice, and return the amount of octets copied as well as the endpoint without removing the packet from the receive buffer. This function otherwise behaves identically to recv_slice.
See also peek.