From 790a794b845e26203fad53fcb28ad6cfeef95a68 Mon Sep 17 00:00:00 2001 From: Aubrey Taylor Date: Mon, 6 Jan 2025 12:34:32 -0600 Subject: [PATCH] fix connect packet logic --- src/faker.rs | 11 +++++++++-- src/main.rs | 9 +++++---- src/player.rs | 37 ++++++++++++++++++++++++++----------- 3 files changed, 40 insertions(+), 17 deletions(-) diff --git a/src/faker.rs b/src/faker.rs index 65f5dd9..ae2c655 100644 --- a/src/faker.rs +++ b/src/faker.rs @@ -5,9 +5,9 @@ use crate::{ manager, packet::{ Packet, PacketData, - PacketData_variants::{Connect, Player}, + PacketData_variants::{Connect, Player}, CLIENT_NAME_SIZE, }, - player::broadcast_packet, + player::{broadcast_packet, VerifyConnected}, protocol::String, server::PlayerConnected, }; @@ -34,6 +34,13 @@ impl Actor for Faker { async fn stopped(self) -> Self::Stop {} } +impl Handler for Faker { + type Return = String; + async fn handle(&mut self, _: VerifyConnected, _: &mut xtra::Context) -> Self::Return { + "Bot".try_into().unwrap() + } +} + impl Handler for Faker { type Return = (); diff --git a/src/main.rs b/src/main.rs index 312c616..56bffcf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,7 @@ use std::{ use clap::Parser; use faker::Faker; use packet::{rw::read_packet, Packet, CLIENT_NAME_SIZE}; -use player::PlayerActor; +use player::{PlayerActor, VerifyConnected}; use server::{web_main, Manager}; use tokio::{ net::{TcpListener, UdpSocket}, @@ -28,8 +28,9 @@ use xtra::{prelude::MessageChannel, Address, Mailbox}; use zerocopy::FromZeros; type PacketChannel = MessageChannel; -pub fn clients() -> &'static RwLock, SocketAddr)>> { - static CLIENTS: LazyLock, SocketAddr)>>> = LazyLock::new(|| RwLock::default()); +type ConnectedChannel = MessageChannel, xtra::refcount::Strong>; +pub fn clients() -> &'static RwLock> { + static CLIENTS: LazyLock>> = LazyLock::new(|| RwLock::default()); &CLIENTS } @@ -84,8 +85,8 @@ async fn main() { clients().write().await.insert( 1, ( + MessageChannel::new(address.clone()), MessageChannel::new(address), - protocol::String::new_zeroed(), "0.0.0.0:0".to_socket_addrs().unwrap().next().unwrap(), ), ); diff --git a/src/player.rs b/src/player.rs index 1f47fdb..3812173 100644 --- a/src/player.rs +++ b/src/player.rs @@ -183,7 +183,10 @@ impl PlayerActor { .instrument(span.clone()), )); - clients().write().await.insert(user_id, (MessageChannel::new(address), connect.client_name, addr)); + clients().write().await.insert( + user_id, + (MessageChannel::new(address.clone()), MessageChannel::new(address), addr), + ); xtra::run( mailbox, @@ -215,16 +218,20 @@ impl Actor for PlayerActor { }), }) .await; - for (user_id, b) in clients().read().await.iter() { - let _ = self.write_sender.send(WriteMessage::Data(Packet { - user_id: *user_id, - udp: false, - data: PacketData::Connect(Connect { - kind: self.connection_kind, - max_player: 10, - client_name: b.1, - }), - })); + for (user_id, (_, connect_chan, _)) in clients().read().await.iter() { + if *user_id != self.id { + if let Ok(name) = connect_chan.send(VerifyConnected).await { + let _ = self.write_sender.send(WriteMessage::Data(Packet { + user_id: *user_id, + udp: false, + data: PacketData::Connect(Connect { + kind: self.connection_kind, + max_player: 10, + client_name: name, + }), + })); + } + } } self .write_sender @@ -255,6 +262,14 @@ impl Handler for PlayerActor { } } +pub struct VerifyConnected; +impl Handler for PlayerActor { + type Return = String; + async fn handle(&mut self, _: VerifyConnected, _: &mut xtra::Context) -> Self::Return { + self.name + } +} + impl Handler for PlayerActor { type Return = ();