Skip to content

Commit 573810c

Browse files
committed
feat: v8.0.0
1 parent 374a14a commit 573810c

File tree

4 files changed

+46
-36
lines changed

4 files changed

+46
-36
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "tcplane"
3-
version = "6.0.1"
3+
version = "8.0.0"
44
readme = "README.md"
55
edition = "2024"
66
authors = ["root@ltpp.vip"]

src/config/impl.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ impl ServerConfig {
3636
/// # Returns
3737
///
3838
/// - `Self` - A new ServerConfig instance.
39+
#[inline(always)]
3940
pub fn new() -> Self {
4041
Self::default()
4142
}

src/server/impl.rs

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,41 @@ use crate::*;
44
impl Default for ServerData {
55
fn default() -> Self {
66
Self {
7-
config: ServerConfig::new(),
7+
server_config: ServerConfigData::default(),
88
hook: vec![],
99
task_panic: vec![],
1010
read_error: vec![],
1111
}
1212
}
1313
}
1414

15+
/// Provides a default implementation for ServerControlHook.
16+
impl Default for ServerControlHook {
17+
fn default() -> Self {
18+
Self {
19+
wait_hook: Arc::new(|| Box::pin(async {})),
20+
shutdown_hook: Arc::new(|| Box::pin(async {})),
21+
}
22+
}
23+
}
24+
1525
impl ServerData {
1626
/// Gets a reference to the configuration.
1727
///
1828
/// # Returns
1929
///
2030
/// - `&ServerConfig` - Reference to the configuration.
21-
pub(crate) fn get_config(&self) -> &ServerConfig {
22-
&self.config
31+
pub(crate) fn get_config(&self) -> &ServerConfigData {
32+
&self.server_config
33+
}
34+
35+
/// Gets a mutable reference to the server configuration.
36+
///
37+
/// # Returns
38+
///
39+
/// - `&mut ServerConfigData` - Mutable reference to the server configuration.
40+
pub(crate) fn get_mut_server_config(&mut self) -> &mut ServerConfigData {
41+
&mut self.server_config
2342
}
2443

2544
/// Gets a reference to the hook list.
@@ -113,6 +132,20 @@ impl Server {
113132
self.0.write().await
114133
}
115134

135+
/// Sets the server configuration.
136+
///
137+
/// # Arguments
138+
///
139+
/// - `ServerConfig` - The server configuration.
140+
///
141+
/// # Returns
142+
///
143+
/// - `&Self` - Reference to self for method chaining.
144+
pub async fn server_config(&self, config: ServerConfig) -> &Self {
145+
*self.write().await.get_mut_server_config() = config.get_data().await;
146+
self
147+
}
148+
116149
/// Constructs a bind address string from host and port。
117150
///
118151
/// # Arguments
@@ -131,20 +164,6 @@ impl Server {
131164
format!("{}{}{}", host.as_ref(), COLON, port)
132165
}
133166

134-
/// Adds a hook to the server's hook list.
135-
///
136-
/// # Arguments
137-
///
138-
/// - `ServerHookHandler` - The hook to add.
139-
///
140-
/// # Returns
141-
///
142-
/// - `&Self` - Reference to self for method chaining.
143-
pub async fn handle(&self, hook: ServerHookHandler) -> &Self {
144-
self.write().await.get_mut_hook().push(hook);
145-
self
146-
}
147-
148167
/// Adds a typed hook to the server's hook list.
149168
///
150169
/// # Arguments
@@ -158,7 +177,11 @@ impl Server {
158177
where
159178
H: ServerHook,
160179
{
161-
self.handle(server_hook_factory::<H>()).await
180+
self.write()
181+
.await
182+
.get_mut_hook()
183+
.push(server_hook_factory::<H>());
184+
self
162185
}
163186

164187
/// Adds a panic handler to the server's task panic handler list.
@@ -207,7 +230,7 @@ impl Server {
207230
///
208231
/// - `Result<TcpListener, ServerError>` - The listener on success, or an error on failure.
209232
async fn create_tcp_listener(&self) -> Result<TcpListener, ServerError> {
210-
let config: ServerConfigData = self.read().await.get_config().get_data().await;
233+
let config: ServerConfigData = self.read().await.get_config().clone();
211234
let host: String = config.host;
212235
let port: u16 = config.port;
213236
let addr: String = Self::get_bind_addr(&host, port);
@@ -225,7 +248,7 @@ impl Server {
225248
let server: Server = self.clone();
226249
let hook: ServerHookList = self.read().await.get_hook().clone();
227250
let task_panic: ServerHookList = self.read().await.get_task_panic().clone();
228-
let buffer_size: usize = self.read().await.get_config().get_data().await.buffer_size;
251+
let buffer_size: usize = self.read().await.get_config().buffer_size;
229252
spawn(async move {
230253
server
231254
.handle_connection(stream, hook, task_panic, buffer_size)
@@ -358,7 +381,6 @@ impl Server {
358381
let server: Server = self.clone();
359382
let (wait_sender, wait_receiver) = channel(());
360383
let (shutdown_sender, mut shutdown_receiver) = channel(());
361-
362384
let accept_connections: JoinHandle<()> = spawn(async move {
363385
loop {
364386
tokio::select! {
@@ -378,25 +400,21 @@ impl Server {
378400
}
379401
let _ = wait_sender.send(());
380402
});
381-
382403
let wait_hook = Arc::new(move || {
383404
let mut wait_receiver_clone = wait_receiver.clone();
384405
Box::pin(async move {
385406
let _ = wait_receiver_clone.changed().await;
386407
}) as Pin<Box<dyn Future<Output = ()> + Send + 'static>>
387408
});
388-
389409
let shutdown_hook = Arc::new(move || {
390410
let shutdown_sender_clone: Sender<()> = shutdown_sender.clone();
391411
Box::pin(async move {
392412
let _ = shutdown_sender_clone.send(());
393413
}) as Pin<Box<dyn Future<Output = ()> + Send + 'static>>
394414
});
395-
396415
spawn(async move {
397416
let _ = accept_connections.await;
398417
});
399-
400418
Ok(ServerControlHook {
401419
wait_hook,
402420
shutdown_hook,

src/server/struct.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::*;
88
#[derive(Clone)]
99
pub(crate) struct ServerData {
1010
/// Stores the server's configuration settings, such as address, port, and buffer size.
11-
pub(crate) config: ServerConfig,
11+
pub(crate) server_config: ServerConfigData,
1212
/// A collection of request hooks that are invoked for each incoming connection.
1313
pub(crate) hook: ServerHookList,
1414
/// A collection of task panic handlers that are invoked when a panic occurs during connection processing.
@@ -35,12 +35,3 @@ pub struct ServerControlHook {
3535
pub(crate) shutdown_hook:
3636
Arc<dyn Fn() -> Pin<Box<dyn Future<Output = ()> + Send + 'static>> + Send + Sync>,
3737
}
38-
39-
impl Default for ServerControlHook {
40-
fn default() -> Self {
41-
Self {
42-
wait_hook: Arc::new(|| Box::pin(async {})),
43-
shutdown_hook: Arc::new(|| Box::pin(async {})),
44-
}
45-
}
46-
}

0 commit comments

Comments
 (0)