fix: be more polite to `ServerListPing`s

kity
Skye 9 months ago
parent 21e1d8431f
commit e930eb2f82
Signed by: me
GPG Key ID: 0104BC05F41B77B8

@ -16,7 +16,7 @@ use tokio::{
}; };
use crate::{ use crate::{
netty::WriteExtNetty, netty::{WriteExtNetty, ReadExtNetty},
proto::{ClientboundControlMessage, ServerboundControlMessage}, proto::{ClientboundControlMessage, ServerboundControlMessage},
}; };
@ -279,6 +279,18 @@ async fn politely_disconnect(
.await?; .await?;
connection.write_varint(buf.len() as i32).await?; connection.write_varint(buf.len() as i32).await?;
connection.write_all(&buf).await?; connection.write_all(&buf).await?;
let packet = netty::read_packet(&mut connection).await?;
let mut packet = packet.as_slice();
let id = packet.read_varint()?;
if id != 1 {
return Err(anyhow!("Packet isn't a Ping Request(0x01), but {:#04x}", id));
}
let payload = packet.read_long()?;
let mut buf = Vec::with_capacity(1 + 8);
buf.write_varint(1).await?;
buf.write_u64(payload).await?;
connection.write_varint(buf.len() as i32).await?;
connection.write_all(&buf).await?;
} }
netty::HandshakeType::Login => { netty::HandshakeType::Login => {
let _ = netty::read_packet(&mut connection).await?; let _ = netty::read_packet(&mut connection).await?;

@ -39,6 +39,12 @@ pub trait ReadExtNetty: Read {
Ok(u16::from_be_bytes(buf)) Ok(u16::from_be_bytes(buf))
} }
fn read_long(&mut self) -> Result<u64, NettyReadError> {
let mut buf = [0u8; 8];
self.read_exact(&mut buf)?;
Ok(u64::from_be_bytes(buf))
}
fn read_string(&mut self) -> Result<String, NettyReadError> { fn read_string(&mut self) -> Result<String, NettyReadError> {
let len = self.read_varint()?; let len = self.read_varint()?;
let mut buf = vec![0u8; len as usize]; let mut buf = vec![0u8; len as usize];

Loading…
Cancel
Save