rust/hedgewars-server/src/main.rs
author alfadur
Mon, 15 Apr 2019 21:22:51 +0300
changeset 14824 92225a708bda
parent 14815 fc2cfec95d86
child 14851 8ddb5842fe0b
permissions -rw-r--r--
add client timeouts
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
14813
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14804
diff changeset
     4
extern crate getopts;
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14804
diff changeset
     5
use getopts::Options;
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
     6
use log::*;
12853
a9d105dc5c95 Improve this code a bit more
unc0rr
parents: 12852
diff changeset
     7
use mio::net::*;
12125
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
     8
use mio::*;
14815
fc2cfec95d86 a bit more option error handling
alfadur
parents: 14814
diff changeset
     9
use std::env;
12125
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    10
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    11
mod protocol;
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
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
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13529
diff changeset
    15
use crate::server::network::NetworkLayer;
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    16
use std::time::Duration;
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
    17
14815
fc2cfec95d86 a bit more option error handling
alfadur
parents: 14814
diff changeset
    18
const PROGRAM_NAME: &'_ str = "Hedgewars Game Server";
fc2cfec95d86 a bit more option error handling
alfadur
parents: 14814
diff changeset
    19
12125
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    20
fn main() {
13769
c5edfcfac68b Bump dependencies
alfadur
parents: 13713
diff changeset
    21
    env_logger::init();
12137
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12133
diff changeset
    22
14813
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14804
diff changeset
    23
    let args: Vec<String> = env::args().collect();
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14804
diff changeset
    24
    let mut opts = Options::new();
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14804
diff changeset
    25
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14804
diff changeset
    26
    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
    27
    opts.optflag("h", "help", "help");
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14804
diff changeset
    28
    let matches = match opts.parse(&args[1..]) {
14815
fc2cfec95d86 a bit more option error handling
alfadur
parents: 14814
diff changeset
    29
        Ok(m) => m,
fc2cfec95d86 a bit more option error handling
alfadur
parents: 14814
diff changeset
    30
        Err(e) => {
fc2cfec95d86 a bit more option error handling
alfadur
parents: 14814
diff changeset
    31
            println!("{}\n{}", e, opts.short_usage(""));
fc2cfec95d86 a bit more option error handling
alfadur
parents: 14814
diff changeset
    32
            return;
fc2cfec95d86 a bit more option error handling
alfadur
parents: 14814
diff changeset
    33
        }
14813
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14804
diff changeset
    34
    };
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14804
diff changeset
    35
    if matches.opt_present("h") {
14815
fc2cfec95d86 a bit more option error handling
alfadur
parents: 14814
diff changeset
    36
        println!("{}", opts.usage(PROGRAM_NAME));
14813
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14804
diff changeset
    37
        return;
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14804
diff changeset
    38
    }
14804
b3adc030104b implement server vars
alfadur
parents: 14800
diff changeset
    39
    info!("Hedgewars game server, protocol {}", utils::SERVER_VERSION);
12125
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    40
14813
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14804
diff changeset
    41
    let address;
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14804
diff changeset
    42
    if matches.opt_present("p") {
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14804
diff changeset
    43
        match matches.opt_str("p") {
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14804
diff changeset
    44
            Some(x) => address = format!("0.0.0.0:{}", x).parse().unwrap(),
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14804
diff changeset
    45
            None => address = "0.0.0.0:46631".parse().unwrap(),
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14804
diff changeset
    46
        }
14815
fc2cfec95d86 a bit more option error handling
alfadur
parents: 14814
diff changeset
    47
    } else {
14813
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14804
diff changeset
    48
        address = "0.0.0.0:46631".parse().unwrap();
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14804
diff changeset
    49
    }
38e66519e585 ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents: 14804
diff changeset
    50
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents: 12125
diff changeset
    51
    let listener = TcpListener::bind(&address).unwrap();
12125
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    52
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    53
    let poll = Poll::new().unwrap();
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
    54
    let mut hw_network = NetworkLayer::new(listener, 1024, 512);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
    55
    hw_network.register_server(&poll).unwrap();
12125
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    56
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    57
    let mut events = Events::with_capacity(1024);
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    58
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    59
    loop {
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    60
        let timeout = if hw_network.has_pending_operations() {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    61
            Some(Duration::from_millis(1))
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    62
        } else {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    63
            None
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    64
        };
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    65
        poll.poll(&mut events, timeout).unwrap();
12125
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    66
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    67
        for event in events.iter() {
12853
a9d105dc5c95 Improve this code a bit more
unc0rr
parents: 12852
diff changeset
    68
            if event.readiness() & Ready::readable() == Ready::readable() {
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    69
                match event.token() {
14800
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14712
diff changeset
    70
                    utils::SERVER_TOKEN => hw_network.accept_client(&poll).unwrap(),
14824
92225a708bda add client timeouts
alfadur
parents: 14815
diff changeset
    71
                    utils::TIMER_TOKEN => hw_network.handle_timeout(&poll).unwrap(),
14800
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14712
diff changeset
    72
                    #[cfg(feature = "official-server")]
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14712
diff changeset
    73
                    utils::IO_TOKEN => hw_network.handle_io_result(),
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
    74
                    Token(tok) => hw_network.client_readable(&poll, tok).unwrap(),
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    75
                }
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    76
            }
12853
a9d105dc5c95 Improve this code a bit more
unc0rr
parents: 12852
diff changeset
    77
            if event.readiness() & Ready::writable() == Ready::writable() {
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    78
                match event.token() {
14824
92225a708bda add client timeouts
alfadur
parents: 14815
diff changeset
    79
                    utils::SERVER_TOKEN | utils::TIMER_TOKEN | utils::IO_TOKEN => unreachable!(),
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
    80
                    Token(tok) => hw_network.client_writable(&poll, tok).unwrap(),
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    81
                }
12125
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    82
            }
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    83
            //            if event.kind().is_hup() || event.kind().is_error() {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    84
            //                match event.token() {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    85
            //                    utils::SERVER => unreachable!(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    86
            //                    Token(tok) => server.client_error(&poll, tok).unwrap(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    87
            //                }
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    88
            //            }
12125
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    89
        }
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    90
        hw_network.on_idle(&poll).unwrap();
12125
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    91
    }
858bf4d04c54 Start server implementation in rust
unc0rr
parents:
diff changeset
    92
}