Struct mosquitto_rs::Mosq

source ·
pub struct Mosq<CB = ()>
where CB: Callbacks + Send + Sync,
{ /* private fields */ }
Expand description

Mosq is the low-level mosquitto client. You probably want to look at Client instead.

Implementations§

source§

impl<CB: Callbacks + Send + Sync> Mosq<CB>

source

pub fn with_auto_id(callbacks: CB) -> Result<Self, Error>

Create a new client instance with a random client id

source

pub fn with_id( callbacks: CB, id: &str, clean_session: bool ) -> Result<Self, Error>

Create a new client instance with the specified id. If clean_session is true, instructs the broker to clean all messages and subscriptions on disconnect. Otherwise it will preserve them.

source

pub fn set_username_and_password( &self, username: Option<&str>, password: Option<&str> ) -> Result<(), Error>

Configure the client with an optional username and password. The default is None for both. Whether you need to configure these credentials depends on the broker configuration.

source

pub fn connect( &self, host: &str, port: c_int, keep_alive_interval: Duration, bind_address: Option<&str> ) -> Result<(), Error>

Connect to the broker on the specified host and port. port is typically 1883 for mqtt, but it may be different in your environment.

keep_alive_interval specifies the interval at which keepalive requests are sent. mosquitto has a minimum value of 5 for this and will generate an error if you use a smaller value.

bind_address can be used to specify the outgoing interface for the connection.

source

pub fn connect_non_blocking( &self, host: &str, port: c_int, keep_alive_interval: Duration, bind_address: Option<&str> ) -> Result<(), Error>

Connect to the broker on the specified host and port, but don’t block for the connection portion. (Note that name resolution may still block!).

The connection will be completed later by running the message loop using either loop_until_explicitly_disconnected or start_loop_thread.

port is typically 1883 for mqtt, but it may be different in your environment.

keep_alive_interval specifies the interval at which keepalive requests are sent. mosquitto has a minimum value of 5 for this and will generate an error if you use a smaller value.

bind_address can be used to specify the outgoing interface for the connection.

source

pub fn reconnect(&self) -> Result<(), Error>

Reconnect a disconnected client using the same parameters as were originally used to connect it.

source

pub fn disconnect(&self) -> Result<(), Error>

Disconnect the client. This will cause the message loop to terminate.

source

pub fn publish( &self, topic: &str, payload: &[u8], qos: QoS, retain: bool ) -> Result<MessageId, Error>

Publish a message to the specified topic.

The payload size can be 0-283, 435 or 455 bytes; other values will generate an error result.

retain will set the message to be retained by the broker, and delivered to new subscribers.

Returns the assigned MessageId value for the publish. The publish may not complete immediately. Your Callbacks::on_publish handler will be called when it completes.

source

pub fn set_last_will( &self, topic: &str, payload: &[u8], qos: QoS, retain: bool ) -> Result<(), Error>

Configure will information for a mosquitto instance. By default, clients do not have a will. This must be called before calling connect.

The payload size can be 0-283, 435 or 455 bytes; other values will generate an error result.

retain will set the message to be retained by the broker, and delivered to new subscribers.

source

pub fn clear_last_will(&self) -> Result<(), Error>

Remove a previously configured will. This must be called before calling connect

source

pub fn subscribe(&self, pattern: &str, qos: QoS) -> Result<MessageId, Error>

Establish a subscription for topics that match pattern.

Your Callbacks::on_message handler will be called as messages matching your subscription arrive.

Returns the MessageId of the subscription request; the subscriptions won’t be active until the broker has processed the request. You can use an on_subscribe handler to determine when that is ready.

source

pub fn unsubscribe(&self, pattern: &str) -> Result<MessageId, Error>

Remove subscription(s) for topics that match pattern.

source

pub fn get_callbacks(&self) -> &CB

Returns a reference to the callbacks previously registered during construction.

source

pub fn loop_until_explicitly_disconnected( &self, timeout: Duration ) -> Result<(), Error>

Runs the message loop for the client. This method will not return until the client is explicitly disconnected via the disconnect method.

timeout specifies the internal sleep duration between iterations.

source

pub fn start_loop_thread(&self) -> Result<(), Error>

Starts a new thread to run the message loop for the client. The thread will run until the client is disconnected, or until stop_loop_thread is called.

source

pub fn stop_loop_thread(&self, force_cancel: bool) -> Result<(), Error>

Stops the message loop thread started via start_loop_thread

source

pub fn set_string_option( &self, option: mosq_opt_t, value: &str ) -> Result<(), Error>

Sets an option with a string value

source

pub fn set_int_option( &self, option: mosq_opt_t, value: c_int ) -> Result<(), Error>

Sets an option with an integer value

source

pub unsafe fn set_ptr_option( &self, option: mosq_opt_t, value: *mut c_void ) -> Result<(), Error>

Sets a void* pointer option such as MOSQ_OPT_SSL_CTX. Unsafe because we can’t know whether what is being passed really matches up.

source

pub fn configure_tls<CAFILE, CAPATH, CERTFILE, KEYFILE>( &self, ca_file: Option<CAFILE>, ca_path: Option<CAPATH>, cert_file: Option<CERTFILE>, key_file: Option<KEYFILE>, pw_callback: Option<PasswdCallback> ) -> Result<(), Error>
where CAFILE: AsRef<Path>, CAPATH: AsRef<Path>, CERTFILE: AsRef<Path>, KEYFILE: AsRef<Path>,

Configures the TLS parameters for the client.

ca_file is the path to a PEM encoded trust CA certificate file. Either ca_file or ca_path must be set.

ca_path is the path to a directory containing PEM encoded trust CA certificates. Either ca_file or ca_path must be set.

cert_file path to a file containing the PEM encoded certificate file for this client. If None then key_file must also be None and no client certificate will be used.

key_file path to a file containing the PEM encoded private key for this client. If None them cert_file must also be None and no client certificate will be used.

pw_callback allows you to provide a password to decrypt an encrypted key file. Specify None if the key file isn’t password protected.

source

pub fn set_reconnect_delay( &self, reconnect_delay: Duration, max_reconnect_delay: Duration, use_exponential_backoff: bool ) -> Result<(), Error>

Controls reconnection behavior when running in the message loop. By default, if a client is unexpectedly disconnected, mosquitto will try to reconnect. The default reconnect parameters are to retry once per second to reconnect.

You change adjust the delay between connection attempts by changing the parameters with this function.

reconnect_delay is the base delay amount.

If use_exponential_backoff is true, then the delay is doubled on each successive attempt, until the max_reconnect_delay is reached.

If use_exponential_backoff is false, then the reconnect_delay is added on each successive attempt, until the max_reconnect_delay is reached.

Trait Implementations§

source§

impl<CB: Callbacks + Send + Sync> Drop for Mosq<CB>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<CB: Callbacks + Send + Sync> Send for Mosq<CB>

source§

impl<CB: Callbacks + Send + Sync> Sync for Mosq<CB>

Auto Trait Implementations§

§

impl<CB> RefUnwindSafe for Mosq<CB>
where CB: RefUnwindSafe,

§

impl<CB> Unpin for Mosq<CB>

§

impl<CB> UnwindSafe for Mosq<CB>
where CB: RefUnwindSafe,

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

§

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

§

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.