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, manager,
packet::{ packet::{
Packet, PacketData, Packet, PacketData,
PacketData_variants::{Connect, Player}, PacketData_variants::{Connect, Player}, CLIENT_NAME_SIZE,
}, },
player::broadcast_packet, player::{broadcast_packet, VerifyConnected},
protocol::String, protocol::String,
server::PlayerConnected, server::PlayerConnected,
}; };
@ -34,6 +34,13 @@ impl Actor for Faker {
async fn stopped(self) -> Self::Stop {} 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 { impl Handler<Packet> for Faker {
type Return = (); type Return = ();

View file

@ -16,7 +16,7 @@ use std::{
use clap::Parser; use clap::Parser;
use faker::Faker; use faker::Faker;
use packet::{rw::read_packet, Packet, CLIENT_NAME_SIZE}; use packet::{rw::read_packet, Packet, CLIENT_NAME_SIZE};
use player::PlayerActor; use player::{PlayerActor, VerifyConnected};
use server::{web_main, Manager}; use server::{web_main, Manager};
use tokio::{ use tokio::{
net::{TcpListener, UdpSocket}, net::{TcpListener, UdpSocket},
@ -28,8 +28,9 @@ use xtra::{prelude::MessageChannel, Address, Mailbox};
use zerocopy::FromZeros; use zerocopy::FromZeros;
type PacketChannel = MessageChannel<Packet, (), xtra::refcount::Strong>; type PacketChannel = MessageChannel<Packet, (), xtra::refcount::Strong>;
pub fn clients() -> &'static RwLock<HashMap<u128, (PacketChannel, protocol::String<CLIENT_NAME_SIZE>, SocketAddr)>> { type ConnectedChannel = MessageChannel<VerifyConnected, protocol::String<CLIENT_NAME_SIZE>, xtra::refcount::Strong>;
static CLIENTS: LazyLock<RwLock<HashMap<u128, (PacketChannel, protocol::String<CLIENT_NAME_SIZE>, SocketAddr)>>> = LazyLock::new(|| RwLock::default()); pub fn clients() -> &'static RwLock<HashMap<u128, (PacketChannel, ConnectedChannel, SocketAddr)>> {
static CLIENTS: LazyLock<RwLock<HashMap<u128, (PacketChannel, ConnectedChannel, SocketAddr)>>> = LazyLock::new(|| RwLock::default());
&CLIENTS &CLIENTS
} }
@ -84,8 +85,8 @@ async fn main() {
clients().write().await.insert( clients().write().await.insert(
1, 1,
( (
MessageChannel::new(address.clone()),
MessageChannel::new(address), MessageChannel::new(address),
protocol::String::new_zeroed(),
"0.0.0.0:0".to_socket_addrs().unwrap().next().unwrap(), "0.0.0.0:0".to_socket_addrs().unwrap().next().unwrap(),
), ),
); );

View file

@ -183,7 +183,10 @@ impl PlayerActor {
.instrument(span.clone()), .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( xtra::run(
mailbox, mailbox,
@ -215,17 +218,21 @@ impl Actor for PlayerActor {
}), }),
}) })
.await; .await;
for (user_id, b) in clients().read().await.iter() { 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 { let _ = self.write_sender.send(WriteMessage::Data(Packet {
user_id: *user_id, user_id: *user_id,
udp: false, udp: false,
data: PacketData::Connect(Connect { data: PacketData::Connect(Connect {
kind: self.connection_kind, kind: self.connection_kind,
max_player: 10, max_player: 10,
client_name: b.1, client_name: name,
}), }),
})); }));
} }
}
}
self self
.write_sender .write_sender
.send(WriteMessage::Data(Packet { .send(WriteMessage::Data(Packet {
@ -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 { impl Handler<Packet> for PlayerActor {
type Return = (); type Return = ();