rust/hedgewars-server/src/main.rs
author alfadur
Wed, 01 May 2019 21:59:41 +0300
changeset 14876 0f39f8efbd2e
parent 14835 8ddb5842fe0b
child 14921 8750530bf7e7
permissions -rw-r--r--
add visual notification on room joins
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12858
diff changeset
     1
#![allow(unused_imports)]
13426
d1368c776a4f Enable all lints from the rust-2018-idioms suite.
marmistrz
parents: 13419
diff changeset
     2
#![deny(bare_trait_objects)]
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12858
diff changeset
     3
14797
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14788
diff changeset
     4
use getopts::Options;
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
     5
use log::*;
14835
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14808
diff changeset
     6
use mio::{net::*, *};
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14808
diff changeset
     7
use std::{env, str::FromStr as _, time::Duration};
12130
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
     8
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
     9
mod protocol;
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    10
mod server;
12130
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    11
mod utils;
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    12
14835
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14808
diff changeset
    13
use crate::server::network::{NetworkLayer, NetworkLayerBuilder};
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12858
diff changeset
    14
14799
fc2cfec95d86 a bit more option error handling
alfadur
parents: 14798
diff changeset
    15
const PROGRAM_NAME: &'_ str = "Hedgewars Game Server";
fc2cfec95d86 a bit more option error handling
alfadur
parents: 14798
diff changeset
    16
12130
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    17
fn main() {
13802
c5edfcfac68b Bump dependencies
alfadur
parents: 13801
diff changeset
    18
    env_logger::init();
12142
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12138
diff changeset
    19
14835
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14808
diff changeset
    20
    info!("Hedgewars game server, protocol {}", utils::SERVER_VERSION);
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14808
diff changeset
    21
14797
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14788
diff changeset
    22
    let args: Vec<String> = env::args().collect();
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14788
diff changeset
    23
    let mut opts = Options::new();
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14788
diff changeset
    24
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14788
diff changeset
    25
    opts.optopt("p", "port", "port - defaults to 46631", "PORT");
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14788
diff changeset
    26
    opts.optflag("h", "help", "help");
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14788
diff changeset
    27
    let matches = match opts.parse(&args[1..]) {
14799
fc2cfec95d86 a bit more option error handling
alfadur
parents: 14798
diff changeset
    28
        Ok(m) => m,
fc2cfec95d86 a bit more option error handling
alfadur
parents: 14798
diff changeset
    29
        Err(e) => {
fc2cfec95d86 a bit more option error handling
alfadur
parents: 14798
diff changeset
    30
            println!("{}\n{}", e, opts.short_usage(""));
fc2cfec95d86 a bit more option error handling
alfadur
parents: 14798
diff changeset
    31
            return;
fc2cfec95d86 a bit more option error handling
alfadur
parents: 14798
diff changeset
    32
        }
14797
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14788
diff changeset
    33
    };
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14788
diff changeset
    34
    if matches.opt_present("h") {
14799
fc2cfec95d86 a bit more option error handling
alfadur
parents: 14798
diff changeset
    35
        println!("{}", opts.usage(PROGRAM_NAME));
14797
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14788
diff changeset
    36
        return;
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14788
diff changeset
    37
    }
12130
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    38
14835
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14808
diff changeset
    39
    let port = matches
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14808
diff changeset
    40
        .opt_str("p")
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14808
diff changeset
    41
        .and_then(|s| u16::from_str(&s).ok())
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14808
diff changeset
    42
        .unwrap_or(46631);
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14808
diff changeset
    43
    let address = format!("0.0.0.0:{}", port).parse().unwrap();
14797
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14788
diff changeset
    44
12131
4348997e502b Refactor code to add more structure to it
unc0rr
parents: 12130
diff changeset
    45
    let listener = TcpListener::bind(&address).unwrap();
12130
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    46
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    47
    let poll = Poll::new().unwrap();
14835
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14808
diff changeset
    48
    let mut hw_builder = NetworkLayerBuilder::default().with_listener(listener);
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14808
diff changeset
    49
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14808
diff changeset
    50
    #[cfg(feature = "tls-connections")]
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14808
diff changeset
    51
    {
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14808
diff changeset
    52
        let address = format!("0.0.0.0:{}", port + 1).parse().unwrap();
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14808
diff changeset
    53
        hw_builder = hw_builder.with_secure_listener(TcpListener::bind(&address).unwrap());
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14808
diff changeset
    54
    }
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14808
diff changeset
    55
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14808
diff changeset
    56
    let mut hw_network = hw_builder.build();
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14808
diff changeset
    57
    hw_network.register(&poll).unwrap();
12130
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    58
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    59
    let mut events = Events::with_capacity(1024);
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    60
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    61
    loop {
13419
28b314ad566d handle edge polling properly
alfadur
parents: 13124
diff changeset
    62
        let timeout = if hw_network.has_pending_operations() {
28b314ad566d handle edge polling properly
alfadur
parents: 13124
diff changeset
    63
            Some(Duration::from_millis(1))
28b314ad566d handle edge polling properly
alfadur
parents: 13124
diff changeset
    64
        } else {
28b314ad566d handle edge polling properly
alfadur
parents: 13124
diff changeset
    65
            None
28b314ad566d handle edge polling properly
alfadur
parents: 13124
diff changeset
    66
        };
28b314ad566d handle edge polling properly
alfadur
parents: 13124
diff changeset
    67
        poll.poll(&mut events, timeout).unwrap();
12130
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    68
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    69
        for event in events.iter() {
12858
a9d105dc5c95 Improve this code a bit more
unc0rr
parents: 12857
diff changeset
    70
            if event.readiness() & Ready::readable() == Ready::readable() {
12132
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    71
                match event.token() {
14835
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14808
diff changeset
    72
                    token @ utils::SERVER_TOKEN | token @ utils::SECURE_SERVER_TOKEN => {
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14808
diff changeset
    73
                        hw_network.accept_client(&poll, token).unwrap()
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14808
diff changeset
    74
                    }
14808
92225a708bda add client timeouts
alfadur
parents: 14799
diff changeset
    75
                    utils::TIMER_TOKEN => hw_network.handle_timeout(&poll).unwrap(),
14784
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14696
diff changeset
    76
                    #[cfg(feature = "official-server")]
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14696
diff changeset
    77
                    utils::IO_TOKEN => hw_network.handle_io_result(),
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12858
diff changeset
    78
                    Token(tok) => hw_network.client_readable(&poll, tok).unwrap(),
12132
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    79
                }
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    80
            }
12858
a9d105dc5c95 Improve this code a bit more
unc0rr
parents: 12857
diff changeset
    81
            if event.readiness() & Ready::writable() == Ready::writable() {
12132
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    82
                match event.token() {
14808
92225a708bda add client timeouts
alfadur
parents: 14799
diff changeset
    83
                    utils::SERVER_TOKEN | utils::TIMER_TOKEN | utils::IO_TOKEN => unreachable!(),
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12858
diff changeset
    84
                    Token(tok) => hw_network.client_writable(&poll, tok).unwrap(),
12132
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    85
                }
12130
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    86
            }
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    87
            //            if event.kind().is_hup() || event.kind().is_error() {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    88
            //                match event.token() {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    89
            //                    utils::SERVER => unreachable!(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    90
            //                    Token(tok) => server.client_error(&poll, tok).unwrap(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    91
            //                }
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    92
            //            }
12130
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    93
        }
13419
28b314ad566d handle edge polling properly
alfadur
parents: 13124
diff changeset
    94
        hw_network.on_idle(&poll).unwrap();
12130
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    95
    }
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    96
}