Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Networking Refactor Pt. 1 #74

Merged
merged 6 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pumpkin-core/src/math/vector3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ impl<T> From<(T, T, T)> for Vector3<T> {
}
}

impl<T> From<Vector3<T>> for (T, T, T) {
fn from(vector: Vector3<T>) -> Self {
(vector.x, vector.y, vector.z)
}
}

pub trait Math:
Mul<Output = Self>
+ Neg<Output = Self>
Expand Down
5 changes: 2 additions & 3 deletions pumpkin/src/client/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{
io::{self, Write},
net::SocketAddr,
sync::Arc,
};

use crate::{
Expand Down Expand Up @@ -71,7 +70,7 @@ pub struct Client {
pub connection_state: ConnectionState,
pub encryption: bool,
pub closed: bool,
pub token: Arc<Token>,
pub token: Token,
pub connection: TcpStream,
pub address: SocketAddr,
enc: PacketEncoder,
Expand All @@ -82,7 +81,7 @@ pub struct Client {
}

impl Client {
pub fn new(token: Arc<Token>, connection: TcpStream, address: SocketAddr) -> Self {
pub fn new(token: Token, connection: TcpStream, address: SocketAddr) -> Self {
Self {
protocol_version: 0,
gameprofile: None,
Expand Down
48 changes: 21 additions & 27 deletions pumpkin/src/client/player_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ impl Player {
return;
}
let entity = &mut self.entity;
self.lastx = entity.pos.x;
self.lasty = entity.pos.y;
self.lastz = entity.pos.z;
self.last_position = entity.pos;
entity.set_pos(
Self::clamp_horizontal(position.x),
Self::clamp_vertical(position.feet_y),
Expand All @@ -89,13 +87,12 @@ impl Player {
// send new position to all other players
let on_ground = self.on_ground;
let entity_id = entity.entity_id;
let (x, lastx) = (entity.pos.x, self.lastx);
let (y, lasty) = (entity.pos.y, self.lasty);
let (z, lastz) = (entity.pos.z, self.lastz);
let (x, y, z) = entity.pos.into();
let (lastx, lasty, lastz) = self.last_position.into();
let world = self.world.clone();
let world = world.lock().await;
world.broadcast_packet(
&[&self.client.token],
&[self.client.token],
&CUpdateEntityPos::new(
entity_id.into(),
(x * 4096.0 - lastx * 4096.0) as i16,
Expand Down Expand Up @@ -125,9 +122,7 @@ impl Player {
}
let entity = &mut self.entity;

self.lastx = entity.pos.x;
self.lasty = entity.pos.y;
self.lastz = entity.pos.z;
self.last_position = entity.pos;
entity.set_pos(
Self::clamp_horizontal(position_rotation.x),
Self::clamp_vertical(position_rotation.feet_y),
Expand All @@ -139,17 +134,16 @@ impl Player {
// send new position to all other players
let on_ground = self.on_ground;
let entity_id = entity.entity_id;
let (x, lastx) = (entity.pos.x, self.lastx);
let (y, lasty) = (entity.pos.y, self.lasty);
let (z, lastz) = (entity.pos.z, self.lastz);
let (x, y, z) = entity.pos.into();
let (lastx, lasty, lastz) = self.last_position.into();
let yaw = modulus(entity.yaw * 256.0 / 360.0, 256.0);
let pitch = modulus(entity.pitch * 256.0 / 360.0, 256.0);
// let head_yaw = (entity.head_yaw * 256.0 / 360.0).floor();
let world = self.world.clone();
let world = world.lock().await;

world.broadcast_packet(
&[&self.client.token],
&[self.client.token],
&CUpdateEntityPosRot::new(
entity_id.into(),
(x * 4096.0 - lastx * 4096.0) as i16,
Expand All @@ -161,7 +155,7 @@ impl Player {
),
);
world.broadcast_packet(
&[&self.client.token],
&[self.client.token],
&CHeadRot::new(entity_id.into(), yaw as u8),
);

Expand All @@ -186,10 +180,10 @@ impl Player {
let world = self.world.lock().await;
let packet = CUpdateEntityRot::new(entity_id.into(), yaw as u8, pitch as u8, on_ground);
// self.client.send_packet(&packet);
world.broadcast_packet(&[&self.client.token], &packet);
world.broadcast_packet(&[self.client.token], &packet);
let packet = CHeadRot::new(entity_id.into(), yaw as u8);
// self.client.send_packet(&packet);
world.broadcast_packet(&[&self.client.token], &packet);
world.broadcast_packet(&[self.client.token], &packet);
}

pub fn handle_chat_command(&mut self, server: &mut Server, command: SChatCommand) {
Expand Down Expand Up @@ -248,7 +242,7 @@ impl Player {
let id = self.entity_id();
let world = self.world.lock().await;
world.broadcast_packet(
&[&self.client.token],
&[self.client.token],
&CEntityAnimation::new(id.into(), animation as u8),
)
}
Expand All @@ -272,7 +266,7 @@ impl Player {

let world = self.world.lock().await;
world.broadcast_packet(
&[&self.client.token],
&[self.client.token],
&CPlayerChatMessage::new(
pumpkin_protocol::uuid::UUID(gameprofile.id),
1.into(),
Expand Down Expand Up @@ -340,7 +334,7 @@ impl Player {
let world = world.lock().await;
let attacked_player = world.get_by_entityid(self, entity_id.0 as EntityId);
if let Some(mut player) = attacked_player {
let token = player.client.token.clone();
let token = player.client.token;
let velo = player.velocity;
if config.protect_creative && player.gamemode == GameMode::Creative {
return;
Expand Down Expand Up @@ -371,7 +365,7 @@ impl Player {
self.client.send_packet(packet);
player.client.send_packet(packet);
world.broadcast_packet(
&[&self.client.token, &token],
&[self.client.token, token],
&CHurtAnimation::new(&entity_id, 10.0),
)
}
Expand Down Expand Up @@ -411,12 +405,12 @@ impl Player {
// TODO: currently this is always dirt replace it
let world = self.world.lock().await;
world.broadcast_packet(
&[&self.client.token],
&[self.client.token],
&CWorldEvent::new(2001, &location, 11, false),
);
// AIR
world.broadcast_packet(
&[&self.client.token],
&[self.client.token],
&CBlockUpdate::new(&location, 0.into()),
);
}
Expand All @@ -439,12 +433,12 @@ impl Player {
// TODO: currently this is always dirt replace it
let world = self.world.lock().await;
world.broadcast_packet(
&[&self.client.token],
&[self.client.token],
&CWorldEvent::new(2001, &location, 11, false),
);
// AIR
world.broadcast_packet(
&[&self.client.token],
&[self.client.token],
&CBlockUpdate::new(&location, 0.into()),
);
// TODO: Send this every tick
Expand Down Expand Up @@ -491,11 +485,11 @@ impl Player {
if let Ok(block_state_id) = BlockId::new(minecraft_id, None) {
let world = self.world.lock().await;
world.broadcast_packet(
&[&self.client.token],
&[self.client.token],
&CBlockUpdate::new(&location, block_state_id.get_id_mojang_repr().into()),
);
world.broadcast_packet(
&[&self.client.token],
&[self.client.token],
&CBlockUpdate::new(
&WorldPosition(location.0 + face.to_offset()),
block_state_id.get_id_mojang_repr().into(),
Expand Down
18 changes: 6 additions & 12 deletions pumpkin/src/entity/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ pub struct Player {
/// send `send_abilties_update` when changed
pub abilities: PlayerAbilities,

pub lastx: f64,
pub lasty: f64,
pub lastz: f64,
pub last_position: Vector3<f64>,
// Client side value, Should be not trusted
pub on_ground: bool,

Expand Down Expand Up @@ -132,9 +130,7 @@ impl Player {
abilities: PlayerAbilities::default(),
gamemode,
watched_section: Vector3::new(0, 0, 0),
lastx: 0.0,
lasty: 0.0,
lastz: 0.0,
last_position: Vector3::new(0.0, 0.0, 0.0),
}
}

Expand Down Expand Up @@ -230,7 +226,7 @@ impl Player {
self.world
.lock()
.await
.broadcast_packet(&[&self.client.token], &packet);
.broadcast_packet(&[self.client.token], &packet);
}

pub async fn set_pose(&mut self, pose: EntityPose) {
Expand All @@ -244,7 +240,7 @@ impl Player {
self.world
.lock()
.await
.broadcast_packet(&[&self.client.token], &packet)
.broadcast_packet(&[self.client.token], &packet)
}

pub fn teleport(&mut self, x: f64, y: f64, z: f64, yaw: f32, pitch: f32) {
Expand All @@ -255,9 +251,7 @@ impl Player {
}
let entity = &mut self.entity;
entity.set_pos(x, y, z);
self.lastx = x;
self.lasty = y;
self.lastz = z;
self.last_position = entity.pos;
entity.yaw = yaw;
entity.pitch = pitch;
self.awaiting_teleport = Some((self.teleport_id_count.into(), Vector3::new(x, y, z)));
Expand Down Expand Up @@ -326,7 +320,7 @@ impl Player {
}],
));
self.world.lock().await.broadcast_packet(
&[&self.client.token],
&[self.client.token],
&CPlayerInfoUpdate::new(
0x04,
&[pumpkin_protocol::client::play::Player {
Expand Down
31 changes: 13 additions & 18 deletions pumpkin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ fn main() -> io::Result<()> {
let rcon = ADVANCED_CONFIG.rcon.clone();

let mut clients: HashMap<Token, Client> = HashMap::new();
let mut players: HashMap<Arc<Token>, Arc<Mutex<Player>>> = HashMap::new();
let mut players: HashMap<Token, Arc<Mutex<Player>>> = HashMap::new();

let server = Arc::new(tokio::sync::Mutex::new(Server::new()));
log::info!("Started Server took {}ms", time.elapsed().as_millis());
Expand Down Expand Up @@ -150,32 +150,28 @@ fn main() -> io::Result<()> {
token,
Interest::READABLE.add(Interest::WRITABLE),
)?;
let rc_token = Arc::new(token);
let client = Client::new(Arc::clone(&rc_token), connection, addr);
let client = Client::new(token, connection, addr);
clients.insert(token, client);
},

token => {
// Poll Players
let done = if let Some(player) = players.get_mut(&token) {
if let Some(player) = players.get_mut(&token) {
let mut player = player.lock().unwrap();
player.client.poll(event).await;
if !player.client.closed {
let mut server = server.lock().await;
player.process_packets(&mut server).await;
}
player.client.closed
} else {
false
};

if done {
if let Some(player) = players.remove(&token) {
let mut player = player.lock().unwrap();
player.remove().await;
poll.registry().deregister(&mut player.client.connection)?;
if player.client.closed {
drop(player);
if let Some(player) = players.remove(&token) {
let mut player = player.lock().unwrap();
player.remove().await;
poll.registry().deregister(&mut player.client.connection)?;
}
}
}
};

// Poll current Clients (non players)
// Maybe received an event for a TCP connection.
Expand All @@ -195,10 +191,9 @@ fn main() -> io::Result<()> {
if done {
poll.registry().deregister(&mut client.connection)?;
} else if make_player {
let token = client.token.clone();
let token = client.token;
let mut server = server.lock().await;
let (player, world) =
server.add_player(token.clone(), client).await;
let (player, world) = server.add_player(token, client).await;
players.insert(token, player.clone());
let mut world = world.lock().await;
world.spawn_player(&BASIC_CONFIG, player).await;
Expand Down
6 changes: 3 additions & 3 deletions pumpkin/src/server.rs → pumpkin/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl Server {

pub async fn add_player(
&mut self,
token: Arc<Token>,
token: Token,
client: Client,
) -> (Arc<Mutex<Player>>, Arc<tokio::sync::Mutex<World>>) {
let entity_id = self.new_entity_id();
Expand All @@ -129,12 +129,12 @@ impl Server {
}

/// Sends a Packet to all Players in all worlds
pub fn broadcast_packet_all<P>(&self, expect: &[&Arc<Token>], packet: &P)
pub fn broadcast_packet_all<P>(&self, except: &[Token], packet: &P)
where
P: ClientPacket,
{
for world in &self.worlds {
world.blocking_lock().broadcast_packet(expect, packet)
world.blocking_lock().broadcast_packet(except, packet)
}
}

Expand Down
Loading