classicube_relay/packet/
mod.rs1mod r#continue;
2mod flags;
3mod scope;
4mod start;
5
6pub use self::{flags::*, r#continue::*, scope::*, start::*};
7use std::io::Read;
8
9#[derive(Debug, thiserror::Error)]
10pub enum StreamError {
11 #[error("{0}")]
12 LengthOverflow(String),
13
14 #[error("can't find free outgoing packet id")]
15 PacketIdLimit,
16
17 #[error(transparent)]
18 Flags(#[from] FlagsError),
19
20 #[error(transparent)]
21 StartPacket(#[from] StartPacketError),
22
23 #[error(transparent)]
24 ContinuePacket(#[from] ContinuePacketError),
25}
26type Result<T> = std::result::Result<T, StreamError>;
27
28#[derive(Debug, PartialEq, Eq)]
30pub enum Packet {
31 Start(StartPacket),
32 Continue(ContinuePacket),
33}
34impl Packet {
35 pub const DATA_LENGTH: usize = 64;
36
37 pub fn encode(&self) -> Result<Vec<u8>> {
38 match self {
39 Self::Start(packet) => Ok(packet.encode()?),
40 Self::Continue(packet) => Ok(packet.encode()?),
41 }
42 }
43
44 pub fn decode(data_stream: &mut impl Read) -> Result<Self> {
45 let flags = Flags::decode(data_stream)?;
46
47 let packet = if flags.is_packet_start {
48 Packet::Start(StartPacket::decode(flags.stream_id, data_stream)?)
49 } else {
50 Packet::Continue(ContinuePacket::decode(flags.stream_id, data_stream)?)
51 };
52
53 Ok(packet)
54 }
55}