From 1a2c034b411e3d0dceb7b4a921ddd42226719bd0 Mon Sep 17 00:00:00 2001 From: mat Date: Mon, 6 Dec 2021 21:15:38 +0000 Subject: [PATCH] remove struct from friendly byte byffer --- minecraft-protocol/src/connection.rs | 14 ++--- minecraft-protocol/src/friendly_byte_buf.rs | 58 ------------------- minecraft-protocol/src/lib.rs | 2 +- .../src/packets/client_intention_packet.rs | 12 ++-- minecraft-protocol/src/packets/mod.rs | 4 +- .../src/server_status_pinger.rs | 10 +++- 6 files changed, 21 insertions(+), 79 deletions(-) delete mode 100644 minecraft-protocol/src/friendly_byte_buf.rs diff --git a/minecraft-protocol/src/connection.rs b/minecraft-protocol/src/connection.rs index 79058a75..b187baff 100644 --- a/minecraft-protocol/src/connection.rs +++ b/minecraft-protocol/src/connection.rs @@ -1,4 +1,4 @@ -use crate::{friendly_byte_buf::FriendlyByteBuf, packets::Packet, ServerIpAddress}; +use crate::{mc_buf, packets::Packet, ServerIpAddress}; use bytes::BytesMut; use tokio::{ io::{AsyncWriteExt, BufWriter}, @@ -44,20 +44,16 @@ impl Connection { // packet structure: // length + id + data - // Is this efficient? I have no idea, probably not. - // getting rid of the FriendlyByteBuffer struct might help - // write the packet id let mut id_and_data_buf = vec![packet.get_id()]; // write the packet data - let mut id_and_data_friendly_buf = FriendlyByteBuf::new(&mut id_and_data_buf); - packet.write(&mut id_and_data_friendly_buf); + packet.write(&mut id_and_data_buf); - // add the packet length to the beginning + // make a new buffer that has the length at the beginning + // and id+data at the end let mut complete_buf: Vec = Vec::new(); - let mut complete_friendly_buf = FriendlyByteBuf::new(&mut complete_buf); - complete_friendly_buf.write_varint(id_and_data_buf.len() as u32); + mc_buf::write_varint(&mut complete_buf, id_and_data_buf.len() as u32); complete_buf.append(&mut id_and_data_buf); // finally, write and flush to the stream diff --git a/minecraft-protocol/src/friendly_byte_buf.rs b/minecraft-protocol/src/friendly_byte_buf.rs deleted file mode 100644 index 586b0857..00000000 --- a/minecraft-protocol/src/friendly_byte_buf.rs +++ /dev/null @@ -1,58 +0,0 @@ -//! Minecraft calls it a "friendly byte buffer". - -use byteorder::{BigEndian, WriteBytesExt}; - -// const MAX_VARINT_SIZE: u32 = 5; -// const MAX_VARLONG_SIZE: u32 = 10; -// const DEFAULT_NBT_QUOTA: u32 = 2097152; -const MAX_STRING_LENGTH: u16 = 32767; -// const MAX_COMPONENT_STRING_LENGTH: u32 = 262144; - -pub struct FriendlyByteBuf<'a> { - source: &'a mut Vec, -} - -impl<'a> FriendlyByteBuf<'a> { - pub fn new(source: &'a mut Vec) -> FriendlyByteBuf<'a> { - FriendlyByteBuf { source } - } - - pub fn write_byte(&mut self, n: u8) { - self.source.write_u8(n).unwrap(); - } - - pub fn write_bytes(&mut self, bytes: &[u8]) { - self.source.extend_from_slice(bytes); - } - - pub fn write_varint(&mut self, mut n: u32) { - loop { - if (n & 0xFFFFFF80) == 0 { - self.write_byte(n as u8); - return (); - } - self.write_byte((n & 0x7F | 0x80) as u8); - n >>= 7; - } - } - - pub fn write_utf_with_len(&mut self, string: &String, len: usize) { - if string.len() > len { - panic!( - "String too big (was {} bytes encoded, max {})", - string.len(), - len - ); - } - self.write_varint(string.len() as u32); - self.write_bytes(string.as_bytes()); - } - - pub fn write_utf(&mut self, string: &String) { - self.write_utf_with_len(string, MAX_STRING_LENGTH as usize); - } - - pub fn write_short(&mut self, n: u16) { - self.source.write_u16::(n).unwrap(); - } -} diff --git a/minecraft-protocol/src/lib.rs b/minecraft-protocol/src/lib.rs index 8c647dc2..b3653499 100644 --- a/minecraft-protocol/src/lib.rs +++ b/minecraft-protocol/src/lib.rs @@ -5,7 +5,7 @@ use std::str::FromStr; use tokio::runtime::Runtime; pub mod connection; -pub mod friendly_byte_buf; +pub mod mc_buf; pub mod packets; pub mod resolver; pub mod server_status_pinger; diff --git a/minecraft-protocol/src/packets/client_intention_packet.rs b/minecraft-protocol/src/packets/client_intention_packet.rs index 30f76387..80c9ce66 100644 --- a/minecraft-protocol/src/packets/client_intention_packet.rs +++ b/minecraft-protocol/src/packets/client_intention_packet.rs @@ -1,6 +1,6 @@ use std::hash::Hash; -use crate::friendly_byte_buf::FriendlyByteBuf; +use crate::mc_buf; use super::{ConnectionProtocol, Packet}; @@ -20,10 +20,10 @@ impl<'a> Packet for ClientIntentionPacket<'a> { } // implement "from_reader" for "ClientIntentionPacket" - fn write(&self, buf: &mut FriendlyByteBuf) { - buf.write_varint(self.protocol_version); - buf.write_utf(&self.hostname); - buf.write_short(self.port); - buf.write_varint(self.intention.clone() as u32); + fn write(&self, buf: &mut Vec) { + mc_buf::write_varint(buf, self.protocol_version); + mc_buf::write_utf(buf, &self.hostname); + mc_buf::write_short(buf, self.port); + mc_buf::write_varint(buf, self.intention.clone() as u32); } } diff --git a/minecraft-protocol/src/packets/mod.rs b/minecraft-protocol/src/packets/mod.rs index bdb80c2f..7d254adc 100644 --- a/minecraft-protocol/src/packets/mod.rs +++ b/minecraft-protocol/src/packets/mod.rs @@ -3,7 +3,7 @@ pub use client_intention_packet::ClientIntentionPacket; mod serverbound_status_request_packet; pub use serverbound_status_request_packet::ServerboundStatusRequestPacket; -use crate::friendly_byte_buf::FriendlyByteBuf; +use crate::mc_buf; #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum ConnectionProtocol { @@ -17,5 +17,5 @@ pub trait Packet { /// Get the id of the packet, this is always a byte. fn get_id(&self) -> u8; - fn write(&self, friendly_byte_buf: &mut FriendlyByteBuf) -> (); + fn write(&self, friendly_byte_buf: &mut Vec) -> (); } diff --git a/minecraft-protocol/src/server_status_pinger.rs b/minecraft-protocol/src/server_status_pinger.rs index e9393179..5608ebea 100644 --- a/minecraft-protocol/src/server_status_pinger.rs +++ b/minecraft-protocol/src/server_status_pinger.rs @@ -1,4 +1,8 @@ -use crate::{connection::Connection, resolver, ServerAddress, packets::{ClientIntentionPacket, ServerboundStatusRequestPacket, ConnectionProtocol}}; +use crate::{ + connection::Connection, + packets::{ClientIntentionPacket, ConnectionProtocol, ServerboundStatusRequestPacket}, + resolver, ServerAddress, +}; use tokio::io::{AsyncReadExt, AsyncWriteExt}; pub async fn ping_server(address: &ServerAddress) -> Result<(), String> { @@ -14,10 +18,10 @@ pub async fn ping_server(address: &ServerAddress) -> Result<(), String> { hostname: &address.host, port: address.port, intention: ConnectionProtocol::Status, - }).await; + }) + .await; conn.send_packet(&ServerboundStatusRequestPacket {}).await; - // log what the server sends back loop { if 0 == conn.stream.read_buf(&mut conn.buffer).await.unwrap() {