Skip to content

Commit

Permalink
aeronet working?
Browse files Browse the repository at this point in the history
  • Loading branch information
kayhhh committed Nov 25, 2023
1 parent 67c3d88 commit 5eb3d7c
Show file tree
Hide file tree
Showing 11 changed files with 327 additions and 89 deletions.
85 changes: 81 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,3 @@ lto = true
[profile.wasm-release]
inherits = "release"
opt-level = 'z'
codegen-units = 1
8 changes: 8 additions & 0 deletions unavi-app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ edition.workspace = true
workspace = true

[dependencies]
aeronet = { version = "0.3.0", features = ["bevy"] }
aeronet_wt_core = "0.3.0"
aeronet_wt_native = { version = "0.3.0", features = ["bevy"] }
anyhow = "1.0.75"
bevy = "0.12.0"
bevy_rapier3d = { version = "0.23.0", features = ["simd-stable"] }
bevy_vrm = "0.0.5"
serde = { version = "1.0.193", features = ["derive"] }
tokio = { version = "1.34.0", features = ["full"] }
tracing = "0.1.40"
wired-protocol = { path = "../wired-protocol" }
wtransport = "0.1.8"
17 changes: 17 additions & 0 deletions unavi-app/examples/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,33 @@ use std::f32::consts::PI;

use bevy::{prelude::*, render::mesh::VertexAttributeValues};
use bevy_rapier3d::prelude::{Real, *};
use unavi_app::networking::JoinWorld;

fn main() {
unavi_app::App::new()
.add_plugins(unavi_app::UnaviPlugin {
file_path: "../assets".to_string(),
log_level: tracing::Level::DEBUG,
})
.add_systems(Startup, setup_world)
.add_systems(Update, join_world)
.run();
}

fn join_world(mut joined: Local<bool>, time: Res<Time>, mut writer: EventWriter<JoinWorld>) {
if *joined {
return;
}

if time.elapsed_seconds() < 1.0 {
return;
}

*joined = true;

writer.send(JoinWorld { world_id: 4 });
}

const GROUND_SIZE: f32 = 40.0;
const GROUND_THICKNESS: f32 = 0.01;

Expand Down
22 changes: 15 additions & 7 deletions unavi-app/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
use bevy::prelude::*;
use bevy::{log::LogPlugin, prelude::*};
use bevy_rapier3d::prelude::*;

mod avatar;
mod player;
mod settings;
mod world;
pub mod avatar;
pub mod networking;
pub mod player;
pub mod settings;
pub mod world;

pub use bevy::app::App;

pub struct UnaviPlugin {
pub file_path: String,
pub log_level: tracing::Level,
}

impl Default for UnaviPlugin {
fn default() -> Self {
Self {
file_path: "assets".to_string(),
log_level: tracing::Level::INFO,
}
}
}
Expand All @@ -34,13 +37,18 @@ impl Plugin for UnaviPlugin {
.set(AssetPlugin {
file_path: self.file_path.clone(),
..default()
})
.set(LogPlugin {
level: self.log_level,
..default()
}),
RapierPhysicsPlugin::<NoUserData>::default(),
RapierDebugRenderPlugin::default(),
avatar::AvatarPlugin,
world::WorldPlugin,
settings::SettingsPlugin,
networking::NetworkingPlugin,
player::PlayerPlugin,
settings::SettingsPlugin,
world::WorldPlugin,
// bevy::diagnostic::LogDiagnosticsPlugin::default(),
// bevy::diagnostic::FrameTimeDiagnosticsPlugin::default(),
));
Expand Down
82 changes: 82 additions & 0 deletions unavi-app/src/networking/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use aeronet::{AsyncRuntime, ClientTransportPlugin, TryFromBytes, TryIntoBytes};
use aeronet_wt_native::{Channels, OnChannel, WebTransportClient};
use anyhow::Result;
use bevy::prelude::*;
use std::time::Duration;
use wtransport::ClientConfig;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Channels)]
#[channel_kind(Datagram)]
struct AppChannel;

#[derive(Debug, Clone, PartialEq, Eq, Hash, OnChannel)]
#[channel_type(AppChannel)]
#[on_channel(AppChannel)]
struct AppMessage(String);

