gamedig / rust-gamedig

Game Server Query Library.
https://crates.io/crates/gamedig
MIT License
38 stars 11 forks source link

Add async support #37

Open Douile opened 1 year ago

Douile commented 1 year ago

No where near top priority but it would be nice to have asynchronous network support preferably using tokio.

CosminPerRam commented 1 year ago

It's something that has been on my mind since starting the project, I didn't really had the time to look into how could this be done (as implementing functionality features such as protocols has priority) but it's coming for sure!

Douile commented 1 year ago

Current blocking things I have found:

cainthebest commented 1 year ago

As you've correctly identified, Tokio doesn't directly support setting timeout options on its asynchronous TCP or UDP streams. That's a design decision often found in asynchronous I/O libraries, because blocking operations (such as those with a timeout) conflict with the design goals of non-blocking, asynchronous I/O. Instead, timeouts are usually handled at a higher level. For example

use std::time::Duration;
use tokio::time::timeout;
use tokio::net::TcpStream;

let fut = TcpStream::connect("127.0.0.1:8080");

let tcp_stream = match timeout(Duration::from_secs(5), fut).await {
    Ok(Ok(stream)) => stream,
    Ok(Err(err)) => panic!("Failed to connect: {:?}", err),
    Err(_) => panic!("Connection timed out"),
};