Socket

Struct Socket 

Source
pub struct Socket { /* private fields */ }
Expand description

A socket for network communication through the nRF modem.

This struct provides an async interface to the nRF modem’s socket functionality, supporting TCP, UDP, TLS, and DTLS protocols. The socket automatically manages the LTE link lifetime and provides non-blocking async operations.

Implementations§

Source§

impl Socket

Source

pub async fn create( family: SocketFamily, s_type: SocketType, protocol: SocketProtocol, ) -> Result<Self, Error>

Create a new socket with the given parameters

Source

pub fn as_raw_fd(&self) -> i32

Get the nrf-modem file descriptor so the user can opt out of using this high level wrapper for things

Source

pub async fn split( self, ) -> Result<(SplitSocketHandle, SplitSocketHandle), Error>

Split the socket into two handles that can be used independently.

This is useful for splitting a socket into separate read and write handles that can be used in different async tasks. Each handle maintains its own LTE link to keep the connection alive.

Source

pub async fn connect(&self, address: SocketAddr) -> Result<(), Error>

Connect to the given socket address.

This calls the nrfxlib_sys::nrf_connect function and can be used for tcp streams, udp connections and dtls connections.

Source

pub async unsafe fn connect_with_cancellation( &self, address: SocketAddr, token: &CancellationToken, ) -> Result<(), Error>

Connect to the given socket address.

This calls the nrfxlib_sys::nrf_connect function and can be used for tcp streams, udp connections and dtls connections.

§Safety

If the connect is cancelled, the socket may be in a weird state and should be dropped.

Source

pub async fn bind(&self, address: SocketAddr) -> Result<(), Error>

Bind the socket to a given address.

This calls the nrfxlib_sys::nrf_bind function and can be used for UDP sockets.

Source

pub async unsafe fn bind_with_cancellation( &self, address: SocketAddr, token: &CancellationToken, ) -> Result<(), Error>

Bind the socket to a given address.

This calls the nrfxlib_sys::nrf_bind function and can be used for UDP sockets.

§Safety

If the bind is cancelled, the socket may be in a weird state and should be dropped.

Source

pub async fn write(&self, buffer: &[u8]) -> Result<usize, Error>

Write data to the socket.

This calls the nrfxlib_sys::nrf_send function and can be used for TCP streams and dTLS connections.

Source

pub async fn write_with_cancellation( &self, buffer: &[u8], token: &CancellationToken, ) -> Result<usize, Error>

Write data to the socket with cancellation support.

This calls the nrfxlib_sys::nrf_send function and can be used for TCP streams and dTLS connections.

This operation can be cancelled using the provided CancellationToken.

Source

pub async fn receive(&self, buffer: &mut [u8]) -> Result<usize, Error>

Receive data from the socket.

This calls the nrfxlib_sys::nrf_recv function and can be used for TCP streams and dTLS connections.

Source

pub async fn receive_with_cancellation( &self, buffer: &mut [u8], token: &CancellationToken, ) -> Result<usize, Error>

Receive data from the socket with cancellation support.

This calls the nrfxlib_sys::nrf_recv function and can be used for TCP streams and dTLS connections.

This operation can be cancelled using the provided CancellationToken.

Source

pub async fn receive_from( &self, buffer: &mut [u8], ) -> Result<(usize, SocketAddr), Error>

Receive data from the socket along with the sender’s address.

This calls the nrfxlib_sys::nrf_recvfrom function and can be used for UDP sockets.

Source

pub async fn receive_from_with_cancellation( &self, buffer: &mut [u8], token: &CancellationToken, ) -> Result<(usize, SocketAddr), Error>

Receive data from the socket along with the sender’s address, with cancellation support.

This calls the nrfxlib_sys::nrf_recvfrom function and can be used for UDP sockets.

This operation can be cancelled using the provided CancellationToken.

Source

pub async fn send_to( &self, buffer: &[u8], address: SocketAddr, ) -> Result<usize, Error>

Send data to a specific address through the socket.

This calls the nrfxlib_sys::nrf_sendto function and can be used for UDP sockets.

Source

pub async fn send_to_with_cancellation( &self, buffer: &[u8], address: SocketAddr, token: &CancellationToken, ) -> Result<usize, Error>

Send data to a specific address through the socket with cancellation support.

This calls the nrfxlib_sys::nrf_sendto function and can be used for UDP sockets.

This operation can be cancelled using the provided CancellationToken.

Source

pub fn set_option<'a>( &'a self, option: SocketOption<'a>, ) -> Result<(), SocketOptionError>

Set a socket option.

This calls the nrfxlib_sys::nrf_setsockopt function and provides access to various socket configuration options including timeouts, TLS settings, PDN binding, and protocol-specific options.

See SocketOption for available options.

Source

pub async fn deactivate(self) -> Result<(), Error>

Deactivates the socket and the LTE link. A normal drop will do the same thing, but blocking.

Trait Implementations§

Source§

impl Debug for Socket

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Drop for Socket

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl PartialEq for Socket

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Socket

Auto Trait Implementations§

§

impl Freeze for Socket

§

impl RefUnwindSafe for Socket

§

impl Send for Socket

§

impl Sync for Socket

§

impl Unpin for Socket

§

impl UnwindSafe for Socket

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

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.