impl TryFromBytes for AppMessage {
fn try_from_bytes(buf: &[u8]) -> Result<Self> {
String::from_utf8(buf.to_vec())
.map(AppMessage)
.map_err(Into::into)
}
}

impl TryIntoBytes for AppMessage {
fn try_into_bytes(self) -> Result<Vec<u8>> {
Ok(self.0.into_bytes())
}
}

type Client = WebTransportClient<AppMessage, AppMessage, AppChannel>;

pub struct NetworkingPlugin;

impl Plugin for NetworkingPlugin {
fn build(&self, app: &mut App) {
app.add_plugins(ClientTransportPlugin::<_, _, Client>::default())
.init_resource::<AsyncRuntime>()
.add_event::<JoinWorld>()
.add_systems(Startup, setup)
.add_systems(Update, join_world);
}
}

fn setup(mut commands: Commands, rt: Res<AsyncRuntime>) {
match create(&rt) {
Ok(client) => {
commands.insert_resource(client);
}
Err(err) => panic!("Failed to create server: {err:#}"),
}
}

fn create(rt: &AsyncRuntime) -> Result<Client> {
let config = ClientConfig::builder()
.with_bind_default()
.with_no_cert_validation()
.keep_alive_interval(Some(Duration::from_secs(5)))
.build();

let (front, back) = aeronet_wt_native::create_client(config);

rt.0.spawn(async move {
back.start().await.unwrap();
});

Ok(front)
}

#[derive(Event)]
pub struct JoinWorld {
pub world_id: u32,
}

fn join_world(mut events: EventReader<JoinWorld>, client: Res<Client>) {
for event in events.read() {
info!("Joining world {}", event.world_id);

let url = "https://localhost:4433";
client.connect(url);
}
}
1 change: 1 addition & 0 deletions unavi-native/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ fn main() {
unavi_app::App::new()
.add_plugins(unavi_app::UnaviPlugin {
file_path: "../assets".to_string(),
..Default::default()
})
.run();
}
9 changes: 9 additions & 0 deletions unavi-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@ web = [
"dep:unavi-web-app"
]
world = [
"dep:aeronet",
"dep:aeronet_wt_core",
"dep:aeronet_wt_native",
"dep:bevy",
"dep:rcgen",
"dep:rustls",
"dep:time",
"dep:wtransport"
]

[dependencies]
anyhow = "1.0.75"
axum = "0.6.20"
tokio = { version = "1.33.0", features = ["full"] }
tower = "0.4.13"
Expand All @@ -34,6 +39,10 @@ leptos = { version = "0.5.1", features = ["ssr"], optional = true }
leptos_axum = { version = "0.5.1", optional = true }
unavi-web-app = { path = "../unavi-web/app", features = ["ssr"], optional = true }

aeronet = { version = "0.3.0", features = ["bevy"], optional = true }
aeronet_wt_core = { version = "0.3.0", optional = true }
aeronet_wt_native = { version = "0.3.0", features = ["bevy"], optional = true }
bevy = { version = "0.12.0", optional = true }
rcgen = { version = "0.11.3", optional = true }
rustls = { version = "0.21.9", optional = true }
time = { version = "0.3.30", optional = true }
Expand Down
6 changes: 4 additions & 2 deletions unavi-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ pub async fn start_server(opts: ServerOptions) -> Result<(), Box<dyn std::error:

#[cfg(feature = "world")]
tokio::spawn(async move {
let ca = world::cert::new_ca();

if let Err(e) = world::start_server(world::WorldOptions {
address: opts.address,
cert_pair: world::CertPair {
cert: rustls::Certificate(world::cert::new_ca().serialize_der().unwrap()),
key: rustls::PrivateKey(world::cert::new_ca().serialize_private_key_der()),
cert: ca.serialize_der().unwrap(),
key: ca.serialize_private_key_der(),
},
})
.await
Expand Down
1 change: 1 addition & 0 deletions unavi-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ async fn main() {
tracing_subscriber::fmt()
.with_target(true)
.with_level(true)
.with_max_level(tracing::Level::DEBUG)
.init();

info!("Features:");
Expand Down
Loading

0 comments on commit 5eb3d7c

Please sign in to comment.