Skip to main content

Client

Struct Client 

Source
pub struct Client<'c, N: Transport, B: BufferProvider<'c>, const MAX_SUBSCRIBES: usize, const RECEIVE_MAXIMUM: usize, const SEND_MAXIMUM: usize, const MAX_SUBSCRIPTION_IDENTIFIERS: usize> { /* private fields */ }
Expand description

An MQTT client.

Configuration via const parameters:

  • MAX_SUBSCRIBES: The maximum amount of in-flight/unacknowledged SUBSCRIBE packets (one per call to Self::subscribe).
  • RECEIVE_MAXIMUM: MQTT’s control flow mechanism. The maximum amount of incoming QoS::AtLeastOnce and QoS::ExactlyOnce publications (accumulated). Must not be 0 and must not be greater than 65535.
  • SEND_MAXIMUM: The maximum amount of outgoing QoS::AtLeastOnce and QoS::ExactlyOnce publications. The server can further limit this with its receive maximum. The client will use the minimum of this value and Self::server_config.
  • MAX_SUBSCRIPTION_IDENTIFIERS: The maximum amount of subscription identifier properties the client can receive within a single PUBLISH packet. If a packet with more subscription identifiers is received, the later identifers will be discarded.

Implementations§

Source§

impl<'c, N: Transport, B: BufferProvider<'c>, const MAX_SUBSCRIBES: usize, const RECEIVE_MAXIMUM: usize, const SEND_MAXIMUM: usize, const MAX_SUBSCRIPTION_IDENTIFIERS: usize> Client<'c, N, B, MAX_SUBSCRIBES, RECEIVE_MAXIMUM, SEND_MAXIMUM, MAX_SUBSCRIPTION_IDENTIFIERS>

Source

