1
0
Fork 0

proto fw: add helper function for 4x writer

This commit is contained in:
morgan 2024-10-09 12:28:23 +08:00
parent 24a16a3a5b
commit cc066933fb
1 changed files with 36 additions and 14 deletions

View File

@ -266,16 +266,48 @@ pub fn receive(channel: usize) -> Result<Option<DownConnPacket>, Error> {
}
trait CxpWrite {
fn write_all_4x(&mut self, buf: &[u8]) -> Result<(), Error>;
fn write_4x_u8(&mut self, value: u8) -> Result<(), Error>;
fn write_4x_u16(&mut self, value: u16) -> Result<(), Error>;
fn write_4x_u32(&mut self, value: u32) -> Result<(), Error>;
fn write_4x_u64(&mut self, value: u64) -> Result<(), Error>;
fn write_u32(&mut self, value: u32) -> Result<(), Error>;
}
impl<Cursor: Write> CxpWrite for Cursor {
fn write_4x_u8(&mut self, value: u8) -> Result<(), Error> {
self.write_all(&[value; 4])?;
fn write_all_4x(&mut self, buf: &[u8]) -> Result<(), Error> {
for byte in buf {
self.write_all(&[*byte; 4])?;
}
Ok(())
}
fn write_4x_u8(&mut self, value: u8) -> Result<(), Error> {
self.write_all_4x(&[value])
}
fn write_4x_u16(&mut self, value: u16) -> Result<(), Error> {
let mut bytes = [0; 2];
NetworkEndian::write_u16(&mut bytes, value);
self.write_all_4x(&bytes)
}
fn write_4x_u32(&mut self, value: u32) -> Result<(), Error> {
let mut bytes = [0; 4];
NetworkEndian::write_u32(&mut bytes, value);
self.write_all_4x(&bytes)
}
fn write_4x_u64(&mut self, value: u64) -> Result<(), Error> {
let mut bytes = [0; 6];
NetworkEndian::write_u64(&mut bytes, value);
self.write_all_4x(&bytes)
}
fn write_u32(&mut self, value: u32) -> Result<(), Error> {
let mut bytes = [0; 4];
NetworkEndian::write_u32(&mut bytes, value);
@ -392,21 +424,11 @@ impl UpConnPacket {
timestamp,
data,
} => {
let id_bytes = conn_id.to_be_bytes();
let length_bytes = length.to_be_bytes();
// event packet header
writer.write_4x_u8(0x07)?;
writer.write_4x_u8(id_bytes[0])?;
writer.write_4x_u8(id_bytes[1])?;
writer.write_4x_u8(id_bytes[2])?;
writer.write_4x_u8(id_bytes[3])?;
writer.write_4x_u32(conn_id)?;
writer.write_4x_u8(packet_tag)?;
writer.write_4x_u8(length_bytes[0])?;
writer.write_4x_u8(length_bytes[1])?;
writer.write_4x_u16(length)?;
// event message
let ev_size = event_size.to_be_bytes();