aboutsummaryrefslogtreecommitdiff
path: root/minecraft-protocol
diff options
context:
space:
mode:
authormat <github@matdoes.dev>2021-12-07 22:12:16 +0000
committermat <github@matdoes.dev>2021-12-07 22:12:16 +0000
commit4a44c58444c901d939a8594669c819ab2bfbac13 (patch)
tree5085824e8af8bb9209f7defa178f1010fc29e436 /minecraft-protocol
parent9c14b3f32346f071ad12faf86105f5fd3ce03959 (diff)
downloadazalea-drasl-4a44c58444c901d939a8594669c819ab2bfbac13.tar.xz
keep doing packet stuff
Diffstat (limited to 'minecraft-protocol')
-rw-r--r--minecraft-protocol/src/connection.rs8
-rw-r--r--minecraft-protocol/src/lib.rs2
-rw-r--r--minecraft-protocol/src/packets/client_intention_packet.rs2
-rw-r--r--minecraft-protocol/src/packets/clientbound_status_response_packet.rs21
-rw-r--r--minecraft-protocol/src/packets/mod.rs4
-rw-r--r--minecraft-protocol/src/packets/serverbound_status_request_packet.rs1
-rw-r--r--minecraft-protocol/src/server_status.rs6
-rw-r--r--minecraft-protocol/src/server_status_pinger.rs4
8 files changed, 45 insertions, 3 deletions
diff --git a/minecraft-protocol/src/connection.rs b/minecraft-protocol/src/connection.rs
index ee03b5e5..a162bb6b 100644
--- a/minecraft-protocol/src/connection.rs
+++ b/minecraft-protocol/src/connection.rs
@@ -1,4 +1,4 @@
-//! Handle sending and receiving packets with a server.
+//! parse sending and receiving packets with a server.
use crate::packets::ConnectionProtocol;
use crate::{mc_buf, packets::Packet, ServerIpAddress};
@@ -41,6 +41,10 @@ impl Connection {
})
}
+ pub fn switch_state(&mut self, state: ConnectionProtocol) {
+ self.state = state;
+ }
+
pub async fn read_packet(&mut self) -> Result<(), String> {
// what this does:
// 1. reads the first 5 bytes, probably only some of this will be used to get the packet length
@@ -68,7 +72,7 @@ impl Connection {
}
/// Write a packet to the server
- pub async fn send_packet(&mut self, packet: &dyn Packet) {
+ pub async fn send_packet(&mut self, packet: &impl Packet) {
// TODO: implement compression
// packet structure:
diff --git a/minecraft-protocol/src/lib.rs b/minecraft-protocol/src/lib.rs
index 0e2b1c45..88b3603f 100644
--- a/minecraft-protocol/src/lib.rs
+++ b/minecraft-protocol/src/lib.rs
@@ -1,3 +1,5 @@
+//! This lib is responsible for parsing Minecraft packets.
+
use std::net::IpAddr;
use std::str::FromStr;
diff --git a/minecraft-protocol/src/packets/client_intention_packet.rs b/minecraft-protocol/src/packets/client_intention_packet.rs
index 90574038..a35e65dc 100644
--- a/minecraft-protocol/src/packets/client_intention_packet.rs
+++ b/minecraft-protocol/src/packets/client_intention_packet.rs
@@ -26,4 +26,6 @@ impl<'a> Packet for ClientIntentionPacket<'a> {
mc_buf::write_short(buf, self.port);
mc_buf::write_varint(buf, self.intention.clone() as u32);
}
+
+ fn parse<T: tokio::io::AsyncRead + std::marker::Unpin>(&self, buf: T) -> () {}
}
diff --git a/minecraft-protocol/src/packets/clientbound_status_response_packet.rs b/minecraft-protocol/src/packets/clientbound_status_response_packet.rs
new file mode 100644
index 00000000..8baa2a4d
--- /dev/null
+++ b/minecraft-protocol/src/packets/clientbound_status_response_packet.rs
@@ -0,0 +1,21 @@
+use std::hash::Hash;
+
+use super::Packet;
+
+#[derive(Hash)]
+pub struct ServerboundStatusRequestPacket {
+ status: ServerStatus,
+}
+
+// implement "Packet" for "ClientIntentionPacket"
+impl Packet for ServerboundStatusRequestPacket {
+ fn get_id(&self) -> u32 {
+ 0x00
+ }
+
+ // implement "from_reader" for "ClientIntentionPacket"
+ fn write(&self, _buf: &mut Vec<u8>) {}
+ fn parse<T: tokio::io::AsyncRead + std::marker::Unpin>(&self, buf: T) -> () {
+ // this.status = GsonHelper.fromJson(GSON, friendlyByteBuf.readUtf(32767), ServerStatus.class);
+ }
+}
diff --git a/minecraft-protocol/src/packets/mod.rs b/minecraft-protocol/src/packets/mod.rs
index 6a053124..8d943be0 100644
--- a/minecraft-protocol/src/packets/mod.rs
+++ b/minecraft-protocol/src/packets/mod.rs
@@ -2,6 +2,7 @@ mod client_intention_packet;
pub use client_intention_packet::ClientIntentionPacket;
mod serverbound_status_request_packet;
pub use serverbound_status_request_packet::ServerboundStatusRequestPacket;
+use tokio::io::AsyncRead;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ConnectionProtocol {
@@ -15,5 +16,6 @@ pub trait Packet {
/// Get the id of the packet, this is always a byte.
fn get_id(&self) -> u32;
- fn write(&self, friendly_byte_buf: &mut Vec<u8>) -> ();
+ fn write(&self, buf: &mut Vec<u8>) -> ();
+ fn parse<T: AsyncRead + std::marker::Unpin>(&self, buf: T) -> ();
}
diff --git a/minecraft-protocol/src/packets/serverbound_status_request_packet.rs b/minecraft-protocol/src/packets/serverbound_status_request_packet.rs
index 94b7bbee..ab9001f4 100644
--- a/minecraft-protocol/src/packets/serverbound_status_request_packet.rs
+++ b/minecraft-protocol/src/packets/serverbound_status_request_packet.rs
@@ -13,4 +13,5 @@ impl Packet for ServerboundStatusRequestPacket {
// implement "from_reader" for "ClientIntentionPacket"
fn write(&self, _buf: &mut Vec<u8>) {}
+ fn parse<T: tokio::io::AsyncRead + std::marker::Unpin>(&self, buf: T) -> () {}
}
diff --git a/minecraft-protocol/src/server_status.rs b/minecraft-protocol/src/server_status.rs
new file mode 100644
index 00000000..93480aaf
--- /dev/null
+++ b/minecraft-protocol/src/server_status.rs
@@ -0,0 +1,6 @@
+struct ServerStatus {
+ description: Component,
+ players: Players,
+ version: Version,
+ favicon: String,
+}
diff --git a/minecraft-protocol/src/server_status_pinger.rs b/minecraft-protocol/src/server_status_pinger.rs
index df53b897..342c4f44 100644
--- a/minecraft-protocol/src/server_status_pinger.rs
+++ b/minecraft-protocol/src/server_status_pinger.rs
@@ -13,6 +13,7 @@ pub async fn ping_server(address: &ServerAddress) -> Result<(), String> {
println!("resolved_address {}", &resolved_address.ip);
println!("writing intention packet {}", address.host);
+ // send the client intention packet and switch to the status state
conn.send_packet(&ClientIntentionPacket {
protocol_version: 757,
hostname: &address.host,
@@ -20,6 +21,9 @@ pub async fn ping_server(address: &ServerAddress) -> Result<(), String> {
intention: ConnectionProtocol::Status,
})
.await;
+ conn.switch_state(ConnectionProtocol::Status);
+
+ // send the empty status request packet
conn.send_packet(&ServerboundStatusRequestPacket {}).await;
conn.read_packet().await.unwrap();