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