Compare commits

..

No commits in common. "41694648bd67383a62ede69c6616dd67c131cd8e" and "40e8f0f7a92c6d08a30e36139b9485363c397b8d" have entirely different histories.

2 changed files with 11 additions and 50 deletions

View file

@ -15,7 +15,7 @@ use std::{
use clap::Parser;
use faker::Faker;
use packet::{rw::read_packet, Packet, CLIENT_NAME_SIZE};
use packet::{rw::read_packet, Packet};
use player::PlayerActor;
use server::{web_main, Manager};
use tokio::{
@ -25,11 +25,10 @@ use tokio::{
use tracing::{error, info, Level};
use tracing_subscriber::{filter::FilterFn, layer::SubscriberExt, util::SubscriberInitExt, Layer};
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());
pub fn clients() -> &'static RwLock<HashMap<u128, (PacketChannel, SocketAddr)>> {
static CLIENTS: LazyLock<RwLock<HashMap<u128, (PacketChannel, SocketAddr)>>> = LazyLock::new(|| RwLock::default());
&CLIENTS
}
@ -85,7 +84,6 @@ async fn main() {
1,
(
MessageChannel::new(address),
protocol::String::new_zeroed(),
"0.0.0.0:0".to_socket_addrs().unwrap().next().unwrap(),
),
);

View file

@ -21,7 +21,7 @@ use crate::{
};
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 {
let _ = address.send(packet.clone()).detach().await;
}
@ -41,8 +41,7 @@ pub async fn broadcast_packet(packet: Packet) {
}
PacketData::Disconnect(..) => {
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) => {
let _ = manager()
@ -147,10 +146,10 @@ impl PlayerActor {
trace!("now connected on udp at {}", SocketAddr::new(ip, port));
addr = Some(SocketAddr::new(ip, port));
// let mut buf = [0; 256];
// let mut writer = Cursor::new(buf.as_mut_slice());
// write_packet(&mut writer, user_id, PacketData::HolePunch(HolePunch)).await.unwrap();
// socket.send_to(&buf, addr.as_ref().unwrap()).await.expect("kys");
let mut buf = [0; 256];
let mut writer = Cursor::new(buf.as_mut_slice());
write_packet(&mut writer, user_id, PacketData::HolePunch(HolePunch)).await.unwrap();
socket.send_to(&buf, addr.as_ref().unwrap()).await.expect("kys");
}
}
}
@ -183,7 +182,7 @@ 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), addr));
xtra::run(
mailbox,
@ -210,22 +209,11 @@ impl Actor for PlayerActor {
udp: false,
data: PacketData::Connect(Connect {
kind: self.connection_kind,
max_player: 10,
max_player: 8,
client_name: self.name,
}),
})
.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
.write_sender
.send(WriteMessage::Data(Packet {
@ -267,36 +255,11 @@ impl Handler<Packet> for PlayerActor {
match packet.data {
PacketData::HolePunch(_) => {
info!("got hole punch!");
return;
}
PacketData::UdpInit(UdpInit { port }) => {
info!("got udp init {port}!");
let _ = self.write_sender.send(WriteMessage::SetUdp(port));
let _ = self
.write_sender
.send(WriteMessage::Data(Packet {
user_id: 0,
udp: false,
data: PacketData::HolePunch(HolePunch),
}))
.map_err(drop);
let _ = self
.write_sender
.send(WriteMessage::Data(Packet {
user_id: 0,
udp: false,
data: PacketData::HolePunch(HolePunch),
}))
.map_err(drop);
let _ = self
.write_sender
.send(WriteMessage::Data(Packet {
user_id: 0,
udp: false,
data: PacketData::HolePunch(HolePunch),
}))
.map_err(drop);
return;
}
PacketData::Connect(..) | PacketData::Init(..) => {