persistence + proper disconnect

This commit is contained in:
2025-11-28 17:29:33 -05:00
parent 029d62cb53
commit 7557507f27
16 changed files with 413 additions and 67 deletions

View File

@@ -12,12 +12,9 @@ use std::{
};
use tracing::Instrument;
pub const DEFAULT_PORT: u16 = 16839;
pub const SERVER_HOST: Ipv6Addr = Ipv6Addr::UNSPECIFIED;
pub const SERVER_SOCKET: SocketAddr =
SocketAddr::V6(SocketAddrV6::new(SERVER_HOST, DEFAULT_PORT, 0, 0));
pub fn init_endpoint(data_path: &Path) -> Endpoint {
pub fn init_endpoint(port: u16, data_path: &Path) -> Endpoint {
let cert_path = data_path.join("cert.der");
let key_path = data_path.join("key.der");
let (cert, key) = match fs::read(&cert_path).and_then(|x| Ok((x, fs::read(&key_path)?))) {
@@ -51,7 +48,8 @@ pub fn init_endpoint(data_path: &Path) -> Endpoint {
// let transport_config = Arc::get_mut(&mut server_config.transport).unwrap();
// transport_config.max_concurrent_uni_streams(0_u8.into());
quinn::Endpoint::server(server_config, SERVER_SOCKET).unwrap()
let server_socket: SocketAddr = SocketAddr::V6(SocketAddrV6::new(SERVER_HOST, port, 0, 0));
quinn::Endpoint::server(server_config, server_socket).unwrap()
}
#[derive(Clone)]
@@ -60,6 +58,9 @@ pub struct ClientSender {
}
impl ClientSender {
pub fn remote(&self) -> SocketAddr {
self.conn.remote_address()
}
pub async fn send(&self, msg: ServerMsg) -> SendResult {
send_uni(&self.conn, msg).await
}
@@ -72,9 +73,9 @@ pub trait ConAccepter: Send + Sync + 'static {
) -> impl Future<Output = impl RecvHandler<ClientMsg>> + Send;
}
pub async fn listen(data_path: &Path, accepter: impl ConAccepter) {
pub async fn listen(port: u16, data_path: &Path, accepter: impl ConAccepter) {
let accepter = Arc::new(accepter);
let endpoint = init_endpoint(data_path);
let endpoint = init_endpoint(port, data_path);
println!("listening on {}", endpoint.local_addr().unwrap());
while let Some(conn) = endpoint.accept().await {
@@ -93,6 +94,7 @@ async fn handle_connection(
) -> std::io::Result<()> {
let conn = conn.await?;
let handler = Arc::new(accepter.accept(ClientSender { conn: conn.clone() }).await);
handler.connect().await;
let span = tracing::info_span!(
"connection",
remote = %conn.remote_address(),