rust/hedgewars-server/src/main.rs
author Wuzzy <Wuzzy2@mail.ru>
Sun, 05 Jul 2020 01:58:48 +0200
branchui-scaling
changeset 15671 f28ca5a87682
parent 15102 80ff12edf5e6
child 15795 40929af15167
permissions -rw-r--r--
[backport] Chat scale: Move setting to Video tab Backport of 9455b9e56aee to branch ui-scaling by sheepluva
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
     1
#![allow(unused_imports)]
13421
d1368c776a4f Enable all lints from the rust-2018-idioms suite.
marmistrz
parents: 13414
diff changeset
     2
#![deny(bare_trait_objects)]
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
     3
14792
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14783
diff changeset
     4
use getopts::Options;
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
     5
use log::*;
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14803
diff changeset
     6
use mio::{net::*, *};
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14803
diff changeset
     7
use std::{env, str::FromStr as _, time::Duration};
12125
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
     8
15074
c5a6e8566425 shuffle server files
alfadur
parents: 14916
diff changeset
     9
mod core;
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    10
mod handlers;
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    11
mod protocol;
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    12
mod server;
12125
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    13
mod utils;
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    14
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14803
diff changeset
    15
use crate::server::network::{NetworkLayer, NetworkLayerBuilder};
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
    16
14794
fc2cfec95d86 a bit more option error handling
alfadur
parents: 14793
diff changeset
    17
const PROGRAM_NAME: &'_ str = "Hedgewars Game Server";
fc2cfec95d86 a bit more option error handling
alfadur
parents: 14793
diff changeset
    18
