mirror of
https://github.com/tribufu/rust-gamedig
synced 2026-06-01 09:42:41 +00:00
feat: Add connect timeout to TimeoutSettings (#158)
Because TcpSocket connects in Socket::new TimeoutSettings are now required for Socket::new. Since we already have TimeoutSettings there Sockets are now expected to apply timeout settings in Socket::new.
This commit is contained in:
parent
7416d54b14
commit
e3bdbc2a41
17 changed files with 97 additions and 42 deletions
|
|
@ -13,7 +13,10 @@ use std::{
|
|||
const DEFAULT_PACKET_SIZE: usize = 1024;
|
||||
|
||||
pub trait Socket {
|
||||
fn new(address: &SocketAddr) -> GDResult<Self>
|
||||
/// Create a new socket and connect to the remote address (if required).
|
||||
///
|
||||
/// Calls [Self::apply_timeout] with the given timeout settings.
|
||||
fn new(address: &SocketAddr, timeout_settings: &Option<TimeoutSettings>) -> GDResult<Self>
|
||||
where Self: Sized;
|
||||
|
||||
fn apply_timeout(&self, timeout_settings: &Option<TimeoutSettings>) -> GDResult<()>;
|
||||
|
|
@ -30,11 +33,21 @@ pub struct TcpSocket {
|
|||
}
|
||||
|
||||
impl Socket for TcpSocket {
|
||||
fn new(address: &SocketAddr) -> GDResult<Self> {
|
||||
Ok(Self {
|
||||
socket: net::TcpStream::connect(address).map_err(|e| SocketConnect.context(e))?,
|
||||
fn new(address: &SocketAddr, timeout_settings: &Option<TimeoutSettings>) -> GDResult<Self> {
|
||||
let socket = if let Some(timeout) = TimeoutSettings::get_connect_or_default(timeout_settings) {
|
||||
net::TcpStream::connect_timeout(address, timeout)
|
||||
} else {
|
||||
net::TcpStream::connect(address)
|
||||
};
|
||||
|
||||
let socket = Self {
|
||||
socket: socket.map_err(|e| SocketConnect.context(e))?,
|
||||
address: *address,
|
||||
})
|
||||
};
|
||||
|
||||
socket.apply_timeout(timeout_settings)?;
|
||||
|
||||
Ok(socket)
|
||||
}
|
||||
|
||||
fn apply_timeout(&self, timeout_settings: &Option<TimeoutSettings>) -> GDResult<()> {
|
||||
|
|
@ -68,13 +81,17 @@ pub struct UdpSocket {
|
|||
}
|
||||
|
||||
impl Socket for UdpSocket {
|
||||
fn new(address: &SocketAddr) -> GDResult<Self> {
|
||||
fn new(address: &SocketAddr, timeout_settings: &Option<TimeoutSettings>) -> GDResult<Self> {
|
||||
let socket = net::UdpSocket::bind("0.0.0.0:0").map_err(|e| SocketBind.context(e))?;
|
||||
|
||||
Ok(Self {
|
||||
let socket = Self {
|
||||
socket,
|
||||
address: *address,
|
||||
})
|
||||
};
|
||||
|
||||
socket.apply_timeout(timeout_settings)?;
|
||||
|
||||
Ok(socket)
|
||||
}
|
||||
|
||||
fn apply_timeout(&self, timeout_settings: &Option<TimeoutSettings>) -> GDResult<()> {
|
||||
|
|
@ -125,7 +142,7 @@ mod tests {
|
|||
});
|
||||
|
||||
// Create a TCP socket and send a message to the server
|
||||
let mut socket = TcpSocket::new(&bound_address).unwrap();
|
||||
let mut socket = TcpSocket::new(&bound_address, &None).unwrap();
|
||||
let message = b"hello, world!";
|
||||
socket.send(message).unwrap();
|
||||
|
||||
|
|
@ -156,7 +173,7 @@ mod tests {
|
|||
});
|
||||
|
||||
// Create a UDP socket and send a message to the server
|
||||
let mut socket = UdpSocket::new(&bound_address).unwrap();
|
||||
let mut socket = UdpSocket::new(&bound_address, &None).unwrap();
|
||||
let message = b"hello, world!";
|
||||
socket.send(message).unwrap();
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue