Struct mosquitto_rs::Mosq
source · pub struct Mosq<CB = ()>{ /* 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>
impl<CB: Callbacks + Send + Sync> Mosq<CB>
sourcepub fn with_auto_id(callbacks: CB) -> Result<Self, Error>
pub fn with_auto_id(callbacks: CB) -> Result<Self, Error>
Create a new client instance with a random client id
sourcepub fn with_id(
callbacks: CB,
id: &str,
clean_session: bool
) -> Result<Self, Error>
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.
sourcepub fn set_username_and_password(
&self,
username: Option<&str>,
password: Option<&str>
) -> Result<(), Error>
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.
sourcepub fn connect(
&self,
host: &str,
port: c_int,
keep_alive_interval: Duration,
bind_address: Option<&str>
) -> Result<(), Error>
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.
sourcepub fn connect_non_blocking(
&self,
host: &str,
port: c_int,
keep_alive_interval: Duration,
bind_address: Option<&str>
) -> Result<(), Error>
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.
sourcepub fn reconnect(&self) -> Result<(), Error>
pub fn reconnect(&self) -> Result<(), Error>
Reconnect a disconnected client using the same parameters as were originally used to connect it.
sourcepub fn disconnect(&self) -> Result<(), Error>
pub fn disconnect(&self) -> Result<(), Error>
Disconnect the client. This will cause the message loop to terminate.
sourcepub fn publish(
&self,
topic: &str,
payload: &[u8],
qos: QoS,
retain: bool
) -> Result<MessageId, Error>
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.
sourcepub fn set_last_will(
&self,
topic: &str,
payload: &[u8],
qos: QoS,
retain: bool
) -> Result<(), Error>
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.
sourcepub fn clear_last_will(&self) -> Result<(), Error>
pub fn clear_last_will(&self) -> Result<(), Error>
Remove a previously configured will. This must be called before calling connect
sourcepub fn subscribe(&self, pattern: &str, qos: QoS) -> Result<MessageId, Error>
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.
sourcepub fn unsubscribe(&self, pattern: &str) -> Result<MessageId, Error>
pub fn unsubscribe(&self, pattern: &str) -> Result<MessageId, Error>
Remove subscription(s) for topics that match pattern
.
sourcepub fn get_callbacks(&self) -> &CB
pub fn get_callbacks(&self) -> &CB
Returns a reference to the callbacks previously registered during construction.
sourcepub fn loop_until_explicitly_disconnected(
&self,
timeout: Duration
) -> Result<(), Error>
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.
sourcepub fn start_loop_thread(&self) -> Result<(), Error>
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.
sourcepub fn stop_loop_thread(&self, force_cancel: bool) -> Result<(), Error>
pub fn stop_loop_thread(&self, force_cancel: bool) -> Result<(), Error>
Stops the message loop thread started via start_loop_thread
sourcepub fn set_string_option(
&self,
option: mosq_opt_t,
value: &str
) -> Result<(), Error>
pub fn set_string_option( &self, option: mosq_opt_t, value: &str ) -> Result<(), Error>
Sets an option with a string value
sourcepub fn set_int_option(
&self,
option: mosq_opt_t,
value: c_int
) -> Result<(), Error>
pub fn set_int_option( &self, option: mosq_opt_t, value: c_int ) -> Result<(), Error>
Sets an option with an integer value
sourcepub unsafe fn set_ptr_option(
&self,
option: mosq_opt_t,
value: *mut c_void
) -> Result<(), Error>
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.
sourcepub 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>
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>
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.
sourcepub fn set_reconnect_delay(
&self,
reconnect_delay: Duration,
max_reconnect_delay: Duration,
use_exponential_backoff: bool
) -> Result<(), Error>
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.