Skip to content

Commit

Permalink
Merge pull request #23 from lukas0008/master
Browse files Browse the repository at this point in the history
Tokio + reqwest async
  • Loading branch information
Snowiiii authored Aug 7, 2024
2 parents 0223943 + d41ee1a commit 054ba4c
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 32 deletions.
26 changes: 14 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ edition = "2021"

[profile.release]
lto = true

[workspace.dependencies]
tokio = { version = "1.39.2", features = ["net", "macros", "rt-multi-thread", "fs", "io-util"] }
3 changes: 2 additions & 1 deletion pumpkin-world/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ edition.workspace = true
pumpkin-protocol = { path = "../pumpkin-protocol"}
fastanvil = "0.31"
fastnbt = { git = "https://github.com/owengage/fastnbt.git" }
fastsnbt = "0.2"
fastsnbt = "0.2"
tokio.workspace = true
5 changes: 2 additions & 3 deletions pumpkin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ rsa-der = "0.3.0"
flate2 = "1.0.30"

# authentication
reqwest = { version = "0.12.5", features = ["json", "blocking"]}
reqwest = { version = "0.12.5", features = ["json"]}

sha1 = "0.10.6"
digest = "=0.11.0-pre.9"
Expand All @@ -49,5 +49,4 @@ crossbeam-channel = "0.5.13"

uuid = { version = "1.10", features = ["serde"]}



tokio.workspace = true
5 changes: 3 additions & 2 deletions pumpkin/src/client/authentication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub struct GameProfile {
pub profile_actions: Option<Vec<ProfileAction>>,
}

pub fn authenticate(
pub async fn authenticate(
username: &str,
server_hash: &str,
ip: &IpAddr,
Expand All @@ -69,13 +69,14 @@ pub fn authenticate(
.unwrap()
.get(address)
.send()
.await
.map_err(|_| AuthError::FailedResponse)?;
match response.status() {
StatusCode::OK => {}
StatusCode::NO_CONTENT => Err(AuthError::UnverifiedUsername)?,
other => Err(AuthError::UnknownStatusCode(other.as_str().to_string()))?,
}
let profile: GameProfile = response.json().map_err(|_| AuthError::FailedParse)?;
let profile: GameProfile = response.json().await.map_err(|_| AuthError::FailedParse)?;
Ok(profile)
}

Expand Down
6 changes: 4 additions & 2 deletions pumpkin/src/client/client_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl Client {
self.send_packet(packet);
}

pub fn handle_encryption_response(
pub async fn handle_encryption_response(
&mut self,
server: &mut Server,
encryption_response: SEncryptionResponse,
Expand All @@ -96,7 +96,9 @@ impl Client {
&hash,
&ip,
server,
) {
)
.await
{
Ok(p) => {
// Check if player should join
if let Some(p) = &p.profile_actions {
Expand Down
12 changes: 6 additions & 6 deletions pumpkin/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,17 +156,17 @@ impl Client {
self.send_packet(CGameEvent::new(3, gamemode.to_f32().unwrap()));
}

pub fn process_packets(&mut self, server: &mut Server) {
pub async fn process_packets(&mut self, server: &mut Server) {
let mut i = 0;
while i < self.client_packets_queue.len() {
let mut packet = self.client_packets_queue.remove(i).unwrap();
self.handle_packet(server, &mut packet);
self.handle_packet(server, &mut packet).await;
i += 1;
}
}

/// Handles an incoming decoded Packet
pub fn handle_packet(&mut self, server: &mut Server, packet: &mut RawPacket) {
pub async fn handle_packet(&mut self, server: &mut Server, packet: &mut RawPacket) {
// TODO: handle each packet's Error instead of calling .unwrap()
let bytebuf = &mut packet.bytebuf;
match self.connection_state {
Expand Down Expand Up @@ -198,7 +198,7 @@ impl Client {
SEncryptionResponse::PACKET_ID => self.handle_encryption_response(
server,
SEncryptionResponse::read(bytebuf).unwrap(),
),
).await,
SLoginPluginResponse::PACKET_ID => self
.handle_plugin_response(server, SLoginPluginResponse::read(bytebuf).unwrap()),
SLoginAcknowledged::PACKET_ID => self
Expand Down Expand Up @@ -267,7 +267,7 @@ impl Client {

// Reads the connection until our buffer of len 4096 is full, then decode
/// Close connection when an error occurs
pub fn poll(&mut self, server: &mut Server, event: &Event) {
pub async fn poll(&mut self, server: &mut Server, event: &Event) {
if event.is_readable() {
let mut received_data = vec![0; 4096];
let mut bytes_read = 0;
Expand Down Expand Up @@ -302,7 +302,7 @@ impl Client {
Ok(packet) => {
if let Some(packet) = packet {
self.add_packet(packet);
self.process_packets(server);
self.process_packets(server).await;
}
}
Err(err) => self.kick(&err.to_string()),
Expand Down
5 changes: 3 additions & 2 deletions pumpkin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ pub mod server;
pub mod util;

#[cfg(not(target_os = "wasi"))]
fn main() -> io::Result<()> {
#[tokio::main]
async fn main() -> io::Result<()> {
use std::{cell::RefCell, time::Instant};

let time = Instant::now();
Expand Down Expand Up @@ -130,7 +131,7 @@ fn main() -> io::Result<()> {
// Maybe received an event for a TCP connection.
let done = if let Some(client) = connections.get_mut(&token) {
let mut client = client.borrow_mut();
client.poll(&mut server, event);
client.poll(&mut server, event).await;
client.closed
} else {
// Sporadic events happen, we can safely ignore them.
Expand Down
8 changes: 4 additions & 4 deletions pumpkin/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub struct Server {
pub advanced_config: AdvancedConfiguration,

/// Used for Authentication, None is Online mode is disabled
pub auth_client: Option<reqwest::blocking::Client>,
pub auth_client: Option<reqwest::Client>,
}

impl Server {
Expand All @@ -75,7 +75,7 @@ impl Server {
)
.into_boxed_slice();
let auth_client = if config.0.online_mode {
Some(reqwest::blocking::Client::new())
Some(reqwest::Client::new())
} else {
None
};
Expand All @@ -99,9 +99,9 @@ impl Server {
}

// Returns Tokens to remove
pub fn poll(&mut self, client: &mut Client, _poll: &Poll, event: &Event) {
pub async fn poll(&mut self, client: &mut Client, _poll: &Poll, event: &Event) {
// TODO: Poll players in every world
client.poll(self, event)
client.poll(self, event).await
}

pub fn add_client(&mut self, token: Rc<Token>, client: Rc<RefCell<Client>>) {
Expand Down

0 comments on commit 054ba4c

Please sign in to comment.