rust/hedgewars-server/src/main.rs
author S.D.
Tue, 27 Sep 2022 14:59:03 +0300
changeset 15900 fc3cb23fd26f
parent 15853 7d0f747afcb8
child 15968 ce47259d5c86
permissions -rw-r--r--
Allow to see rooms of incompatible versions in the lobby For the new clients the room version is shown in a separate column. There is also a hack for previous versions clients: the room vesion specifier is prepended to the room names for rooms of incompatible versions, and the server shows 'incompatible version' error if the client tries to join them.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15853
7d0f747afcb8 move server network to tokio
alfadur
parents: 15822
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)]
15853
7d0f747afcb8 move server network to tokio
alfadur
parents: 15822
diff changeset
     3
#![allow(dead_code)]
7d0f747afcb8 move server network to tokio
alfadur
parents: 15822
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
14813
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14804
diff changeset
     7
use getopts::Options;
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
     8
use log::*;
15853
7d0f747afcb8 move server network to tokio
alfadur
parents: 15822
diff changeset
     9
use std::{env, net::SocketAddr, str::FromStr as _};
12125
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    10
15095
c5a6e8566425 shuffle server files
alfadur
parents: 14937
diff changeset
    11
mod core;
15096
e935b1ad23f3 normalize type names
alfadur
parents: 15095
diff changeset
    12
mod handlers;
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    13
mod protocol;
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
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
14851
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14824
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
14815
fc2cfec95d86 a bit more option error handling
alfadur
parents: 14814
diff changeset
    19
const PROGRAM_NAME: &'_ str = "Hedgewars Game Server";
fc2cfec95d86 a bit more option error handling
alfadur
parents: 14814
diff changeset
    20
15853
7d0f747afcb8 move server network to tokio
alfadur
parents: 15822
diff changeset
    21
#[tokio::main]
7d0f747afcb8 move server network to tokio
alfadur
parents: 15822
diff changeset
    22
async fn main() -> tokio::io::Result<()> {
13769
c5edfcfac68b Bump dependencies
alfadur
parents: 13713
diff changeset
    23
    env_logger::init();
12137
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12133
diff changeset
    24
14851
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14824
diff changeset
    25
    info!("Hedgewars game server, protocol {}", utils::SERVER_VERSION);
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14824
diff changeset
    26
14813
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14804
diff changeset
    27
    let args: Vec<String> = env::args().collect();
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14804
diff changeset
    28
    let mut opts = Options::new();
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14804
diff changeset
    29
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14804
diff changeset
    30
    opts.optopt("p", "port", "port - defaults to 46631", "PORT");
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14804
diff changeset
    31
    opts.optflag("h", "help", "help");
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14804
diff changeset
    32
    let matches = match opts.parse(&args[1..]) {
14815
fc2cfec95d86 a bit more option error handling
alfadur
parents: 14814
diff changeset
    33
        Ok(m) => m,
fc2cfec95d86 a bit more option error handling
alfadur
parents: 14814
diff changeset
    34
        Err(e) => {
fc2cfec95d86 a bit more option error handling
alfadur
parents: 14814
diff changeset
    35
            println!("{}\n{}", e, opts.short_usage(""));
15853
7d0f747afcb8 move server network to tokio
alfadur
parents: 15822
diff changeset
    36
            return Ok(());
14815
fc2cfec95d86 a bit more option error handling
alfadur
parents: 14814
diff changeset
    37
        }
14813
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14804
diff changeset
    38
    };
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14804
diff changeset
    39
    if matches.opt_present("h") {
14815
fc2cfec95d86 a bit more option error handling
alfadur
parents: 14814
diff changeset
    40
        println!("{}", opts.usage(PROGRAM_NAME));
15853
7d0f747afcb8 move server network to tokio
alfadur
parents: 15822
diff changeset
    41
        return Ok(());
14813
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14804
diff changeset
    42
    }
12125
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    43
14851
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14824
diff changeset
    44
    let port = matches
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14824
diff changeset
    45
        .opt_str("p")
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14824
diff changeset
    46
        .and_then(|s| u16::from_str(&s).ok())
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14824
diff changeset
    47
        .unwrap_or(46631);
15853
7d0f747afcb8 move server network to tokio
alfadur
parents: 15822
diff changeset
    48
    let address: SocketAddr = format!("0.0.0.0:{}", port).parse().unwrap();
14851
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14824
diff changeset
    49
15853
7d0f747afcb8 move server network to tokio
alfadur
parents: 15822
diff changeset
    50
    let server = tokio::net::TcpListener::bind(address).await.unwrap();
12125
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    51
15853
7d0f747afcb8 move server network to tokio
alfadur
parents: 15822
diff changeset
    52
    let mut hw_network = NetworkLayerBuilder::default().with_listener(server).build();
14937
8750530bf7e7 avoid crashing server in the event loop
alfadur
parents: 14851
diff changeset
    53
15853
7d0f747afcb8 move server network to tokio
alfadur
parents: 15822
diff changeset
    54
    hw_network.run().await;
7d0f747afcb8 move server network to tokio
alfadur
parents: 15822
diff changeset
    55
    Ok(())
12125
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    56
}