Skip to content

Commit

Permalink
fix: decompress buffer overfilled - added a size header in message
Browse files Browse the repository at this point in the history
  • Loading branch information
Sequal32 committed Nov 20, 2022
1 parent f65d0e9 commit d3eb22b
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/yourcontrols-net/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ pub enum Payloads {
Heartbeat,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct PayloadWrapper {
pub data: Vec<u8>,
pub size: usize, // used for decompression buffer size
}

pub enum Message {
Payload(SocketAddr, Payloads),
ConnectionClosed(SocketAddr),
Expand Down Expand Up @@ -148,8 +154,12 @@ impl SenderReceiver {
_ => return Err(Error::NotProcessed),
};

// Decode wrapper struct
let wrapper: PayloadWrapper = rmp_serde::from_slice(packet.payload())?;

// Decompress
let payload_bytes = self.decompressor.decompress(packet.payload(), 131072)?;
let payload_bytes = self.decompressor.decompress(&wrapper.data, wrapper.size)?;

// Decode to struct
let payload = rmp_serde::from_slice(&payload_bytes)?;
Ok(Message::Payload(packet.addr(), payload))
Expand All @@ -161,10 +171,19 @@ impl SenderReceiver {

fn prepare_payload_bytes(&mut self, message: &Payloads) -> Result<Vec<u8>, Error> {
// Struct to MessagePack
let payload = rmp_serde::to_vec(&message)?;
let payload_bytes = rmp_serde::to_vec(&message)?;

// Compress
let compressed = self.compressor.compress(&payload_bytes, 0)?;

// Wrap
let wrapper = PayloadWrapper {
data: compressed,
size: payload_bytes.len(),
};

// Compress payload
Ok(self.compressor.compress(&payload, 0)?)
// Serialize
Ok(rmp_serde::to_vec(&wrapper)?)
}

pub fn send_message(&mut self, message: Payloads, target: SocketAddr) -> Result<(), Error> {
Expand Down

0 comments on commit d3eb22b

Please sign in to comment.