fix connect packet logic

This commit is contained in:
Aubrey 2025-01-06 12:34:32 -06:00
parent 7864967435
commit 790a794b84
No known key found for this signature in database
3 changed files with 40 additions and 17 deletions

View file

@ -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<VerifyConnected> for Faker {
type Return = String<CLIENT_NAME_SIZE>;
async fn handle(&mut self, _: VerifyConnected, _: &mut xtra::Context<Self>) -> Self::Return {
"Bot".try_into().unwrap()
}
}
impl Handler<Packet> for Faker {
type Return = ();

View file

@ -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<Packet, (), xtra::refcount::Strong>;
pub fn clients() -> &'static RwLock<HashMap<u128, (PacketChannel, protocol::String<CLIENT_NAME_SIZE>, SocketAddr)>> {
static CLIENTS: LazyLock<RwLock<HashMap<u128, (PacketChannel, protocol::String<CLIENT_NAME_SIZE>, SocketAddr)>>> = LazyLock::new(|| RwLock::default());
type ConnectedChannel = MessageChannel<VerifyConnected, protocol::String<CLIENT_NAME_SIZE>, xtra::refcount::Strong>;
pub fn clients() -> &'static RwLock<HashMap<u128, (PacketChannel, ConnectedChannel, SocketAddr)>> {
static CLIENTS: LazyLock<RwLock<HashMap<u128, (PacketChannel, ConnectedChannel, SocketAddr)>>> = 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(),
),
);

View file

@ -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<StopError> for PlayerActor {
}
}
pub struct VerifyConnected;
impl Handler<VerifyConnected> for PlayerActor {
type Return = String<CLIENT_NAME_SIZE>;
async fn handle(&mut self, _: VerifyConnected, _: &mut xtra::Context<Self>) -> Self::Return {
self.name
}
}
impl Handler<Packet> for PlayerActor {
type Return = ();