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
use crate::{
net::constants::{DEFAULT_ORDERING_STREAM, DEFAULT_SEQUENCING_STREAM},
packet::{
header::{
AckedPacketHeader, ArrangingHeader, FragmentHeader, HeaderWriter, StandardHeader,
},
DeliveryGuarantee, OrderingGuarantee, PacketType,
},
};
pub struct OutgoingPacketBuilder<'p> {
header: Vec<u8>,
payload: &'p [u8],
}
impl<'p> OutgoingPacketBuilder<'p> {
pub fn new(payload: &'p [u8]) -> OutgoingPacketBuilder<'p> {
OutgoingPacketBuilder {
header: Vec::new(),
payload,
}
}
pub fn with_fragment_header(mut self, packet_seq: u16, id: u8, num_fragments: u8) -> Self {
let header = FragmentHeader::new(packet_seq, id, num_fragments);
header
.parse(&mut self.header)
.expect("Could not write fragment header to buffer");
self
}
pub fn with_default_header(
mut self,
packet_type: PacketType,
delivery_guarantee: DeliveryGuarantee,
ordering_guarantee: OrderingGuarantee,
) -> Self {
let header = StandardHeader::new(delivery_guarantee, ordering_guarantee, packet_type);
header
.parse(&mut self.header)
.expect("Could not write default header to buffer");
self
}
pub fn with_acknowledgment_header(
mut self,
seq_num: u16,
last_seq: u16,
bit_field: u32,
) -> Self {
let header = AckedPacketHeader::new(seq_num, last_seq, bit_field);
header
.parse(&mut self.header)
.expect("Could not write acknowledgment header to buffer");
self
}
pub fn with_sequencing_header(mut self, arranging_id: u16, stream_id: Option<u8>) -> Self {
let header =
ArrangingHeader::new(arranging_id, stream_id.unwrap_or(DEFAULT_SEQUENCING_STREAM));
header
.parse(&mut self.header)
.expect("Could not write arranging header to buffer");
self
}
pub fn with_ordering_header(mut self, arranging_id: u16, stream_id: Option<u8>) -> Self {
let header =
ArrangingHeader::new(arranging_id, stream_id.unwrap_or(DEFAULT_ORDERING_STREAM));
header
.parse(&mut self.header)
.expect("Could not write arranging header to buffer");
self
}
pub fn build(self) -> OutgoingPacket<'p> {
OutgoingPacket {
header: self.header,
payload: self.payload,
}
}
}
pub struct OutgoingPacket<'p> {
header: Vec<u8>,
payload: &'p [u8],
}
impl<'p> OutgoingPacket<'p> {
pub fn contents(&self) -> Box<[u8]> {
[self.header.as_slice(), &self.payload]
.concat()
.into_boxed_slice()
}
}
pub enum Outgoing<'a> {
Packet(OutgoingPacket<'a>),
Fragments(Vec<OutgoingPacket<'a>>),
}
#[cfg(test)]
mod tests {
use crate::packet::PacketType;
use crate::packet::{DeliveryGuarantee, OrderingGuarantee, OutgoingPacketBuilder};
fn test_payload() -> Vec<u8> {
return "test".as_bytes().to_vec();
}
#[test]
fn assure_creation_fragment_header() {
let payload = test_payload();
let outgoing = OutgoingPacketBuilder::new(&payload)
.with_fragment_header(0, 0, 0)
.build();
let expected: Vec<u8> = [vec![0, 0, 0, 0], test_payload()].concat().to_vec();
assert_eq!(outgoing.contents().to_vec(), expected);
}
#[test]
fn assure_creation_arranging_header() {
let payload = test_payload();
let outgoing = OutgoingPacketBuilder::new(&payload)
.with_sequencing_header(1, Some(2))
.build();
let expected: Vec<u8> = [vec![0, 1, 2], test_payload()].concat().to_vec();
assert_eq!(outgoing.contents().to_vec(), expected);
}
#[test]
fn assure_creation_acknowledgment_header() {
let payload = test_payload();
let outgoing = OutgoingPacketBuilder::new(&payload)
.with_acknowledgment_header(1, 2, 3)
.build();
let expected: Vec<u8> = [vec![0, 1, 0, 2, 0, 0, 0, 3], test_payload()]
.concat()
.to_vec();
assert_eq!(outgoing.contents().to_vec(), expected);
}
#[test]
fn assure_creation_default_header() {
let payload = test_payload();
let outgoing = OutgoingPacketBuilder::new(&payload)
.with_default_header(
PacketType::Packet,
DeliveryGuarantee::Reliable,
OrderingGuarantee::Sequenced(None),
)
.build();
let expected: Vec<u8> = [vec![0, 1, 1], test_payload()].concat().to_vec();
assert_eq!(
outgoing.contents()[2..outgoing.contents().len()].to_vec(),
expected
);
}
}