Skip to content

Commit 287ed6d

Browse files
committed
Move client network code to separate file
1 parent df3b5c5 commit 287ed6d

File tree

3 files changed

+91
-87
lines changed

3 files changed

+91
-87
lines changed

minigolf_client/src/main.rs

Lines changed: 3 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1+
mod network;
12
mod ui;
23

34
use {
4-
crate::ui::ClientUiPlugin,
5+
crate::{network::ClientNetworkPlugin, ui::ClientUiPlugin},
56
aeronet::io::{Session, connection::Disconnected},
6-
aeronet_replicon::client::AeronetRepliconClientPlugin,
7-
aeronet_websocket::client::WebSocketClientPlugin,
8-
aeronet_webtransport::{cert, client::WebTransportClientPlugin},
97
bevy::{
108
color::palettes::basic::RED,
119
input::{
@@ -14,24 +12,16 @@ use {
1412
},
1513
prelude::*,
1614
},
17-
bevy_replicon::prelude::*,
1815
minigolf::{GameState, LevelMesh, MinigolfPlugin, Player, PlayerInput},
1916
web_sys::{HtmlCanvasElement, wasm_bindgen::JsCast},
2017
};
2118

2219
fn main() -> AppExit {
2320
App::new()
2421
.add_plugins((
25-
// core
2622
DefaultPlugins,
2723
ClientUiPlugin,
28-
// transport
29-
WebTransportClientPlugin,
30-
WebSocketClientPlugin,
31-
// replication
32-
RepliconPlugins,
33-
AeronetRepliconClientPlugin,
34-
// game
24+
ClientNetworkPlugin,
3525
MinigolfPlugin,
3626
))
3727
.add_systems(Startup, setup_level)
@@ -144,79 +134,6 @@ fn on_disconnected(_trigger: Trigger<Disconnected>, mut game_state: ResMut<NextS
144134
game_state.set(GameState::None);
145135
}
146136

147-
//
148-
// WebTransport
149-
//
150-
151-
type WebTransportClientConfig = aeronet_webtransport::client::ClientConfig;
152-
153-
#[cfg(target_family = "wasm")]
154-
fn web_transport_config(cert_hash: String) -> WebTransportClientConfig {
155-
use aeronet_webtransport::xwt_web::{CertificateHash, HashAlgorithm};
156-
157-
let server_certificate_hashes = match cert::hash_from_b64(&cert_hash) {
158-
Ok(hash) => vec![CertificateHash {
159-
algorithm: HashAlgorithm::Sha256,
160-
value: Vec::from(hash),
161-
}],
162-
Err(err) => {
163-
warn!("Failed to read certificate hash from string: {err:?}");
164-
Vec::new()
165-
}
166-
};
167-
168-
WebTransportClientConfig {
169-
server_certificate_hashes,
170-
..Default::default()
171-
}
172-
}
173-
174-
#[cfg(not(target_family = "wasm"))]
175-
fn web_transport_config(cert_hash: String) -> WebTransportClientConfig {
176-
use {aeronet_webtransport::wtransport::tls::Sha256Digest, core::time::Duration};
177-
178-
let config = WebTransportClientConfig::builder().with_bind_default();
179-
180-
let config = if cert_hash.is_empty() {
181-
warn!("Connecting without certificate validation");
182-
config.with_no_cert_validation()
183-
} else {
184-
match cert::hash_from_b64(&cert_hash) {
185-
Ok(hash) => config.with_server_certificate_hashes([Sha256Digest::new(hash)]),
186-
Err(err) => {
187-
warn!("Failed to read certificate hash from string: {err:?}");
188-
config.with_server_certificate_hashes([])
189-
}
190-
}
191-
};
192-
193-
config
194-
.keep_alive_interval(Some(Duration::from_secs(1)))
195-
.max_idle_timeout(Some(Duration::from_secs(5)))
196-
.expect("should be a valid idle timeout")
197-
.build()
198-
}
199-
200-
//
201-
// WebSocket
202-
//
203-
204-
type WebSocketClientConfig = aeronet_websocket::client::ClientConfig;
205-
206-
#[cfg(target_family = "wasm")]
207-
fn web_socket_config() -> WebSocketClientConfig {
208-
WebSocketClientConfig::default()
209-
}
210-
211-
#[cfg(not(target_family = "wasm"))]
212-
fn web_socket_config() -> WebSocketClientConfig {
213-
WebSocketClientConfig::builder().with_no_cert_validation()
214-
}
215-
216-
//
217-
// game logic
218-
//
219-
220137
fn handle_inputs(mut inputs: EventWriter<PlayerInput>, input: Res<ButtonInput<KeyCode>>) {
221138
let mut movement = Vec2::ZERO;
222139
if input.just_released(KeyCode::ArrowRight) {

minigolf_client/src/network.rs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
use {
2+
aeronet_replicon::client::AeronetRepliconClientPlugin,
3+
aeronet_websocket::client::WebSocketClientPlugin,
4+
aeronet_webtransport::{cert, client::WebTransportClientPlugin},
5+
bevy::prelude::*,
6+
bevy_replicon::prelude::*,
7+
};
8+
9+
/// Sets up minigolf client networking.
10+
#[derive(Debug)]
11+
pub(crate) struct ClientNetworkPlugin;
12+
13+
impl Plugin for ClientNetworkPlugin {
14+
fn build(&self, app: &mut App) {
15+
app.add_plugins((WebTransportClientPlugin, WebSocketClientPlugin))
16+
.add_plugins((RepliconPlugins, AeronetRepliconClientPlugin));
17+
}
18+
}
19+
20+
//
21+
// WebTransport
22+
//
23+
24+
type WebTransportClientConfig = aeronet_webtransport::client::ClientConfig;
25+
26+
#[cfg(target_family = "wasm")]
27+
pub(crate) fn web_transport_config(cert_hash: String) -> WebTransportClientConfig {
28+
use aeronet_webtransport::xwt_web::{CertificateHash, HashAlgorithm};
29+
30+
let server_certificate_hashes = match cert::hash_from_b64(&cert_hash) {
31+
Ok(hash) => vec![CertificateHash {
32+
algorithm: HashAlgorithm::Sha256,
33+
value: Vec::from(hash),
34+
}],
35+
Err(err) => {
36+
warn!("Failed to read certificate hash from string: {err:?}");
37+
Vec::new()
38+
}
39+
};
40+
41+
WebTransportClientConfig {
42+
server_certificate_hashes,
43+
..Default::default()
44+
}
45+
}
46+
47+
#[cfg(not(target_family = "wasm"))]
48+
pub(crate) fn web_transport_config(cert_hash: String) -> WebTransportClientConfig {
49+
use {aeronet_webtransport::wtransport::tls::Sha256Digest, core::time::Duration};
50+
51+
let config = WebTransportClientConfig::builder().with_bind_default();
52+
53+
let config = if cert_hash.is_empty() {
54+
warn!("Connecting without certificate validation");
55+
config.with_no_cert_validation()
56+
} else {
57+
match cert::hash_from_b64(&cert_hash) {
58+
Ok(hash) => config.with_server_certificate_hashes([Sha256Digest::new(hash)]),
59+
Err(err) => {
60+
warn!("Failed to read certificate hash from string: {err:?}");
61+
config.with_server_certificate_hashes([])
62+
}
63+
}
64+
};
65+
66+
config
67+
.keep_alive_interval(Some(Duration::from_secs(1)))
68+
.max_idle_timeout(Some(Duration::from_secs(5)))
69+
.expect("should be a valid idle timeout")
70+
.build()
71+
}
72+
73+
//
74+
// WebSocket
75+
//
76+
77+
type WebSocketClientConfig = aeronet_websocket::client::ClientConfig;
78+
79+
#[cfg(target_family = "wasm")]
80+
pub(crate) fn web_socket_config() -> WebSocketClientConfig {
81+
WebSocketClientConfig::default()
82+
}
83+
84+
#[cfg(not(target_family = "wasm"))]
85+
pub(crate) fn web_socket_config() -> WebSocketClientConfig {
86+
WebSocketClientConfig::builder().with_no_cert_validation()
87+
}

minigolf_client/src/ui.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use {
2-
crate::{web_socket_config, web_transport_config},
2+
crate::network::{web_socket_config, web_transport_config},
33
aeronet::io::{
44
Session, SessionEndpoint,
55
connection::{Disconnect, DisconnectReason, Disconnected},

0 commit comments

Comments
 (0)