Skip to content

Commit

Permalink
issue #8 Resolved.
Browse files Browse the repository at this point in the history
  • Loading branch information
00imvj00 committed Sep 16, 2019
1 parent b51c57f commit 69da040
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 8 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cargo-features = ["edition"]

[package]
name = "mqttrs"
version = "0.1.3"
version = "0.1.4"
authors = ["00imvj00 <vijaybambhaniya007@gmail.com>"]
edition = "2018"
description = "mqttrs is encoding & decoding library for mqtt protocol, it can work with both sync as well as async apps"
Expand All @@ -13,4 +13,5 @@ license = "Apache-2.0"


[dependencies]
bytes = "0.4.11"
bytes = "0.4"

7 changes: 4 additions & 3 deletions src/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ use std::io;
#[allow(dead_code)]
pub fn decode(buffer: &mut BytesMut) -> Result<Option<Packet>, io::Error> {
if let Some((header, header_size)) = read_header(buffer) {
buffer.split_to(header_size);
if buffer.len() >= header.len() {
let p = read_packet(header, buffer)?;
if buffer.len() >= header.len() + header_size {
//NOTE: Check if buffer has, header bytes + remaining length bytes in buffer.
buffer.split_to(header_size); //NOTE: Remove header bytes from buffer.
let p = read_packet(header, buffer)?; //NOTE: Read remaining packet.
Ok(Some(p))
} else {
Ok(None)
Expand Down
9 changes: 6 additions & 3 deletions src/decoder_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,18 @@ fn test_disconnect() {
#[test]
fn test_publish() {
let mut data = BytesMut::from(vec![
0b00110000, 12, 0x00, 0x03, 'a' as u8, '/' as u8, 'b' as u8, 0x00, 0x10, 'h' as u8,
'e' as u8, 'l' as u8, 'l' as u8, 'o' as u8, 0b00111000, 12, 0x00, 0x03, 'a' as u8,
'/' as u8, 'b' as u8, 0x00, 0x10, 'h' as u8, 'e' as u8, 'l' as u8, 'l' as u8, 'o' as u8,
0b00110000, 10, 0x00, 0x03, 'a' as u8, '/' as u8, 'b' as u8, 'h' as u8, 'e' as u8,
'l' as u8, 'l' as u8, 'o' as u8, //
0b00111000, 10, 0x00, 0x03, 'a' as u8, '/' as u8, 'b' as u8, 'h' as u8, 'e' as u8,
'l' as u8, 'l' as u8, 'o' as u8, //
0b00111101, 12, 0x00, 0x03, 'a' as u8, '/' as u8, 'b' as u8, 0 as u8, 10 as u8, 'h' as u8,
'e' as u8, 'l' as u8, 'l' as u8, 'o' as u8,
]);
let d1 = decoder::decode(&mut data).unwrap();
let d2 = decoder::decode(&mut data).unwrap();
let d3 = decoder::decode(&mut data).unwrap();

println!("{:?}", d1);
match d1 {
Some(Packet::Publish(p)) => {
assert_eq!(p.dup, false);
Expand Down
2 changes: 2 additions & 0 deletions src/encoder_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ fn test_publish() {
let mut buffer = BytesMut::with_capacity(1024);
let _ = encoder::encode(&Packet::Publish(packet), &mut buffer);
let decoded = decoder::decode(&mut buffer).unwrap();
println!("{:?}", decoded);
match decoded {
Some(Packet::Publish(_c)) => {
assert!(true);
Expand Down Expand Up @@ -101,6 +102,7 @@ fn test_pubrel() {
let mut buffer = BytesMut::with_capacity(1024);
let _ = encoder::encode(&packet, &mut buffer);
let decoded = decoder::decode(&mut buffer).unwrap();
println!("{:?}", decoded);
match decoded {
Some(Packet::Pubrel(_c)) => {
assert!(true);
Expand Down
1 change: 1 addition & 0 deletions src/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ impl Publish {
} else {
Some(PacketIdentifier(buffer.split_to(2).into_buf().get_u16_be()))
};

let payload = buffer.to_vec();
Ok(Publish {
dup: header.dup(),
Expand Down

0 comments on commit 69da040

Please sign in to comment.