Skip to content

Commit 2fca9e0

Browse files
committed
feat: v1.1.0
1 parent 1e41a9f commit 2fca9e0

File tree

26 files changed

+403
-5
lines changed

26 files changed

+403
-5
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "udp"
3-
version = "1.0.5"
3+
version = "1.1.0"
44
edition = "2024"
55
authors = ["root@ltpp.vip"]
66
license = "MIT"
@@ -18,7 +18,7 @@ exclude = [
1818
]
1919

2020
[dependencies]
21-
tokio = { version = "1.46.1", features = ["full"] }
21+
tokio = { version = "1.47.0", features = ["full"] }
2222

2323
[profile.dev]
2424
incremental = false

src/common/const.rs

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

3+
/// Default socket address (0.0.0.0:0).
34
pub const DEFAULT_SOCKET_ADDR: SocketAddr =
45
SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(0, 0, 0, 0), 0));

src/common/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+
/// Thread-safe reference-counted read-write lock wrapper.
34
pub type ArcRwLock<T> = Arc<RwLock<T>>;

src/config/impl.rs

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

3+
/// Default implementation for ServerConfig.
34
impl Default for ServerConfig {
5+
/// Creates a default server configuration.
6+
///
7+
/// # Returns
8+
///
9+
/// - `ServerConfig` - New config with default values.
410
fn default() -> Self {
511
Self {
612
host: DEFAULT_HOST.to_owned(),

src/config/struct.rs

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

3+
/// Server configuration settings.
4+
///
5+
/// Contains all necessary parameters to configure a UDP server.
36
#[derive(Clone)]
47
pub struct ServerConfig {
8+
/// Server host address.
59
pub(crate) host: String,
10+
/// Server port number.
611
pub(crate) port: usize,
12+
/// Maximum buffer size for incoming packets.
713
pub(crate) buffer_size: usize,
14+
/// Error handling callback function.
815
pub(crate) error_handle: ArcErrorHandle,
916
}

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+
/// Thread-safe read-write locked server configuration.
34
pub type ArcRwLockServerConfig = ArcRwLock<ServerConfig>;

src/context/impl.rs

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

3+
/// Implementation of InnerContext methods.
34
impl InnerContext {
5+
/// Creates a new InnerContext with default values.
6+
///
7+
/// # Returns
8+
///
9+
/// - `InnerContext` - New context instance.
410
pub fn new() -> Self {
511
InnerContext {
612
socket: None,
@@ -12,39 +18,91 @@ impl InnerContext {
1218
}
1319
}
1420

21+
/// Implementation of Context methods.
22+
///
23+
/// Provides thread-safe operations on the UDP context.
1524
impl Context {
25+
/// Creates a Context from an InnerContext.
26+
///
27+
/// # Arguments
28+
///
29+
/// - `InnerContext` - The inner context to wrap.
30+
///
31+
/// # Returns
32+
///
33+
/// - `Context` - New thread-safe context wrapper.
1634
pub(crate) fn from_inner_context(ctx: InnerContext) -> Self {
1735
Self(Arc::new(RwLock::new(ctx)))
1836
}
1937

38+
/// Acquires a read lock on the inner context.
39+
///
40+
/// # Returns
41+
///
42+
/// - `RwLockReadContext` - Read guard for the inner context.
2043
pub async fn get_read_lock(&self) -> RwLockReadContext {
2144
self.0.read().await
2245
}
2346

47+
/// Acquires a write lock on the inner context.
48+
///
49+
/// # Returns
50+
///
51+
/// - `RwLockWriteContext` - Write guard for the inner context.
2452
pub async fn get_write_lock(&self) -> RwLockWriteContext {
2553
self.0.write().await
2654
}
2755

56+
/// Gets a clone of the inner context.
57+
///
58+
/// # Returns
59+
///
60+
/// - `InnerContext` - Clone of the inner context.
2861
pub async fn get(&self) -> InnerContext {
2962
self.get_read_lock().await.clone()
3063
}
3164

65+
/// Gets the request data from the context.
66+
///
67+
/// # Returns
68+
///
69+
/// - `Request` - Clone of the request data.
3270
pub async fn get_request(&self) -> Request {
3371
self.get().await.request.clone()
3472
}
3573

74+
/// Gets the response data from the context.
75+
///
76+
/// # Returns
77+
///
78+
/// - `Response` - Clone of the response data.
3679
pub async fn get_response(&self) -> Response {
3780
self.get().await.response.clone()
3881
}
3982

83+
/// Gets the UDP socket from the context.
84+
///
85+
/// # Returns
86+
///
87+
/// - `OptionArcRwLockUdpSocket` - Clone of the socket if present.
4088
pub async fn get_socket(&self) -> OptionArcRwLockUdpSocket {
4189
self.get().await.socket.clone()
4290
}
4391

92+
/// Gets the socket address from the context.
93+
///
94+
/// # Returns
95+
///
96+
/// - `OptionSocketAddr` - Clone of the socket address if present.
4497
pub async fn get_socket_addr(&self) -> OptionSocketAddr {
4598
self.get().await.socket_addr.clone()
4699
}
47100

101+
/// Gets the socket address or default if not present.
102+
///
103+
/// # Returns
104+
///
105+
/// - `SocketAddr` - Socket address or default (0.0.0.0:0).
48106
pub async fn get_socket_addr_or_default(&self) -> SocketAddr {
49107
let socket_result: OptionArcRwLockUdpSocket = self.get_socket().await;
50108
if socket_result.is_none() {
@@ -59,31 +117,69 @@ impl Context {
59117
socket_addr
60118
}
61119

120+
/// Gets the socket address as a string.
121+
///
122+
/// # Returns
123+
///
124+
/// - `Option<String>` - Socket address string if present.
62125
pub async fn get_socket_addr_string(&self) -> Option<String> {
63126
self.get_socket_addr().await.map(|data| data.to_string())
64127
}
65128

129+
/// Gets the socket address or default as a string.
130+
///
131+
/// # Returns
132+
///
133+
/// - `String` - Socket address string or default.
66134
pub async fn get_socket_addr_or_default_string(&self) -> String {
67135
self.get_socket_addr_or_default().await.to_string()
68136
}
69137

138+
/// Gets the socket host IP address.
139+
///
140+
/// # Returns
141+
///
142+
/// - `OptionSocketHost` - Host IP address if present.
70143
pub async fn get_socket_host(&self) -> OptionSocketHost {
71144
self.get_socket_addr()
72145
.await
73146
.map(|socket_addr: SocketAddr| socket_addr.ip())
74147
}
75148

149+
/// Gets the socket port number.
150+
///
151+
/// # Returns
152+
///
153+
/// - `OptionSocketPort` - Port number if present.
76154
pub async fn get_socket_port(&self) -> OptionSocketPort {
77155
self.get_socket_addr()
78156
.await
79157
.map(|socket_addr: SocketAddr| socket_addr.port())
80158
}
81159

160+
/// Sets the response data in the context.
161+
///
162+
/// # Arguments
163+
///
164+
/// - `T` - Data convertible to ResponseData.
165+
///
166+
/// # Returns
167+
///
168+
/// - `&Self` - Reference to self for chaining.
82169
pub(super) async fn set_response<T: Into<ResponseData>>(&self, data: T) -> &Self {
83170
self.get_write_lock().await.response = Response::from(data);
84171
self
85172
}
86173

174+
/// Sends response data through the socket.
175+
///
176+
/// # Arguments
177+
///
178+
/// - `T` - Data convertible to ResponseData.
179+
///
180+
/// # Returns
181+
///
182+
/// - `ResponseResult` - Result of the send operation.
87183
pub async fn send<T: Into<ResponseData>>(&self, data: T) -> ResponseResult {
88184
let response_result: ResponseResult = self
89185
.set_response(data)
@@ -95,6 +191,16 @@ impl Context {
95191
return response_result;
96192
}
97193

194+
/// Sets a value in the context data storage.
195+
///
196+
/// # Arguments
197+
///
198+
/// - `&str` - Key for the value.
199+
/// - `&T` - Value to store (must be Any + Send + Sync + Clone).
200+
///
201+
/// # Returns
202+
///
203+
/// - `&Self` - Reference to self for chaining.
98204
pub async fn set_data_value<T: Any + Send + Sync + Clone>(
99205
&self,
100206
key: &str,
@@ -107,6 +213,15 @@ impl Context {
107213
self
108214
}
109215

216+
/// Gets a value from the context data storage.
217+
///
218+
/// # Arguments
219+
///
220+
/// - `&str` - Key for the value.
221+
///
222+
/// # Returns
223+
///
224+
/// - `Option<T>` - Retrieved value if present and of correct type.
110225
pub async fn get_data_value<T: Any + Send + Sync + Clone>(&self, key: &str) -> Option<T> {
111226
self.get_read_lock()
112227
.await
@@ -116,11 +231,25 @@ impl Context {
116231
.cloned()
117232
}
118233

234+
/// Removes a value from the context data storage.
235+
///
236+
/// # Arguments
237+
///
238+
/// - `&str` - Key for the value to remove.
239+
///
240+
/// # Returns
241+
///
242+
/// - `&Self` - Reference to self for chaining.
119243
pub async fn remove_data_value(&self, key: &str) -> &Self {
120244
self.get_write_lock().await.data.remove(key);
121245
self
122246
}
123247

248+
/// Clears all data from the context data storage.
249+
///
250+
/// # Returns
251+
///
252+
/// - `&Self` - Reference to self for chaining.
124253
pub async fn clear_data(&self) -> &Self {
125254
self.get_write_lock().await.data.clear();
126255
self

src/context/struct.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
use crate::*;
22

3+
/// Inner context containing all UDP communication components.
4+
///
5+
/// Stores the socket, request/response data and additional context information.
36
#[derive(Clone)]
47
pub struct InnerContext {
8+
/// UDP socket wrapper with read-write lock.
59
pub(crate) socket: OptionArcRwLockUdpSocket,
10+
/// Incoming request data.
611
pub(crate) request: Request,
12+
/// Outgoing response data.
713
pub(crate) response: Response,
14+
/// Remote socket address.
815
pub(crate) socket_addr: OptionSocketAddr,
16+
/// Additional context data storage.
917
pub(crate) data: HashMapArcAnySendSync,
1018
}
1119

20+
/// Thread-safe context wrapper for UDP operations.
21+
///
22+
/// Provides synchronized access to the inner context.
1223
#[derive(Clone)]
13-
pub struct Context(pub(super) ArcRwLock<InnerContext>);
24+
pub struct Context(
25+
/// Thread-safe reference to inner context.
26+
pub(super) ArcRwLock<InnerContext>,
27+
);

src/handler/fn.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
/// Default error handler that prints errors to stderr.
2+
///
3+
/// # Arguments
4+
///
5+
/// - `String` - The error message to be printed.
6+
///
7+
/// # Returns
8+
///
9+
/// - `()` - This function does not return any value.
110
pub(crate) fn print_error_handle(error: String) {
211
eprintln!("{}", error);
312
let _ = std::io::Write::flush(&mut std::io::stderr());

src/handler/trait.rs

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

3+
/// Trait for error handling functions.
34
pub trait ErrorHandle: Fn(String) {}
45

6+
/// Trait for async handler functions without Pin requirement.
7+
///
8+
/// # Generic Parameters
9+
///
10+
/// - `Fut` - Future type returned by the function.
511
pub trait AsyncFuncWithoutPin<Fut>: Fn(Context) -> Fut + Send + Sync + 'static
612
where
713
Fut: Future<Output = ()> + Send + 'static,
814
{
915
}
1016

17+
/// Trait for general handler functions.
18+
///
19+
/// Wraps async functions in Pin<Box<dyn Future>>.
1120
pub trait Func:
1221
Fn(Context) -> Pin<Box<dyn Future<Output = ()> + Send + 'static>> + Send + Sync + 'static
1322
{

0 commit comments

Comments
 (0)