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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
use std::{cmp::Ordering, fmt};

use thiserror::Error;

/// Number of bytes (at the beginning of each datagram) used up by the header.
pub(crate) const HEADER_SIZE: usize = 4;

/// This bit is set in protocol control datagrams.
const CONTROL_BIT: u8 = 0b1000_0000;
/// This bit is set on datagrams which are sent to the server instead of other
/// players.
const SERVER_PEER_BIT: u8 = 0b0001_0000;

#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) enum DatagramHeader {
    Confirmation,
    Package(PackageHeader),
}

impl DatagramHeader {
    /// Writes the header to the beginning of a bytes buffer.
    ///
    /// # Panics
    ///
    /// Panics if the buffer is smaller than the header.
    pub(crate) fn write(&self, buf: &mut [u8]) {
        assert!(buf.len() >= HEADER_SIZE);
        let (mask, id) = match self {
            Self::Confirmation => (CONTROL_BIT, [0, 0, 0]),
            Self::Package(package_header) => {
                let mut mask = package_header.reliability().to_bits();
                if matches!(package_header.peers, Peers::Server) {
                    mask |= SERVER_PEER_BIT;
                }
                (mask, package_header.id.to_bytes())
            }
        };

        buf[0] = mask;
        buf[1..HEADER_SIZE].copy_from_slice(&id);
    }

    /// Reads the header from the beginning of a bytes buffer.
    pub(crate) fn read(data: &[u8]) -> Result<Self, HeaderError> {
        if data.len() < 4 {
            return Err(HeaderError::Incomplete);
        }

        debug_assert!(u32::BITS == (HEADER_SIZE as u32) * 8);

        let mask = data[0];

        if mask & CONTROL_BIT > 0 {
            if mask == CONTROL_BIT {
                Ok(Self::Confirmation)
            } else {
                Err(HeaderError::Invalid)
            }
        } else {
            let reliability = Reliability::from_bits(mask)?;
            let peers = if mask & SERVER_PEER_BIT > 0 {
                Peers::Server
            } else {
                Peers::Players
            };
            Ok(Self::Package(PackageHeader {
                reliability,
                peers,
                id: PackageId::from_bytes(&data[1..HEADER_SIZE]),
            }))
        }
    }
}

impl fmt::Display for DatagramHeader {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Confirmation => write!(f, "Confirmation"),
            Self::Package(header) => {
                write!(
                    f,
                    "Package {{ reliability: {}, peers: {}, id: {} }}",
                    header.reliability, header.peers, header.id
                )
            }
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct PackageHeader {
    /// True if the package is delivered reliably.
    reliability: Reliability,
    peers: Peers,
    id: PackageId,
}

impl PackageHeader {
    pub(crate) fn new(reliability: Reliability, peers: Peers, id: PackageId) -> Self {
        Self {
            reliability,
            peers,
            id,
        }
    }

    pub(crate) fn reliability(&self) -> Reliability {
        self.reliability
    }

    pub(crate) fn peers(&self) -> Peers {
        self.peers
    }

    pub(crate) fn id(&self) -> PackageId {
        self.id
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Reliability {
    /// There are no guarantees on reliability, ordering or duplicate delivery
    /// of the package.
    Unreliable,
    /// Non-duplicate delivery of the package is guaranteed. There are no
    /// guarantees on ordering of the package with respect to other packages.
    Unordered,
    /// Non-duplicate delivery of the package is guaranteed. The package is
    /// guaranteed to be delivered after all other previously reliably sent
    /// packages. There are no guarantees on ordering of the package with
    /// respect to other packages sent after this one.
    SemiOrdered,
}

impl Reliability {
    fn to_bits(self) -> u8 {
        let bits = match self {
            Self::Unreliable => 0,
            Self::Unordered => 1,
            Self::SemiOrdered => 2,
        };
        bits << 5
    }

    fn from_bits(bits: u8) -> Result<Self, HeaderError> {
        let bits = (bits >> 5) & 3;
        match bits {
            0 => Ok(Self::Unreliable),
            1 => Ok(Self::Unordered),
            2 => Ok(Self::SemiOrdered),
            _ => Err(HeaderError::Invalid),
        }
    }

    /// Returns true if the package is delivered reliably, independently on
    /// ordering.
    pub fn is_reliable(&self) -> bool {
        match self {
            Self::SemiOrdered | Self::Unordered => true,
            Self::Unreliable => false,
        }
    }

    /// Returns true if there are any guarantees on ordering of the package.
    pub fn is_ordered(&self) -> bool {
        match self {
            Self::SemiOrdered => true,
            Self::Unordered | Self::Unreliable => false,
        }
    }
}

impl fmt::Display for Reliability {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Unreliable => write!(f, "unreliable"),
            Self::Unordered => write!(f, "unordered"),
            Self::SemiOrdered => write!(f, "semi-ordered"),
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Peers {
    /// Communication between networking server and a player/client.
    Server,
    /// Communication between a players (one-to-all).
    Players,
}

impl fmt::Display for Peers {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Server => write!(f, "Server"),
            Self::Players => write!(f, "Players"),
        }
    }
}

#[derive(Error, Debug)]
pub(crate) enum HeaderError {
    #[error("The header is invalid")]
    Invalid,
    #[error("The data is too short and does not contain full header.")]
    Incomplete,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub(crate) struct PackageId(u32);

impl PackageId {
    const MAX: u32 = 0xffffff;

