tcplane/server/struct.rs
1use crate::*;
2
3/// Represents the internal, mutable state of the TCP server.
4///
5/// This struct consolidates all the core components required for the server to operate,
6/// including configuration and handler list. It is not intended to be used directly by end-users,
7/// but rather wrapped within the `Server` struct for thread-safe access.
8#[derive(Clone)]
9pub(crate) struct ServerData {
10 /// Stores the server's configuration settings, such as address, port, and buffer size.
11 pub(crate) server_config: ServerConfigData,
12 /// A collection of request hooks that are invoked for each incoming connection.
13 pub(crate) hook: ServerHookList,
14 /// A collection of task panic handlers that are invoked when a panic occurs during connection processing.
15 pub(crate) task_panic: ServerHookList,
16 /// The error handlers for server operations.
17 pub(crate) read_error: ServerHookList,
18}
19
20/// The primary server structure that provides a thread-safe interface to the server's state.
21///
22/// This struct acts as a public-facing wrapper around an `Arc<RwLock<ServerData>>`.
23/// It allows multiple parts of the application to safely share and modify the server's
24/// configuration and state across different threads and asynchronous tasks.
25#[derive(Clone)]
26pub struct Server(pub(super) ArcRwLock<ServerData>);
27
28/// Represents the hooks for managing the server's lifecycle, specifically for waiting and shutting down.
29#[derive(Clone)]
30pub struct ServerControlHook {
31 /// A hook that returns a future, which completes when the server's main task finishes.
32 pub(crate) wait_hook:
33 Arc<dyn Fn() -> Pin<Box<dyn Future<Output = ()> + Send + 'static>> + Send + Sync>,
34 /// A hook that, when called, initiates a graceful shutdown of the server.
35 pub(crate) shutdown_hook:
36 Arc<dyn Fn() -> Pin<Box<dyn Future<Output = ()> + Send + 'static>> + Send + Sync>,
37}