-
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added packet data types Correctly parse a handshake packet Add tokio to pumpkin protocol for `AsyncReadExt` convenience functions
- Loading branch information
1 parent
14c8c7e
commit 813ef02
Showing
7 changed files
with
148 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use serde_inline_default::serde_inline_default; | ||
|
||
#[serde_inline_default] | ||
#[derive(Deserialize, Serialize)] | ||
pub struct QueryConfig { | ||
#[serde_inline_default(false)] | ||
pub enabled: bool, | ||
// Optional so if not specified the port server is running on will be used | ||
#[serde_inline_default(None)] | ||
pub port: Option<u16>, | ||
} | ||
|
||
impl Default for QueryConfig { | ||
fn default() -> Self { | ||
Self { | ||
enabled: false, | ||
port: None, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
use tokio::io::AsyncReadExt; | ||
|
||
#[derive(Debug)] | ||
pub enum PacketType { | ||
Handshake = 9, | ||
Stat = 0, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct SBasePacket { | ||
pub magic: u16, | ||
pub packet_type: PacketType, | ||
pub session_id: i32, | ||
pub payload: SBasePayload, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum SBasePayload { | ||
Handshake, | ||
BasicInfo(i32), | ||
FullInfo(i32), | ||
} | ||
|
||
impl SBasePacket { | ||
pub async fn decode(mut reader: impl AsyncReadExt + Unpin) -> Self { | ||
let magic = reader.read_u16().await.unwrap(); | ||
match reader.read_u8().await.unwrap() { | ||
0 => todo!(), | ||
// Handshake | ||
9 => Self { | ||
magic, | ||
packet_type: PacketType::Handshake, | ||
session_id: reader.read_i32().await.unwrap(), | ||
payload: SBasePayload::Handshake, | ||
}, | ||
_ => todo!(), | ||
} | ||
} | ||
} | ||
|
||
pub struct CBasePacket { | ||
pub packet_type: PacketType, | ||
pub session_id: i32, | ||
pub payload: CBasePayload, | ||
} | ||
|
||
pub enum CBasePayload { | ||
Handshake { | ||
// For simplicity use a number type | ||
// Should be encoded as string here | ||
// Will be converted in encoding | ||
challange_token: i32 | ||
}, | ||
BasicInfo { | ||
// Use CString as protocol requires nul terminated strings | ||
motd: String, | ||
gametype: String, | ||
map: String, | ||
num_players: String, | ||
max_players: String, | ||
host_port: u16, | ||
host_ip: String, | ||
}, | ||
FullInfo { | ||
hostname: String, | ||
// Game type and game id are hardcoded into protocol | ||
// They are not here as they cannot be changed | ||
version: String, | ||
plugins: String, | ||
map: String, | ||
num_players: u16, | ||
max_players: u16, | ||
host_port: u16, | ||
host_ip: String, | ||
players: Vec<String>, | ||
} | ||
} | ||
|
||
impl CBasePacket { | ||
fn encode(&self) -> Vec<u8> { | ||
// let buf = Vec::new(); | ||
|
||
match &self.payload { | ||
CBasePayload::Handshake { challange_token } => todo!(), | ||
CBasePayload::BasicInfo { motd, gametype, map, num_players, max_players, host_port, host_ip } => todo!(), | ||
CBasePayload::FullInfo { hostname, version, plugins, map, num_players, max_players, host_port, host_ip, players } => todo!(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Query protocol | ||
|
||
use std::io::Cursor; | ||
|
||
use pumpkin_protocol::query::SBasePacket; | ||
use tokio::net::UdpSocket; | ||
|
||
pub async fn start_query_handler() { | ||
let socket = UdpSocket::bind("0.0.0.0:25565").await.expect("Unable to bind to address"); | ||
log::info!("Query socket created"); | ||
|
||
loop { | ||
let mut buf= vec![0; 1024]; | ||
log::info!("Waiting for requests"); | ||
let (len, addr) = socket.recv_from(&mut buf).await.unwrap(); | ||
|
||
tokio::spawn(async move { | ||
let cursor = Cursor::new(buf); | ||
let packet = SBasePacket::decode(cursor).await; | ||
|
||
println!("{:#?}", packet); | ||
|
||
}); | ||
} | ||
} |