1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use super::{HeaderReader, HeaderWriter};
use crate::error::Result;
use crate::net::constants::ARRANGING_PACKET_HEADER;
use crate::packet::SequenceNumber;
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use std::io::Cursor;
#[derive(Copy, Clone, Debug)]
pub struct ArrangingHeader {
arranging_id: SequenceNumber,
stream_id: u8,
}
impl ArrangingHeader {
pub fn new(arranging_id: SequenceNumber, stream_id: u8) -> Self {
ArrangingHeader {
arranging_id,
stream_id,
}
}
pub fn arranging_id(&self) -> SequenceNumber {
self.arranging_id
}
pub fn stream_id(&self) -> u8 {
self.stream_id
}
}
impl HeaderWriter for ArrangingHeader {
type Output = Result<()>;
fn parse(&self, buffer: &mut Vec<u8>) -> Self::Output {
buffer.write_u16::<BigEndian>(self.arranging_id)?;
buffer.write_u8(self.stream_id)?;
Ok(())
}
}
impl HeaderReader for ArrangingHeader {
type Header = Result<ArrangingHeader>;
fn read(rdr: &mut Cursor<&[u8]>) -> Self::Header {
let arranging_id = rdr.read_u16::<BigEndian>()?;
let stream_id = rdr.read_u8()?;
let header = ArrangingHeader {
arranging_id,
stream_id,
};
Ok(header)
}
fn size() -> u8 {
ARRANGING_PACKET_HEADER
}
}
#[cfg(test)]
mod tests {
use crate::net::constants::ARRANGING_PACKET_HEADER;
use crate::packet::header::{ArrangingHeader, HeaderReader, HeaderWriter};
use std::io::Cursor;
#[test]
fn serialize() {
let mut buffer = Vec::new();
let header = ArrangingHeader::new(1, 2);
header.parse(&mut buffer).is_ok();
assert_eq!(buffer[1], 1);
assert_eq!(buffer[2], 2);
}
#[test]
fn deserialize() {
let buffer = vec![0, 1, 2];
let mut cursor = Cursor::new(buffer.as_slice());
let header = ArrangingHeader::read(&mut cursor).unwrap();
assert_eq!(header.arranging_id(), 1);
assert_eq!(header.stream_id(), 2);
}
#[test]
fn size() {
assert_eq!(ArrangingHeader::size(), ARRANGING_PACKET_HEADER);
}
}