rust-gamedig/crates/lib/examples/valve_protocol_query.rs
Tom 89ed19f089
feat(protocols): Add more control over gathering additional information (#180)
* protocols: Add more control over gathering additional information

Adds GatherToggle which allows choosing the behaviour for how the query
handles fetching additional information. The choices are:
- DontGather - Don't attempt to fetch information
- AttemptGather - Try to fetch the information but ignore errors
- Required - Try to fetch information and fail if it errors

A handy macro was also added to utils to dispatch additional queries
based on a GatherToggle value.

* Add/Update badge

* protocols: Improve GatherToggle enum names

Co-Authored-By: Cain <75994858+cainthebest@users.noreply.github.com>
Co-Authored-By: CosminPerRam <cosmin.p@live.com>

* Add/Update badge

---------

Co-authored-by: GitHub Action <action@github.com>
Co-authored-by: Cain <75994858+cainthebest@users.noreply.github.com>
Co-authored-by: CosminPerRam <cosmin.p@live.com>
2024-01-22 13:36:17 +02:00

36 lines
1.3 KiB
Rust

use gamedig::protocols::types::GatherToggle;
use gamedig::protocols::valve;
use gamedig::protocols::valve::{Engine, GatheringSettings};
use gamedig::TimeoutSettings;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::time::Duration;
fn main() {
let address = &SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 27015);
let engine = Engine::Source(None); // We don't specify a steam app id, let the query try to find it.
let gather_settings = GatheringSettings {
players: GatherToggle::Enforce, // We want to query for players
rules: GatherToggle::Skip, // We don't want to query for rules
check_app_id: false, // Loosen up the query a bit by not checking app id
};
let read_timeout = Duration::from_secs(2);
let write_timeout = Duration::from_secs(3);
let connect_timeout = Duration::from_secs(4);
let retries = 1; // does another request if the first one fails.
let timeout_settings = TimeoutSettings::new(
Some(read_timeout),
Some(write_timeout),
Some(connect_timeout),
retries,
)
.unwrap();
let response = valve::query(
address,
engine,
Some(gather_settings),
Some(timeout_settings),
);
println!("{response:#?}");
}