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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
use core::convert::TryInto;
use core::fmt;
use core::mem::replace;
use alloc::{vec, vec::Vec};
const MINIMAL_PACKET_LENGTH: usize = 32;
pub struct PacketReader {
pending_packet: Vec<u8>,
already_read: usize,
}
impl fmt::Debug for PacketReader {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("PacketReader")
.field(&format_args!(
"{}/{}",
self.already_read,
self.pending_packet.len()
))
.finish()
}
}
impl Default for PacketReader {
fn default() -> Self {
Self::new()
}
}
impl PacketReader {
pub fn new() -> Self {
Self {
pending_packet: vec![0; MINIMAL_PACKET_LENGTH],
already_read: 0,
}
}
pub fn buffer(&mut self) -> &mut [u8] {
&mut self.pending_packet[self.already_read..]
}
pub fn remaining_capacity(&self) -> usize {
self.pending_packet.len() - self.already_read
}
pub fn advance(&mut self, amount: usize) -> Option<Vec<u8>> {
self.already_read += amount;
debug_assert!(self.already_read <= self.pending_packet.len());
if self.already_read == MINIMAL_PACKET_LENGTH {
let extra_length = extra_length(&self.pending_packet);
if extra_length > 0 {
let total_length = MINIMAL_PACKET_LENGTH + extra_length;
self.pending_packet.resize(total_length, 0);
return None;
}
} else if self.already_read != self.pending_packet.len() {
return None;
}
self.already_read = 0;
Some(replace(
&mut self.pending_packet,
vec![0; MINIMAL_PACKET_LENGTH],
))
}
}
fn extra_length(buffer: &[u8]) -> usize {
use crate::protocol::xproto::GE_GENERIC_EVENT;
const REPLY: u8 = 1;
let response_type = buffer[0];
if response_type == REPLY || response_type & 0x7f == GE_GENERIC_EVENT {
let length_field = buffer[4..8].try_into().unwrap();
let length_field = u32::from_ne_bytes(length_field) as usize;
4 * length_field
} else {
0
}
}
#[cfg(test)]
mod tests {
use super::PacketReader;
use alloc::{vec, vec::Vec};
fn test_packets(packets: Vec<Vec<u8>>) {
let mut reader = PacketReader::new();
for mut packet in packets {
let original_packet = packet.clone();
loop {
let buffer = reader.buffer();
let amount = std::cmp::min(buffer.len(), packet.len());
buffer.copy_from_slice(&packet[..amount]);
let _ = packet.drain(..amount);
if let Some(read_packet) = reader.advance(amount) {
assert_eq!(read_packet, original_packet);
return;
}
}
}
}
#[test]
fn fixed_size_packet() {
let packet = vec![0; 32];
test_packets(vec![packet]);
}
#[test]
fn variable_size_packet() {
let mut len = 1200;
let mut packet = vec![0; len];
len = (len - 32) / 4;
let len_bytes = (len as u32).to_ne_bytes();
packet[4..8].copy_from_slice(&len_bytes);
packet[0] = 1;
test_packets(vec![packet]);
}
#[test]
fn test_many_fixed_size_packets() {
let mut packets = vec![];
for _ in 0..100 {
packets.push(vec![0; 32]);
}
test_packets(packets);
}
#[test]
fn test_many_variable_size_packets() {
let mut packets = vec![];
for i in 0..100 {
let variation = ((i - 50) * (i - 50)) as f32;
let variation = -1.0 / 25.0 * variation + 100.0;
let variation = variation as usize;
let mut len = 1200 + variation;
let mut packet = vec![0; len];
len = (len - 32) / 4;
let len_bytes = (len as u32).to_ne_bytes();
packet[4..8].copy_from_slice(&len_bytes);
packet[0] = 1;
packets.push(packet);
}
test_packets(packets);
}
#[test]
fn text_many_size_packets_mixed() {
let mut packets = vec![];
for i in 0..100 {
let mut len = if i & 1 == 1 {
let variation = ((i - 50) * (i - 50)) as f32;
let variation = -1.0 / 25.0 * variation + 100.0;
let variation = variation as usize;
1200 + variation
} else {
32
};
let mut packet = vec![0; len];
len = (len - 32) / 4;
let len_bytes = (len as u32).to_ne_bytes();
packet[4..8].copy_from_slice(&len_bytes);
packet[0] = 1;
packets.push(packet);
}
test_packets(packets);
}
}