aboutsummaryrefslogtreecommitdiff
path: root/minecraft-protocol/src/read.rs
diff options
context:
space:
mode:
authormat <github@matdoes.dev>2021-12-15 13:43:57 -0600
committermat <github@matdoes.dev>2021-12-15 13:43:57 -0600
commit732de94d7b9f1bf2bc9239c8138a37c53242b470 (patch)
treeeaeaddbf73bf5379fbb1924e57a28b8dad5be48b /minecraft-protocol/src/read.rs
parentace140500734d33fe53126086a8d9278fa861e21 (diff)
downloadazalea-drasl-732de94d7b9f1bf2bc9239c8138a37c53242b470.tar.xz
oh yeah it compiles
Diffstat (limited to 'minecraft-protocol/src/read.rs')
-rw-r--r--minecraft-protocol/src/read.rs28
1 files changed, 28 insertions, 0 deletions
diff --git a/minecraft-protocol/src/read.rs b/minecraft-protocol/src/read.rs
new file mode 100644
index 00000000..7f4eeaac
--- /dev/null
+++ b/minecraft-protocol/src/read.rs
@@ -0,0 +1,28 @@
+use tokio::{io::BufReader, net::TcpStream};
+
+use crate::{connect::PacketFlow, mc_buf, packets::ProtocolPacket};
+
+pub async fn read_packet<P: ProtocolPacket>(
+ flow: &PacketFlow,
+ stream: &mut TcpStream,
+) -> Result<P, String> {
+ // what this does:
+ // 1. reads the first 5 bytes, probably only some of this will be used to get the packet length
+ // 2. how much we should read = packet length - 5
+ // 3. read the rest of the packet and add it to the cursor
+ // 4. figure out what packet this is and parse it
+
+ // the first thing minecraft sends us is the length as a varint, which can be up to 5 bytes long
+ let mut buf = BufReader::with_capacity(4 * 1024 * 1024, stream);
+
+ let (_packet_size, _packet_size_varint_size) = mc_buf::read_varint(&mut buf).await?;
+
+ // then, minecraft tells us the packet id as a varint
+ let (packet_id, _packet_id_size) = mc_buf::read_varint(&mut buf).await?;
+
+ // if we recognize the packet id, parse it
+
+ let packet = P::read(packet_id.try_into().unwrap(), &flow, &mut buf).await?;
+
+ Ok(packet)
+}