Skip to content

Commit

Permalink
Renet: move netcode transport to new renet_netcode crate
Browse files Browse the repository at this point in the history
Move all bevy related code to bevy_renet, use feature flags for steam/netcode transport plugins
  • Loading branch information
lucaspoffo committed Nov 9, 2024
1 parent 3518e3d commit 042ede9
Show file tree
Hide file tree
Showing 26 changed files with 103 additions and 153 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ members = [
"bevy_renet",
"renet_visualizer",
"renet_steam",
"renet_netcode",
]
resolver = "2"
16 changes: 10 additions & 6 deletions bevy_renet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@ version = "0.0.12"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
default = ["transport"]
serde = ["renet/serde"]
transport = ["renet/transport"]
default = ["netcode"]
netcode = ["dep:renet_netcode"]
steam = ["dep:renet_steam"]

[[example]]
name = "simple"
required-features = ["serde", "transport"]
required-features = ["netcode"]

[dependencies]
bevy = {version = "0.14", default-features = false}
renet = {path = "../renet", version = "0.0.16", default-features=false, features = ["bevy"]}
bevy_app = "0.14"
bevy_ecs = "0.14"
bevy_time = "0.14"
renet = { path="../renet", version = "0.0.16", features = ["bevy"] }
renet_netcode = { path="../renet_netcode", version = "0.0.1", features = ["bevy"], optional = true }
renet_steam = { path="../renet_steam", version = "0.0.2", features = ["bevy"], optional = true }

