handle ctrlc
This commit is contained in:
@@ -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<Output = impl RecvHandler<ClientMsg>> + 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(
|
||||
|
||||
Reference in New Issue
Block a user