Skip to main content

CoapTransport

Struct CoapTransport 

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

A CoAP client that implements the core [Transport] trait.

A transport is created disconnected; connect binds the socket and spawns the background task that decodes inbound datagrams for the life of the connection. Observe notifications are queued and read with recv.

Implementations§

Source§

impl CoapTransport

Source

pub fn new(config: CoapConfig) -> Self

Creates a transport from the given configuration without connecting.

§Arguments
  • config - the server connection settings.
§Returns

A disconnected transport ready for connect.

Source

pub fn is_connected(&self) -> bool

Reports whether the transport currently holds a bound socket.

§Returns

true once connect has succeeded and before disconnect is called.

Source

pub async fn disconnect(&mut self) -> Result<()>

Closes the socket and stops the background task.

Calling this on a transport that is not connected is a no-op.

§Returns

Ok(()) once the background task has been stopped and the socket released.

§Errors

This call is best-effort and currently always returns Ok(()).

Trait Implementations§

Source§

impl Receive for CoapTransport

Source§

async fn recv(&mut self) -> Result<Option<Message>>

Awaits the next notification from an observed resource.

§Returns

Some(message) for the next queued notification, or None once the background task has stopped and no further messages will arrive.

§Errors

Returns [Error::Closed] if the transport is not connected.

Source§

impl Transport for CoapTransport

Source§

async fn subscribe(&mut self, topic: &str) -> Result<()>

Registers an observation of a resource path, so its notifications arrive.

A notification carries no path of its own, only the token of the request that registered it (RFC 7641 section 3.2), so the client remembers which path each token observes and delivers every notification under that path. Registering a path again reuses its token, which a server takes as renewing the observation rather than adding a second one (RFC 7641 section 4.1), so a node can register again now and then in case the server has restarted.

§Returns

Ok(()) once the server has registered the observation, or has acknowledged the request with its response to follow.

§Errors

Returns [Error::Transport] if the server refuses the request, answers it without registering an observation, or never acknowledges it, and [Error::Closed] if the transport is not connected.

Source§

async fn connect(&mut self) -> Result<()>

Establishes the connection to the broker, peer, or bus. Read more
Source§

async fn send(&mut self, topic: &str, payload: &[u8]) -> Result<()>

Publishes a payload to a topic. Read more
§

fn send_text( &mut self, topic: &str, text: &str, ) -> impl Future<Output = Result<(), Error>> + Send

Publishes text to a topic: words, or a number written out. Read more

Auto Trait Implementations§

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.