Skip to content

Commit

Permalink
de_net: Add various asserts (#744)
Browse files Browse the repository at this point in the history
  • Loading branch information
Indy2222 authored Sep 24, 2023
1 parent 31b1cad commit d374aea
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 6 deletions.
10 changes: 7 additions & 3 deletions crates/net/src/connection/delivery/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use self::{
received::{IdContinuity, Received},
};
use super::book::{Connection, ConnectionBook};
use crate::{record::DeliveryRecord, tasks::OutDatagram, Reliability};
use crate::{record::DeliveryRecord, tasks::OutDatagram, Reliability, MAX_PACKAGE_SIZE};

mod confirms;
mod deliveries;
Expand Down Expand Up @@ -129,14 +129,18 @@ impl ConnDeliveryHandler {
///
/// # Panics
///
/// Panics if `buf` len is smaller than length of any of the drained
/// buffered pending package.
/// * If `buf` len is smaller than length of any of the drained buffered
/// pending package.
///
/// * If `data` is longer than [`MAX_PACKAGE_SIZE`].
fn push<'b>(
&mut self,
record: DeliveryRecord,
data: Vec<u8>,
buf: &'b mut [u8],
) -> Result<Deliveries<'_, 'b>, ReceivedIdError> {
assert!(data.len() <= MAX_PACKAGE_SIZE);

let result = self.received.process(record.header().id());
if let Ok(_) | Err(ReceivedIdError::Duplicate) = result {
// Push to the buffer even duplicate packages, because the reason
Expand Down
7 changes: 6 additions & 1 deletion crates/net/src/connection/delivery/pending.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::collections::BTreeMap;

use crate::{connection::databuf::DataBuf, header::PackageId, record::DeliveryRecord};
use crate::{
connection::databuf::DataBuf, header::PackageId, record::DeliveryRecord, MAX_PACKAGE_SIZE,
};

/// Buffer for packages received out-of-order and thus awaiting the right
/// moment to be delivered.
Expand All @@ -24,8 +26,11 @@ impl Pending {
/// * When there already is a pending package with the given `id`.
///
/// * It is not a (semi-)ordered package.
///
/// * If the data is longer than [`MAX_PACKAGE_SIZE`].
pub(super) fn store(&mut self, record: DeliveryRecord, data: &[u8]) {
assert!(record.header().reliability().is_ordered());
assert!(data.len() <= MAX_PACKAGE_SIZE);
let id = record.header().id();
let result = self.ids.insert(id, record);
assert!(result.is_none());
Expand Down
7 changes: 7 additions & 0 deletions crates/net/src/connection/dispatch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use super::book::{Connection, ConnectionBook};
use crate::{
header::{DatagramHeader, PackageHeader, PackageId, PackageIdRange},
tasks::OutDatagram,
MAX_PACKAGE_SIZE,
};

mod resends;
Expand All @@ -39,13 +40,19 @@ impl DispatchHandler {
handler.next_package_id()
}

/// # Panics
///
/// * If the package is already registered as sent.
///
/// * If the data are longer than [`MAX_PACKAGE_SIZE`].
pub(crate) async fn sent(
&mut self,
time: Instant,
addr: SocketAddr,
header: PackageHeader,
data: &[u8],
) {
assert!(data.len() <= MAX_PACKAGE_SIZE);
let mut book = self.book.lock().await;
let handler = book.update(time, addr, ConnDispatchHandler::new);
handler.resends.push(header, data, time);
Expand Down
11 changes: 10 additions & 1 deletion crates/net/src/connection/dispatch/resends.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use priority_queue::PriorityQueue;
use crate::{
connection::{book::MAX_CONN_AGE, databuf::DataBuf},
header::{PackageHeader, PackageId},
MAX_PACKAGE_SIZE,
};

pub(super) const START_BACKOFF_MS: u64 = 220;
Expand Down Expand Up @@ -43,8 +44,16 @@ impl Resends {
}

/// Registers new package for re-sending until it is resolved.
///
/// # Panics
///
/// * If the package (ID) is already stored.
///
/// * If data is longer than [`MAX_PACKAGE_SIZE`].
pub(super) fn push(&mut self, header: PackageHeader, data: &[u8], now: Instant) {
self.queue.push(header.id(), Timing::new(now));
assert!(data.len() <= MAX_PACKAGE_SIZE);
let result = self.queue.push(header.id(), Timing::new(now));
assert!(result.is_none());
self.headers.insert(header.id(), header);
self.data.push(header.id(), data);
}
Expand Down
4 changes: 3 additions & 1 deletion crates/net/src/tasks/dreceiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use tracing::{error, info, warn};
use crate::{
header::{DatagramHeader, PackageHeader},
protocol::{MsgRecvError, ProtocolSocket},
MAX_DATAGRAM_SIZE,
MAX_DATAGRAM_SIZE, MAX_PACKAGE_SIZE,
};

pub(super) struct InSystemDatagram {
Expand Down Expand Up @@ -54,6 +54,8 @@ pub(super) async fn run(
}
};

assert!(data.len() <= MAX_PACKAGE_SIZE);

// Closed channel(s) are handled at the top part of the loop,
// therefore errors from .send() are not treated below.
match header {
Expand Down

0 comments on commit d374aea

Please sign in to comment.