use lightyear_core::time::Instant;
use lightyear_utils::sequence_buffer::SequenceBuffer;
use lightyear_utils::wrapping_id;
wrapping_id!(PingId);
const PING_BUFFER_SIZE: usize = 128;
#[derive(Debug)]
pub struct PingStore {
latest_ping_id: PingId,
buffer: SequenceBuffer<PingId, Instant, PING_BUFFER_SIZE>,
}
impl Default for PingStore {
fn default() -> Self {
Self::new()
}
}
impl PingStore {
pub fn new() -> Self {
PingStore {
latest_ping_id: PingId(0),
buffer: SequenceBuffer::new(),
}
}
pub fn push_new(&mut self, now: Instant) -> PingId {
let ping_id = self.latest_ping_id;
self.latest_ping_id += 1;
self.buffer.push(&ping_id, now);
ping_id
}
pub fn remove(&mut self, ping_id: PingId) -> Option<Instant> {
self.buffer.remove(&ping_id)
}
pub fn reset(&mut self) {
self.latest_ping_id = PingId(0);
self.buffer.clear();
}
}