stuff sort-of works now yay
This commit is contained in:
41
src/main.rs
41
src/main.rs
@@ -1,4 +1,5 @@
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::marker;
|
||||
use std::time::Instant;
|
||||
use std::{io::Cursor, net::SocketAddr, str::FromStr, sync::Arc, time::Duration};
|
||||
|
||||
@@ -156,20 +157,18 @@ impl AppState {
|
||||
let Some(client) = clients.get(&peer_addr) else {
|
||||
return Err("Client not found".into());
|
||||
};
|
||||
dbg!(&msg);
|
||||
client.tx.send(msg).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn broadcast(&self, msg: Message) -> Result<()> {
|
||||
let clients = self.clients.read().await;
|
||||
let addresses = clients.keys().cloned();
|
||||
for peer_addr in addresses {
|
||||
self.send(peer_addr, msg.clone()).await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
// pub async fn broadcast(&self, msg: Message) -> Result<()> {
|
||||
// let clients = self.clients.read().await;
|
||||
// let addresses = clients.keys().cloned();
|
||||
// for peer_addr in addresses {
|
||||
// self.send(peer_addr, msg.clone()).await?;
|
||||
// }
|
||||
// Ok(())
|
||||
// }
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
@@ -244,13 +243,10 @@ async fn main() -> Result<()> {
|
||||
tokio::spawn(async move {
|
||||
loop {
|
||||
let clients = state.clients.read().await;
|
||||
let mut mark_for_delete = vec![];
|
||||
for (peer_addr, client) in clients.iter() {
|
||||
if Instant::now().duration_since(client.last_pong).as_secs() > 120 {
|
||||
println!(
|
||||
"It's been 2 minutes, where the fuck did you go {}?",
|
||||
peer_addr
|
||||
);
|
||||
// disconnected
|
||||
if Instant::now().duration_since(client.last_pong).as_secs() >= 110 {
|
||||
mark_for_delete.push(*peer_addr);
|
||||
}
|
||||
println!("PING :{}", client.username.clone().unwrap_or_default());
|
||||
let _ = state
|
||||
@@ -263,9 +259,17 @@ async fn main() -> Result<()> {
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
drop(clients);
|
||||
|
||||
// Drop stale clients, if there are any
|
||||
if !mark_for_delete.is_empty() {
|
||||
state
|
||||
.clients
|
||||
.write()
|
||||
.await
|
||||
.retain(|p, _| !mark_for_delete.contains(p));
|
||||
}
|
||||
|
||||
tokio::time::sleep(Duration::from_secs(30)).await;
|
||||
}
|
||||
|
||||
@@ -376,7 +380,10 @@ async fn connect(
|
||||
break 'outer;
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
let _ = c2s_tx.send("QUIT :Broken Pipe".into()).await;
|
||||
break;
|
||||
}
|
||||
println!("{:?} closed", peer_addr);
|
||||
});
|
||||
|
Reference in New Issue
Block a user