From 9642558f8f8d983a7087f15d68be8cf07a85f0c2 Mon Sep 17 00:00:00 2001 From: mat Date: Wed, 15 Dec 2021 23:10:55 -0600 Subject: azalea --- azalea-protocol/src/write.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 azalea-protocol/src/write.rs (limited to 'azalea-protocol/src/write.rs') diff --git a/azalea-protocol/src/write.rs b/azalea-protocol/src/write.rs new file mode 100644 index 00000000..3d8540eb --- /dev/null +++ b/azalea-protocol/src/write.rs @@ -0,0 +1,27 @@ +use tokio::{io::AsyncWriteExt, net::TcpStream}; + +use crate::{mc_buf::Writable, packets::ProtocolPacket}; + +pub async fn write_packet(packet: impl ProtocolPacket, stream: &mut TcpStream) { + // TODO: implement compression + + // packet structure: + // length (varint) + id (varint) + data + + // write the packet id + let mut id_and_data_buf = vec![]; + id_and_data_buf.write_varint(packet.id() as i32); + packet.write(&mut id_and_data_buf); + + // write the packet data + + // make a new buffer that has the length at the beginning + // and id+data at the end + let mut complete_buf: Vec = Vec::new(); + complete_buf.write_varint(id_and_data_buf.len() as i32); + complete_buf.append(&mut id_and_data_buf); + + // finally, write and flush to the stream + stream.write_all(&complete_buf).await.unwrap(); + stream.flush().await.unwrap(); +} -- cgit v1.2.3