mirror of
https://github.com/mat-1/azalea.git
synced 2025-08-02 06:16:04 +00:00
remove struct from friendly byte byffer
This commit is contained in:
parent
0b484df40c
commit
1a2c034b41
6 changed files with 21 additions and 79 deletions
|
@ -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<u8> = 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
|
||||
|
|
|
@ -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<u8>,
|
||||
}
|
||||
|
||||
impl<'a> FriendlyByteBuf<'a> {
|
||||
pub fn new(source: &'a mut Vec<u8>) -> 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::<BigEndian>(n).unwrap();
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
|
|
@ -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<u8>) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<u8>) -> ();
|
||||
}
|
||||
|
|
|
@ -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() {
|
||||
|
|
Loading…
Add table
Reference in a new issue