From 974e093e2305db7a9988f01f86ec0d4468ca211f Mon Sep 17 00:00:00 2001 From: CosminPerRam Date: Wed, 16 Nov 2022 00:06:13 +0200 Subject: [PATCH] Move TimeoutSettings to be a type that can be used by multiple protocols --- src/protocols/mod.rs | 2 ++ src/protocols/types.rs | 50 +++++++++++++++++++++++++++++++++ src/protocols/valve/protocol.rs | 3 +- src/protocols/valve/types.rs | 48 ------------------------------- 4 files changed, 54 insertions(+), 49 deletions(-) create mode 100644 src/protocols/types.rs diff --git a/src/protocols/mod.rs b/src/protocols/mod.rs index f84b187..0e4e770 100644 --- a/src/protocols/mod.rs +++ b/src/protocols/mod.rs @@ -6,3 +6,5 @@ /// Reference: [Server Query](https://developer.valvesoftware.com/wiki/Server_queries) pub mod valve; +/// General types that are used by all protocols. +pub mod types; diff --git a/src/protocols/types.rs b/src/protocols/types.rs new file mode 100644 index 0000000..4d8be27 --- /dev/null +++ b/src/protocols/types.rs @@ -0,0 +1,50 @@ +use std::time::Duration; +use crate::{GDError, GDResult}; + +/// Timeout settings for socket operations +pub struct TimeoutSettings { + read: Option, + write: Option +} + +impl TimeoutSettings { + /// Construct new settings, passing None will block indefinitely. Passing zero Duration throws GDError::[InvalidInput](GDError::InvalidInput). + pub fn new(read: Option, write: Option) -> GDResult { + 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 { + self.read + } + + /// Get the write timeout. + pub fn get_write(&self) -> Option { + 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)) + } + } +} diff --git a/src/protocols/valve/protocol.rs b/src/protocols/valve/protocol.rs index 8aeceab..ac9ac25 100644 --- a/src/protocols/valve/protocol.rs +++ b/src/protocols/valve/protocol.rs @@ -1,7 +1,8 @@ use std::net::UdpSocket; use bzip2_rs::decoder::Decoder; use crate::{GDError, GDResult}; -use crate::protocols::valve::{App, ModData, SteamID, TimeoutSettings}; +use crate::protocols::types::TimeoutSettings; +use crate::protocols::valve::{App, ModData, SteamID}; use crate::protocols::valve::types::{Environment, ExtraData, GatheringSettings, Request, Response, Server, ServerInfo, ServerPlayer, ServerRule, TheShip}; use crate::utils::{buffer, complete_address, u8_lower_upper}; diff --git a/src/protocols/valve/types.rs b/src/protocols/valve/types.rs index a3819d2..6767e27 100644 --- a/src/protocols/valve/types.rs +++ b/src/protocols/valve/types.rs @@ -219,54 +219,6 @@ impl Default for GatheringSettings { } } -/// Timeout settings for socket operations -pub struct TimeoutSettings { - read: Option, - write: Option -} - -impl TimeoutSettings { - /// Construct new settings, passing None will block indefinitely. Passing zero Duration throws GDError::[InvalidInput](GDError::InvalidInput). - pub fn new(read: Option, write: Option) -> GDResult { - 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 { - self.read - } - - /// Get the write timeout. - pub fn get_write(&self) -> Option { - 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)) - } - } -} - /// Generic response types that are used by many games, they are the protocol ones, but without the /// unnecessary bits (example: the **The Ship**-only fields) pub mod game {