From 71e6ca35c79abe112ba93738d45e525d5d70fbd4 Mon Sep 17 00:00:00 2001 From: Aubrey Taylor Date: Thu, 2 Jan 2025 19:03:58 -0600 Subject: [PATCH] add global speak flag --- src/player.rs | 27 ++++++++++++++++----------- src/server/prox.rs | 33 +++++++++++++++++++++++---------- 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/src/player.rs b/src/player.rs index 6efaa19..cd23456 100644 --- a/src/player.rs +++ b/src/player.rs @@ -29,13 +29,15 @@ pub async fn broadcast_packet(packet: Packet) { match packet.data { PacketData::Connect(connect) => { - manager() - .send(PlayerConnected { - id: packet.user_id, - name: connect.client_name, - }) - .await - .unwrap(); + if packet.user_id != 1 { + manager() + .send(PlayerConnected { + id: packet.user_id, + name: connect.client_name, + }) + .await + .unwrap(); + } } PacketData::Disconnect(..) => { manager().send(PlayerDisconnected { id: packet.user_id }).await.unwrap(); @@ -95,7 +97,7 @@ impl PlayerActor { return; }; - write_packet(&mut writer, user_id, PacketData::Init(Init { max_players: 8 })) + write_packet(&mut writer, user_id, PacketData::Init(Init { max_players: 10 })) .await .expect("msfrarausfhsdagsdgkog"); @@ -107,19 +109,18 @@ impl PlayerActor { tokio::spawn(scoped(&address.downgrade(), { let address = address.downgrade(); async move { + // sending packets to the web app or setting up udp let ip = addr.ip(); let mut addr = None; while let Some(message) = receiver.recv().await { match message { WriteMessage::Data(Packet { user_id, udp, data }) => { - // trace!("writing packet {udp:?}, {data:?}"); let res = if udp && addr.is_some() { async { let Some(addr) = addr else { trace!("no address set yet"); return Ok(()); }; - // trace!("sending udp packet: {data:?}"); let mut buf = [0; 256]; let mut writer = Cursor::new(buf.as_mut_slice()); @@ -143,7 +144,7 @@ impl PlayerActor { WriteMessage::SetUdp(port) => { trace!("now connected on udp!"); 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(); @@ -154,9 +155,11 @@ impl PlayerActor { } .instrument(span.clone()) })); + tokio::spawn(scoped( &address.downgrade(), { + // read packets and send them to the actor let address = address.downgrade(); async move { info!("connected {user_id}"); @@ -177,7 +180,9 @@ impl PlayerActor { } .instrument(span.clone()), )); + clients().write().await.insert(user_id, (MessageChannel::new(address), addr)); + xtra::run( mailbox, PlayerActor { diff --git a/src/server/prox.rs b/src/server/prox.rs index b0be8a7..f7d1093 100644 --- a/src/server/prox.rs +++ b/src/server/prox.rs @@ -4,7 +4,7 @@ use packet::{ Event, Event_variants::{Answer, Candidate, Offer}, }; -use tokio::io::AsyncWriteExt; +use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tracing::{error, info, info_span, trace, warn, Instrument}; use wtransport::endpoint::IncomingSession; use xtra::{Actor, Address, Handler, Mailbox}; @@ -19,6 +19,7 @@ use super::{ pub struct ProximityPlayer { id: UuidString, name: String, + global_speak: bool, send: wtransport::SendStream, connection: Arc, } @@ -37,6 +38,7 @@ impl ProximityPlayer { recv.read_exact(id.as_mut_bytes()).await.expect("failed to read uuid"); let mut name = String::new_zeroed(); recv.read_exact(name.as_mut_bytes()).await.expect("failed to read name"); + let global_speak = recv.read_u8().await.expect("failed to read global speak state") != 0; let span = info_span!("", %id); span.in_scope(|| trace!("uuid parsed")); info!(parent: &span, "connected as {name}"); @@ -53,9 +55,10 @@ impl ProximityPlayer { let listeners = listeners().read().await; send.write_u32_le(listeners.len() as u32).await.expect("failed to write peer length"); for (id, listener) in listeners.iter() { - if let Ok(name) = listener.send(GetName).await { + if let Ok((name, global_speak)) = listener.send(GetStartInfo).await { send.write_all(id.as_bytes()).await.expect("failed to write peer id"); send.write_all(name.as_bytes()).await.expect("failed to write peer name"); + send.write_all(&[if global_speak { 1 } else { 0 }]).await.expect("failed to write peer global speaking status"); } } } @@ -94,6 +97,7 @@ impl ProximityPlayer { ProximityPlayer { id, name, + global_speak, send, connection, }, @@ -116,16 +120,18 @@ impl Actor for ProximityPlayer { id: FromZeros::new_zeroed(), connected: true, name: FromZeros::new_zeroed(), + global_speak: false }, }; for (id, listener) in listeners().write().await.iter() { if *id != self.id { - if let Ok(Ok(name)) = listener + if let Ok(Ok((name, global_speak))) = listener .send(PeerConnectionChanged { id: self.id, connected: true, name: self.name, + global_speak: self.global_speak, }) .await { @@ -133,6 +139,7 @@ impl Actor for ProximityPlayer { data: packet::PeerConnectionChanged { id: *id, name, + global_speak, ..changed_event.data }, ..changed_event @@ -156,6 +163,7 @@ impl Actor for ProximityPlayer { id: self.id, connected: false, name: self.name, + global_speak: false, }) .detach() .await; @@ -174,12 +182,12 @@ impl Handler for ProximityPlayer { } } -struct GetName; -impl Handler for ProximityPlayer { - type Return = String; +struct GetStartInfo; +impl Handler for ProximityPlayer { + type Return = (String, bool); - async fn handle(&mut self, _: GetName, _: &mut xtra::Context) -> Self::Return { - self.name + async fn handle(&mut self, _: GetStartInfo, _: &mut xtra::Context) -> Self::Return { + (self.name, self.global_speak) } } @@ -274,9 +282,10 @@ struct PeerConnectionChanged { id: UuidString, connected: bool, name: String, + global_speak: bool, } impl Handler for ProximityPlayer { - type Return = Result, ()>; + type Return = Result<(String, bool), ()>; async fn handle(&mut self, message: PeerConnectionChanged, ctx: &mut xtra::Context) -> Self::Return { let event = packet::Packet { @@ -285,6 +294,7 @@ impl Handler for ProximityPlayer { id: message.id, connected: message.connected, name: message.name, + global_speak: message.global_speak }, }; @@ -294,7 +304,7 @@ impl Handler for ProximityPlayer { return Err(()); } - Ok(self.name) + Ok((self.name, self.global_speak)) } } @@ -443,6 +453,7 @@ pub mod packet { pub id: UuidString, pub connected: bool, pub name: String, + pub global_speak: bool, } #[newtype_enum] @@ -462,6 +473,7 @@ pub mod packet { }, TargetChanged { pub name: String, + pub global_speak: bool, }, } @@ -524,6 +536,7 @@ pub mod packet { }), 3 => Self::TargetChanged(TargetChanged { name: read_fixed_string(recv).await?, + global_speak: recv.read_u8().await? != 0 }), kind => bail!("invalid kind: {kind}"), };