broadcast connects on connect lol
This commit is contained in:
parent
d97a2a81c3
commit
41694648bd
|
@ -15,7 +15,7 @@ use std::{
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use faker::Faker;
|
use faker::Faker;
|
||||||
use packet::{rw::read_packet, Packet};
|
use packet::{rw::read_packet, Packet, CLIENT_NAME_SIZE};
|
||||||
use player::PlayerActor;
|
use player::PlayerActor;
|
||||||
use server::{web_main, Manager};
|
use server::{web_main, Manager};
|
||||||
use tokio::{
|
use tokio::{
|
||||||
|
@ -25,10 +25,11 @@ use tokio::{
|
||||||
use tracing::{error, info, Level};
|
use tracing::{error, info, Level};
|
||||||
use tracing_subscriber::{filter::FilterFn, layer::SubscriberExt, util::SubscriberInitExt, Layer};
|
use tracing_subscriber::{filter::FilterFn, layer::SubscriberExt, util::SubscriberInitExt, Layer};
|
||||||
use xtra::{prelude::MessageChannel, Address, Mailbox};
|
use xtra::{prelude::MessageChannel, Address, Mailbox};
|
||||||
|
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, SocketAddr)>> {
|
pub fn clients() -> &'static RwLock<HashMap<u128, (PacketChannel, protocol::String<CLIENT_NAME_SIZE>, SocketAddr)>> {
|
||||||
static CLIENTS: LazyLock<RwLock<HashMap<u128, (PacketChannel, SocketAddr)>>> = LazyLock::new(|| RwLock::default());
|
static CLIENTS: LazyLock<RwLock<HashMap<u128, (PacketChannel, protocol::String<CLIENT_NAME_SIZE>, SocketAddr)>>> = LazyLock::new(|| RwLock::default());
|
||||||
|
|
||||||
&CLIENTS
|
&CLIENTS
|
||||||
}
|
}
|
||||||
|
@ -84,6 +85,7 @@ async fn main() {
|
||||||
1,
|
1,
|
||||||
(
|
(
|
||||||
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(),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
|
@ -21,7 +21,7 @@ use crate::{
|
||||||
};
|
};
|
||||||
|
|
||||||
pub async fn broadcast_packet(packet: Packet) {
|
pub async fn broadcast_packet(packet: Packet) {
|
||||||
for (id, (address, _)) in clients().read().await.iter() {
|
for (id, (address, _, _)) in clients().read().await.iter() {
|
||||||
if *id != packet.user_id {
|
if *id != packet.user_id {
|
||||||
let _ = address.send(packet.clone()).detach().await;
|
let _ = address.send(packet.clone()).detach().await;
|
||||||
}
|
}
|
||||||
|
@ -41,7 +41,8 @@ pub async fn broadcast_packet(packet: Packet) {
|
||||||
}
|
}
|
||||||
PacketData::Disconnect(..) => {
|
PacketData::Disconnect(..) => {
|
||||||
if packet.user_id != 1 {
|
if packet.user_id != 1 {
|
||||||
manager().send(PlayerDisconnected { id: packet.user_id }).await.unwrap();}
|
manager().send(PlayerDisconnected { id: packet.user_id }).await.unwrap();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
PacketData::Player(player) => {
|
PacketData::Player(player) => {
|
||||||
let _ = manager()
|
let _ = manager()
|
||||||
|
@ -182,7 +183,7 @@ impl PlayerActor {
|
||||||
.instrument(span.clone()),
|
.instrument(span.clone()),
|
||||||
));
|
));
|
||||||
|
|
||||||
clients().write().await.insert(user_id, (MessageChannel::new(address), addr));
|
clients().write().await.insert(user_id, (MessageChannel::new(address), connect.client_name, addr));
|
||||||
|
|
||||||
xtra::run(
|
xtra::run(
|
||||||
mailbox,
|
mailbox,
|
||||||
|
@ -209,11 +210,22 @@ impl Actor for PlayerActor {
|
||||||
udp: false,
|
udp: false,
|
||||||
data: PacketData::Connect(Connect {
|
data: PacketData::Connect(Connect {
|
||||||
kind: self.connection_kind,
|
kind: self.connection_kind,
|
||||||
max_player: 8,
|
max_player: 10,
|
||||||
client_name: self.name,
|
client_name: self.name,
|
||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
.await;
|
.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,
|
||||||
|
}),
|
||||||
|
}));
|
||||||
|
}
|
||||||
self
|
self
|
||||||
.write_sender
|
.write_sender
|
||||||
.send(WriteMessage::Data(Packet {
|
.send(WriteMessage::Data(Packet {
|
||||||
|
@ -255,6 +267,7 @@ impl Handler<Packet> for PlayerActor {
|
||||||
match packet.data {
|
match packet.data {
|
||||||
PacketData::HolePunch(_) => {
|
PacketData::HolePunch(_) => {
|
||||||
info!("got hole punch!");
|
info!("got hole punch!");
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
PacketData::UdpInit(UdpInit { port }) => {
|
PacketData::UdpInit(UdpInit { port }) => {
|
||||||
|
|
Loading…
Reference in a new issue