Skip to content

Commit

Permalink
Seperate connection from client
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas0008 committed Aug 18, 2024
1 parent 3e1d103 commit 375e80f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 32 deletions.
31 changes: 31 additions & 0 deletions pumpkin/src/client/connection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use pumpkin_protocol::{
packet_decoder::PacketDecoder, packet_encoder::PacketEncoder, ClientPacket, PacketError,
};
use tokio::{io::AsyncWriteExt, net::TcpStream};

pub struct Connection {
pub client: TcpStream,
pub enc: PacketEncoder,
pub dec: PacketDecoder,
}

impl Connection {
pub fn new(client: TcpStream) -> Self {
Self {
client,
enc: PacketEncoder::default(),
dec: PacketDecoder::default(),
}
}
pub async fn try_send_packet<P: ClientPacket>(
&mut self,
packet: &P,
) -> Result<(), PacketError> {
self.enc.append_packet(packet)?;
self.client
.write_all(&self.enc.take())
.await
.map_err(|_| PacketError::ConnectionWrite)?;
Ok(())
}
}
50 changes: 22 additions & 28 deletions pumpkin/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::{

use authentication::GameProfile;
use bytes::BytesMut;
use connection::Connection;
use num_traits::ToPrimitive;
use pumpkin_protocol::{
bytebuf::packet_id::Packet,
Expand Down Expand Up @@ -49,6 +50,7 @@ use thiserror::Error;

pub mod authentication;
mod client_packet;
pub mod connection;
pub mod player_packet;

pub struct PlayerConfig {
Expand All @@ -75,15 +77,13 @@ pub struct Client {
pub encrytion: bool,
pub closed: bool,
pub token: u32,
pub connection: TcpStream,
pub connection: Connection,
pub address: SocketAddr,
enc: PacketEncoder,
dec: PacketDecoder,
pub client_packets_queue: VecDeque<RawPacket>,
}

impl Client {
pub fn new(token: u32, connection: TcpStream, address: SocketAddr) -> Self {
pub fn new(token: u32, client: TcpStream, address: SocketAddr) -> Self {
Self {
protocol_version: 0,
gameprofile: None,
Expand All @@ -93,9 +93,7 @@ impl Client {
address,
player: None,
connection_state: ConnectionState::HandShake,
connection,
enc: PacketEncoder::default(),
dec: PacketDecoder::default(),
connection: Connection::new(client),
encrytion: true,
closed: false,
client_packets_queue: VecDeque::new(),
Expand All @@ -116,15 +114,17 @@ impl Client {
let crypt_key: [u8; 16] = shared_secret
.try_into()
.map_err(|_| EncryptionError::SharedWrongLength)?;
self.dec.enable_encryption(&crypt_key);
self.enc.enable_encryption(&crypt_key);
self.connection.dec.enable_encryption(&crypt_key);
self.connection.enc.enable_encryption(&crypt_key);
Ok(())
}

// Compression threshold, Compression level
pub fn set_compression(&mut self, compression: Option<(u32, u32)>) {
self.dec.set_compression(compression.map(|v| v.0));
self.enc.set_compression(compression);
self.connection
.dec
.set_compression(compression.map(|v| v.0));
self.connection.enc.set_compression(compression);
}

pub fn is_player(&self) -> bool {
Expand All @@ -133,26 +133,14 @@ impl Client {

/// Send a Clientbound Packet to the Client
pub async fn send_packet<P: ClientPacket>(&mut self, packet: &P) {
match self.try_send_packet(packet).await {
match self.connection.try_send_packet(packet).await {
Ok(_) => {}
Err(e) => {
self.kick(&e.to_string()).await;
}
};
}

pub async fn try_send_packet<P: ClientPacket>(
&mut self,
packet: &P,
) -> Result<(), PacketError> {
self.enc.append_packet(packet)?;
self.connection
.write_all(&self.enc.take())
.await
.map_err(|_| PacketError::ConnectionWrite)?;
Ok(())
}

pub async fn teleport(&mut self, x: f64, y: f64, z: f64, yaw: f32, pitch: f32) {
assert!(self.is_player());
// TODO
Expand Down Expand Up @@ -371,14 +359,14 @@ impl Client {
let mut buf = BytesMut::new();
loop {
select! {
result = self.connection.read_buf(&mut buf) => {
result = self.connection.client.read_buf(&mut buf) => {
match result {
Ok(0) => {
self.close();
break;
}
Ok(_) => {
self.dec.queue_bytes(buf.split());
self.connection.dec.queue_bytes(buf.split());
}
Err(e) => {
log::error!("{}", e);
Expand All @@ -387,7 +375,7 @@ impl Client {
}
};
loop {
match self.dec.decode() {
match self.connection.dec.decode() {
Ok(Some(packet)) => {
self.add_packet(packet);
let mut server = server.write().await;
Expand Down Expand Up @@ -420,6 +408,7 @@ impl Client {
match self.connection_state {
ConnectionState::Login => {
match self
.connection
.try_send_packet(&CLoginDisconnect::new(
&serde_json::to_string_pretty(&reason).unwrap(),
))
Expand All @@ -430,13 +419,18 @@ impl Client {
}
}
ConnectionState::Config => {
match self.try_send_packet(&CConfigDisconnect::new(reason)).await {
match self
.connection
.try_send_packet(&CConfigDisconnect::new(reason))
.await
{
Ok(_) => {}
Err(_) => self.close(),
}
}
ConnectionState::Play => {
match self
.connection
.try_send_packet(&CPlayDisconnect::new(TextComponent::from(reason)))
.await
{
Expand Down
10 changes: 6 additions & 4 deletions pumpkin/src/client/player_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,12 @@ impl Client {
let packet = &CHurtAnimation::new(&entity_id, 10.0);
self.send_packet(packet).await;
client.send_packet(packet).await;
server.broadcast_packet_expect(
&[self.token, token],
&CHurtAnimation::new(&entity_id, 10.0),
).await
server
.broadcast_packet_expect(
&[self.token, token],
&CHurtAnimation::new(&entity_id, 10.0),
)
.await
}
} else {
self.kick("Interacted with invalid entitiy id").await
Expand Down

0 comments on commit 375e80f

Please sign in to comment.