Skip to content

Commit

Permalink
No more unwraps
Browse files Browse the repository at this point in the history
  • Loading branch information
aecsocket committed Aug 25, 2024
1 parent 9f364b3 commit f2a3ab8
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 27 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ all = { level = "warn", priority = -1 }
nursery = { level = "warn", priority = -1 }
pedantic = { level = "warn", priority = -1 }

unwrap_used = "warn"

cast_possible_truncation = "allow"
cast_precision_loss = "allow"
cast_sign_loss = "allow"
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ This crate aims to be:
- Correct and non-panicking
- Error handling is explicit - it's clear what operations can fail, how they may fail, and how you
should handle it
- If any aeronet code panics during normal operation, it's a bug - open an issue!
- No `unwrap`s - all panicking paths are a bug unless explicitly stated otherwise. If you
encounter one, open an issue!
- Transport implementations are designed to be resilient against denial-of-service attacks, be it
from memory exhaustion, malicious peers, etc., and the problems + mitigations are documented
- Integrated with Bevy
Expand Down
39 changes: 19 additions & 20 deletions crates/aeronet_webtransport/src/internal/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ pub async fn handle_connection<E: maybe::Send + 'static>(
let conn = conn.clone();
let mut send_err = send_err.clone();
async move {
let err = send_loop(conn, recv_sending_closed, recv_s)
.await
.unwrap_err();
let Err(err) = send_loop(conn, recv_sending_closed, recv_s).await else {
unreachable!();
};
let _ = send_err.try_send(err);
}
});
Expand All @@ -65,9 +65,9 @@ pub async fn handle_connection<E: maybe::Send + 'static>(
let conn = conn.clone();
let mut send_err = send_err.clone();
async move {
let err = recv_loop(conn, recv_receiving_closed, send_r)
.await
.unwrap_err();
let Err(err) = recv_loop(conn, recv_receiving_closed, send_r).await else {
unreachable!();
};
let _ = send_err.try_send(err);
}
});
Expand All @@ -78,9 +78,9 @@ pub async fn handle_connection<E: maybe::Send + 'static>(
let conn = conn.clone();
let mut send_err = send_err.clone();
async move {
let err = meta_loop(runtime, conn, recv_meta_closed, send_meta)
.await
.unwrap_err();
let Err(err) = meta_loop(runtime, conn, recv_meta_closed, send_meta).await else {
unreachable!();
};
let _ = send_err.try_send(err);
}
});
Expand Down Expand Up @@ -150,11 +150,11 @@ async fn send_loop<E>(
conn: Arc<Connection>,
mut recv_closed: oneshot::Receiver<()>,
mut recv_s: mpsc::UnboundedReceiver<Bytes>,
) -> Result<(), InternalError<E>> {
) -> Result<Never, InternalError<E>> {
loop {
let packet = futures::select! {
x = recv_s.next() => x,
_ = recv_closed => return Ok(()),
_ = recv_closed => return Err(InternalError::FrontendClosed),
}
.ok_or(InternalError::FrontendClosed)?;

Expand Down Expand Up @@ -204,12 +204,12 @@ async fn recv_loop<E>(
conn: Arc<Connection>,
mut recv_closed: oneshot::Receiver<()>,
mut send_r: mpsc::Sender<Bytes>,
) -> Result<(), InternalError<E>> {
) -> Result<Never, InternalError<E>> {
loop {
#[allow(clippy::useless_conversion)] // multi-target support
let packet = futures::select! {
x = conn.receive_datagram().fuse() => x,
_ = recv_closed => return Ok(()),
_ = recv_closed => return Err(InternalError::FrontendClosed),
}
.map_err(|err| InternalError::ConnectionLost(err.into()))?;

Expand All @@ -236,14 +236,13 @@ async fn meta_loop<E>(
conn: Arc<Connection>,
mut recv_closed: oneshot::Receiver<()>,
mut send_meta: mpsc::Sender<ConnectionMeta>,
) -> Result<(), InternalError<E>> {
) -> Result<Never, InternalError<E>> {
loop {
{
futures::select! {
() = runtime.sleep(STATS_UPDATE_INTERVAL).fuse() => {},
_ = recv_closed => return Ok(()),
};
}
futures::select! {
() = runtime.sleep(STATS_UPDATE_INTERVAL).fuse() => {},
_ = recv_closed => return Err(InternalError::FrontendClosed),
};

let meta = ConnectionMeta {
#[cfg(not(target_family = "wasm"))]
rtt: conn.0.rtt(),
Expand Down
2 changes: 1 addition & 1 deletion examples/move_box_client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ fn net_config(cert_hash: String) -> ClientConfig {
config
.keep_alive_interval(Some(Duration::from_secs(1)))
.max_idle_timeout(Some(Duration::from_secs(5)))
.unwrap()
.expect("should be a valid max idle timeout")
.build()
}

Expand Down
14 changes: 9 additions & 5 deletions examples/move_box_server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,11 @@ fn open_server(
args: Res<Args>,
channels: Res<RepliconChannels>,
) {
let identity = wtransport::Identity::self_signed(["localhost", "127.0.0.1", "::1"]).unwrap();
let identity = wtransport::Identity::self_signed(["localhost", "127.0.0.1", "::1"])
.expect("should be able to generate self-signed certs");
let cert = &identity.certificate_chain().as_slice()[0];
let spki_fingerprint = cert::spki_fingerprint_b64(cert).unwrap();
let spki_fingerprint =
cert::spki_fingerprint_b64(cert).expect("should be able to get SPKI fingerprint of cert");
let cert_hash = cert::hash_to_b64(cert.hash());
info!("************************");
info!("SPKI FINGERPRINT");
Expand All @@ -98,14 +100,14 @@ fn open_server(
.with_identity(&identity)
.keep_alive_interval(Some(Duration::from_secs(1)))
.max_idle_timeout(Some(Duration::from_secs(5)))
.unwrap()
.expect("should be a valid max idle timeout")
.build();

let session_config = move_box::session_config(channels.as_ref());

server
.open(runtime.as_ref(), net_config, session_config)
.unwrap();
.expect("server should be closed");
}

fn on_opened(
Expand Down Expand Up @@ -149,7 +151,9 @@ fn on_server_event(
for event in events.read() {
match event {
ServerEvent::ClientConnected { client_id } => {
let client_key = client_keys.get_by_right(client_id).unwrap();
let Some(client_key) = client_keys.get_by_right(client_id) else {
continue;
};
info!("{client_id:?} controlled by {client_key:?} connected");
let color = Color::srgb(rand::random(), rand::random(), rand::random());
commands.spawn((
Expand Down

0 comments on commit f2a3ab8

Please sign in to comment.