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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
use std::{collections::VecDeque, mem, net::SocketAddr, time::Instant};

use bincode::{encode_into_slice, error::EncodeError};

use crate::{
    header::{Peers, Reliability, HEADER_SIZE},
    tasks::communicator::BINCODE_CONF,
    OutPackage, MAX_DATAGRAM_SIZE,
};

/// It cumulatively builds output packages from individual messages.
pub struct PackageBuilder {
    reliability: Reliability,
    peers: Peers,
    target: SocketAddr,
    buffer: Buffer,
    latest: Option<Instant>,
    packages: VecDeque<OutPackage>,
}

impl PackageBuilder {
    pub fn new(reliability: Reliability, peers: Peers, target: SocketAddr) -> Self {
        Self {
            reliability,
            peers,
            target,
            latest: None,
            buffer: Buffer::new(),
            packages: VecDeque::new(),
        }
    }

    /// Time of newest message in the buffer.
    pub fn latest(&self) -> Option<Instant> {
        self.latest
    }

    /// Build packages from all messages pushed before a given threshold. The
    /// last yielded package may contain newer data.
    ///
    /// See [`Self::build_all`].
    pub fn build_old(&mut self, threshold: Instant) -> PackageIterator<'_> {
        if self.buffer.birth().map_or(false, |t| t <= threshold) {
            self.build_package(false);
        }

        // Threshold is used only in the condition to build package from
        // currently buffered messages. It makes little sense to make yielding
        // of already build packages conditional because the packages cannot
        // change in the future.
        PackageIterator::new(&mut self.latest, &mut self.packages)
    }

    /// Build packages from all pushed messages.
    ///
    /// The messages are distributed among the packages in a sequential order.
    /// Each package except the last one is filled with as many messages as it
    /// can accommodate.
    pub fn build_all(&mut self) -> PackageIterator<'_> {
        self.build_package(true);
        PackageIterator::new(&mut self.latest, &mut self.packages)
    }

    /// Push another message to the builder so that it is included in one of
    /// the resulting packages.
    ///
    /// It is assumed that messages are pushed in the order of their time.
    ///
    /// # Arguments
    ///
    /// * `message` - message to be pushed to the buffer.
    ///
    /// * `time` - time of creation of the message.
    pub fn push<E>(&mut self, message: &E, time: Instant) -> Result<(), EncodeError>
    where
        E: bincode::Encode,
    {
        self.latest = Some(time);
        match self.push_inner(message, time) {
            Err(EncodeError::UnexpectedEnd) => {
                self.build_package(false);
                self.push_inner(message, time)
            }
            Err(err) => Err(err),
            Ok(()) => Ok(()),
        }
    }

    fn push_inner<E>(&mut self, message: &E, time: Instant) -> Result<(), EncodeError>
    where
        E: bincode::Encode,
    {
        let len = encode_into_slice(message, self.buffer.unused_mut(), BINCODE_CONF)?;
        self.buffer.forward(len, time);
        Ok(())
    }

    /// Build and store another package from already buffered data (if there is
    /// any).
    ///
    /// # Arguments
    ///
    /// * `empty` - if true, newly created buffer for further messages will
    ///   be empty.
    fn build_package(&mut self, empty: bool) {
        let Some(data) = self.buffer.consume(empty) else {
            return;
        };

        self.packages.push_back(OutPackage::new(
            data,
            self.reliability,
            self.peers,
            self.target,
        ));
    }
}

struct Buffer {
    /// Time of the first piece of data.
    birth: Option<Instant>,
    data: Vec<u8>,
    used: usize,
}

impl Buffer {
    fn new() -> Self {
        Self {
            birth: None,
            data: vec![0; MAX_DATAGRAM_SIZE],
            used: HEADER_SIZE,
        }
    }

    /// Returns true if no data was pushed to the buffer.
    fn empty(&self) -> bool {
        self.used <= HEADER_SIZE
    }

    fn birth(&self) -> Option<Instant> {
        self.birth
    }

    /// Resets the buffer and returns the old data (before the reset). If there
    /// was no data pushed, it returns None.
    ///
    /// # Arguments
    ///
    /// * `empty` - if true, the new buffer may be created with zero capacity
    ///   as an optimization.
    fn consume(&mut self, empty: bool) -> Option<Vec<u8>> {
        if self.empty() {
            return None;
        }

        let (mut data, used) = if empty {
            (Vec::new(), 0)
        } else {
            (vec![0; MAX_DATAGRAM_SIZE], HEADER_SIZE)
        };

        mem::swap(&mut data, &mut self.data);
        data.truncate(self.used);
        self.used = used;
        self.birth = None;

        Some(data)
    }

    /// Returns mutable slice to the unused part of the buffer.
    fn unused_mut(&mut self) -> &mut [u8] {
        &mut self.data[self.used..]
    }

    /// Moves used data pointer forward and sets birth time to now if it is not
    /// set already.
    ///
    /// # Panics
    ///
    /// May panic if the pointer is moved beyond the buffer capacity.
    fn forward(&mut self, amount: usize, time: Instant) {
        if self.birth.is_none() {
            self.birth = Some(time);
        }

        self.used += amount;
        debug_assert!(self.used <= self.data.len());
    }
}

/// Iterator over already build packages.
///
/// Not consumed packages stay in the buffer.
pub struct PackageIterator<'a> {
    latest: &'a mut Option<Instant>,
    packages: &'a mut VecDeque<OutPackage>,
}

impl<'a> PackageIterator<'a> {
    /// # Arguments
    ///
    /// * `latest` - mutable reference to a timestamp which will be reset by
    ///   this iterator (set to None) after it is fully consumed.
    ///
    /// * `packages` - packages to be pop_font by this iterator as it is
    ///   consumed.
    fn new(latest: &'a mut Option<Instant>, packages: &'a mut VecDeque<OutPackage>) -> Self {
        Self { latest, packages }
    }
}

impl<'a> Iterator for PackageIterator<'a> {
    type Item = OutPackage;

    fn next(&mut self) -> Option<Self::Item> {
        let result = self.packages.pop_front();
        if result.is_none() {
            *self.latest = None;
        }
        result
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_out_message_builder() {
        #[derive(bincode::Encode)]
        struct TestData {
            values: [u64; 16], // up to 128 bytes
        }

        let mut builder = PackageBuilder::new(
            Reliability::Unordered,
            Peers::Players,
            "127.0.0.1:1111".parse::<SocketAddr>().unwrap(),
        );

        for i in 0..10 {
            builder
                .push(
                    &TestData {
                        // Use large u64 so that the value cannot be shrunk.
                        values: [u64::MAX - (i as u64); 16],
                    },
                    Instant::now(),
                )
                .unwrap();
        }

        let packages: Vec<OutPackage> = builder.build_all().collect();
        assert_eq!(packages.len(), 4);
        // 3 items + something extra for the encoding
        assert!(packages[0].data_slice().len() >= 128 * 3);
        // less then 4 items
        assert!(packages[0].data_slice().len() < 128 * 4);

        assert!(packages[1].data_slice().len() >= 128 * 3);
        assert!(packages[1].data_slice().len() < 128 * 4);
        assert!(packages[2].data_slice().len() >= 128 * 3);
        assert!(packages[2].data_slice().len() < 128 * 4);
        // last one contains only one leftover item
        assert!(packages[3].data_slice().len() >= 128);
        assert!(packages[3].data_slice().len() < 128 * 2);
    }
}