Skip to content

Commit 9ee732d

Browse files
committed
feat: v4.1.1
1 parent 11583d4 commit 9ee732d

File tree

12 files changed

+272
-6
lines changed

12 files changed

+272
-6
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 = "4.1.0"
3+
version = "4.1.1"
44
edition = "2024"
55
authors = ["root@ltpp.vip"]
66
license = "MIT"

src/common/type.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
use crate::*;
22

3+
/// A type alias for `Arc<RwLock<T>>`.
4+
///
5+
/// This type alias simplifies the use of `Arc<RwLock<T>>`, providing thread-safe shared ownership with read-write access.
36
pub type ArcRwLock<T> = Arc<RwLock<T>>;

src/config/const.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
1+
/// Default inner print setting.
12
pub const DEFAULT_INNER_PRINT: bool = true;
3+
4+
/// Default inner log setting.
25
pub const DEFAULT_INNER_LOG: bool = true;
6+
7+
/// Colon space separator string.
38
pub const COLON_SPACE: &str = ": ";
9+
10+
/// Colon space symbol string.
411
pub const COLON_SPACE_SYMBOL: &str = ":";
12+
13+
/// Default host address.
514
pub const DEFAULT_HOST: &str = "0.0.0.0";
15+
16+
/// Default listen port number.
617
pub const DEFAULT_LISTEN_PORT: usize = 60000;
18+
19+
/// Request split marker string.
720
pub const SPLIT_REQUEST: &str = "\r\n\r\n";
21+
22+
/// Request split marker bytes.
823
pub const SPLIT_REQUEST_BYTES: &[u8] = SPLIT_REQUEST.as_bytes();
24+
25+
/// Default buffer size for requests.
926
pub const DEFAULT_BUFFER_SIZE: usize = 512_000;

src/config/impl.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use crate::*;
22

