use ntex_bytes::{ByteString, Bytes};
use ntex_net::connect::Address;
use ntex_util::time::Seconds;
mod connection;
mod connector;
pub mod control;
mod dispatcher;
pub use self::connection::{Client, ClientRouter};
pub use self::connector::{MqttConnector, MqttConnectorService};
pub use self::control::{ProtocolMessage, ProtocolMessageAck};
pub use crate::topic::{TopicFilter, TopicFilterError};
pub use crate::types::QoS;
pub use crate::v3::{codec, error, error::ClientError, sink::MqttSink};
#[derive(Clone, Debug)]
pub struct Connect<A: Address> {
addr: A,
pkt: codec::Connect,
}
impl<A: Address> Connect<A> {
#[inline]
pub fn new(addr: A) -> Self {
Self { addr, pkt: codec::Connect::default() }
}
#[inline]
pub fn with(addr: A, pkt: codec::Connect) -> Self {
Self { addr, pkt }
}
#[inline]
#[must_use]
pub fn client_id<U>(mut self, client_id: U) -> Self
where
ByteString: From<U>,
{
self.pkt.client_id = client_id.into();
self
}
#[inline]
#[must_use]
pub fn clean_session(mut self) -> Self {
self.pkt.clean_session = true;
self
}
#[inline]
#[must_use]
pub fn keep_alive(mut self, val: Seconds) -> Self {
self.pkt.keep_alive = val.seconds() as u16;
self
}
#[inline]
#[must_use]
pub fn last_will(mut self, val: codec::LastWill) -> Self {
self.pkt.last_will = Some(val);
self
}
#[inline]
#[must_use]
pub fn username<U>(mut self, val: U) -> Self
where
ByteString: From<U>,
{
self.pkt.username = Some(val.into());
self
}
#[inline]
#[must_use]
pub fn password(mut self, val: Bytes) -> Self {
self.pkt.password = Some(val);
self
}
#[inline]
#[must_use]
pub fn packet<F>(mut self, f: F) -> Self
where
F: FnOnce(&mut codec::Connect),
{
f(&mut self.pkt);
self
}
fn into_parts(self) -> (A, codec::Connect) {
(self.addr, self.pkt)
}
}