|
1 | 1 | use crate::*; |
2 | 2 |
|
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. |
6 | 17 | /// |
7 | 18 | /// # Returns |
8 | 19 | /// |
9 | | - /// - `ServerConfig` - New config with default values. |
| 20 | + /// - `Self` - A new instance with default configuration. |
10 | 21 | #[inline(always)] |
11 | 22 | fn default() -> Self { |
12 | 23 | Self { |
13 | 24 | host: DEFAULT_HOST.to_owned(), |
14 | | - port: DEFAULT_LISTEN_PORT, |
| 25 | + port: DEFAULT_PORT, |
15 | 26 | buffer_size: DEFAULT_BUFFER_SIZE, |
16 | | - error_handle: Arc::new(print_error_handle), |
| 27 | + nodelay: DEFAULT_NODELAY, |
| 28 | + ttl: DEFAULT_TTL, |
17 | 29 | } |
18 | 30 | } |
19 | 31 | } |
| 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