Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/plugin/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
2 changes: 1 addition & 1 deletion crates/plugin/src/component_updates/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
20 changes: 18 additions & 2 deletions crates/plugin/src/disconnect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
),
);
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -106,9 +113,16 @@ fn handle_internal_disconnect_message(
mut message_writer: MessageWriter<ToClients<DespawnNetEntities>>,
mut client_disconnect_message_writer: MessageWriter<ToClients<ClientDisconnected>>,
mut alive_checks: ResMut<AliveChecks>,
mut connected_clients: ResMut<ConnectedClients>,
socket_addr_to_peer_id: Res<SocketAddrToPeerId>,
) {
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);

Expand Down Expand Up @@ -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,
});
}
Expand Down
2 changes: 1 addition & 1 deletion crates/plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down
2 changes: 1 addition & 1 deletion crates/plugin/src/net_entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion crates/plugin/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion crates/plugin/src/network_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion crates/plugin/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
File renamed without changes.
Loading