71 lines
1.9 KiB
Rust
71 lines
1.9 KiB
Rust
|
use crate::packets::{PacketParseError, SptprpPacket};
|
||
|
|
||
|
#[derive(Debug, PartialEq)]
|
||
|
pub struct ChannelWaitPacket {
|
||
|
pub channel: [u8; 32]
|
||
|
}
|
||
|
impl SptprpPacket for ChannelWaitPacket {
|
||
|
const PACKET_ID: u8 = 0x01;
|
||
|
|
||
|
fn to_bytes(&self) -> Vec<u8> {
|
||
|
let mut bytes = vec![0x01];
|
||
|
bytes.extend_from_slice(&self.channel);
|
||
|
bytes
|
||
|
}
|
||
|
|
||
|
fn from_bytes(bytes: &Vec<u8>) -> Result<Self, PacketParseError> {
|
||
|
if bytes.len() != 33 {
|
||
|
return Err(PacketParseError::IncorrectPacketLength)
|
||
|
}
|
||
|
if bytes[0] != 0x01u8 {
|
||
|
return Err(PacketParseError::IncorrectPacketType)
|
||
|
}
|
||
|
Ok(Self {
|
||
|
channel: bytes[1..].try_into().unwrap()
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[derive(Debug, PartialEq)]
|
||
|
pub struct RelayDataPacket {
|
||
|
pub channel: [u8; 32],
|
||
|
pub data_len: u64,
|
||
|
pub data: Vec<u8>
|
||
|
}
|
||
|
impl SptprpPacket for RelayDataPacket {
|
||
|
const PACKET_ID: u8 = 0x04;
|
||
|
|
||
|
fn to_bytes(&self) -> Vec<u8> {
|
||
|
let mut bytes = vec![4u8];
|
||
|
bytes.extend_from_slice(&self.channel);
|
||
|
bytes.extend_from_slice(&self.data_len.to_le_bytes());
|
||
|
bytes.extend_from_slice(&self.data[..]);
|
||
|
bytes.push(0xfe);
|
||
|
bytes
|
||
|
}
|
||
|
|
||
|
fn from_bytes(bytes: &Vec<u8>) -> Result<Self, PacketParseError> where Self: Sized {
|
||
|
|
||
|
if bytes.len() < 42 {
|
||
|
return Err(PacketParseError::IncorrectPacketLength)
|
||
|
}
|
||
|
|
||
|
if bytes[0] != 0x04 && bytes[bytes.len()-1] != 0xfe {
|
||
|
return Err(PacketParseError::IncorrectPacketType)
|
||
|
}
|
||
|
|
||
|
let channel = &bytes[1..33];
|
||
|
let data_len = u64::from_le_bytes(bytes[33..41].try_into().unwrap());
|
||
|
let data = &bytes[41..41 + data_len as usize];
|
||
|
|
||
|
if bytes[40 + data_len as usize + 1] != 0xfe {
|
||
|
return Err(PacketParseError::IncorrectPacketType)
|
||
|
}
|
||
|
|
||
|
Ok(Self {
|
||
|
channel: channel.try_into().unwrap(),
|
||
|
data_len,
|
||
|
data: data.to_vec(),
|
||
|
})
|
||
|
}
|
||
|
}
|