~starkingdoms/starkingdoms

ref: 9b91629f33034dab27fe10f953bd39b6477fcac2 starkingdoms/crates/unified/src/server/mod.rs -rw-r--r-- 3.4 KiB
9b91629f — core chore(lint): lint/format 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
mod gravity;
pub mod planets;
pub mod player;
mod world_config;

use bevy::prelude::*;
use bevy_replicon::prelude::RepliconChannels;
use bevy_replicon_renet2::netcode::{
    BoxedSocket, NetcodeServerTransport, ServerAuthentication, ServerSetupConfig,
};
use std::net::{SocketAddr, UdpSocket};
use std::time::{SystemTime, UNIX_EPOCH};

#[cfg(not(target_arch = "wasm32"))]
use bevy_replicon_renet2::netcode::{
    NativeSocket, WebSocketAcceptor, WebSocketServer, WebSocketServerConfig,
};

use crate::server::gravity::newtonian_gravity_plugin;
use crate::server::planets::planets_plugin;
use crate::server::player::player_management_plugin;
use crate::server::world_config::world_config_plugin;
use bevy_replicon_renet2::RenetChannelsExt;
use bevy_replicon_renet2::renet2::{ConnectionConfig, RenetServer};

pub struct ServerPlugin {
    pub bind_ws: SocketAddr,
    pub bind_native: SocketAddr,
    pub max_clients: usize,
}
impl Plugin for ServerPlugin {
    fn build(&self, app: &mut App) {
        let bind_ws = self.bind_ws;
        let bind_native = self.bind_native;
        let max_clients = self.max_clients;

        app.add_systems(FixedPreUpdate, bevy_replicon::server::increment_tick) // !!important!! do not remove or move
            .add_systems(
                Startup,
                move |mut commands: Commands, channels: Res<RepliconChannels>| {
                    let server = RenetServer::new(ConnectionConfig::from_channels(
                        channels.server_configs(),
                        channels.client_configs(),
                    ));

                    #[cfg(not(target_arch = "wasm32"))]
                    {
                        let server_config = ServerSetupConfig {
                            current_time: SystemTime::now().duration_since(UNIX_EPOCH).unwrap(),
                            max_clients,
                            protocol_id: 0,
                            authentication: ServerAuthentication::Unsecure,
                            socket_addresses: vec![vec![bind_native], vec![bind_ws]],
                        };

                        let rt = tokio::runtime::Runtime::new().unwrap();

                        let ws_config = WebSocketServerConfig {
                            acceptor: WebSocketAcceptor::Plain {
                                has_tls_proxy: true,
                            },
                            listen: bind_ws,
                            max_clients,
                        };
                        let ws_server =
                            WebSocketServer::new(ws_config, rt.handle().clone()).unwrap();

                        let native_socket =
                            NativeSocket::new(UdpSocket::bind(bind_native).unwrap()).unwrap();

                        let transport = NetcodeServerTransport::new_with_sockets(
                            server_config,
                            vec![BoxedSocket::new(native_socket), BoxedSocket::new(ws_server)],
                        )
                        .unwrap();

                        commands.insert_resource(server);
                        commands.insert_resource(transport);

                        info!("websocket/native server listening");
                    }
                },
            )
            .add_plugins(planets_plugin)
            .add_plugins(world_config_plugin)
            .add_plugins(newtonian_gravity_plugin)
            .add_plugins(player_management_plugin);
    }
}