Skip to content

Commit 0d9dbc5

Browse files
committed
feat: v3.0.0
1 parent 9518312 commit 0d9dbc5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1646
-548
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 = "udp"
3-
version = "2.0.12"
3+
version = "3.0.0"
44
readme = "README.md"
55
edition = "2024"
66
authors = ["root@ltpp.vip"]

src/attribute/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
mod r#type;
2+
3+
pub use r#type::*;

src/attribute/type.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use crate::*;
2+
3+
/// Type alias for thread-safe attribute storage.
4+
///
5+
/// This type is used to store arbitrary data that can be shared across
6+
/// different parts of the application in a thread-safe manner.
7+
pub type ThreadSafeAttributeStore = HashMap<String, Arc<dyn Any + Send + Sync>>;
8+
9+
/// Type alias for types that implement Any + Send + Sync + Clone.
10+
///
11+
/// This is a trait alias for types that can be stored in the attribute store
12+
/// and safely shared across threads.
13+
pub trait AnySendSyncClone: Any + Send + Sync + Clone {}
14+
impl<T> AnySendSyncClone for T where T: Any + Send + Sync + Clone {}

src/common/const.rs

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/common/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
mod r#const;
21
mod r#type;
32

4-
pub use {r#const::*, r#type::*};
3+
pub use r#type::*;

src/common/type.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,17 @@ use crate::*;
22

33
/// Thread-safe reference-counted read-write lock wrapper.
44
pub type ArcRwLock<T> = Arc<RwLock<T>>;
5+
6+
/// Helper function to wrap data in an `Arc<RwLock<T>>`.
7+
///
8+
/// # Arguments
9+
///
10+
/// - `T` - The data to wrap.
11+
///
12+
/// # Returns
13+
///
14+
/// - `ArcRwLock<T>` - The wrapped data.
15+
#[inline(always)]
16+
pub fn arc_rwlock<T>(data: T) -> ArcRwLock<T> {
17+
Arc::new(RwLock::new(data))
18+
}

src/config/const.rs

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/config/impl.rs

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

3-
/// Default implementation for ServerConfig.
4-
impl Default for ServerConfig {
5-
/// Creates a default server configuration.
3+
/// Default host address.
4+
pub const DEFAULT_HOST: &str = "0.0.0.0";
5+
/// Default UDP port.
6+
pub const DEFAULT_PORT: u16 = 60000;
7+
/// Default buffer size for UDP packets (512KB).
8+
pub const DEFAULT_BUFFER_SIZE: usize = 524288;
9+
/// Default `TCP_NODELAY` setting.
10+
pub const DEFAULT_NODELAY: Option<bool> = None;
11+
/// Default `IP_TTL` setting.
12+
pub const DEFAULT_TTL: Option<u32> = None;
13+
14+
/// Provides a default implementation for ServerConfigData.
15+
impl Default for ServerConfigData {
16+
/// Creates a new ServerConfigData instance with default values.
617
///
718
/// # Returns
819
///
9-
/// - `ServerConfig` - New config with default values.
20+
/// - `Self` - A new instance with default configuration.
1021
#[inline(always)]
1122
fn default() -> Self {
1223
Self {
1324
host: DEFAULT_HOST.to_owned(),
14-
port: DEFAULT_LISTEN_PORT,
25+
port: DEFAULT_PORT,
1526
buffer_size: DEFAULT_BUFFER_SIZE,
16-
error_handle: Arc::new(print_error_handle),
27+
nodelay: DEFAULT_NODELAY,
28+
ttl: DEFAULT_TTL,
1729
}
1830
}
1931
}
32+
33+
/// Provides a default implementation for ServerConfig.
34+
impl Default for ServerConfig {
35+
/// Creates a new ServerConfig instance with default values.
36+
///
37+
/// # Returns
38+
///
39+
/// - `Self` - A new instance wrapping default ServerConfigData.
40+
#[inline(always)]
41+
fn default() -> Self {
42+
Self(arc_rwlock(ServerConfigData::default()))
43+
}
44+
}
45+
46+
/// Implements the `PartialEq` trait for `ServerConfig`.
47+
///
48+
/// This allows for comparing two `ServerConfig` instances for equality.
49+
impl PartialEq for ServerConfig {
50+
/// Checks if two `ServerConfig` instances are equal.
51+
///
52+
/// It first checks for pointer equality for performance. If the pointers are not equal,
53+
/// it compares the inner `ServerConfigData` values.
54+
///
55+
/// # Arguments
56+
///
57+
/// - `&Self` - The other `ServerConfig` to compare against.
58+
///
59+
/// # Returns
60+
///
61+
/// - `bool` - Indicating whether the configurations are equal.
62+
#[inline(always)]
63+
fn eq(&self, other: &Self) -> bool {
64+
if Arc::ptr_eq(&self.0, &other.0) {
65+
return true;
66+
}
67+
if let (Ok(s), Ok(o)) = (self.0.try_read(), other.0.try_read()) {
68+
*s == *o
69+
} else {
70+
false
71+
}
72+
}
73+
}
74+
75+
/// Implements the `Eq` trait for `ServerConfig`.
76+
///
77+
/// This indicates that `ServerConfig` has a total equality relation.
78+
impl Eq for ServerConfig {}
79+
80+
/// Implementation of `From` trait for `ServerConfig`.
81+
impl From<ServerConfigData> for ServerConfig {
82+
/// Creates a `ServerConfig` from a `ServerConfigData`.
83+
///
84+
/// # Arguments
85+
///
86+
/// - `ServerConfigData` - The configuration data.
87+
///
88+
/// # Returns
89+
///
90+
/// - `ServerConfig` - A `ServerConfig` instance.
91+
#[inline(always)]
92+
fn from(data: ServerConfigData) -> Self {
93+
Self(arc_rwlock(data))
94+
}
95+
}
96+
97+
/// Implementation block for `ServerConfig`.
98+
impl ServerConfig {
99+
/// Creates a new `ServerConfig` with default values.
100+
///
101+
/// # Returns
102+
///
103+
/// - `Self` - A new `ServerConfig` instance.
104+
pub async fn new() -> Self {
105+
Self::default()
106+
}
107+
108+
/// Acquires a read lock on the server configuration.
109+
///
110+
/// # Returns
111+
///
112+
/// - `RwLockReadGuard<ServerConfigData>` - A read guard for the inner configuration.
113+
async fn read(&self) -> RwLockReadGuard<'_, ServerConfigData> {
114+
self.0.read().await
115+
}
116+
117+
/// Acquires a write lock on the server configuration.
118+
///
119+
/// # Returns
120+
///
121+
/// - `RwLockWriteGuard<ServerConfigData>` - A write guard for the inner configuration.
122+
async fn write(&self) -> RwLockWriteGuard<'_, ServerConfigData> {
123+
self.0.write().await
124+
}
125+
126+
/// Retrieves a clone of the inner server configuration.
127+
///
128+
/// This function provides a snapshot of the current configuration by acquiring a read lock
129+
/// and cloning the inner `ServerConfigData`.
130+
///
131+
/// # Returns
132+
///
133+
/// - `ServerConfigData` - A `ServerConfigData` instance containing the current server configuration.
134+
pub(crate) async fn get_data(&self) -> ServerConfigData {
135+
self.read().await.clone()
136+
}
137+
138+
/// Sets the host address for the server.
139+
///
140+
/// # Arguments
141+
///
142+
/// - `H` - The host address to set.
143+
///
144+
/// # Returns
145+
///
146+
/// - `&Self` - A reference to `Self` for method chaining.
147+
pub async fn host<H>(&self, host: H) -> &Self
148+
where
149+
H: AsRef<str>,
150+
{
151+
self.write().await.host = host.as_ref().to_owned();
152+
self
153+
}
154+
155+
/// Sets the port for the server.
156+
///
157+
/// # Arguments
158+
///
159+
/// - `port` - The port number to set.
160+
///
161+
/// # Returns
162+
///
163+
/// - `&Self` - A reference to `Self` for method chaining.
164+
pub async fn port(&self, port: u16) -> &Self {
165+
self.write().await.port = port;
166+
self
167+
}
168+
169+
/// Sets the buffer size for receiving UDP packets.
170+
///
171+
/// # Arguments
172+
///
173+
/// - `size` - The buffer size in bytes.
174+
///
175+
/// # Returns
176+
///
177+
/// - `&Self` - A reference to `Self` for method chaining.
178+
pub async fn buffer_size(&self, size: usize) -> &Self {
179+
self.write().await.buffer_size = size;
180+
self
181+
}
182+
183+
/// Sets the `TCP_NODELAY` option.
184+
///
185+
/// # Arguments
186+
///
187+
/// - `nodelay` - The value for `TCP_NODELAY`.
188+
///
189+
/// # Returns
190+
///
191+
/// - `&Self` - A reference to `Self` for method chaining.
192+
pub async fn nodelay(&self, nodelay: bool) -> &Self {
193+
self.write().await.nodelay = Some(nodelay);
194+
self
195+
}
196+
197+
/// Enables the `TCP_NODELAY` option.
198+
///
199+
/// # Returns
200+
///
201+
/// - `&Self` - A reference to `Self` for method chaining.
202+
pub async fn enable_nodelay(&self) -> &Self {
203+
self.nodelay(true).await
204+
}
205+
206+
/// Disables the `TCP_NODELAY` option.
207+
///
208+
/// # Returns
209+
///
210+
/// - `&Self` - A reference to `Self` for method chaining.
211+
pub async fn disable_nodelay(&self) -> &Self {
212+
self.nodelay(false).await
213+
}
214+
215+
/// Sets the `IP_TTL` option.
216+
///
217+
/// # Arguments
218+
///
219+
/// - `ttl` - The value for `IP_TTL`.
220+
///
221+
/// # Returns
222+
///
223+
/// - `&Self` - A reference to `Self` for method chaining.
224+
pub async fn ttl(&self, ttl: u32) -> &Self {
225+
self.write().await.ttl = Some(ttl);
226+
self
227+
}
228+
}
229+
230+
/// Implementation block for `ServerConfigData`.
231+
impl ServerConfigData {
232+
/// Gets the host address.
233+
///
234+
/// # Returns
235+
///
236+
/// - `&String` - Reference to the host address.
237+
pub fn get_host(&self) -> &String {
238+
&self.host
239+
}
240+
241+
/// Gets the port number.
242+
///
243+
/// # Returns
244+
///
245+
/// - `u16` - The port number.
246+
pub fn get_port(&self) -> u16 {
247+
self.port
248+
}
249+
250+
/// Gets the buffer size.
251+
///
252+
/// # Returns
253+
///
254+
/// - `usize` - The buffer size in bytes.
255+
pub fn get_buffer_size(&self) -> usize {
256+
self.buffer_size
257+
}
258+
259+
/// Gets the `TCP_NODELAY` option.
260+
///
261+
/// # Returns
262+
///
263+
/// - `Option<bool>` - The `TCP_NODELAY` value if set.
264+
pub fn get_nodelay(&self) -> Option<bool> {
265+
self.nodelay
266+
}
267+
268+
/// Gets the `IP_TTL` option.
269+
///
270+
/// # Returns
271+
///
272+
/// - `Option<u32>` - The `IP_TTL` value if set.
273+
pub fn get_ttl(&self) -> Option<u32> {
274+
self.ttl
275+
}
276+
277+
/// Sets the host address.
278+
///
279+
/// # Arguments
280+
///
281+
/// - `impl AsRef<str>` - The host address to set.
282+
pub fn set_host<H>(&mut self, host: H)
283+
where
284+
H: AsRef<str>,
285+
{
286+
self.host = host.as_ref().to_owned();
287+
}
288+
289+
/// Sets the port number.
290+
///
291+
/// # Arguments
292+
///
293+
/// - `u16` - The port number to set.
294+
pub fn set_port(&mut self, port: u16) {
295+
self.port = port;
296+
}
297+
298+
/// Sets the buffer size.
299+
///
300+
/// # Arguments
301+
///
302+
/// - `usize` - The buffer size in bytes.
303+
pub fn set_buffer_size(&mut self, size: usize) {
304+
self.buffer_size = size;
305+
}
306+
307+
/// Sets the `TCP_NODELAY` option.
308+
///
309+
/// # Arguments
310+
///
311+
/// - `Option<bool>` - The `TCP_NODELAY` value.
312+
pub fn set_nodelay(&mut self, nodelay: Option<bool>) {
313+
self.nodelay = nodelay;
314+
}
315+
316+
/// Sets the `IP_TTL` option.
317+
///
318+
/// # Arguments
319+
///
320+
/// - `Option<u32>` - The `IP_TTL` value.
321+
pub fn set_ttl(&mut self, ttl: Option<u32>) {
322+
self.ttl = ttl;
323+
}
324+
}

0 commit comments

Comments
 (0)