Skip to content

Commit

Permalink
[#52] refactored Packet trait, replaced with DecodablePacket
Browse files Browse the repository at this point in the history
- splits Packet into EncodablePacket and DecodablePacket
- removes default payload() and payload_ref() methods
- fixes encoded_length() implementations for all packets
- add PublishPacketRef for performance optimization
  • Loading branch information
zonyitoo committed Feb 26, 2021
1 parent 1579d72 commit e910579
Show file tree
Hide file tree
Showing 21 changed files with 218 additions and 233 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ rust:

script:
- cargo test -v
- cargo test --features "tokio-codec"
3 changes: 2 additions & 1 deletion examples/pub-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ fn main() {
let message = format!("{}: {}", user_name, line.trim_end());

for chan in &channels {
let publish_packet = PublishPacket::new(chan.clone(), QoSWithPacketIdentifier::Level0, message.clone());
// let publish_packet = PublishPacket::new(chan.clone(), QoSWithPacketIdentifier::Level0, message.clone());
let publish_packet = PublishPacketRef::new(chan, QoSWithPacketIdentifier::Level0, message.as_bytes());
let mut buf = Vec::new();
publish_packet.encode(&mut buf).unwrap();
stream.write_all(&buf[..]).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion examples/sub-client-async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ async fn main() {
info!("Receiving PINGRESP from broker ..");
}
VariablePacket::PublishPacket(ref publ) => {
let msg = match str::from_utf8(&publ.payload_ref()[..]) {
let msg = match str::from_utf8(publ.payload()) {
Ok(msg) => msg,
Err(err) => {
error!("Failed to decode publish message {:?}", err);
Expand Down
2 changes: 1 addition & 1 deletion examples/sub-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ fn main() {
info!("Receiving PINGRESP from broker ..");
}
VariablePacket::PublishPacket(ref publ) => {
let msg = match str::from_utf8(&publ.payload_ref()[..]) {
let msg = match str::from_utf8(publ.payload()) {
Ok(msg) => msg,
Err(err) => {
error!("Failed to decode publish message {:?}", err);
Expand Down
16 changes: 8 additions & 8 deletions src/encodable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ pub trait Encodable {
fn encoded_length(&self) -> u32;
}

impl<T: Encodable> Encodable for &T {
fn encode<W: Write>(&self, writer: &mut W) -> io::Result<()> {
(**self).encode(writer)
}
fn encoded_length(&self) -> u32 {
(**self).encoded_length()
}
}
// impl<T: Encodable> Encodable for &T {
// fn encode<W: Write>(&self, writer: &mut W) -> io::Result<()> {
// (**self).encode(writer)
// }
// fn encoded_length(&self) -> u32 {
// (**self).encoded_length()
// }
// }

impl<T: Encodable> Encodable for Option<T> {
fn encode<W: Write>(&self, writer: &mut W) -> io::Result<()> {
Expand Down
15 changes: 2 additions & 13 deletions src/packet/connack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::io::Read;

use crate::control::variable_header::{ConnackFlags, ConnectReturnCode};
use crate::control::{ControlType, FixedHeader, PacketType};
use crate::packet::{Packet, PacketError};
use crate::packet::{DecodablePacket, PacketError};
use crate::Decodable;

/// `CONNACK` packet
Expand All @@ -13,7 +13,6 @@ pub struct ConnackPacket {
fixed_header: FixedHeader,
flags: ConnackFlags,
ret_code: ConnectReturnCode,
payload: (),
}

encodable_packet!(ConnackPacket(flags, ret_code));
Expand All @@ -24,7 +23,6 @@ impl ConnackPacket {
fixed_header: FixedHeader::new(PacketType::with_default(ControlType::ConnectAcknowledgement), 2),
flags: ConnackFlags { session_present },
ret_code,
payload: (),
}
}

Expand All @@ -37,17 +35,9 @@ impl ConnackPacket {
}
}

impl Packet for ConnackPacket {
impl DecodablePacket for ConnackPacket {
type Payload = ();

fn payload(self) -> Self::Payload {
self.payload
}

fn payload_ref(&self) -> &Self::Payload {
&self.payload
}

fn decode_packet<R: Read>(reader: &mut R, fixed_header: FixedHeader) -> Result<Self, PacketError<Self>> {
let flags: ConnackFlags = Decodable::decode(reader)?;
let code: ConnectReturnCode = Decodable::decode(reader)?;
Expand All @@ -56,7 +46,6 @@ impl Packet for ConnackPacket {
fixed_header,
flags,
ret_code: code,
payload: (),
})
}
}
Expand Down
14 changes: 3 additions & 11 deletions src/packet/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::control::variable_header::protocol_level::SPEC_3_1_1;
use crate::control::variable_header::{ConnectFlags, KeepAlive, ProtocolLevel, ProtocolName, VariableHeaderError};
use crate::control::{ControlType, FixedHeader, PacketType};
use crate::encodable::VarBytes;
use crate::packet::{Packet, PacketError};
use crate::packet::{DecodablePacket, PacketError};
use crate::topic_name::{TopicName, TopicNameDecodeError, TopicNameError};
use crate::{Decodable, Encodable};

Expand All @@ -23,7 +23,7 @@ pub struct ConnectPacket {
payload: ConnectPacketPayload,
}

encodable_packet!(ConnectPacket(protocol_name, protocol_level, flags, keep_alive));
encodable_packet!(ConnectPacket(protocol_name, protocol_level, flags, keep_alive, payload));

impl ConnectPacket {
pub fn new<C>(client_identifier: C) -> ConnectPacket
Expand Down Expand Up @@ -151,17 +151,9 @@ impl ConnectPacket {
}
}

impl Packet for ConnectPacket {
impl DecodablePacket for ConnectPacket {
type Payload = ConnectPacketPayload;

fn payload(self) -> ConnectPacketPayload {
self.payload
}

fn payload_ref(&self) -> &ConnectPacketPayload {
&self.payload
}

fn decode_packet<R: Read>(reader: &mut R, fixed_header: FixedHeader) -> Result<Self, PacketError<Self>> {
let protoname: ProtocolName = Decodable::decode(reader)?;
let protocol_level: ProtocolLevel = Decodable::decode(reader)?;
Expand Down
19 changes: 3 additions & 16 deletions src/packet/disconnect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
use std::io::Read;

use crate::control::{ControlType, FixedHeader, PacketType};
use crate::packet::{Packet, PacketError};
use crate::packet::{DecodablePacket, PacketError};

/// `DISCONNECT` packet
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct DisconnectPacket {
fixed_header: FixedHeader,
payload: (),
}

encodable_packet!(DisconnectPacket());
Expand All @@ -18,7 +17,6 @@ impl DisconnectPacket {
pub fn new() -> DisconnectPacket {
DisconnectPacket {
fixed_header: FixedHeader::new(PacketType::with_default(ControlType::Disconnect), 0),
payload: (),
}
}
}
Expand All @@ -29,21 +27,10 @@ impl Default for DisconnectPacket {
}
}

impl Packet for DisconnectPacket {
impl DecodablePacket for DisconnectPacket {
type Payload = ();

fn payload(self) -> Self::Payload {
self.payload
}

fn payload_ref(&self) -> &Self::Payload {
&self.payload
}

fn decode_packet<R: Read>(_reader: &mut R, fixed_header: FixedHeader) -> Result<Self, PacketError<Self>> {
Ok(DisconnectPacket {
fixed_header,
payload: (),
})
Ok(DisconnectPacket { fixed_header })
}
}
Loading

0 comments on commit e910579

Please sign in to comment.