From 9642558f8f8d983a7087f15d68be8cf07a85f0c2 Mon Sep 17 00:00:00 2001 From: mat Date: Wed, 15 Dec 2021 23:10:55 -0600 Subject: azalea --- azalea-client/src/ping.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 azalea-client/src/ping.rs (limited to 'azalea-client/src/ping.rs') diff --git a/azalea-client/src/ping.rs b/azalea-client/src/ping.rs new file mode 100644 index 00000000..87ccdf66 --- /dev/null +++ b/azalea-client/src/ping.rs @@ -0,0 +1,44 @@ +///! Ping Minecraft servers. +use azalea_protocol::{ + connect::HandshakeConnection, + packets::{ + handshake::client_intention_packet::ClientIntentionPacket, + status::{ + clientbound_status_response_packet::ClientboundStatusResponsePacket, + serverbound_status_request_packet::ServerboundStatusRequestPacket, StatusPacket, + }, + ConnectionProtocol, PROTOCOL_VERSION, + }, + resolver, ServerAddress, +}; + +pub async fn ping_server( + address: &ServerAddress, +) -> Result { + let resolved_address = resolver::resolve_address(address).await?; + + let mut conn = HandshakeConnection::new(&resolved_address).await?; + + // send the client intention packet and switch to the status state + conn.write( + ClientIntentionPacket { + protocol_version: PROTOCOL_VERSION, + hostname: address.host.clone(), + port: address.port, + intention: ConnectionProtocol::Status, + } + .get(), + ) + .await; + let mut conn = conn.status(); + + // send the empty status request packet + conn.write(ServerboundStatusRequestPacket {}.get()).await; + + let packet = conn.read().await.unwrap(); + + match packet { + StatusPacket::ClientboundStatusResponsePacket(p) => Ok(*p), + _ => Err("Invalid packet type".to_string()), + } +} -- cgit v1.2.3