3+
/// Implements default values for `ServerConfig`.
4+
///
5+
/// Provides a default server configuration with default host address, port, buffer size, and error handling function.
36
impl Default for ServerConfig {
47
fn default() -> Self {
58
Self {

src/config/struct.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ use crate::*;
22

33
/// Server configuration parameters.
44
///
5-
/// Contains all necessary settings for server initialization and operation.
5+
/// Contains all settings required for server initialization and operation.
66
#[derive(Clone)]
77
pub struct ServerConfig {
8-
/// Server host address.
8+
/// The server host address.
99
pub(crate) host: String,
10-
/// Server listening port.
10+
/// The server listening port.
1111
pub(crate) port: usize,
1212
/// Network buffer size in bytes.
1313
pub(crate) buffer_size: usize,
14-
/// Error handler for server operations.
14+
/// Error handling function for server operations.
1515
pub(crate) error_handle: ArcErrorHandle,
1616
}

src/config/type.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
use crate::*;
22

3+
/// A type alias for `Arc<RwLock<ServerConfig>>`.
34
pub type ArcRwLockServerConfig = ArcRwLock<ServerConfig>;

src/context/impl.rs

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
use crate::*;
22

3+
/// Manages the internal state of the context.
34
impl InnerContext {
5+
/// Creates a new `InnerContext` with default values.
6+
///
7+
/// # Returns
8+
///
9+
/// - `InnerContext` - A new instance of `InnerContext`.
410
pub fn new() -> Self {
511
InnerContext {
612
stream: None,
@@ -11,40 +17,123 @@ impl InnerContext {
1117
}
1218
}
1319

20+
/// Provides thread-safe access and manipulation of the context.
1421
impl Context {
22+
/// Creates a `Context` from an `InnerContext`.
23+
///
24+
/// # Arguments
25+
///
26+
/// - `InnerContext` - The inner context to wrap.
27+
///
28+
/// # Returns
29+
///
30+
/// - `Self` - A new `Context` instance.
1531
pub(crate) fn from_inner_context(ctx: InnerContext) -> Self {
1632
Self(Arc::new(RwLock::new(ctx)))
1733
}
1834

35+
/// Gets a read lock for the inner context.
36+
///
37+
/// # Arguments
38+
///
39+
/// - `&self` - A reference to the `Context`.
40+
///
41+
/// # Returns
42+
///
43+
/// - `RwLockReadContext` - A read guard for the inner context.
1944
pub async fn get_read_lock(&self) -> RwLockReadContext {
2045
self.0.read().await
2146
}
2247

48+
/// Gets a write lock for the inner context.
49+
///
50+
/// # Arguments
51+
///
52+
/// - `&self` - A reference to the `Context`.
53+
///
54+
/// # Returns
55+
///
56+
/// - `RwLockWriteContext` - A write guard for the inner context.
2357
pub async fn get_write_lock(&self) -> RwLockWriteContext {
2458
self.0.write().await
2559
}
2660

61+
/// Gets a clone of the inner context.
62+
///
63+
/// # Arguments
64+
///
65+
/// - `&self` - A reference to the `Context`.
66+
///
67+
/// # Returns
68+
///
69+
/// - `InnerContext` - A cloned inner context.
2770
pub async fn get(&self) -> InnerContext {
2871
self.get_read_lock().await.clone()
2972
}
3073

74+
/// Gets the stream from the inner context.
75+
///
76+
/// # Arguments
77+
///
78+
/// - `&self` - A reference to the `Context`.
79+
///
80+
/// # Returns
81+
///
82+
/// - `OptionArcRwLockStream` - The optional stream.
3183
pub async fn get_stream(&self) -> OptionArcRwLockStream {
3284
self.get().await.stream.clone()
3385
}
3486

87+
/// Gets the request from the inner context.
88+
///
89+
/// # Arguments
90+
///
91+
/// - `&self` - A reference to the `Context`.
92+
///
93+
/// # Returns
94+
///
95+
/// - `Request` - The request object.
3596
pub async fn get_request(&self) -> Request {
3697
self.get().await.request.clone()
3798
}
3899

100+
/// Gets the response from the inner context.
101+
///
102+
/// # Arguments
103+
///
104+
/// - `&self` - A reference to the `Context`.
105+
///
106+
/// # Returns
107+
///
108+
/// - `Response` - The response object.
39109
pub async fn get_response(&self) -> Response {
40110
self.get().await.response.clone()
41111
}
42112

113+
/// Sets response data.
114+
///
115+
/// # Arguments
116+
///
117+
/// - `&self` - A reference to the `Context`.
118+
/// - `data` - The data to set, which can be converted into `ResponseData`.
119+
///
120+
/// # Returns
121+
///
122+
/// - `&Self` - A reference to the `Context`.
43123
pub(super) async fn set_response_data<T: Into<ResponseData>>(&self, data: T) -> &Self {
44124
self.get_write_lock().await.response.set_response_data(data);
45125
self
46126
}
47127

128+
/// Gets the socket address from the stream.
129+
///
130+
/// # Arguments
131+
///
132+
/// - `&self` - A reference to the `Context`.
133+
///
134+
/// # Returns
135+
///
136+
/// - `OptionSocketAddr` - The optional socket address.
48137
pub async fn get_socket_addr(&self) -> OptionSocketAddr {
49138
let stream_result: OptionArcRwLockStream = self.get_stream().await;
50139
if stream_result.is_none() {
@@ -59,6 +148,15 @@ impl Context {
59148
socket_addr_opt
60149
}
61150

151+
/// Gets the socket address or a default if none is available.
152+
///
153+
/// # Arguments
154+
///
155+
/// - `&self` - A reference to the `Context`.
156+
///
157+
/// # Returns
158+
///
159+
/// - `SocketAddr` - The socket address or the default socket address.
62160
pub async fn get_socket_addr_or_default(&self) -> SocketAddr {
63161
let stream_result: OptionArcRwLockStream = self.get_stream().await;
64162
if stream_result.is_none() {
@@ -73,26 +171,72 @@ impl Context {
73171
socket_addr
74172
}
75173

174+
/// Gets the socket address as a string.
175+
///
176+
/// # Arguments
177+
///
178+
/// - `&self` - A reference to the `Context`.
179+
///
180+
/// # Returns
181+
///
182+
/// - `Option<String>` - The socket address as a string, if available.
76183
pub async fn get_socket_addr_string(&self) -> Option<String> {
77184
self.get_socket_addr().await.map(|data| data.to_string())
78185
}
79186

187+
/// Gets the socket address as a string, or a default string if none is available.
188+
///
189+
/// # Arguments
190+
///
191+
/// - `&self` - A reference to the `Context`.
192+
///
193+
/// # Returns
194+
///
195+
/// - `String` - The socket address as a string, or the default socket address string.
80196
pub async fn get_socket_addr_or_default_string(&self) -> String {
81197
self.get_socket_addr_or_default().await.to_string()
82198
}
83199

200+
/// Gets the socket host from the socket address.
201+
///
202+
/// # Arguments
203+
///
204+
/// - `&self` - A reference to the `Context`.
205+
///
206+
/// # Returns
207+
///
208+
/// - `OptionSocketHost` - The optional socket host.
84209
pub async fn get_socket_host(&self) -> OptionSocketHost {
85210
self.get_socket_addr()
86211
.await
87212
.map(|socket_addr: SocketAddr| socket_addr.ip())
88213
}
89214

215+
/// Gets the socket port from the socket address.
216+
///
217+
/// # Arguments
218+
///
219+
/// - `&self` - A reference to the `Context`.
220+
///
221+
/// # Returns
222+
///
223+
/// - `OptionSocketPort` - The optional socket port.
90224
pub async fn get_socket_port(&self) -> OptionSocketPort {
91225
self.get_socket_addr()
92226
.await
93227
.map(|socket_addr: SocketAddr| socket_addr.port())
94228
}
95229

230+
/// Sends data through the stream.
231+
///
232+
/// # Arguments
233+
///
234+
/// - `&self` - A reference to the `Context`.
235+
/// - `data` - The data to send, which can be converted into `ResponseData`.
236+
///
237+
/// # Returns
238+
///
239+
/// - `ResponseResult` - Ok(()) on success, or an error if the stream is not found or sending fails.
96240
pub async fn send<T: Into<ResponseData>>(&self, data: T) -> ResponseResult {
97241
if let Some(stream) = self.get_stream().await {
98242
self.set_response_data(data)
@@ -106,6 +250,15 @@ impl Context {
106250
Err(ResponseError::NotFoundStream)
107251
}
108252

253+
/// Closes the stream.
254+
///
255+
/// # Arguments
256+
///
257+
/// - `&self` - A reference to the `Context`.
258+
///
259+
/// # Returns
260+
///
261+
/// - `ResponseResult` - Ok(()) on success, or an error if the stream is not found or closing fails.
109262
pub async fn close(&self) -> ResponseResult {
110263
if let Some(stream) = self.get_stream().await {
111264
self.get_response().await.close(&stream).await?;
@@ -114,6 +267,15 @@ impl Context {
114267
Err(ResponseError::NotFoundStream)
115268
}
116269

270+
/// Flushes the stream.
271+
///
272+
/// # Arguments
273+
///
274+
/// - `&self` - A reference to the `Context`.
275+
///
276+
/// # Returns
277+
///
278+
/// - `ResponseResult` - Ok(()) on success, or an error if the stream is not found or flushing fails.
117279
pub async fn flush(&self) -> ResponseResult {
118280
if let Some(stream) = self.get_stream().await {
119281
self.get_response().await.flush(&stream).await?;
@@ -122,6 +284,17 @@ impl Context {
122284
Err(ResponseError::NotFoundStream)
123285
}
124286

287+
/// Sets a data value in the context's data map.
288+
///
289+
/// # Arguments
290+
///
291+
/// - `&self` - A reference to the `Context`.
292+
/// - `key` - The key for the data.
293+
/// - `value` - The value to set, which must be cloneable and thread-safe.
294+
///
295+
/// # Returns
296+
///
297+
/// - `&Self` - A reference to the `Context`.
125298
pub async fn set_data_value<T: Any + Send + Sync + Clone>(
126299
&self,
127300
key: &str,
@@ -134,6 +307,16 @@ impl Context {
134307
self
135308
}
136309

310+
/// Gets a data value from the context's data map.
311+
///
312+
/// # Arguments
313+
///
314+
/// - `&self` - A reference to the `Context`.
315+
/// - `key` - The key for the data.
316+
///
317+
/// # Returns
318+
///
319+
/// - `Option<T>` - The data value if found and successfully downcasted, otherwise `None`.
137320
pub async fn get_data_value<T: Any + Send + Sync + Clone>(&self, key: &str) -> Option<T> {
138321
self.get_read_lock()
139322
.await
@@ -143,11 +326,30 @@ impl Context {
143326
.cloned()
144327
}
145328

329+
/// Removes a data value from the context's data map.
330+
///
331+
/// # Arguments
332+
///
333+
/// - `&self` - A reference to the `Context`.
334+
/// - `key` - The key of the data to remove.
335+
///
336+
/// # Returns
337+
///
338+
/// - `&Self` - A reference to the `Context`.
146339
pub async fn remove_data_value(&self, key: &str) -> &Self {
147340
self.get_write_lock().await.data.remove(key);
148341
self
149342
}
150343

344+
/// Clears all data from the context's data map.
345+
///
346+
/// # Arguments
347+
///
348+
/// - `&self` - A reference to the `Context`.
349+
///
350+
/// # Returns
351+
///
352+
/// - `&Self` - A reference to the `Context`.
151353
pub async fn clear_data(&self) -> &Self {
152354
self.get_write_lock().await.data.clear();
153355
self

0 commit comments

Comments
 (0)