Compare commits

...

2 commits

Author SHA1 Message Date
Aubrey 41694648bd
broadcast connects on connect lol 2025-01-04 02:43:14 -06:00
Aubrey d97a2a81c3
hole punch harder fuck you 2025-01-04 02:33:12 -06:00
2 changed files with 50 additions and 11 deletions

View file

@ -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(),
), ),
); );

View file

@ -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()
@ -146,10 +147,10 @@ impl PlayerActor {
trace!("now connected on udp at {}", SocketAddr::new(ip, port)); trace!("now connected on udp at {}", SocketAddr::new(ip, port));
addr = Some(SocketAddr::new(ip, port)); addr = Some(SocketAddr::new(ip, port));
let mut buf = [0; 256]; // let mut buf = [0; 256];
let mut writer = Cursor::new(buf.as_mut_slice()); // let mut writer = Cursor::new(buf.as_mut_slice());
write_packet(&mut writer, user_id, PacketData::HolePunch(HolePunch)).await.unwrap(); // write_packet(&mut writer, user_id, PacketData::HolePunch(HolePunch)).await.unwrap();
socket.send_to(&buf, addr.as_ref().unwrap()).await.expect("kys"); // socket.send_to(&buf, addr.as_ref().unwrap()).await.expect("kys");
} }
} }
} }
@ -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,11 +267,36 @@ 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 }) => {
info!("got udp init {port}!"); info!("got udp init {port}!");
let _ = self.write_sender.send(WriteMessage::SetUdp(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; return;
} }
PacketData::Connect(..) | PacketData::Init(..) => { PacketData::Connect(..) | PacketData::Init(..) => {