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
use std::collections::VecDeque;
use crate::{sequence_greater_than, GameInstant};
pub type PingIndex = u16;
const SENT_PINGS_HISTORY_SIZE: u16 = 32;
pub struct PingStore {
ping_index: PingIndex,
buffer: VecDeque<(PingIndex, GameInstant)>,
}
impl PingStore {
pub fn new() -> Self {
PingStore {
ping_index: 0,
buffer: VecDeque::new(),
}
}
pub fn push_new(&mut self, now: GameInstant) -> PingIndex {
let ping_index = self.ping_index;
self.ping_index = self.ping_index.wrapping_add(1);
self.buffer.push_front((ping_index, now));
while self.buffer.len() > SENT_PINGS_HISTORY_SIZE.into() {
self.buffer.pop_back();
}
ping_index
}
pub fn remove(&mut self, ping_index: PingIndex) -> Option<GameInstant> {
let mut vec_index = self.buffer.len();
if vec_index == 0 {
return None;
}
let mut found = false;
loop {
vec_index -= 1;
if let Some((old_index, _)) = self.buffer.get(vec_index) {
if *old_index == ping_index {
found = true;
} else {
if sequence_greater_than(*old_index, ping_index) {
return None;
}
}
}
if found {
let (_, ping_instant) = self.buffer.remove(vec_index).unwrap();
return Some(ping_instant);
}
if vec_index == 0 {
return None;
}
}
}
pub fn clear(&mut self) {
self.buffer.clear();
}
}