1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 06:16:04 +00:00

start implementing reading

This commit is contained in:
mat 2021-12-06 22:12:43 +00:00
parent 1a2c034b41
commit 515ad377b3
5 changed files with 17 additions and 14 deletions

View file

@ -10,6 +10,6 @@ async-recursion = "^0.3.2"
byteorder = "^1.4.3"
bytes = "^1.1.0"
thiserror = "^1.0.30"
tokio = {version = "^1.14.0", features = ["io-util"]}
tokio = {version = "^1.14.0", features = ["io-util", "net"]}
tokio-util = "^0.6.9"
trust-dns-resolver = "^0.20.3"

View file

@ -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:

View file

@ -1,9 +1,6 @@
use std::net::IpAddr;
use std::net::TcpStream;
use std::str::FromStr;
use tokio::runtime::Runtime;
pub mod connection;
pub mod mc_buf;
pub mod packets;

View file

@ -3,8 +3,6 @@ pub use client_intention_packet::ClientIntentionPacket;
mod serverbound_status_request_packet;
pub use serverbound_status_request_packet::ServerboundStatusRequestPacket;
use crate::mc_buf;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ConnectionProtocol {
Handshaking = -1,

View file

@ -1,9 +1,10 @@
use crate::{
connection::Connection,
mc_buf,
packets::{ClientIntentionPacket, ConnectionProtocol, ServerboundStatusRequestPacket},
resolver, ServerAddress,
};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::io::AsyncReadExt;
pub async fn ping_server(address: &ServerAddress) -> Result<(), String> {
let resolved_address = resolver::resolve_address(&address).await?;
@ -22,6 +23,9 @@ pub async fn ping_server(address: &ServerAddress) -> Result<(), String> {
.await;
conn.send_packet(&ServerboundStatusRequestPacket {}).await;
let data = mc_buf::read_varint(conn.stream);
println!("data {}", data);
// log what the server sends back
loop {
if 0 == conn.stream.read_buf(&mut conn.buffer).await.unwrap() {