Compare commits
2 commits
40e8f0f7a9
...
41694648bd
Author | SHA1 | Date | |
---|---|---|---|
|
41694648bd | ||
|
d97a2a81c3 |
|
@ -15,7 +15,7 @@ use std::{
|
|||
|
||||
use clap::Parser;
|
||||
use faker::Faker;
|
||||
use packet::{rw::read_packet, Packet};
|
||||
use packet::{rw::read_packet, Packet, CLIENT_NAME_SIZE};
|
||||
use player::PlayerActor;
|
||||
use server::{web_main, Manager};
|
||||
use tokio::{
|
||||
|
@ -25,10 +25,11 @@ 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, SocketAddr)>> {
|
||||
static CLIENTS: LazyLock<RwLock<HashMap<u128, (PacketChannel, SocketAddr)>>> = LazyLock::new(|| RwLock::default());
|
||||
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());
|
||||
|
||||
&CLIENTS
|
||||
}
|
||||
|
@ -84,6 +85,7 @@ async fn main() {
|
|||
1,
|
||||
(
|
||||
MessageChannel::new(address),
|
||||
protocol::String::new_zeroed(),
|
||||
"0.0.0.0:0".to_socket_addrs().unwrap().next().unwrap(),
|
||||
),
|
||||
);
|
||||
|
|
|
@ -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,7 +41,8 @@ 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()
|
||||
|
@ -146,10 +147,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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -182,7 +183,7 @@ impl PlayerActor {
|
|||
.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(
|
||||
mailbox,
|
||||
|
@ -209,11 +210,22 @@ impl Actor for PlayerActor {
|
|||
udp: false,
|
||||
data: PacketData::Connect(Connect {
|
||||
kind: self.connection_kind,
|
||||
max_player: 8,
|
||||
max_player: 10,
|
||||
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 {
|
||||
|
@ -255,11 +267,36 @@ 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(..) => {
|
||||
|
|
Loading…
Reference in a new issue