From 5b8f4fd087c0abd2a11477e3d535ddd23d6b69dc Mon Sep 17 00:00:00 2001 From: jake Date: Mon, 30 Jan 2023 18:50:05 -0700 Subject: [PATCH] disconnect a client if any error occurs --- src/common/mainloop/client.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/common/mainloop/client.rs b/src/common/mainloop/client.rs index bca79d2..0ace697 100644 --- a/src/common/mainloop/client.rs +++ b/src/common/mainloop/client.rs @@ -1,5 +1,6 @@ use std::collections::HashMap; use std::fmt::Debug; +use std::io::Write; use async_std::channel; use async_std::io::prelude::{ReadExt, WriteExt}; use async_std::sync::{Arc, RwLock}; @@ -148,6 +149,26 @@ where }, Err(err) => { error!("[client recv {:?}] error {:?} ", client_id, err); + + let mut f = std::fs::File::options().create(true).append(true).open("errors.txt").unwrap(); + f.write_all(format!("[{client_id:?}] {err:?}").as_bytes()).unwrap(); + + // disconnect client on an error + for pkt in state.on_disconnect(client_id).await.unwrap() { + clients + .read() + .await + .get(&pkt.0) + .unwrap() + .send(pkt.1) + .await + .unwrap(); + } + clients + .write() + .await + .remove(&client_id); + break; } } }