From 4aa22de61b4cc27148402e8403c58955441013c4 Mon Sep 17 00:00:00 2001 From: shadow cat Date: Fri, 28 Nov 2025 20:24:26 -0500 Subject: [PATCH] handle ctrlc --- src/bin/client/net.rs | 10 +--------- src/bin/server/main.rs | 15 ++++++++++++--- src/bin/server/net.rs | 33 +++++++++++++++++++++++---------- 3 files changed, 36 insertions(+), 22 deletions(-) diff --git a/src/bin/client/net.rs b/src/bin/client/net.rs index e46f589..fe1686a 100644 --- a/src/bin/client/net.rs +++ b/src/bin/client/net.rs @@ -43,13 +43,6 @@ impl NetState { } } - pub fn connection(&self) -> Option<&NetHandle> { - match self { - NetState::Connected(net_handle) => Some(net_handle), - _ => None, - } - } - pub fn take(&mut self) -> Self { std::mem::replace(self, Self::None) } @@ -65,7 +58,6 @@ pub fn connect(handle: AppHandle, info: ConnectInfo) -> JoinHandle<()> { type NetResult = Result; -type MsgPayload = ClientMsg; #[derive(Clone)] pub struct NetSender { send: UnboundedSender, @@ -175,7 +167,7 @@ async fn connect_the(handle: AppHandle, info: ConnectInfo) -> NetResult<()> { } } NetCtrlMsg::Exit => { - conn.close(quinn::VarInt::from_u32(0), &[]); + conn.close(0u32.into(), &[]); endpoint.wait_idle().await; break; } diff --git a/src/bin/server/main.rs b/src/bin/server/main.rs index 61b512f..43157b9 100644 --- a/src/bin/server/main.rs +++ b/src/bin/server/main.rs @@ -17,7 +17,7 @@ use std::{ atomic::{AtomicU64, Ordering}, }, }; -use tokio::sync::RwLock; +use tokio::{signal::ctrl_c, sync::RwLock}; #[derive(Parser, Debug)] #[command(version, about, long_about = None)] @@ -42,9 +42,18 @@ pub async fn run_server(port: u16) { msgs: db.open_tree("msgs").unwrap(), senders: Default::default(), count: 0.into(), - db, + db: db.clone(), }; - listen(port, path, handler).await; + let (endpoint, handle) = listen(port, path, handler); + let _ = ctrl_c().await; + println!("stopping server"); + println!("closing connections..."); + endpoint.close(0u32.into(), &[]); + let _ = handle.await; + endpoint.wait_idle().await; + println!("saving..."); + db.flush_async().await.unwrap(); + println!("saved"); } type ClientId = u64; diff --git a/src/bin/server/net.rs b/src/bin/server/net.rs index a9b011a..258666b 100644 --- a/src/bin/server/net.rs +++ b/src/bin/server/net.rs @@ -10,6 +10,7 @@ use std::{ net::{Ipv6Addr, SocketAddr, SocketAddrV6}, sync::Arc, }; +use tokio::task::JoinHandle; use tracing::Instrument; pub const SERVER_HOST: Ipv6Addr = Ipv6Addr::UNSPECIFIED; @@ -73,19 +74,31 @@ pub trait ConAccepter: Send + Sync + 'static { ) -> impl Future> + Send; } -pub async fn listen(port: u16, data_path: &Path, accepter: impl ConAccepter) { +pub fn listen( + port: u16, + data_path: &Path, + accepter: impl ConAccepter, +) -> (Endpoint, JoinHandle<()>) { let accepter = Arc::new(accepter); let endpoint = init_endpoint(port, data_path); - println!("listening on {}", endpoint.local_addr().unwrap()); + let res = endpoint.clone(); - while let Some(conn) = endpoint.accept().await { - let fut = handle_connection(conn, accepter.clone()); - tokio::spawn(async move { - if let Err(e) = fut.await { - eprintln!("connection failed: {reason}", reason = e) - } - }); - } + let handle = tokio::spawn(async move { + println!("listening on {}", endpoint.local_addr().unwrap()); + let mut tasks = Vec::new(); + while let Some(conn) = endpoint.accept().await { + let fut = handle_connection(conn, accepter.clone()); + tasks.push(tokio::spawn(async move { + if let Err(e) = fut.await { + eprintln!("connection failed: {reason}", reason = e) + } + })); + } + for task in tasks { + let _ = task.await; + } + }); + (res, handle) } async fn handle_connection(