Struct smoltcp::socket::raw::Socket

source ·
pub struct Socket<'a> { /* private fields */ }
Expand description

A raw IP socket.

A raw socket is bound to a specific IP protocol, and owns transmit and receive packet buffers.

Implementations§

source§

impl<'a> Socket<'a>

source

pub fn new( ip_version: IpVersion, ip_protocol: IpProtocol, rx_buffer: PacketBuffer<'a>, tx_buffer: PacketBuffer<'a> ) -> Socket<'a>

Create a raw IP socket bound to the given IP version and datagram protocol, with the given buffers.

source

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.
source

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.
source

pub fn ip_version(&self) -> IpVersion

Return the IP version the socket is bound to.

source

pub fn ip_protocol(&self) -> IpProtocol

Return the IP protocol the socket is bound to.

source

pub fn can_send(&self) -> bool

Check whether the transmit buffer is full.

source

pub fn can_recv(&self) -> bool

Check whether the receive buffer is not empty.

source

pub fn packet_recv_capacity(&self) -> usize

Return the maximum number packets the socket can receive.

source

pub fn packet_send_capacity(&self) -> usize

Return the maximum number packets the socket can transmit.

source

pub fn payload_recv_capacity(&self) -> usize

Return the maximum number of bytes inside the recv buffer.

source

pub fn payload_send_capacity(&self) -> usize

Return the maximum number of bytes inside the transmit buffer.

source

pub fn send(&mut self, size: usize) -> Result<&mut [u8], SendError>

Enqueue a packet to send, and return a pointer to its payload.

This function returns Err(Error::Exhausted) if the transmit buffer is full, and Err(Error::Truncated) if there is not enough transmit buffer capacity to ever send this packet.

If the buffer is filled in a way that does not match the socket’s IP version or protocol, the packet will be silently dropped.

Note: The IP header is parsed and re-serialized, and may not match the header actually transmitted bit for bit.

source

pub fn send_with<F>( &mut self, max_size: usize, f: F ) -> Result<usize, SendError>
where F: FnOnce(&mut [u8]) -> usize,

Enqueue a packet to be send and pass the buffer to the provided closure. The closure then returns the size of the data written into the buffer.

Also see send.

source

pub fn send_slice(&mut self, data: &[u8]) -> Result<(), SendError>

Enqueue a packet to send, and fill it from a slice.

See also send.

source

pub fn recv(&mut self) -> Result<&[u8], RecvError>

Dequeue a packet, and return a pointer to the payload.

This function returns Err(Error::Exhausted) if the receive buffer is empty.

Note: The IP header is parsed and re-serialized, and may not match the header actually received bit for bit.

source

pub fn recv_slice(&mut self, data: &mut [u8]) -> Result<usize, RecvError>

Dequeue a packet, and copy the payload into the given slice.

Note: when the size of the provided buffer is smaller than the size of the payload, the packet is dropped and a RecvError::Truncated error is returned.

See also recv.

source

pub fn peek(&mut self) -> Result<&[u8], RecvError>

Peek at a packet in the receive buffer and return 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.

source

pub fn peek_slice(&mut self, data: &mut [u8]) -> Result<usize, RecvError>

Peek at a packet in the receive buffer, copy the payload into the given slice, and return the amount of octets copied without removing the packet from the receive buffer. This function otherwise behaves identically to recv_slice.

Note: when the size of the provided buffer is smaller than the size of the payload, no data is copied into the provided buffer and a RecvError::Truncated error is returned.

See also peek.

Trait Implementations§

source§

impl<'a> AnySocket<'a> for Socket<'a>

source§

fn upcast(self) -> Socket<'a>

source§

fn downcast<'c>(socket: &'c Socket<'a>) -> Option<&'c Self>

source§

fn downcast_mut<'c>(socket: &'c mut Socket<'a>) -> Option<&'c mut Self>

source§

impl<'a> Debug for Socket<'a>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> RefUnwindSafe for Socket<'a>

§

impl<'a> Send for Socket<'a>

§

impl<'a> Sync for Socket<'a>

§

impl<'a> Unpin for Socket<'a>

§

impl<'a> !UnwindSafe for Socket<'a>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.