From 515ad377b314cd97a8b663dbd738681917434434 Mon Sep 17 00:00:00 2001 From: mat Date: Mon, 6 Dec 2021 22:12:43 +0000 Subject: start implementing reading --- minecraft-protocol/src/connection.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'minecraft-protocol/src/connection.rs') diff --git a/minecraft-protocol/src/connection.rs b/minecraft-protocol/src/connection.rs index b187baff..73b5959f 100644 --- a/minecraft-protocol/src/connection.rs +++ b/minecraft-protocol/src/connection.rs @@ -1,7 +1,7 @@ use crate::{mc_buf, packets::Packet, ServerIpAddress}; use bytes::BytesMut; use tokio::{ - io::{AsyncWriteExt, BufWriter}, + io::{AsyncReadExt, AsyncWriteExt, BufReader, BufWriter}, net::TcpStream, }; @@ -12,9 +12,8 @@ pub enum PacketFlow { pub struct Connection { pub flow: PacketFlow, - pub stream: BufWriter, - /// The read buffer - pub buffer: BytesMut, + /// The buffered writer + pub stream: TcpStream, } impl Connection { @@ -33,12 +32,17 @@ impl Connection { Ok(Connection { flow: PacketFlow::ClientToServer, - stream: BufWriter::new(stream), - // 4mb read buffer - buffer: BytesMut::with_capacity(4 * 1024 * 1024), + stream, }) } + pub async fn read_packet(&mut self) { + // the first thing minecraft sends us is the length as a varint, which can be up to 5 bytes + let mut buf = Vec::new(); + self.stream.read_buf(&mut buf).await; + mc_buf::read_varint(buf) + } + /// Write a packet to the server pub async fn send_packet(&mut self, packet: &dyn Packet) { // packet structure: -- cgit v1.2.3