rust/hedgewars-server/src/main.rs
author unC0Rr
Sat, 28 Sep 2024 22:27:13 +0200
changeset 16037 2b4f361e3891
parent 15989 fb389df02e3e
permissions -rw-r--r--
Wrap chat lines into div
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15800
diff changeset
     1
#![forbid(unsafe_code)]
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
     2
#![allow(unused_imports)]
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15800
diff changeset
     3
#![allow(dead_code)]
7d0f747afcb8 move server network to tokio
alfadur
parents: 15800
diff changeset
     4
#![allow(unused_variables)]
13421
d1368c776a4f Enable all lints from the rust-2018-idioms suite.
marmistrz
parents: 13414
diff changeset
     5
#![deny(bare_trait_objects)]
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
     6
14792
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14783
diff changeset
     7
use getopts::Options;
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
     8
use log::*;
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15800
diff changeset
     9
use std::{env, net::SocketAddr, str::FromStr as _};
12125
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    10
15074
c5a6e8566425 shuffle server files
alfadur
parents: 14916
diff changeset
    11
mod core;
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    12
mod handlers;
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    13
mod protocol;
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    14
mod server;
12125
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    15
mod utils;
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    16
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14803
diff changeset
    17
use crate::server::network::{NetworkLayer, NetworkLayerBuilder};
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
    18
14794
fc2cfec95d86 a bit more option error handling
alfadur
parents: 14793
diff changeset
    19
const PROGRAM_NAME: &'_ str = "Hedgewars Game Server";
fc2cfec95d86 a bit more option error handling
alfadur
parents: 14793
diff changeset
    20
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15800
diff changeset
    21
#[tokio::main]
7d0f747afcb8 move server network to tokio
alfadur
parents: 15800
diff changeset
    22
async fn main() -> tokio::io::Result<()> {
13797
c5edfcfac68b Bump dependencies
alfadur
parents: 13796
diff changeset
    23
    env_logger::init();
12137
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12133
diff changeset
    24
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14803
diff changeset
    25
    info!("Hedgewars game server, protocol {}", utils::SERVER_VERSION);
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14803
diff changeset
    26
14792
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14783
diff changeset
    27
    let args: Vec<String> = env::args().collect();
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14783
diff changeset
    28
    let mut opts = Options::new();
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14783
diff changeset
    29
15989
fb389df02e3e add some more todo!s
alfadur
parents: 15938
diff changeset
    30
    //todo!("Add options for cert paths");
14792
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14783
diff changeset
    31
    opts.optopt("p", "port", "port - defaults to 46631", "PORT");
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14783
diff changeset
    32
    opts.optflag("h", "help", "help");
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14783
diff changeset
    33
    let matches = match opts.parse(&args[1..]) {
14794
fc2cfec95d86 a bit more option error handling
alfadur
parents: 14793
diff changeset
    34
        Ok(m) => m,
fc2cfec95d86 a bit more option error handling
alfadur
parents: 14793
diff changeset
    35
        Err(e) => {
fc2cfec95d86 a bit more option error handling
alfadur
parents: 14793
diff changeset
    36
            println!("{}\n{}", e, opts.short_usage(""));
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15800
diff changeset
    37
            return Ok(());
14794
fc2cfec95d86 a bit more option error handling
alfadur
parents: 14793
diff changeset
    38
        }
14792
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14783
diff changeset
    39
    };
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14783
diff changeset
    40
    if matches.opt_present("h") {
14794
fc2cfec95d86 a bit more option error handling
alfadur
parents: 14793
diff changeset
    41
        println!("{}", opts.usage(PROGRAM_NAME));
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15800
diff changeset
    42
        return Ok(());
14792
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14783
diff changeset
    43
    }
12125
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    44
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14803
diff changeset
    45
    let port = matches
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14803
diff changeset
    46
        .opt_str("p")
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14803
diff changeset
    47
        .and_then(|s| u16::from_str(&s).ok())
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14803
diff changeset
    48
        .unwrap_or(46631);
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15800
diff changeset
    49
    let address: SocketAddr = format!("0.0.0.0:{}", port).parse().unwrap();
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14803
diff changeset
    50
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15800
diff changeset
    51
    let server = tokio::net::TcpListener::bind(address).await.unwrap();
12125
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    52
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15800
diff changeset
    53
    let mut hw_network = NetworkLayerBuilder::default().with_listener(server).build();
14916
8750530bf7e7 avoid crashing server in the event loop
alfadur
parents: 14830
diff changeset
    54
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15800
diff changeset
    55
    hw_network.run().await;
7d0f747afcb8 move server network to tokio
alfadur
parents: 15800
diff changeset
    56
    Ok(())
12125
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    57
}