Struct UdpSocket

Source
#[repr(C)]
pub struct UdpSocket<S: SocketState = Unbound, B: SocketBuffer = Vec<u8>> { /* private fields */ }
Expand description

A UDP socket

§Notes

  • Remote host (Self::remote) is set when the socket is bound calling bind()
  • In addition to supporting the creation (with new) and manual management of the socket, this struct implements EasySocket trait, which allows for an easier management of the socket, providing the open method as an alternative to new. This method return a UdpSocket already connected, and ready to send/receive data (using the write and read methods).
  • The socket is closed when the struct is dropped. Closing via drop is best-effort.

Implementations§

Source§

impl UdpSocket

Source

pub fn new() -> Result<UdpSocket<Unbound>, SocketError>

Create a socket

§Notes
§Errors
Source§

impl<S: SocketState> UdpSocket<S>

Source

pub fn fd(&self) -> i32

Get the file descriptor of the socket

Source

pub fn remote(&self) -> Option<SocketAddr>

Get the remote address of the socket

Source

pub fn send_flags(&self) -> SocketSendFlags

Flags used when sending data

Source

pub fn set_send_flags(&mut self, send_flags: SocketSendFlags)

Set the flags used when sending data

Source

pub fn recv_flags(&self) -> SocketRecvFlags

Flags used when receiving data

Source

pub fn set_recv_flags(&mut self, recv_flags: SocketRecvFlags)

Set the flags used when receiving data

Source§

impl UdpSocket<Unbound>

Source

pub fn bind( self, addr: Option<SocketAddr>, ) -> Result<UdpSocket<Bound>, SocketError>

Bind the socket

§Parameters
  • addr: The address to bind to, if None binds to 0.0.0.0:0
§Returns
  • Ok(UdpSocket<Bound>) if the binding was successful
  • Err(SocketError) if the binding was unsuccessful.
§Errors
Source§

impl UdpSocket<Bound>

Source

pub fn connect( self, addr: SocketAddr, ) -> Result<UdpSocket<Connected>, SocketError>

Connect to a remote host

§Notes

The socket must be in state [UdpSocketState::Bound] to connect to a remote host. To bind the socket use bind().

§Returns
  • Ok(UdpSocket<Connected>) if the connection was successful
  • Err(SocketError) if the connection was unsuccessful
§Errors
Source

pub fn _read_from( self, buf: &mut [u8], ) -> Result<(usize, UdpSocket<Connected>), SocketError>

Read from a bound socket

§Parameters
  • buf: The buffer where to store the received data
§Returns
  • Ok((usize, UdpSocket<Connected>)) if the write was successful. The number of bytes read
  • Err(SocketError) if the read was unsuccessful.
§Errors
Source

pub fn _write_to( self, buf: &[u8], len: usize, to: SocketAddr, ) -> Result<(usize, UdpSocket<Connected>), SocketError>

Write to a bound socket

§Parameters
  • buf: The buffer containing the data to send
§Returns
  • Ok((usize, UdpSocket<Connected>)) if the send was successful. The number of bytes sent
  • Err(SocketError) if the send was unsuccessful.
§Errors
Source§

impl UdpSocket<Connected>

Source

pub fn internal_read(&mut self, buf: &mut [u8]) -> Result<usize, SocketError>

Read from a socket

§Parameters
  • buf: The buffer where to store the received data
§Returns
  • Ok(usize) if the read was successful. The number of bytes read
  • Err(SocketError) if the read was unsuccessful.
§Errors
Source

pub fn internal_write(&mut self, buf: &[u8]) -> Result<usize, SocketError>

Write to a socket

§Returns
  • Ok(usize) if the send was successful. The number of bytes sent
  • Err(SocketError) if the send was unsuccessful.
§Errors
Source

pub fn internal_flush(&mut self) -> Result<(), SocketError>

Flush the send buffer

§Errors

Trait Implementations§

Source§

impl<S: Clone + SocketState, B: Clone + SocketBuffer> Clone for UdpSocket<S, B>

Source§

fn clone(&self) -> UdpSocket<S, B>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<S: SocketState> ErrorType for UdpSocket<S>

Source§

type Error = SocketError

Error type of all the IO operations on this type.
Source§

impl Open<'_, '_> for UdpSocket<Unbound>

Source§

fn open(self, options: &Self::Options<'_>) -> Result<Self::Return, Self::Error>

Open the socket

§Parameters
  • options: The options to use when opening the socket.
§Returns
  • Ok(UdpSocket<Connected>) if the socket was opened successfully
  • Err(SocketError) if the socket failed to open.
§Examples
let socket = UdpSocket::new()?;
let socket = socket.open(&SocketOptions::default())?;
Source§

type Return = UdpSocket<Connected>

Source§

impl<S: SocketState> OptionType for UdpSocket<S>

Source§

impl Read for UdpSocket<Connected>

Source§

fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error>

Read from the socket

§Parameters
  • buf: The buffer where the read data will be stored
§Returns
  • Ok(usize) if the read was successful. The number of bytes read
  • Err(SocketError) if the read was unsuccessful.
Source§

fn read_exact( &mut self, buf: &mut [u8], ) -> Result<(), ReadExactError<Self::Error>>

Read the exact number of bytes required to fill buf. Read more
Source§

impl Write for UdpSocket<Connected>

Source§

fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error>

Write to the socket

§Parameters
  • buf: The data to write
§Returns
  • Ok(usize) if the write was successful. The number of bytes written
  • Err(SocketError) if the write was unsuccessful.
Source§

fn flush(&mut self) -> Result<(), Self::Error>

Flush the socket

§Errors
Source§

fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error>

Write an entire buffer into this writer. Read more
Source§

fn write_fmt( &mut self, fmt: Arguments<'_>, ) -> Result<(), WriteFmtError<Self::Error>>

Write a formatted string into this writer, returning any error encountered. Read more
Source§

impl EasySocket for UdpSocket<Connected>

Auto Trait Implementations§

§

impl<S, B> Freeze for UdpSocket<S, B>
where B: Freeze,

§

impl<S, B> RefUnwindSafe for UdpSocket<S, B>

§

impl<S = Unbound, B = Vec<u8>> !Send for UdpSocket<S, B>

§

impl<S = Unbound, B = Vec<u8>> !Sync for UdpSocket<S, B>

§

impl<S, B> Unpin for UdpSocket<S, B>
where B: Unpin, S: Unpin,

§

impl<S, B> UnwindSafe for UdpSocket<S, B>
where B: UnwindSafe, S: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

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>,

Source§

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V