Skip to content

Commit

Permalink
refactor(valve rcon): clean up packet
Browse files Browse the repository at this point in the history
  • Loading branch information
cainthebest committed Dec 13, 2024
1 parent fa7c23a commit 059a826
Showing 1 changed file with 62 additions and 25 deletions.
87 changes: 62 additions & 25 deletions crates/lib/src/protocol/rcon/valve_source/packet.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
use crate::{core::Buffer, error::Result};
use {
crate::{
core::Buffer,
error::{diagnostic::FailureReason, PacketError, Report, Result},
},

error_stack::ResultExt,
};

// packet type is i32 but we know
// it will always be 0, 2, or 3
Expand Down Expand Up @@ -50,41 +57,71 @@ impl Packet {
pub fn serialize(&self) -> Vec<u8> {
let mut buf = Vec::with_capacity((self.size + Self::FIELD_SIZE_LENGTH as i32) as usize);

buf.extend_from_slice(&self.size.to_le_bytes());
buf.extend_from_slice(&self.id.to_le_bytes());
buf.extend_from_slice(&(self.r#type as i32).to_le_bytes());
buf.extend(&self.size.to_le_bytes());
buf.extend(&self.id.to_le_bytes());
buf.extend(&(self.r#type as i32).to_le_bytes());

if let Some(body) = &self.body {
buf.extend_from_slice(body.as_bytes());
buf.extend(body.as_bytes());
}

buf.extend_from_slice(&Self::PADDING);
buf.extend(&Self::PADDING);

buf
}

// buffer is a internal interface so the function cannot be pub
#[allow(dead_code)]
pub(crate) fn deserialize(b: &mut Buffer) -> Result<Self> {
// TODO: add error context here
Ok(Self {
size: b.read_i32_le()?,
id: b.read_i32_le()?,
r#type: b.read_i32_le()? as PacketType,
body: match b.peek( 1)?[0] {
Self::BODY_DELIMITER => {
b.move_pos(Self::PADDING_LENGTH as isize)?;

None
}

_ => {
let body = b.read_string_utf8(Some([Self::BODY_DELIMITER]), false)?;

b.move_pos(Self::TAIL_TERMINATOR as isize)?;
let size = b
.read_i32_le()
.map_err(|e| {
Report::from(e).change_context(PacketError::PacketDeserializeError {}.into())
})
.attach_printable(FailureReason::new(
"Failed to deserialize size field of packet.",
))?;

let id = b
.read_i32_le()
.map_err(|e| {
Report::from(e).change_context(PacketError::PacketDeserializeError {}.into())
})
.attach_printable(FailureReason::new(
"Failed to deserialize id field of packet.",
))?;

let r#type = b
.read_i32_le()
.map_err(|e| {
Report::from(e).change_context(PacketError::PacketDeserializeError {}.into())
})
.attach_printable(FailureReason::new(
"Failed to deserialize type field of packet.",
))? as PacketType;

let body = match b.peek(1)?[0] {
Self::BODY_DELIMITER => {
b.move_pos(Self::PADDING_LENGTH as isize)?;

None
}

_ => {
let body = b.read_string_utf8(Some([Self::BODY_DELIMITER]), false)?;

// skip tail
b.move_pos(1)?;

Some(body)
}
};

Some(body)
}
},
Ok(Self {
size,
id,
r#type,
body,
})
}
}

0 comments on commit 059a826

Please sign in to comment.