Started milestone 2

This commit is contained in:
sam
2026-05-03 15:53:25 +02:00
parent 43483c2145
commit 041955345b
18 changed files with 4616 additions and 60 deletions

View File

@@ -1,7 +1,7 @@
//! Concurrent server state management.
//!
//! This module defines the global application state shared across all active
//! Tokio tasks. It heavily relies on lock-free and fine-grained locking primitives
//! This module defines the global application state shared across all active
//! Tokio tasks. It heavily relies on lock-free and fine-grained locking primitives
//! like `DashMap` and `AtomicU32` to ensure high performance without stuttering.
use dashmap::DashMap;
@@ -33,8 +33,8 @@ impl AppState {
}
/// Generates a strictly unique, monotonically increasing session token.
///
/// We use `Ordering::Relaxed` because we only need atomicity on the counter
///
/// We use `Ordering::Relaxed` because we only need atomicity on the counter
/// itself, not strict memory ordering with other operations.
#[must_use]
pub fn generate_token(&self) -> u32 {

View File

@@ -1,7 +1,7 @@
//! Reliable TCP routing and connection management.
//!
//! This module implements the reliable control lane for client communication.
//! It handles the initial authentication handshake and routes incoming TCP events
//! It handles the initial authentication handshake and routes incoming TCP events
//! from the length-delimited codec to the broader application state.
use anyhow::{Context, Result};
@@ -9,8 +9,8 @@ use core_protocol::tcp_events::TcpEvent;
use futures::{SinkExt, StreamExt};
use std::sync::Arc;
use tokio::net::TcpStream;
use tokio_serde::formats::Bincode;
use tokio_serde::SymmetricallyFramed;
use tokio_serde::formats::Bincode;
use tokio_util::codec::{Framed, LengthDelimitedCodec};
use tracing::{error, info, instrument, warn};
@@ -26,7 +26,7 @@ type FramedStream = SymmetricallyFramed<
/// Handles the lifecycle of a newly connected client's TCP stream.
///
/// This spans an instrumented task for the connection, setting up the necessary
/// This spans an instrumented task for the connection, setting up the necessary
/// framers and codecs before entering the event loop.
#[instrument(skip(stream, state))]
pub async fn handle_connection(stream: TcpStream, state: Arc<AppState>) {
@@ -39,13 +39,11 @@ pub async fn handle_connection(stream: TcpStream, state: Arc<AppState>) {
};
info!("New connection from {}", peer_addr);
// We pad the TCP stream with a length-delimited codec to guarantee frame boundaries,
// We pad the TCP stream with a length-delimited codec to guarantee frame boundaries,
// avoiding fragmentation issues common in raw TCP sockets.
let length_delimited = Framed::new(stream, LengthDelimitedCodec::new());
let mut framed: FramedStream = SymmetricallyFramed::new(
length_delimited,
Bincode::<TcpEvent, TcpEvent>::default(),
);
let mut framed: FramedStream =
SymmetricallyFramed::new(length_delimited, Bincode::<TcpEvent, TcpEvent>::default());
if let Err(e) = process_connection(&mut framed, state).await {
warn!("Connection closed with error: {:?}", e);
@@ -65,10 +63,10 @@ async fn process_connection(framed: &mut FramedStream, state: Arc<AppState>) ->
match event {
TcpEvent::AuthRequest { username } => {
// REDACTED standard: we might log the username, but this is a reminder
// REDACTED standard: we might log the username, but this is a reminder
// for future sensitive items to use [REDACTED].
info!("AuthRequest received for user: {}", username);
let session_token = state.generate_token();
state.active_users.insert(