Properly handle client disconnect
-use ::anyhow::Result;
+use ::anyhow::{Result, anyhow};
use ::async_std::net::{TcpListener, TcpStream};
use ::async_std::task;
use ::log::{info, warn};
@@ -61,10 +61,16 @@ async fn handle_incoming(
loop {
// Receive the secret and check, whether the client is allowed to connect
let payload_bytes = receive_bytes(&mut socket).await?;
+
+ // Didn't receive any bytes. The client disconnected
+ if payload_bytes.len() == 0 {
+ return Ok(());
+ }
+
let secret = String::from_utf8(payload_bytes)?;
if secret != settings.daemon.secret {
warn!("Received invalid secret: {}", secret);
- return Ok(());
+ return Err(anyhow!("Received invalid secret"));
}
// Receive the actual instruction from the client
