aboutsummaryrefslogtreecommitdiff
path: root/minecraft-protocol/src/connection.rs
diff options
context:
space:
mode:
authormat <github@matdoes.dev>2021-12-06 22:12:43 +0000
committermat <github@matdoes.dev>2021-12-06 22:12:43 +0000
commit515ad377b314cd97a8b663dbd738681917434434 (patch)
treecb1cf65c8e726121ed002aa913132375ab5dc1e7 /minecraft-protocol/src/connection.rs
parent1a2c034b411e3d0dceb7b4a921ddd42226719bd0 (diff)
downloadazalea-drasl-515ad377b314cd97a8b663dbd738681917434434.tar.xz
start implementing reading
Diffstat (limited to 'minecraft-protocol/src/connection.rs')
-rw-r--r--minecraft-protocol/src/connection.rs18
1 files changed, 11 insertions, 7 deletions
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<TcpStream>,
- /// 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: