use std::io;
pub(in crate::engine) struct UnsolicitedBudget {
frames: u64,
bytes: u64,
}
impl UnsolicitedBudget {
pub(in crate::engine) fn new() -> Self {
Self {
frames: crate::engine::profile::PEER_IGNORED_FRAMES,
bytes: crate::engine::profile::PEER_IGNORED_BYTES,
}
}
pub(in crate::engine) fn remaining(&self) -> (u64, u64) {
(self.frames, self.bytes)
}
pub(in crate::engine) fn charge(&mut self, wire_bytes: usize) -> io::Result<()> {
let remaining = u64::try_from(wire_bytes)
.ok()
.and_then(|bytes| self.bytes.checked_sub(bytes))
.zip(self.frames.checked_sub(1));
let Some((bytes, frames)) = remaining else {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"unsolicited peer traffic limit exceeded",
));
};
self.bytes = bytes;
self.frames = frames;
Ok(())
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, serde::Serialize)]
pub(in crate::engine) struct ConnectionId(pub(in crate::engine) u64);