empty socket error propagation

This commit is contained in:
17ms 2023-05-01 16:15:12 +03:00
parent 0bc1ec8a9a
commit f8e2806406
4 changed files with 21 additions and 13 deletions

View File

@ -15,7 +15,7 @@ const PUBLIC_IPV6: &str = "https://ipv6.icanhazip.com";
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
pub enum Message { pub enum Message {
ErrorMsg(String), Error(String),
Files(Vec<PathBuf>), Files(Vec<PathBuf>),
Metadata(HashMap<String, (u64, String)>), Metadata(HashMap<String, (u64, String)>),
ClientConnect(SocketAddr), ClientConnect(SocketAddr),

View File

@ -41,7 +41,7 @@ pub async fn recv(
let n = reader.read_until(b':', &mut buf).await?; let n = reader.read_until(b':', &mut buf).await?;
if n == 0 { if n == 0 {
todo!("maybe error handling :)"); return Err("Received 0 bytes from the socket".into());
} }
buf.pop(); buf.pop();

View File

@ -83,7 +83,7 @@ impl Connector {
let msg = String::from_utf8(buf)?; let msg = String::from_utf8(buf)?;
if msg == "FIN" { if msg == "FIN" {
todo!("maybe error handling :)"); return Err("Incorrect access key".into());
} }
Ok(()) Ok(())

View File

@ -55,15 +55,23 @@ impl Listener {
let files = files.clone(); let files = files.clone();
let (mut socket, addr) = listener.accept().await?; let (mut socket, addr) = listener.accept().await?;
tx.send(Message::ClientConnect(addr)).await?; tx.send(Message::ClientConnect(addr)).await?;
let this_tx = tx.clone(); let conn_tx = tx.clone();
let this_self = Arc::clone(&self); let err_tx = tx.clone();
let conn_self = Arc::clone(&self);
tokio::spawn(async move { match tokio::spawn(async move {
this_self conn_self
.connection(&mut socket, addr, this_tx, &files) .connection(&mut socket, addr, conn_tx, &files)
.await?; .await
Ok::<(), Box<dyn Error + Send + Sync>>(()) })
}); .await
{
Ok(_) => {}
Err(e) => {
let err_msg = format!("{}: {}", addr, e);
err_tx.send(Message::Error(err_msg)).await?;
}
};
} }
} }
@ -162,7 +170,7 @@ impl Listener {
let msg = String::from_utf8(buf)?; let msg = String::from_utf8(buf)?;
if msg != "AMT" { if msg != "AMT" {
todo!("maybe error handling :)"); return Err("Broken message sequence".into());
} }
for file in metadata { for file in metadata {
@ -217,7 +225,7 @@ impl Listener {
let msg = String::from_utf8(buf)?; let msg = String::from_utf8(buf)?;
if msg == "ERROR" { if msg == "ERROR" {
todo!("maybe error handling :)"); return Err("Incomplete file request (hashes don't match)".into());
} }
} }