🐛 bug: fix constructing errors and forgetting to actually throw the errors

main
(on company time) 2022-12-20 14:46:08 -06:00
parent 200c07da2f
commit 5e7f691a60
1 changed files with 6 additions and 5 deletions

View File

@ -2,6 +2,7 @@ use std::{
sync::Arc, sync::Arc,
}; };
use anyhow::bail;
use tokio::{ use tokio::{
io::{ io::{
AsyncReadExt, AsyncReadExt,
@ -26,27 +27,27 @@ pub async fn rx (
let mut tag = [0u8, 0, 0, 0]; let mut tag = [0u8, 0, 0, 0];
let bytes_read = tcp_read.read (&mut tag).await?; let bytes_read = tcp_read.read (&mut tag).await?;
if bytes_read != 4 { if bytes_read != 4 {
anyhow::anyhow! ("loops::rx: Couldn't read 4 bytes for tag"); bail! ("loops::rx: Couldn't read 4 bytes for tag");
} }
if tag != [1, 0, 0, 0] { if tag != [1, 0, 0, 0] {
anyhow::anyhow! ("loops::rx: unexpected tag in framing"); bail! ("loops::rx: unexpected tag in framing");
} }
let mut length = [0u8, 0, 0, 0]; let mut length = [0u8, 0, 0, 0];
let bytes_read = tcp_read.read (&mut length).await?; let bytes_read = tcp_read.read (&mut length).await?;
if bytes_read != 4 { if bytes_read != 4 {
anyhow::anyhow! ("loops::rx: Couldn't read 4 bytes for tag"); bail! ("loops::rx: Couldn't read 4 bytes for tag");
} }
let length = usize::try_from (u32::from_le_bytes (length))?; let length = usize::try_from (u32::from_le_bytes (length))?;
if length >= 8_192 { if length >= 8_192 {
anyhow::anyhow! ("loops::rx: Length too big for UDP packets"); bail! ("loops::rx: Length too big for UDP packets");
} }
let mut buf = vec! [0u8; length]; let mut buf = vec! [0u8; length];
let bytes_read = tcp_read.read_exact (&mut buf).await?; let bytes_read = tcp_read.read_exact (&mut buf).await?;
if length != bytes_read { if length != bytes_read {
anyhow::anyhow! ("loops::rx: read_exact failed"); bail! ("loops::rx: read_exact failed");
} }
buf.truncate (bytes_read); buf.truncate (bytes_read);