pub fn new(buffer: &'c mut B) -> Self

Creates a new, disconnected MQTT client using a buffer provider to store dynamically sized fields of received packets. The session state is initialised as a new session. If you want to start the client with an existing session, use Self::with_session.

Source

pub fn with_session( session: Session<RECEIVE_MAXIMUM, SEND_MAXIMUM>, buffer: &'c mut B, ) -> Self

Creates a new, disconnected MQTT client using a buffer provider to store dynamically sized fields of received packets.

Source

pub fn client_config(&self) -> &ClientConfig

Returns configuration for this client.

Source

pub fn server_config(&self) -> &ServerConfig

Returns the configuration of the currently or last connected server if there is one.

Source

pub fn shared_config(&self) -> &SharedConfig

Returns the configuration negotiated between the client and server.

Source

pub fn session(&self) -> &Session<RECEIVE_MAXIMUM, SEND_MAXIMUM>

Returns session related configuration and tracking information.

Source

pub fn buffer(&self) -> &B

Returns an immutable reference to the supplied BufferProvider implementation.

Source

pub fn buffer_mut(&mut self) -> &mut B

Returns a mutable reference to the supplied BufferProvider implementation.

This can for example be used to reset the underlying buffer if using BumpBuffer.

Source

pub async fn connect<'d>( &mut self, net: N, options: &ConnectOptions<'_>, client_identifier: Option<MqttString<'d>>, ) -> Result<ConnectInfo<'d>, MqttError<'c>>
where 'c: 'd,

Connect the client to an MQTT server on the other end of the net argument. Sends a CONNECT message and awaits the CONNACK response by the server.

Only call this when

  • the client is newly constructed.
  • a non-recoverable error has occured and Self::abort has been called.
  • Self::disconnect has been called.

The session expiry interval in ConnectOptions overrides the one in the session of the client.

Configuration that was negotiated with the server is stored in the client_config, server_config, shared_config, and session fields, which have getters (Self::client_config, Self::server_config, Self::shared_config, Self::session).

If the server does not have a session present, the client’s session is cleared. In case you would want to keep the session state, you can call Self::session and clone the session before.

§Returns:

Information about the session/connection that the client does currently not use and therefore not store in its configuration fields.

§Errors
Source

pub async fn ping(&mut self) -> Result<(), MqttError<'c>>

Start a ping handshake by sending a PINGRESP packet.

§Errors
Source

pub async fn subscribe( &mut self, topic_filter: TopicFilter<'_>, options: SubscriptionOptions, ) -> Result<PacketIdentifier, MqttError<'c>>

Subscribes to a single topic with the given options.

The client keeps track of the packet identifier sent in the SUBSCRIBE packet. If no Event::Suback is received within a custom time, this method can be used to send the SUBSCRIBE packet again.

A subscription identifier should only be set if the server supports subscription identifiers (Can be checked with Self::server_config). The client does not double-check whether this feature is supported and will always include the subscription identifier argument if present.

§Returns:

The packet identifier of the sent SUBSCRIBE packet.

§Errors
Source

pub async fn unsubscribe( &mut self, topic_filter: TopicFilter<'_>, ) -> Result<PacketIdentifier, MqttError<'c>>

Unsubscribes from a single topic filter.

The client keeps track of the packet identifier sent in the UNSUBSCRIBE packet. If no Event::Unsuback is received within a custom time, this method can be used to send the UNSUBSCRIBE packet again.

§Returns:

The packet identifier of the sent UNSUBSCRIBE packet.

§Errors
Source

pub async fn publish( &mut self, options: &PublicationOptions<'_>, message: Bytes<'_>, ) -> Result<Option<PacketIdentifier>, MqttError<'c>>

Publish a message. If QoS is greater than 0, the packet identifier is also kept track of by the client

§Returns:
  • In case of QoS 0: None
  • In case of QoS 1 or 2: Some with the packet identifier of the published packet
§Errors
Source

pub async fn republish( &mut self, packet_identifier: PacketIdentifier, options: &PublicationOptions<'_>, message: Bytes<'_>, ) -> Result<(), MqttError<'c>>

Resends a PUBLISH packet with DUP flag set.

This method must be called and must only be called after a reconnection with clean start set to 0, as resending packets at any other time is a protocol error. (Compare Message delivery retry, [MQTT-4.4.0-1]).

For a packet to be resent:

  • it must have a quality of service > 0
  • its packet identifier must have an in flight entry with a quality of service matching the quality of service in the options parameter
  • in case of quality of service 2, it must not already be awaiting a PUBCOMP packet
§Errors
§Panics

This function may panic if the QoS in the options is QoS::AtMostOnce.

Source

pub async fn rerelease(&mut self) -> Result<(), MqttError<'c>>

Resends all pending PUBREL packets.

This method must be called and must only be called after a reconnection with clean start set to 0, as resending packets at any other time is a protocol error. (Compare Message delivery retry, [MQTT-4.4.0-1]).

This method assumes that the server’s receive maximum after the reconnection is great enough to handle as many publication flows as dragged between the two connections.

§Errors
Source

pub async fn abort(&mut self)

Disconnects from the server after an error occured in a situation-aware way by either:

  • dropping the connection
  • sending a DISCONNECT with the deposited reason code and dropping the connection.

After an MQTT communication fails, usually either the client or the server closes the connection.

This is not cancel-safe but you can set a timeout if reconnecting later anyway or you don’t reuse the client.

Source

pub async fn disconnect( &mut self, options: &DisconnectOptions, ) -> Result<(), MqttError<'c>>

Disconnects gracefully from the server by sending a DISCONNECT packet.

§Preconditions:
  • The client did not return a non-recoverable Error before
§Errors
Source

pub async fn poll( &mut self, ) -> Result<Event<'c, MAX_SUBSCRIPTION_IDENTIFIERS>, MqttError<'c>>

Combines Self::poll_header and Self::poll_body.

Polls the network for a full packet. Not cancel-safe.

§Preconditions:
  • The last MQTT packet was received completely
  • The client did not return a non-recoverable Error before
§Returns:

MQTT Events. Their further meaning is documented in Event.

§Errors

Returns the errors that Client::poll_header and Client::poll_body return. For further information view their docs.

Source

pub async fn poll_header(&mut self) -> Result<FixedHeader, MqttError<'c>>

Polls the network for a fixed header in a cancel-safe way.

If a fixed header is received, the first 4 bits (packet type) are checked for correctness.

§Preconditions:
  • The last MQTT packet was received completely
  • The client did not return a non-recoverable Error before
§Returns:

The received fixed header with a valid packet type. It can be used to call Self::poll_body.

§Errors
Source

pub async fn poll_body( &mut self, header: FixedHeader, ) -> Result<Event<'c, MAX_SUBSCRIPTION_IDENTIFIERS>, MqttError<'c>>

Polls the network for the variable header and payload of a packet. Not cancel-safe.

§Preconditions:
  • The FixedHeader argument was received from the network right before.
  • The client did not return a non-recoverable MqttError before
§Returns:

MQTT Events for regular communication. Their further meaning is documented in Event.

§Errors
  • MqttError::RecoveryRequired if an unrecoverable error occured previously
  • MqttError::Network if the underlying Transport returned an error
  • MqttError::Alloc if the underlying BufferProvider returned an error
  • MqttError::Server if:
    • the server sends a malformed packet
    • the server causes a protocol error
    • the packet following this header exceeds the client’s maximum packet size
    • the server sends a PUBLISH packet with an invalid topic alias
    • the server exceeded the client’s receive maximum with a new QoS 2 PUBLISH
    • the server sends a PUBACK/PUBREC/PUBREL/PUBCOMP packet which mismatches what the client expects for this packet identifier from its session state
    • the fixed header has the packet type CONNECT/SUBSCRIBE/UNSUBSCRIBE/PINGREQ
  • MqttError::Disconnect if a DISCONNECT packet is received
  • MqttError::AuthPacketReceived if the fixed header has the packet type AUTH

Trait Implementations§

Source§

impl<'c, N: Debug + Transport, B: Debug + BufferProvider<'c>, const MAX_SUBSCRIBES: usize, const RECEIVE_MAXIMUM: usize, const SEND_MAXIMUM: usize, const MAX_SUBSCRIPTION_IDENTIFIERS: usize> Debug for Client<'c, N, B, MAX_SUBSCRIBES, RECEIVE_MAXIMUM, SEND_MAXIMUM, MAX_SUBSCRIPTION_IDENTIFIERS>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'c, N, B, const MAX_SUBSCRIBES: usize, const RECEIVE_MAXIMUM: usize, const SEND_MAXIMUM: usize, const MAX_SUBSCRIPTION_IDENTIFIERS: usize> Freeze for Client<'c, N, B, MAX_SUBSCRIBES, RECEIVE_MAXIMUM, SEND_MAXIMUM, MAX_SUBSCRIPTION_IDENTIFIERS>
where N: Freeze,

§

impl<'c, N, B, const MAX_SUBSCRIBES: usize, const RECEIVE_MAXIMUM: usize, const SEND_MAXIMUM: usize, const MAX_SUBSCRIPTION_IDENTIFIERS: usize> RefUnwindSafe for Client<'c, N, B, MAX_SUBSCRIBES, RECEIVE_MAXIMUM, SEND_MAXIMUM, MAX_SUBSCRIPTION_IDENTIFIERS>

§

impl<'c, N, B, const MAX_SUBSCRIBES: usize, const RECEIVE_MAXIMUM: usize, const SEND_MAXIMUM: usize, const MAX_SUBSCRIPTION_IDENTIFIERS: usize> Send for Client<'c, N, B, MAX_SUBSCRIBES, RECEIVE_MAXIMUM, SEND_MAXIMUM, MAX_SUBSCRIPTION_IDENTIFIERS>
where N: Send, B: Send,

§

impl<'c, N, B, const MAX_SUBSCRIBES: usize, const RECEIVE_MAXIMUM: usize, const SEND_MAXIMUM: usize, const MAX_SUBSCRIPTION_IDENTIFIERS: usize> Sync for Client<'c, N, B, MAX_SUBSCRIBES, RECEIVE_MAXIMUM, SEND_MAXIMUM, MAX_SUBSCRIPTION_IDENTIFIERS>
where N: Sync, B: Sync,

§

impl<'c, N, B, const MAX_SUBSCRIBES: usize, const RECEIVE_MAXIMUM: usize, const SEND_MAXIMUM: usize, const MAX_SUBSCRIPTION_IDENTIFIERS: usize> Unpin for Client<'c, N, B, MAX_SUBSCRIBES, RECEIVE_MAXIMUM, SEND_MAXIMUM, MAX_SUBSCRIPTION_IDENTIFIERS>
where N: Unpin,

§

impl<'c, N, B, const MAX_SUBSCRIBES: usize, const RECEIVE_MAXIMUM: usize, const SEND_MAXIMUM: usize, const MAX_SUBSCRIPTION_IDENTIFIERS: usize> UnsafeUnpin for Client<'c, N, B, MAX_SUBSCRIBES, RECEIVE_MAXIMUM, SEND_MAXIMUM, MAX_SUBSCRIPTION_IDENTIFIERS>
where N: UnsafeUnpin,

§

impl<'c, N, B, const MAX_SUBSCRIBES: usize, const RECEIVE_MAXIMUM: usize, const SEND_MAXIMUM: usize, const MAX_SUBSCRIPTION_IDENTIFIERS: usize> !UnwindSafe for Client<'c, N, B, MAX_SUBSCRIBES, RECEIVE_MAXIMUM, SEND_MAXIMUM, MAX_SUBSCRIPTION_IDENTIFIERS>

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.