diff --git a/crates/plugin/src/client.rs b/crates/plugin/src/client.rs index aab6f7d..6170f27 100644 --- a/crates/plugin/src/client.rs +++ b/crates/plugin/src/client.rs @@ -8,7 +8,7 @@ use crate::{ }, network::connect_to_server, network_messages::{NetworkMessageId, NetworkMessageRegistry}, - util::{ + utils::{ DatagramType, get_byte_header_for_datagram_type, get_datagram_type, parse_u32_from_u8_arr, receive_all_packets_from_socket, }, diff --git a/crates/plugin/src/component_updates/mod.rs b/crates/plugin/src/component_updates/mod.rs index 4fb5dc4..e1e4c1f 100644 --- a/crates/plugin/src/component_updates/mod.rs +++ b/crates/plugin/src/component_updates/mod.rs @@ -11,7 +11,7 @@ use crate::{ get_or_create_mut_update_sequence_number, net_entity::NetEntityId, server::ConnectedClients, - util::{ + utils::{ DatagramType, get_byte_header_for_datagram_type, parse_u32_from_u8_arr, should_log_component_update, }, diff --git a/crates/plugin/src/disconnect.rs b/crates/plugin/src/disconnect.rs index 2f86e49..a6d0e99 100644 --- a/crates/plugin/src/disconnect.rs +++ b/crates/plugin/src/disconnect.rs @@ -2,13 +2,15 @@ use bevy::prelude::*; use serde::{Deserialize, Serialize}; use crate::{ - ClientSocket, OurPeerId, Owner, PeerId, + ClientSocket, NetvyMode, OurPeerId, Owner, PeerId, alive_check::AliveChecks, net_entity::NetEntityId, network_messages::{ AppNetworkMessageExt, FromClient, FromServer, MessageDirection, NetworkMessageTarget, ToClients, ToServer, }, + server::{ConnectedClients, SocketAddrToPeerId}, + utils::reverse_hash_map_lookup, }; pub mod prelude { @@ -27,7 +29,10 @@ impl Plugin for DisconnectPlugin { FixedUpdate, ( read_despawn_net_entities_messages, - handle_internal_disconnect_message, + handle_internal_disconnect_message.run_if( + resource_equals(NetvyMode::Server) + .or_else(resource_equals(NetvyMode::HostClient)), + ), handle_client_disconnected_message, ), ); @@ -70,6 +75,8 @@ fn read_despawn_net_entities_messages( } } +/// A client can trigger the `Disconnect` event. netvy will handle this event and send this +/// `InternalDisconnectMessage` network message to the server. #[derive(Message, Serialize, Deserialize)] struct InternalDisconnectMessage; @@ -106,9 +113,16 @@ fn handle_internal_disconnect_message( mut message_writer: MessageWriter>, mut client_disconnect_message_writer: MessageWriter>, mut alive_checks: ResMut, + mut connected_clients: ResMut, + socket_addr_to_peer_id: Res, ) { for message in message_reader.read() { let peer_id_client = message.source_client; + let socket_addr = reverse_hash_map_lookup(&socket_addr_to_peer_id.0, peer_id_client) + .expect("Invariant violation: A PeerId must always have a SocketAddr."); + let index = connected_clients.0.iter().position(|s| *s == socket_addr).expect("Invariant violation: A SocketAddr contained in SocketAddrToPeerId must also be contained in ConnectedClients."); + + connected_clients.0.swap_remove(index); alive_checks.0.remove(&peer_id_client); @@ -143,6 +157,8 @@ fn handle_internal_disconnect_message( // Also send this message to the disconnected client wait that makes no sense it doesnt // receive the message if its disconnected? oh it does, we actually need it so we know // when to close the client UDP socket. + // TODO: right now we dont actually close the UDP socket on the client. i think we are + // missing a bunch of cleanup for disconnected clients. target: NetworkMessageTarget::All, }); } diff --git a/crates/plugin/src/lib.rs b/crates/plugin/src/lib.rs index 12b5cca..5b7bca0 100644 --- a/crates/plugin/src/lib.rs +++ b/crates/plugin/src/lib.rs @@ -27,7 +27,7 @@ mod network; mod network_messages; mod server; mod sync_position; -mod util; +mod utils; pub mod prelude { pub use crate::client::prelude::*; diff --git a/crates/plugin/src/net_entity.rs b/crates/plugin/src/net_entity.rs index ac2fab8..f554674 100644 --- a/crates/plugin/src/net_entity.rs +++ b/crates/plugin/src/net_entity.rs @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize}; use crate::{ ClientSocket, - util::{DatagramType, get_byte_header_for_datagram_type}, + utils::{DatagramType, get_byte_header_for_datagram_type}, }; /// A NetEntityId identifies an replicated entity across clients and servers. diff --git a/crates/plugin/src/network.rs b/crates/plugin/src/network.rs index f8fa42e..0195c44 100644 --- a/crates/plugin/src/network.rs +++ b/crates/plugin/src/network.rs @@ -2,7 +2,7 @@ use std::net::{SocketAddr, UdpSocket}; use bevy::prelude::*; -use crate::util::bind_socket_local; +use crate::utils::bind_socket_local; /// Creates a UdpSocket and connects to the given server /// This function does not ensure succesful connection diff --git a/crates/plugin/src/network_messages.rs b/crates/plugin/src/network_messages.rs index 8544159..a1e01f3 100644 --- a/crates/plugin/src/network_messages.rs +++ b/crates/plugin/src/network_messages.rs @@ -7,7 +7,7 @@ use serde::{Serialize, de::DeserializeOwned}; use crate::{ BINCODE_CONFIG, ClientSocket, NetvyMode, PeerId, ServerSocket, server::{ConnectedClients, SocketAddrToPeerId}, - util::{DatagramType, get_byte_header_for_datagram_type, reverse_hash_map_lookup}, + utils::{DatagramType, get_byte_header_for_datagram_type, reverse_hash_map_lookup}, }; pub mod prelude { diff --git a/crates/plugin/src/server.rs b/crates/plugin/src/server.rs index cfff89b..e543444 100644 --- a/crates/plugin/src/server.rs +++ b/crates/plugin/src/server.rs @@ -11,7 +11,7 @@ use crate::{ }, net_entity::NetEntityId, network_messages::{MessageDirection, NetworkMessageId, NetworkMessageRegistry}, - util::{ + utils::{ DatagramType, bind_socket_local, get_byte_header_for_datagram_type, get_datagram_type, parse_u32_from_u8_arr, receive_all_packets_from_socket, }, diff --git a/crates/plugin/src/util.rs b/crates/plugin/src/utils.rs similarity index 100% rename from crates/plugin/src/util.rs rename to crates/plugin/src/utils.rs