12125
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    19
fn main() {
13797
c5edfcfac68b Bump dependencies
alfadur
parents: 13796
diff changeset
    20
    env_logger::init();
12137
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12133
diff changeset
    21
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14803
diff changeset
    22
    info!("Hedgewars game server, protocol {}", utils::SERVER_VERSION);
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14803
diff changeset
    23
14792
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14783
diff changeset
    24
    let args: Vec<String> = env::args().collect();
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14783
diff changeset
    25
    let mut opts = Options::new();
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14783
diff changeset
    26
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14783
diff changeset
    27
    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
    28
    opts.optflag("h", "help", "help");
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14783
diff changeset
    29
    let matches = match opts.parse(&args[1..]) {
14794
fc2cfec95d86 a bit more option error handling
alfadur
parents: 14793
diff changeset
    30
        Ok(m) => m,
fc2cfec95d86 a bit more option error handling
alfadur
parents: 14793
diff changeset
    31
        Err(e) => {
fc2cfec95d86 a bit more option error handling
alfadur
parents: 14793
diff changeset
    32
            println!("{}\n{}", e, opts.short_usage(""));
fc2cfec95d86 a bit more option error handling
alfadur
parents: 14793
diff changeset
    33
            return;
fc2cfec95d86 a bit more option error handling
alfadur
parents: 14793
diff changeset
    34
        }
14792
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14783
diff changeset
    35
    };
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14783
diff changeset
    36
    if matches.opt_present("h") {
14794
fc2cfec95d86 a bit more option error handling
alfadur
parents: 14793
diff changeset
    37
        println!("{}", opts.usage(PROGRAM_NAME));
14792
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14783
diff changeset
    38
        return;
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14783
diff changeset
    39
    }
12125
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    40
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14803
diff changeset
    41
    let port = matches
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14803
diff changeset
    42
        .opt_str("p")
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14803
diff changeset
    43
        .and_then(|s| u16::from_str(&s).ok())
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14803
diff changeset
    44
        .unwrap_or(46631);
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14803
diff changeset
    45
    let address = format!("0.0.0.0:{}", port).parse().unwrap();
14792
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14783
diff changeset
    46
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents: 12125
diff changeset
    47
    let listener = TcpListener::bind(&address).unwrap();
12125
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    48
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    49
    let poll = Poll::new().unwrap();
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14803
diff changeset
    50
    let mut hw_builder = NetworkLayerBuilder::default().with_listener(listener);
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14803
diff changeset
    51
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14803
diff changeset
    52
    #[cfg(feature = "tls-connections")]
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14803
diff changeset
    53
    {
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14803
diff changeset
    54
        let address = format!("0.0.0.0:{}", port + 1).parse().unwrap();
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14803
diff changeset
    55
        hw_builder = hw_builder.with_secure_listener(TcpListener::bind(&address).unwrap());
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14803
diff changeset
    56
    }
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14803
diff changeset
    57
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14803
diff changeset
    58
    let mut hw_network = hw_builder.build();
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14803
diff changeset
    59
    hw_network.register(&poll).unwrap();
12125
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    60
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    61
    let mut events = Events::with_capacity(1024);
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    62
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    63
    loop {
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    64
        let timeout = if hw_network.has_pending_operations() {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    65
            Some(Duration::from_millis(1))
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    66
        } else {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    67
            None
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    68
        };
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    69
        poll.poll(&mut events, timeout).unwrap();
12125
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    70
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    71
        for event in events.iter() {
12853
a9d105dc5c95 Improve this code a bit more
unc0rr
parents: 12852
diff changeset
    72
            if event.readiness() & Ready::readable() == Ready::readable() {
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    73
                match event.token() {
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14803
diff changeset
    74
                    token @ utils::SERVER_TOKEN | token @ utils::SECURE_SERVER_TOKEN => {
14916
8750530bf7e7 avoid crashing server in the event loop
alfadur
parents: 14830
diff changeset
    75
                        match hw_network.accept_client(&poll, token) {
8750530bf7e7 avoid crashing server in the event loop
alfadur
parents: 14830
diff changeset
    76
                            Ok(()) => (),
8750530bf7e7 avoid crashing server in the event loop
alfadur
parents: 14830
diff changeset
    77
                            Err(e) => debug!("Error accepting client: {}", e),
8750530bf7e7 avoid crashing server in the event loop
alfadur
parents: 14830
diff changeset
    78
                        }
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14803
diff changeset
    79
                    }
14916
8750530bf7e7 avoid crashing server in the event loop
alfadur
parents: 14830
diff changeset
    80
                    utils::TIMER_TOKEN => match hw_network.handle_timeout(&poll) {
8750530bf7e7 avoid crashing server in the event loop
alfadur
parents: 14830
diff changeset
    81
                        Ok(()) => (),
8750530bf7e7 avoid crashing server in the event loop
alfadur
parents: 14830
diff changeset
    82
                        Err(e) => debug!("Error in timer event: {}", e),
8750530bf7e7 avoid crashing server in the event loop
alfadur
parents: 14830
diff changeset
    83
                    },
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14691
diff changeset
    84
                    #[cfg(feature = "official-server")]
15102
80ff12edf5e6 handle response from IO result handler
alfadur
parents: 15075
diff changeset
    85
                    utils::IO_TOKEN => match hw_network.handle_io_result(&poll) {
14916
8750530bf7e7 avoid crashing server in the event loop
alfadur
parents: 14830
diff changeset
    86
                        Ok(()) => (),
8750530bf7e7 avoid crashing server in the event loop
alfadur
parents: 14830
diff changeset
    87
                        Err(e) => debug!("Error in IO task: {}", e),
8750530bf7e7 avoid crashing server in the event loop
alfadur
parents: 14830
diff changeset
    88
                    },
8750530bf7e7 avoid crashing server in the event loop
alfadur
parents: 14830
diff changeset
    89
                    Token(token) => match hw_network.client_readable(&poll, token) {
8750530bf7e7 avoid crashing server in the event loop
alfadur
parents: 14830
diff changeset
    90
                        Ok(()) => (),
8750530bf7e7 avoid crashing server in the event loop
alfadur
parents: 14830
diff changeset
    91
                        Err(e) => debug!("Error reading from client socket {}: {}", token, e),
8750530bf7e7 avoid crashing server in the event loop
alfadur
parents: 14830
diff changeset
    92
                    },
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    93
                }
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    94
            }
12853
a9d105dc5c95 Improve this code a bit more
unc0rr
parents: 12852
diff changeset
    95
            if event.readiness() & Ready::writable() == Ready::writable() {
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    96
                match event.token() {
14916
8750530bf7e7 avoid crashing server in the event loop
alfadur
parents: 14830
diff changeset
    97
                    utils::SERVER_TOKEN
8750530bf7e7 avoid crashing server in the event loop
alfadur
parents: 14830
diff changeset
    98
                    | utils::SECURE_SERVER_TOKEN
8750530bf7e7 avoid crashing server in the event loop
alfadur
parents: 14830
diff changeset
    99
                    | utils::TIMER_TOKEN
8750530bf7e7 avoid crashing server in the event loop
alfadur
parents: 14830
diff changeset
   100
                    | utils::IO_TOKEN => unreachable!(),
8750530bf7e7 avoid crashing server in the event loop
alfadur
parents: 14830
diff changeset
   101
                    Token(token) => match hw_network.client_writable(&poll, token) {
8750530bf7e7 avoid crashing server in the event loop
alfadur
parents: 14830
diff changeset
   102
                        Ok(()) => (),
8750530bf7e7 avoid crashing server in the event loop
alfadur
parents: 14830
diff changeset
   103
                        Err(e) => debug!("Error writing to client socket {}: {}", token, e),
8750530bf7e7 avoid crashing server in the event loop
alfadur
parents: 14830
diff changeset
   104
                    },
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
   105
                }
12125
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
   106
            }
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
   107
        }
14916
8750530bf7e7 avoid crashing server in the event loop
alfadur
parents: 14830
diff changeset
   108
8750530bf7e7 avoid crashing server in the event loop
alfadur
parents: 14830
diff changeset
   109
        match hw_network.on_idle(&poll) {
8750530bf7e7 avoid crashing server in the event loop
alfadur
parents: 14830
diff changeset
   110
            Ok(()) => (),
8750530bf7e7 avoid crashing server in the event loop
alfadur
parents: 14830
diff changeset
   111
            Err(e) => debug!("Error in idle handler: {}", e),
8750530bf7e7 avoid crashing server in the event loop
alfadur
parents: 14830
diff changeset
   112
        };
12125
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
   113
    }
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
   114
}