aboutsummaryrefslogtreecommitdiff
path: root/minecraft-protocol/src/write.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/write.rs
parentace140500734d33fe53126086a8d9278fa861e21 (diff)
downloadazalea-drasl-732de94d7b9f1bf2bc9239c8138a37c53242b470.tar.xz
oh yeah it compiles
Diffstat (limited to 'minecraft-protocol/src/write.rs')
-rw-r--r--minecraft-protocol/src/write.rs27
1 files changed, 27 insertions, 0 deletions
diff --git a/minecraft-protocol/src/write.rs b/minecraft-protocol/src/write.rs
new file mode 100644
index 00000000..529bb210
--- /dev/null
+++ b/minecraft-protocol/src/write.rs
@@ -0,0 +1,27 @@
+use tokio::{io::AsyncWriteExt, net::TcpStream};
+
+use crate::{mc_buf, 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![];
+ mc_buf::write_varint(&mut id_and_data_buf, 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<u8> = Vec::new();
+ mc_buf::write_varint(&mut complete_buf, 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();
+}