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
impl Socket
Sourcepub async fn create(
family: SocketFamily,
s_type: SocketType,
protocol: SocketProtocol,
) -> Result<Self, Error>
pub async fn create( family: SocketFamily, s_type: SocketType, protocol: SocketProtocol, ) -> Result<Self, Error>
Create a new socket with the given parameters
Sourcepub fn as_raw_fd(&self) -> i32
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
Sourcepub async fn split(
self,
) -> Result<(SplitSocketHandle, SplitSocketHandle), Error>
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.
Sourcepub async fn connect(&self, address: SocketAddr) -> Result<(), Error>
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.
Sourcepub async unsafe fn connect_with_cancellation(
&self,
address: SocketAddr,
token: &CancellationToken,
) -> Result<(), Error>
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.
Sourcepub async fn bind(&self, address: SocketAddr) -> Result<(), Error>
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.
Sourcepub async unsafe fn bind_with_cancellation(
&self,
address: SocketAddr,
token: &CancellationToken,
) -> Result<(), Error>
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.
Sourcepub async fn write(&self, buffer: &[u8]) -> Result<usize, Error>
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.
Sourcepub async fn write_with_cancellation(
&self,
buffer: &[u8],
token: &CancellationToken,
) -> Result<usize, Error>
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.
Sourcepub async fn receive(&self, buffer: &mut [u8]) -> Result<usize, Error>
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.
Sourcepub async fn receive_with_cancellation(
&self,
buffer: &mut [u8],
token: &CancellationToken,
) -> Result<usize, Error>
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.
Sourcepub async fn receive_from(
&self,
buffer: &mut [u8],
) -> Result<(usize, SocketAddr), Error>
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.
Sourcepub async fn receive_from_with_cancellation(
&self,
buffer: &mut [u8],
token: &CancellationToken,
) -> Result<(usize, SocketAddr), Error>
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.
Sourcepub async fn send_to(
&self,
buffer: &[u8],
address: SocketAddr,
) -> Result<usize, Error>
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.
Sourcepub async fn send_to_with_cancellation(
&self,
buffer: &[u8],
address: SocketAddr,
token: &CancellationToken,
) -> Result<usize, Error>
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.
Sourcepub fn set_option<'a>(
&'a self,
option: SocketOption<'a>,
) -> Result<(), SocketOptionError>
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.
Sourcepub async fn deactivate(self) -> Result<(), Error>
pub async fn deactivate(self) -> Result<(), Error>
Deactivates the socket and the LTE link. A normal drop will do the same thing, but blocking.