[dev-dependencies]
bevy = {version = "0.14", default-features = false, features = ["bevy_core_pipeline", "bevy_render", "bevy_asset", "bevy_pbr", "x11", "tonemapping_luts", "ktx2", "zstd"]}
Expand Down
17 changes: 5 additions & 12 deletions bevy_renet/examples/simple.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
use bevy::{prelude::*, render::mesh::PlaneMeshBuilder};
use bevy_renet::{
client_connected,
renet::{
transport::{ClientAuthentication, ServerAuthentication, ServerConfig},
ConnectionConfig, DefaultChannel, RenetClient, RenetServer, ServerEvent,
},
transport::{NetcodeClientPlugin, NetcodeServerPlugin},
RenetClientPlugin, RenetServerPlugin,
};
use renet::{
transport::{NetcodeClientTransport, NetcodeServerTransport, NetcodeTransportError},
ClientId,
use bevy_renet::netcode::{
ClientAuthentication, NetcodeClientPlugin, NetcodeClientTransport, NetcodeServerPlugin, NetcodeServerTransport, NetcodeTransportError,
ServerAuthentication, ServerConfig,
};
use bevy_renet::renet::{ClientId, ConnectionConfig, DefaultChannel, RenetClient, RenetServer, ServerEvent};
use bevy_renet::{client_connected, RenetClientPlugin, RenetServerPlugin};

use std::time::SystemTime;
use std::{collections::HashMap, net::UdpSocket};
Expand Down
11 changes: 8 additions & 3 deletions bevy_renet/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
pub use renet;

use bevy::prelude::*;
use bevy_app::prelude::*;
use bevy_ecs::prelude::*;
use bevy_time::prelude::*;

use renet::{RenetClient, RenetServer, ServerEvent};

#[cfg(feature = "transport")]
pub mod transport;
#[cfg(feature = "netcode")]
pub mod netcode;

#[cfg(feature = "steam")]
pub mod steam;

/// This system set is where all transports receive messages
///
Expand Down
10 changes: 5 additions & 5 deletions bevy_renet/src/transport.rs → bevy_renet/src/netcode.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use renet::{
transport::{NetcodeClientTransport, NetcodeServerTransport, NetcodeTransportError},
RenetClient, RenetServer,
};
use renet::{RenetClient, RenetServer};
pub use renet_netcode::*;

use bevy::{app::AppExit, prelude::*};
use bevy_app::prelude::*;
use bevy_ecs::prelude::*;
use bevy_time::prelude::*;

use crate::{RenetClientPlugin, RenetReceive, RenetSend, RenetServerPlugin};

Expand Down
4 changes: 2 additions & 2 deletions renet_steam/src/bevy.rs → bevy_renet/src/steam.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use bevy_ecs::prelude::*;
use renet::{RenetClient, RenetServer};
use steamworks::SteamError;

use bevy_renet::{RenetClientPlugin, RenetReceive, RenetSend, RenetServerPlugin};
use crate::{RenetClientPlugin, RenetReceive, RenetSend, RenetServerPlugin};

pub use crate::{AccessPermission, SteamClientTransport, SteamServerConfig, SteamServerTransport};
pub use renet_steam::*;

pub struct SteamServerPlugin;

Expand Down
10 changes: 5 additions & 5 deletions demo_bevy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ name = "server"
path = "src/bin/server.rs"

[features]
transport = ["bevy_renet/transport"]
steam = ["dep:renet_steam", "steamworks"]
netcode = ["bevy_renet/netcode"]
steam = ["bevy_renet/steam"]

[dependencies]
bevy = { version = "0.14", default-features = false, features = [
Expand All @@ -25,9 +25,9 @@ bevy = { version = "0.14", default-features = false, features = [
"ktx2",
"zstd",
"serialize",
] }
bevy_renet = { path = "../bevy_renet", features = ["serde"] }
renet_steam = { path = "../renet_steam", features = ["bevy"], optional = true }
]}

bevy_renet = { path = "../bevy_renet", default-features = false }
serde = { version = "1.0", features = ["derive"] }
bincode = "1.3"
bevy_egui = "0.28"
Expand Down
12 changes: 6 additions & 6 deletions demo_bevy/src/bin/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ struct CurrentClientId(u64);
#[derive(SystemSet, Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct Connected;

#[cfg(feature = "transport")]
#[cfg(feature = "netcode")]
fn add_netcode_network(app: &mut App) {
use bevy_renet::renet::transport::{ClientAuthentication, NetcodeClientTransport, NetcodeTransportError};
use bevy_renet::netcode::{ClientAuthentication, NetcodeClientPlugin, NetcodeClientTransport, NetcodeTransportError};
use demo_bevy::PROTOCOL_ID;
use std::{net::UdpSocket, time::SystemTime};

app.add_plugins(bevy_renet::transport::NetcodeClientPlugin);
app.add_plugins(NetcodeClientPlugin);

app.configure_sets(Update, Connected.run_if(client_connected));

Expand Down Expand Up @@ -82,7 +82,7 @@ fn add_netcode_network(app: &mut App) {

#[cfg(feature = "steam")]
fn add_steam_network(app: &mut App) {
use renet_steam::bevy::{SteamClientPlugin, SteamClientTransport, SteamTransportError};
use bevy_renet::steam::{SteamClientPlugin, SteamClientTransport, SteamTransportError};
use steamworks::{SingleClient, SteamId};

let (steam_client, single) = steamworks::Client::init_app(480).unwrap();
Expand Down Expand Up @@ -130,7 +130,7 @@ fn main() {
app.add_plugins(LogDiagnosticsPlugin::default());
app.add_plugins(EguiPlugin);

#[cfg(feature = "transport")]
#[cfg(feature = "netcode")]
add_netcode_network(&mut app);

#[cfg(feature = "steam")]
Expand Down Expand Up @@ -227,7 +227,7 @@ fn client_sync_players(
..Default::default()
});

if client_id == id.raw() {
if client_id == id {
client_entity.insert(ControlledPlayer);
}

Expand Down
12 changes: 5 additions & 7 deletions demo_bevy/src/bin/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ struct Bot {
#[derive(Debug, Resource)]
struct BotId(u64);

#[cfg(feature = "transport")]
#[cfg(feature = "netcode")]
fn add_netcode_network(app: &mut App) {
use bevy_renet::renet::transport::{NetcodeServerTransport, ServerAuthentication, ServerConfig};
use bevy_renet::transport::NetcodeServerPlugin;
use bevy_renet::netcode::{NetcodeServerPlugin, NetcodeServerTransport, ServerAuthentication, ServerConfig};
use demo_bevy::{connection_config, PROTOCOL_ID};
use std::{net::UdpSocket, time::SystemTime};

Expand All @@ -59,9 +58,8 @@ fn add_netcode_network(app: &mut App) {

#[cfg(feature = "steam")]
fn add_steam_network(app: &mut App) {
use bevy_renet::steam::{AccessPermission, SteamServerConfig, SteamServerPlugin, SteamServerTransport};
use demo_bevy::connection_config;
use renet_steam::bevy::{SteamServerConfig, SteamServerPlugin, SteamServerTransport};
use renet_steam::AccessPermission;
use steamworks::SingleClient;

let (steam_client, single) = steamworks::Client::init_app(480).unwrap();
Expand Down Expand Up @@ -100,7 +98,7 @@ fn main() {

app.insert_resource(RenetServerVisualizer::<200>::default());

#[cfg(feature = "transport")]
#[cfg(feature = "netcode")]
add_netcode_network(&mut app);

#[cfg(feature = "steam")]
Expand Down Expand Up @@ -300,7 +298,7 @@ fn spawn_bot(
mut commands: Commands,
) {
if keyboard_input.just_pressed(KeyCode::Space) {
let client_id = ClientId::from_raw(bot_id.0);
let client_id: ClientId = bot_id.0;
bot_id.0 += 1;
// Spawn new player
let transform = Transform::from_xyz((fastrand::f32() - 0.5) * 40., 0.51, (fastrand::f32() - 0.5) * 40.);
Expand Down
6 changes: 3 additions & 3 deletions demo_bevy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use bevy::prelude::*;
use bevy_renet::renet::{ChannelConfig, ClientId, ConnectionConfig, SendType};
use serde::{Deserialize, Serialize};

#[cfg(feature = "transport")]
pub const PRIVATE_KEY: &[u8; bevy_renet::renet::transport::NETCODE_KEY_BYTES] = b"an example very very secret key."; // 32-bytes
#[cfg(feature = "transport")]
#[cfg(feature = "netcode")]
pub const PRIVATE_KEY: &[u8; bevy_renet::netcode::NETCODE_KEY_BYTES] = b"an example very very secret key."; // 32-bytes
#[cfg(feature = "netcode")]
pub const PROTOCOL_ID: u64 = 7;

#[derive(Debug, Component)]
Expand Down
3 changes: 2 additions & 1 deletion demo_chat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
renet = { path = "../renet", features = ["transport", "serde"] }
renet = { path = "../renet" }
renet_netcode = { path = "../renet_netcode" }
renet_visualizer = { path = "../renet_visualizer" }
eframe = "0.28"
serde = { version = "1.0", features = ["derive"] }
Expand Down
3 changes: 2 additions & 1 deletion demo_chat/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use bincode::Options;
use eframe::egui;
use log::error;
use renet::{transport::NetcodeClientTransport, ClientId, DefaultChannel, RenetClient};
use renet::{ClientId, DefaultChannel, RenetClient};
use renet_netcode::NetcodeClientTransport;
use renet_visualizer::RenetClientVisualizer;

use std::{collections::HashMap, time::Instant};
Expand Down
3 changes: 2 additions & 1 deletion demo_chat/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use client::ChatApp;
use eframe::{egui, App};
use renet::{transport::NETCODE_USER_DATA_BYTES, ClientId};
use renet::ClientId;
use renet_netcode::NETCODE_USER_DATA_BYTES;
use serde::{Deserialize, Serialize};

use std::collections::HashMap;
Expand Down
10 changes: 4 additions & 6 deletions demo_chat/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,16 @@ use std::{
time::{Duration, SystemTime},
};

use renet::{
transport::{NetcodeServerTransport, ServerAuthentication, ServerConfig},
ClientId, ConnectionConfig, DefaultChannel, RenetServer, ServerEvent,
};
use renet::{ClientId, ConnectionConfig, DefaultChannel, RenetServer, ServerEvent};
use renet_netcode::{NetcodeServerTransport, ServerAuthentication, ServerConfig};
use renet_visualizer::RenetServerVisualizer;

use crate::{ClientMessages, Message, ServerMessages, Username, PROTOCOL_ID};
use bincode::Options;
use log::info;

pub const SYSTEM_MESSAGE_CLIENT_ID: ClientId = ClientId::from_raw(0);
pub const HOST_CLIENT_ID: ClientId = ClientId::from_raw(1);
pub const SYSTEM_MESSAGE_CLIENT_ID: ClientId = 0;
pub const HOST_CLIENT_ID: ClientId = 1;

pub struct ChatServer {
pub server: RenetServer,
Expand Down
6 changes: 2 additions & 4 deletions demo_chat/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ use eframe::{
egui::{self, lerp, Color32, Layout, Pos2, Ui, Vec2},
epaint::{PathShape, PathStroke},
};
use renet::{
transport::{ClientAuthentication, NetcodeClientTransport},
ClientId, ConnectionConfig, DefaultChannel, RenetClient,
};
use renet::{ClientId, ConnectionConfig, DefaultChannel, RenetClient};
use renet_netcode::{ClientAuthentication, NetcodeClientTransport};

use std::{
collections::HashMap,
Expand Down
8 changes: 1 addition & 7 deletions renet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,14 @@ readme = "../README.md"
repository = "https://github.com/lucaspoffo/renet"
version = "0.0.16"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
bevy = ["dep:bevy_ecs"]
default = ["transport"]
transport = ["dep:renetcode"]
serde = ["dep:serde"]

[dependencies]
bevy_ecs = { version = "0.14", optional = true }
bytes = "1.1"
log = "0.4.17"
octets = "0.3"
renetcode = { path = "../renetcode", version = "0.0.12", optional = true }
serde = {version = "1.0", optional = true}

[dev-dependencies]
env_logger = "0.11"
env_logger = "0.11"
50 changes: 1 addition & 49 deletions renet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,59 +5,11 @@ mod packet;
mod remote_connection;
mod server;

#[cfg(feature = "transport")]
pub mod transport;

pub use channel::{ChannelConfig, DefaultChannel, SendType};
pub use error::{ChannelError, ClientNotFound, DisconnectReason};
pub use remote_connection::{ConnectionConfig, NetworkInfo, RenetClient, RenetConnectionStatus};
pub use server::{RenetServer, ServerEvent};

pub use bytes::Bytes;

/// Unique identifier for clients.
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Ord, PartialOrd)]
pub struct ClientId(u64);

impl ClientId {
/// Creates a [`ClientId`] from a raw 64 bit value.
pub const fn from_raw(value: u64) -> Self {
Self(value)
}

/// Returns the raw 64 bit value of the [`ClientId`]
pub fn raw(&self) -> u64 {
self.0
}
}

impl std::fmt::Display for ClientId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}

#[cfg(feature = "serde")]
impl serde::Serialize for ClientId {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.0.serialize(serializer)
}
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for ClientId {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
u64::deserialize(deserializer).map(ClientId::from_raw)
}
}

#[cfg(feature = "bevy")]
impl bevy_ecs::component::Component for ClientId {
const STORAGE_TYPE: bevy_ecs::component::StorageType = bevy_ecs::component::StorageType::Table;
}
pub type ClientId = u64;
2 changes: 1 addition & 1 deletion renet/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn test_remote_connection_reliable_channel() {
let mut server = RenetServer::new(ConnectionConfig::default());
let mut client = RenetClient::new(ConnectionConfig::default());

let client_id = ClientId::from_raw(0);
let client_id: ClientId = 0;
server.add_connection(client_id);
assert_eq!(server.connected_clients(), 1);
assert!(server.has_connections());
Expand Down
Loading

0 comments on commit 042ede9

Please sign in to comment.