mirror of
https://github.com/tribufu/rust-gamedig
synced 2026-05-18 09:35:50 +00:00
Move TimeoutSettings to be a type that can be used by multiple protocols
This commit is contained in:
parent
f04c883269
commit
974e093e23
4 changed files with 54 additions and 49 deletions
50
src/protocols/types.rs
Normal file
50
src/protocols/types.rs
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
use std::time::Duration;
|
||||
use crate::{GDError, GDResult};
|
||||
|
||||
/// Timeout settings for socket operations
|
||||
pub struct TimeoutSettings {
|
||||
read: Option<Duration>,
|
||||
write: Option<Duration>
|
||||
}
|
||||
|
||||
impl TimeoutSettings {
|
||||
/// Construct new settings, passing None will block indefinitely. Passing zero Duration throws GDError::[InvalidInput](GDError::InvalidInput).
|
||||
pub fn new(read: Option<Duration>, write: Option<Duration>) -> GDResult<Self> {
|
||||
if let Some(read_duration) = read {
|
||||
if read_duration == Duration::new(0, 0) {
|
||||
return Err(GDError::InvalidInput("Can't pass duration 0 to timeout settings".to_owned()))
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(write_duration) = write {
|
||||
if write_duration == Duration::new(0, 0) {
|
||||
return Err(GDError::InvalidInput("Can't pass duration 0 to timeout settings".to_owned()))
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Self {
|
||||
read,
|
||||
write
|
||||
})
|
||||
}
|
||||
|
||||
/// Get the read timeout.
|
||||
pub fn get_read(&self) -> Option<Duration> {
|
||||
self.read
|
||||
}
|
||||
|
||||
/// Get the write timeout.
|
||||
pub fn get_write(&self) -> Option<Duration> {
|
||||
self.write
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for TimeoutSettings {
|
||||
/// Default values are 4 seconds for both read and write.
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
read: Some(Duration::from_secs(4)),
|
||||
write: Some(Duration::from_secs(4))
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue