use tokio for pinging code

This commit is contained in:
Funtimes909
2025-02-22 23:52:21 +13:00
parent 4c5786f9e3
commit 4ea0962fba
3 changed files with 26 additions and 55 deletions
Generated
-25
View File
@@ -6,9 +6,7 @@ version = 4
name = "ServerSeekerV2-Rescanner"
version = "0.1.0"
dependencies = [
"bigdecimal",
"indicatif",
"log",
"serde",
"serde_json",
"sqlx",
@@ -79,19 +77,6 @@ version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8c3c1a368f70d6cf7302d78f8f7093da241fb8e8807c05cc9e51a125895a6d5b"
[[package]]
name = "bigdecimal"
version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f31f3af01c5c65a07985c804d3366560e6fa7883d640a122819b14ec327482c"
dependencies = [
"autocfg",
"libm",
"num-bigint",
"num-integer",
"num-traits",
]
[[package]]
name = "bitflags"
version = "2.8.0"
@@ -779,16 +764,6 @@ dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "num-bigint"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9"
dependencies = [
"num-integer",
"num-traits",
]
[[package]]
name = "num-bigint-dig"
version = "0.8.4"
+1 -3
View File
@@ -9,7 +9,5 @@ serde = { version = "1.0.217", features = ["derive"] }
serde_json = "1.0.138"
toml = "0.8.19"
tokio = { version = "1.0.0", features = ["rt", "rt-multi-thread", "macros"] }
bigdecimal = "0.4.7"
sqlx = { version = "0.8.3", features = ["postgres", "runtime-tokio"] }
log = "0.4.25"
indicatif = "0.17.11"
indicatif = "0.17.11"
+25 -27
View File
@@ -1,9 +1,12 @@
use std::io::{Error, ErrorKind, Read, Write};
use std::net::{SocketAddr, TcpStream};
use std::io::{Error, ErrorKind};
use std::net::SocketAddr;
use std::str::FromStr;
use std::time::Duration;
use tokio::time::timeout;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpStream;
const REQUEST: [u8; 9] = [
const PAYLOAD: [u8; 9] = [
6, // Size: Amount of bytes in the message
0, // ID: Has to be 0
0, // Protocol Version: Can be anything as long as it's a valid varint
@@ -18,34 +21,29 @@ pub async fn ping_server(address: &str, port: u16) -> Result<String, Error> {
let address = format!("{}:{}", address, port);
let socket = SocketAddr::from_str(address.as_str()).map_err(|e| Error::new(ErrorKind::InvalidInput, e))?;
match TcpStream::connect_timeout(&socket, Duration::from_secs(3)) {
Ok(mut stream) => {
stream.write(&REQUEST)?;
let mut buff = [0; 1024];
// Connect and create buffer
let mut stream = timeout(Duration::from_secs(3), TcpStream::connect(&socket)).await??;
let mut buffer = [0; 2048];
// TODO! REPLACE ALL OF THIS
// Read the first buffer, getting the initial bytes needed
let mut total_read = stream.read(&mut buff)?;
// Grab the final buffer size
let buff_size = decode(&buff.to_vec());
// and use that to calculate the total amount of bytes needed (in the entire packet)
let bytes_needed = buff_size.0 + (buff_size.1 as usize);
let mut out_buff = vec![];
out_buff.extend_from_slice(&buff[..total_read]);
// Just incase, we can also calculate the final size of the json
let json_bytes = decode(&(buff[(buff_size.1+1).into()..]));
// Send payload
stream.write(&PAYLOAD).await?;
let mut total_read = stream.read(&mut buffer).await?;
// Repeat until we read everything
while total_read < bytes_needed {
let read = stream.read(&mut buff)?;
out_buff.extend_from_slice(&buff[..read]);
total_read += read;
}
// Decode
let decoded_bytes = decode(&buffer);
let bytes_needed = decoded_bytes.0 + decoded_bytes.1 as usize;
let mut output = vec![];
output.extend_from_slice(&buffer[..total_read]);
let json = decode(&(buffer[(decoded_bytes.1+1).into()..]));
Ok(String::from_utf8_lossy(&out_buff[(buff_size.1 + 1 + json_bytes.1).into()..]).to_string())
}
Err(e) => Err(e),
// Read everything
while total_read < bytes_needed {
let read = stream.read(&mut buffer).await?;
output.extend_from_slice(&buffer[..read]);
total_read += read;
}
Ok(String::from_utf8_lossy(&output[(decoded_bytes.1 + 1 + json.1).into()..]).to_string())
}
fn decode(bytes: &[u8]) -> (usize, u8) {