From 0a57a7fae03e6bd7b6175bc9fe9005c6fd82c0d7 Mon Sep 17 00:00:00 2001 From: Viktor Persson Date: Sat, 4 May 2024 13:06:58 +0200 Subject: [PATCH] Replace udp module with external crate. --- Cargo.toml | 4 ++-- src/command.rs | 7 +++++-- src/lib.rs | 1 - src/udp.rs | 36 ------------------------------------ 4 files changed, 7 insertions(+), 41 deletions(-) delete mode 100644 src/udp.rs diff --git a/Cargo.toml b/Cargo.toml index d203230..947cc7b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,11 +4,10 @@ description = "Get server addresses from QuakeWorld master servers." keywords = ["quake", "quakeworld", "servers"] repository = "https://github.com/vikpe/masterstat-rust" authors = ["Viktor Persson "] -version = "0.1.2" +version = "0.1.3" edition = "2021" license = "MIT" include = [ - "/benches/**", "/Cargo.toml", "/LICENSE", "/README.md", @@ -21,6 +20,7 @@ include = [ [dependencies] anyhow = "1.0.82" futures = "0.3.30" +tinyudp = "0.2.1" tokio = { version = "1.37.0", features = ["rt", "sync"] } zerocopy = "0.7.32" zerocopy-derive = "0.7.32" diff --git a/src/command.rs b/src/command.rs index 421dcac..3f5e7a0 100644 --- a/src/command.rs +++ b/src/command.rs @@ -6,7 +6,6 @@ use tokio::sync::Mutex; use zerocopy::FromBytes; use crate::server_address::{RawServerAddress, ServerAddress, RAW_ADDRESS_SIZE}; -use crate::udp; const SERVERS_COMMAND: [u8; 3] = [0x63, 0x0a, 0x00]; const SERVERS_RESPONSE_HEADER: [u8; 6] = [0xff, 0xff, 0xff, 0xff, 0x64, 0x0a]; @@ -29,7 +28,11 @@ pub fn server_addresses( master_address: &str, timeout: Option, ) -> Result> { - let response = udp::send_and_receive(master_address, &SERVERS_COMMAND, timeout)?; + let options = tinyudp::ReadOptions { + timeout, + buffer_size: 16 * 1024, // 16 kb + }; + let response = tinyudp::send_and_read(master_address, &SERVERS_COMMAND, &options)?; let server_addresses = parse_servers_response(&response)?; Ok(sorted_and_unique(&server_addresses)) } diff --git a/src/lib.rs b/src/lib.rs index 25bf2cb..0992e6b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,7 +4,6 @@ mod command; mod server_address; -mod udp; pub use crate::command::server_addresses; pub use crate::command::server_addresses_from_many; diff --git a/src/udp.rs b/src/udp.rs deleted file mode 100644 index dfd1181..0000000 --- a/src/udp.rs +++ /dev/null @@ -1,36 +0,0 @@ -use std::net::{Ipv4Addr, UdpSocket}; -use std::time::Duration; - -use anyhow::{anyhow as e, Result}; - -pub fn connect(address: &str) -> Result { - let from_address = (Ipv4Addr::UNSPECIFIED, 0); - let socket = UdpSocket::bind(from_address).map_err(|e| e!("udp::connect: {}", e))?; - socket.connect(address)?; - Ok(socket) -} - -pub fn send(address: &str, message: &[u8]) -> Result { - let socket = connect(address)?; - socket.send(message).map_err(|e| e!("udp::send: {}", e))?; - Ok(socket) -} - -pub fn receive(socket: &UdpSocket, timeout: Option) -> Result> { - let mut buffer = [0; 8 * 1024]; - socket.set_read_timeout(timeout)?; - let bytes_read = socket - .recv(&mut buffer) - .map_err(|e| e!("udp::receive: {}", e))?; - let response = &buffer[..bytes_read]; - Ok(Vec::from(response)) -} - -pub fn send_and_receive( - address: &str, - message: &[u8], - timeout: Option, -) -> Result> { - let socket = send(address, message)?; - receive(&socket, timeout) -}