From 0b484df40c356cf4fe87f3d3a20eadf59eef42c9 Mon Sep 17 00:00:00 2001 From: mat Date: Mon, 6 Dec 2021 19:59:20 +0000 Subject: implement new packet implementation --- minecraft-protocol/src/server_status_pinger.rs | 79 +++----------------------- 1 file changed, 9 insertions(+), 70 deletions(-) (limited to 'minecraft-protocol/src/server_status_pinger.rs') diff --git a/minecraft-protocol/src/server_status_pinger.rs b/minecraft-protocol/src/server_status_pinger.rs index 86d0cae9..e9393179 100644 --- a/minecraft-protocol/src/server_status_pinger.rs +++ b/minecraft-protocol/src/server_status_pinger.rs @@ -1,81 +1,22 @@ -use crate::{connection::Connection, resolver, ServerAddress}; -use tokio::{ - io::{AsyncReadExt, AsyncWriteExt, BufWriter}, - net::TcpStream, -}; - -struct ServerStatus {} - -async fn write_byte(buf: &mut Vec, n: u8) { - buf.write_u8(n).await.unwrap(); - println!("write_byte: {}", n); -} - -async fn write_bytes(buf: &mut Vec, bytes: &[u8]) { - buf.write_all(bytes).await.unwrap(); - println!("write_bytes: {:?}", buf); -} - -async fn write_varint(buf: &mut Vec, mut n: u32) { - loop { - if (n & 0xFFFFFF80) == 0 { - write_byte(buf, n as u8).await; - return (); - } - write_byte(buf, (n & 0x7F | 0x80) as u8).await; - n >>= 7; - } -} - -async fn write_utf(buf: &mut Vec, string: &[u8], len: usize) { - if string.len() > len { - panic!( - "String too big (was {} bytes encoded, max {})", - string.len(), - len - ); - } - write_varint(buf, string.len() as u32).await; - write_bytes(buf, string).await; -} - -async fn write_short(buf: &mut Vec, n: u16) { - buf.write_u16(n).await.unwrap(); - println!("write_short: {}", n); -} +use crate::{connection::Connection, resolver, ServerAddress, packets::{ClientIntentionPacket, ServerboundStatusRequestPacket, ConnectionProtocol}}; +use tokio::io::{AsyncReadExt, AsyncWriteExt}; pub async fn ping_server(address: &ServerAddress) -> Result<(), String> { let resolved_address = resolver::resolve_address(&address).await?; let mut conn = Connection::new(&resolved_address).await?; - // protocol version is 757 - - // client intention packet - // friendlyByteBuf.writeVarInt(this.protocolVersion); - // friendlyByteBuf.writeUtf(this.hostName); - // friendlyByteBuf.writeShort(this.port); - // friendlyByteBuf.writeVarInt(this.intention.getId()); - println!("resolved_address {}", &resolved_address.ip); println!("writing intention packet {}", address.host); - let mut buf: Vec = vec![0x00]; // 0 is the packet id for handshake - write_varint(&mut buf, 757).await; - write_utf(&mut buf, address.host.as_bytes(), 32767).await; - write_short(&mut buf, address.port).await; - write_varint(&mut buf, 1).await; + conn.send_packet(&ClientIntentionPacket { + protocol_version: 757, + hostname: &address.host, + port: address.port, + intention: ConnectionProtocol::Status, + }).await; + conn.send_packet(&ServerboundStatusRequestPacket {}).await; - let mut full_buffer = vec![]; - write_varint(&mut full_buffer, buf.len() as u32).await; // length of 1st packet id + data as VarInt - full_buffer.append(&mut buf); - full_buffer.extend_from_slice(&[ - 1, // length of 2nd packet id + data as VarInt - 0x00, // 2nd packet id: 0 for request as VarInt - ]); - - conn.stream.write_all(&full_buffer).await.unwrap(); - conn.stream.flush().await.unwrap(); // log what the server sends back loop { @@ -95,6 +36,4 @@ pub async fn ping_server(address: &ServerAddress) -> Result<(), String> { } } } - - Ok(()) } -- cgit v1.2.3