Skip to content

Commit

Permalink
Tls support (#16)
Browse files Browse the repository at this point in the history
* TLS support
  • Loading branch information
obabec authored May 2, 2022
1 parent 37bf023 commit 63314fc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rust-mqtt"
version = "0.1.2"
version = "0.1.3"
authors = ["Ondrej Babec <[email protected]>"]
edition = "2021"
resolver = "2"
Expand Down Expand Up @@ -28,4 +28,5 @@ serial_test = "0.6.0"
[features]
default = ["std"]
std = ["tokio", "log"]
no_std = ["defmt"]
no_std = ["defmt"]
tls = []
25 changes: 23 additions & 2 deletions src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ where
}
}

pub async fn send_ping_v5<'b>(&'b mut self) -> Result<(), ReasonCode> {
async fn send_ping_v5<'b>(&'b mut self) -> Result<(), ReasonCode> {
if self.connection.is_none() {
return Err(ReasonCode::NetworkError);
}
Expand Down Expand Up @@ -513,6 +513,7 @@ where
}
}

#[cfg(not(feature = "tls"))]
async fn receive_packet<'c, T: NetworkConnection>(
buffer: &mut [u8],
buffer_len: usize,
Expand Down Expand Up @@ -548,7 +549,7 @@ async fn receive_packet<'c, T: NetworkConnection>(
}
}
}

trace!("Lenght done!");
let rem_len_len = i;
i = 0;
if let Ok(l) = VariableByteIntegerDecoder::decode(rem_len.unwrap()) {
Expand All @@ -575,3 +576,23 @@ async fn receive_packet<'c, T: NetworkConnection>(
}
}
}

#[cfg(feature = "tls")]
async fn receive_packet<'c, T: NetworkConnection>(
buffer: &mut [u8],
buffer_len: usize,
recv_buffer: &mut [u8],
conn: &'c mut T,
) -> Result<usize, ReasonCode> {
trace!("Reading packet");
let mut writer = BuffWriter::new(buffer, buffer_len);
let len = conn
.receive(recv_buffer)
.await?;
if let Err(_e) = writer.insert_ref(len, &recv_buffer[writer.position..(writer.position + len)])
{
error!("Error occurred during write to buffer!");
return Err(BuffError);
}
Ok(len)
}
4 changes: 1 addition & 3 deletions src/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ pub trait NetworkConnection {
where
Self: 'm;

type CloseFuture<'m>: Future<Output = Result<(), ReasonCode>>
where
Self: 'm;
type CloseFuture<'m>: Future<Output = Result<(), ReasonCode>>;

fn send<'m>(&'m mut self, buffer: &'m [u8]) -> Self::SendFuture<'m>;

Expand Down

0 comments on commit 63314fc

Please sign in to comment.