    pub(crate) const fn zero() -> Self {
        Self(0)
    }

    /// Increments the counter by one. It wraps around to zero after reaching
    /// maximum value.
    pub(crate) fn incremented(self) -> Self {
        if self.0 >= Self::MAX {
            Self(0)
        } else {
            Self(self.0 + 1)
        }
    }

    /// # Panics
    ///
    /// If not exactly 3 bytes are passed.
    pub(crate) fn from_bytes(bytes: &[u8]) -> Self {
        assert_eq!(bytes.len(), 3);
        let a = (bytes[0] as u32) << 16;
        let b = (bytes[1] as u32) << 8;
        let c = bytes[2] as u32;
        Self(a + b + c)
    }

    pub(crate) fn to_bytes(self) -> [u8; 3] {
        [
            ((self.0 >> 16) & 0xff) as u8,
            ((self.0 >> 8) & 0xff) as u8,
            (self.0 & 0xff) as u8,
        ]
    }
}

impl Ord for PackageId {
    /// Returns probable relative ordering of two package IDs.
    ///
    /// Note that the implementation is circular due to wrapping around maximum
    /// value and thus the ordering is not transitive.
    fn cmp(&self, other: &Self) -> Ordering {
        match self.0.cmp(&other.0) {
            Ordering::Greater => {
                if self.0.abs_diff(other.0) < Self::MAX / 2 {
                    Ordering::Greater
                } else {
                    Ordering::Less
                }
            }
            Ordering::Less => {
                if self.0.abs_diff(other.0) < Self::MAX / 2 {
                    Ordering::Less
                } else {
                    Ordering::Greater
                }
            }
            Ordering::Equal => Ordering::Equal,
        }
    }
}

impl PartialOrd for PackageId {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

pub(crate) struct PackageIdRange {
    current: PackageId,
    stop: Option<PackageId>,
}

impl PackageIdRange {
    pub(crate) fn counter() -> Self {
        Self {
            current: PackageId::zero(),
            stop: None,
        }
    }

    /// # Arguments
    ///
    /// * `start` - inclusive start.
    ///
    /// * `stop` - exclusive stop.
    pub(crate) fn range(start: PackageId, stop: PackageId) -> Self {
        Self {
            current: start,
            stop: Some(stop),
        }
    }
}

impl Iterator for PackageIdRange {
    type Item = PackageId;

    fn next(&mut self) -> Option<Self::Item> {
        if Some(self.current) == self.stop {
            return None;
        }

        let current = self.current;
        self.current = current.incremented();
        Some(current)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let Some(stop) = self.stop else {
            return (usize::MAX, None);
        };

        let exact = match self.current.0.cmp(&stop.0) {
            Ordering::Less => stop.0 - self.current.0,
            Ordering::Equal => 0,
            Ordering::Greater => stop.0 + (PackageId::MAX - self.current.0),
        } as usize;

        (exact, Some(exact))
    }
}

impl fmt::Display for PackageId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl TryFrom<u32> for PackageId {
    type Error = &'static str;

    fn try_from(value: u32) -> Result<Self, Self::Error> {
        if value > 0xffffff {
            Err("ID is too large")
        } else {
            Ok(Self(value))
        }
    }
}

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

