Skip to content

Commit

Permalink
Rename ConnectedClients into ReplicatedClients
Browse files Browse the repository at this point in the history
  • Loading branch information
Shatur committed Aug 9, 2024
1 parent 573de5b commit c0cc6b7
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 108 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- `ServerEventAppExt::make_independent` to let events be triggered without waiting for replication on the same tick.

### Changed

- Rename `connected_clients` into `replicated_clients`.
- Rename `ConnectedClients` to `ReplicatedClients`.
- Rename `ConnectedClient` to `ReplicatedClient`.

## [0.27.0] - 2024-07-04

### Changed
Expand Down
2 changes: 1 addition & 1 deletion src/core.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
pub mod channels;
pub mod command_markers;
pub mod common_conditions;
pub mod connected_clients;
pub mod ctx;
pub mod event_registry;
pub mod replicated_clients;
pub mod replication_registry;
pub mod replication_rules;
pub mod replicon_client;
Expand Down
22 changes: 11 additions & 11 deletions src/core/event_registry/server_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use serde::{de::DeserializeOwned, Serialize};
use super::EventRegistry;
use crate::core::{
channels::{RepliconChannel, RepliconChannels},
connected_clients::{ConnectedClient, ConnectedClients},
ctx::{ClientReceiveCtx, ServerSendCtx},
replicated_clients::{ReplicatedClient, ReplicatedClients},
replicon_client::RepliconClient,
replicon_server::RepliconServer,
replicon_tick::RepliconTick,
Expand Down Expand Up @@ -304,9 +304,9 @@ impl ServerEvent {
ctx: &mut ServerSendCtx,
server_events: &Ptr,
server: &mut RepliconServer,
connected_clients: &ConnectedClients,
replicated_clients: &ReplicatedClients,
) {
(self.send)(self, ctx, server_events, server, connected_clients);
(self.send)(self, ctx, server_events, server, replicated_clients);
}

/// Receives an event from the server.
Expand Down Expand Up @@ -398,7 +398,7 @@ pub type DeserializeFn<E> = fn(&mut ClientReceiveCtx, &mut Cursor<&[u8]>) -> bin

/// Signature of server event sending functions.
type SendFn =
unsafe fn(&ServerEvent, &mut ServerSendCtx, &Ptr, &mut RepliconServer, &ConnectedClients);
unsafe fn(&ServerEvent, &mut ServerSendCtx, &Ptr, &mut RepliconServer, &ReplicatedClients);

/// Signature of server event receiving functions.
type ReceiveFn = unsafe fn(
Expand Down Expand Up @@ -427,14 +427,14 @@ unsafe fn send<E: Event>(
ctx: &mut ServerSendCtx,
server_events: &Ptr,
server: &mut RepliconServer,
connected_clients: &ConnectedClients,
replicated_clients: &ReplicatedClients,
) {
let events: &Events<ToClients<E>> = server_events.deref();
// For server events we don't track read events because
// all of them will always be drained in the local resending system.
for ToClients { event, mode } in events.get_reader().read(events) {
trace!("sending event `{}` with `{mode:?}`", any::type_name::<E>());
send_with(event_data, ctx, event, mode, server, connected_clients)
send_with(event_data, ctx, event, mode, server, replicated_clients)
.expect("server event should be serializable");
}
}
Expand Down Expand Up @@ -539,20 +539,20 @@ unsafe fn send_with<E: Event>(
event: &E,
mode: &SendMode,
server: &mut RepliconServer,
connected_clients: &ConnectedClients,
replicated_clients: &ReplicatedClients,
) -> bincode::Result<()> {
match *mode {
SendMode::Broadcast => {
let mut previous_message = None;
for client in connected_clients.iter() {
for client in replicated_clients.iter() {
let message = serialize_with(event_data, ctx, event, client, previous_message)?;
server.send(client.id(), event_data.channel_id, message.bytes.clone());
previous_message = Some(message);
}
}
SendMode::BroadcastExcept(client_id) => {
let mut previous_message = None;
for client in connected_clients.iter() {
for client in replicated_clients.iter() {
if client.id() == client_id {
continue;
}
Expand All @@ -563,7 +563,7 @@ unsafe fn send_with<E: Event>(
}
SendMode::Direct(client_id) => {
if client_id != ClientId::SERVER {
if let Some(client) = connected_clients.get_client(client_id) {
if let Some(client) = replicated_clients.get_client(client_id) {
let message = serialize_with(event_data, ctx, event, client, None)?;
server.send(client.id(), event_data.channel_id, message.bytes);
}
Expand All @@ -586,7 +586,7 @@ unsafe fn serialize_with<E: Event>(
event_data: &ServerEvent,
ctx: &mut ServerSendCtx,
event: &E,
client: &ConnectedClient,
client: &ReplicatedClient,
previous_message: Option<SerializedMessage>,
) -> bincode::Result<SerializedMessage> {
if let Some(previous_message) = previous_message {
Expand Down
43 changes: 22 additions & 21 deletions src/core/connected_clients.rs → src/core/replicated_clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ use bevy::{
};

use crate::core::{replicon_tick::RepliconTick, ClientId};

use client_visibility::ClientVisibility;

/// Stores information about connected clients.
/// Stores information about connected clients which are enabled for replication.
///
/// Inserted as resource by [`ServerPlugin`](crate::server::ServerPlugin).
#[derive(Resource, Default)]
pub struct ConnectedClients {
clients: Vec<ConnectedClient>,
pub struct ReplicatedClients {
clients: Vec<ReplicatedClient>,
policy: VisibilityPolicy,
}

impl ConnectedClients {
impl ReplicatedClients {
pub(crate) fn new(policy: VisibilityPolicy) -> Self {
Self {
clients: Default::default(),
Expand All @@ -41,7 +42,7 @@ impl ConnectedClients {
/// # Panics
///
/// Panics if the passed client ID is not connected.
pub fn client(&self, client_id: ClientId) -> &ConnectedClient {
pub fn client(&self, client_id: ClientId) -> &ReplicatedClient {
self.get_client(client_id)
.unwrap_or_else(|| panic!("{client_id:?} should be connected"))
}
Expand All @@ -54,7 +55,7 @@ impl ConnectedClients {
/// # Panics
///
/// Panics if the passed client ID is not connected.
pub fn client_mut(&mut self, client_id: ClientId) -> &mut ConnectedClient {
pub fn client_mut(&mut self, client_id: ClientId) -> &mut ReplicatedClient {
self.get_client_mut(client_id)
.unwrap_or_else(|| panic!("{client_id:?} should be connected"))
}
Expand All @@ -63,15 +64,15 @@ impl ConnectedClients {
///
/// This operation is *O*(*n*).
/// See also [`Self::client`] for the panicking version.
pub fn get_client(&self, client_id: ClientId) -> Option<&ConnectedClient> {
pub fn get_client(&self, client_id: ClientId) -> Option<&ReplicatedClient> {
self.clients.iter().find(|client| client.id == client_id)
}

/// Returns a mutable reference to a connected client.
///
/// This operation is *O*(*n*).
/// See also [`Self::client`] for the panicking version.
pub fn get_client_mut(&mut self, client_id: ClientId) -> Option<&mut ConnectedClient> {
pub fn get_client_mut(&mut self, client_id: ClientId) -> Option<&mut ReplicatedClient> {
self.clients
.iter_mut()
.find(|client| client.id == client_id)
Expand All @@ -83,12 +84,12 @@ impl ConnectedClients {
}

/// Returns an iterator over connected clients.
pub fn iter(&self) -> impl Iterator<Item = &ConnectedClient> {
pub fn iter(&self) -> impl Iterator<Item = &ReplicatedClient> {
self.clients.iter()
}

/// Returns a mutable iterator over connected clients.
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut ConnectedClient> {
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut ReplicatedClient> {
self.clients.iter_mut()
}

Expand All @@ -102,27 +103,27 @@ impl ConnectedClients {
self.clients.is_empty()
}

/// Initializes a new [`ConnectedClient`] for this client.
/// Initializes a new [`ReplicatedClient`] for this client.
///
/// Reuses the memory from the buffers if available.
pub(crate) fn add(&mut self, client_buffers: &mut ClientBuffers, client_id: ClientId) {
debug!("adding connected `{client_id:?}`");
debug!("starting replication for `{client_id:?}`");

let client = if let Some(mut client) = client_buffers.clients.pop() {
client.reset(client_id);
client
} else {
ConnectedClient::new(client_id, self.policy)
ReplicatedClient::new(client_id, self.policy)
};

self.clients.push(client);
}

/// Removes a connected client.
/// Removes a replicated client if replication has already been enabled for it.
///
/// Keeps allocated memory in the buffers for reuse.
pub(crate) fn remove(&mut self, client_buffers: &mut ClientBuffers, client_id: ClientId) {
debug!("removing disconnected `{client_id:?}`");
debug!("stopping replication for `{client_id:?}`");

let index = self
.clients
Expand All @@ -145,7 +146,7 @@ impl ConnectedClients {
}
}

pub struct ConnectedClient {
pub struct ReplicatedClient {
/// Client's ID.
id: ClientId,

Expand All @@ -171,7 +172,7 @@ pub struct ConnectedClient {
next_update_index: u16,
}

impl ConnectedClient {
impl ReplicatedClient {
fn new(id: ClientId, policy: VisibilityPolicy) -> Self {
Self {
id,
Expand Down Expand Up @@ -349,15 +350,15 @@ impl ConnectedClient {
}
}

/// Reusable buffers for [`ConnectedClients`] and [`ConnectedClient`].
/// Reusable buffers for [`ReplicatedClients`] and [`ReplicatedClient`].
#[derive(Default, Resource)]
pub(crate) struct ClientBuffers {
/// [`ConnectedClient`]'s of previously disconnected clients.
/// [`ReplicatedClient`]'s of previously disconnected clients.
///
/// Stored to reuse allocated memory.
clients: Vec<ConnectedClient>,
clients: Vec<ReplicatedClient>,

/// [`Vec`]'s from acknowledged update indexes from [`ConnectedClient`].
/// [`Vec`]'s from acknowledged update indexes from [`ReplicatedClient`].
///
/// Stored to reuse allocated capacity.
entities: Vec<Vec<Entity>>,
Expand Down
16 changes: 8 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,8 @@ For events that require special serialization and deserialization functions you
You can control which parts of the world are visible for each client by setting visibility policy
in [`ServerPlugin`] to [`VisibilityPolicy::Whitelist`] or [`VisibilityPolicy::Blacklist`].
In order to set which entity is visible, you need to use the [`ConnectedClients`] resource
to obtain the [`ConnectedClient`] for a specific client and get its [`ClientVisibility`]:
In order to set which entity is visible, you need to use the [`ReplicatedClients`] resource
to obtain the [`ReplicatedClient`] for a specific client and get its [`ClientVisibility`]:
```
# use bevy::prelude::*;
Expand All @@ -434,12 +434,12 @@ app.add_plugins((
/// Disables the visibility of other players' entities that are further away than the visible distance.
fn update_visibility(
mut connected_clients: ResMut<ConnectedClients>,
mut replicated_clients: ResMut<ReplicatedClients>,
moved_players: Query<(&Transform, &Player), Changed<Transform>>,
other_players: Query<(Entity, &Transform, &Player)>,
) {
for (moved_transform, moved_player) in &moved_players {
let client = connected_clients.client_mut(moved_player.0);
let client = replicated_clients.client_mut(moved_player.0);
for (entity, transform, _) in other_players
.iter()
.filter(|(.., player)| player.0 != moved_player.0)
Expand Down Expand Up @@ -507,14 +507,14 @@ pub mod prelude {
channels::{ChannelKind, RepliconChannel, RepliconChannels},
command_markers::AppMarkerExt,
common_conditions::*,
connected_clients::{
client_visibility::ClientVisibility, ConnectedClient, ConnectedClients,
VisibilityPolicy,
},
event_registry::{
client_event::{ClientEventAppExt, FromClient},
server_event::{SendMode, ServerEventAppExt, ToClients},
},
replicated_clients::{
client_visibility::ClientVisibility, ReplicatedClient, ReplicatedClients,
VisibilityPolicy,
},
replication_rules::AppRuleExt,
replicon_client::{RepliconClient, RepliconClientStatus},
replicon_server::RepliconServer,
Expand Down
Loading

0 comments on commit c0cc6b7

Please sign in to comment.