    #[test]
    fn test_write_header() {
        let mut buf = [0u8; 256];

        DatagramHeader::Package(PackageHeader::new(
            Reliability::SemiOrdered,
            Peers::Server,
            PackageId::zero(),
        ))
        .write(&mut buf);
        assert_eq![&buf[0..4], &[0b0101_0000, 0, 0, 0]];
        assert_eq![&buf[4..], &[0; 252]];

        DatagramHeader::Package(PackageHeader::new(
            Reliability::Unordered,
            Peers::Server,
            256.try_into().unwrap(),
        ))
        .write(&mut buf);
        assert_eq![&buf[0..4], &[0b0011_0000, 0, 1, 0]];
        assert_eq![&buf[4..], &[0; 252]];

        DatagramHeader::Package(PackageHeader::new(
            Reliability::Unreliable,
            Peers::Players,
            1033.try_into().unwrap(),
        ))
        .write(&mut buf);
        assert_eq![&buf[0..4], &[0b0000_0000, 0, 4, 9]];
        assert_eq![&buf[4..], &[0; 252]];
    }

    #[test]
    fn test_read_header() {
        let mut buf = [88u8; 256];

        buf[0..4].copy_from_slice(&[64, 0, 0, 0]);
        assert_eq!(
            DatagramHeader::read(&buf).unwrap(),
            DatagramHeader::Package(PackageHeader::new(
                Reliability::SemiOrdered,
                Peers::Players,
                0.try_into().unwrap()
            ))
        );

        buf[0..4].copy_from_slice(&[32, 1, 0, 3]);
        assert_eq!(
            DatagramHeader::read(&buf).unwrap(),
            DatagramHeader::Package(PackageHeader::new(
                Reliability::Unordered,
                Peers::Players,
                65539.try_into().unwrap()
            ))
        );

        buf[0..4].copy_from_slice(&[16, 0, 0, 2]);
        assert_eq!(
            DatagramHeader::read(&buf).unwrap(),
            DatagramHeader::Package(PackageHeader::new(
                Reliability::Unreliable,
                Peers::Server,
                2.try_into().unwrap()
            ))
        );
    }

    #[test]
    fn test_incremented() {
        let id = PackageId::from_bytes(&[0, 1, 0]);
        assert_eq!(id.incremented().to_bytes(), [0, 1, 1]);

        let id: PackageId = 0xffffff.try_into().unwrap();
        assert_eq!(id.incremented(), 0.try_into().unwrap());
    }

    #[test]
    fn test_ordering() {
        assert_eq!(
            PackageId::from_bytes(&[0, 1, 1]).cmp(&PackageId::from_bytes(&[0, 1, 2])),
            Ordering::Less
        );
        assert_eq!(
            PackageId::from_bytes(&[0, 1, 1]).cmp(&PackageId::from_bytes(&[0, 1, 0])),
            Ordering::Greater
        );
        assert_eq!(
            PackageId::from_bytes(&[0, 1, 1]).cmp(&PackageId::from_bytes(&[0, 1, 1])),
            Ordering::Equal
        );

        assert_eq!(
            PackageId::from_bytes(&[0, 1, 2]).cmp(&PackageId::from_bytes(&[255, 255, 1])),
            Ordering::Greater
        );
        assert_eq!(
            PackageId::from_bytes(&[255, 255, 1]).cmp(&PackageId::from_bytes(&[0, 1, 2])),
            Ordering::Less
        );
    }

    #[test]
    fn test_iter() {
        let mut counter = PackageIdRange::counter();
        assert_eq!(counter.next().unwrap(), PackageId::zero());
        assert_eq!(counter.next().unwrap(), PackageId::zero().incremented());
        assert_eq!(
            counter.next().unwrap(),
            PackageId::zero().incremented().incremented()
        );
        assert_eq!(counter.next().unwrap(), PackageId::from_bytes(&[0, 0, 3]));

        let mut counter = PackageIdRange::range(
            PackageId::from_bytes(&[0, 1, 2]),
            PackageId::from_bytes(&[0, 1, 4]),
        );
        assert_eq!(counter.next().unwrap(), PackageId::from_bytes(&[0, 1, 2]));
        assert_eq!(counter.next().unwrap(), PackageId::from_bytes(&[0, 1, 3]));
        assert!(counter.next().is_none());
    }
}