rust/hedgewars-server/src/main.rs
changeset 14851 8ddb5842fe0b
parent 14824 92225a708bda
child 14937 8750530bf7e7
equal deleted inserted replaced
14850:f56936207a65 14851:8ddb5842fe0b
     1 #![allow(unused_imports)]
     1 #![allow(unused_imports)]
     2 #![deny(bare_trait_objects)]
     2 #![deny(bare_trait_objects)]
     3 
     3 
     4 extern crate getopts;
       
     5 use getopts::Options;
     4 use getopts::Options;
     6 use log::*;
     5 use log::*;
     7 use mio::net::*;
     6 use mio::{net::*, *};
     8 use mio::*;
     7 use std::{env, str::FromStr as _, time::Duration};
     9 use std::env;
       
    10 
     8 
    11 mod protocol;
     9 mod protocol;
    12 mod server;
    10 mod server;
    13 mod utils;
    11 mod utils;
    14 
    12 
    15 use crate::server::network::NetworkLayer;
    13 use crate::server::network::{NetworkLayer, NetworkLayerBuilder};
    16 use std::time::Duration;
       
    17 
    14 
    18 const PROGRAM_NAME: &'_ str = "Hedgewars Game Server";
    15 const PROGRAM_NAME: &'_ str = "Hedgewars Game Server";
    19 
    16 
    20 fn main() {
    17 fn main() {
    21     env_logger::init();
    18     env_logger::init();
       
    19 
       
    20     info!("Hedgewars game server, protocol {}", utils::SERVER_VERSION);
    22 
    21 
    23     let args: Vec<String> = env::args().collect();
    22     let args: Vec<String> = env::args().collect();
    24     let mut opts = Options::new();
    23     let mut opts = Options::new();
    25 
    24 
    26     opts.optopt("p", "port", "port - defaults to 46631", "PORT");
    25     opts.optopt("p", "port", "port - defaults to 46631", "PORT");
    34     };
    33     };
    35     if matches.opt_present("h") {
    34     if matches.opt_present("h") {
    36         println!("{}", opts.usage(PROGRAM_NAME));
    35         println!("{}", opts.usage(PROGRAM_NAME));
    37         return;
    36         return;
    38     }
    37     }
    39     info!("Hedgewars game server, protocol {}", utils::SERVER_VERSION);
       
    40 
    38 
    41     let address;
    39     let port = matches
    42     if matches.opt_present("p") {
    40         .opt_str("p")
    43         match matches.opt_str("p") {
    41         .and_then(|s| u16::from_str(&s).ok())
    44             Some(x) => address = format!("0.0.0.0:{}", x).parse().unwrap(),
    42         .unwrap_or(46631);
    45             None => address = "0.0.0.0:46631".parse().unwrap(),
    43     let address = format!("0.0.0.0:{}", port).parse().unwrap();
    46         }
       
    47     } else {
       
    48         address = "0.0.0.0:46631".parse().unwrap();
       
    49     }
       
    50 
    44 
    51     let listener = TcpListener::bind(&address).unwrap();
    45     let listener = TcpListener::bind(&address).unwrap();
    52 
    46 
    53     let poll = Poll::new().unwrap();
    47     let poll = Poll::new().unwrap();
    54     let mut hw_network = NetworkLayer::new(listener, 1024, 512);
    48     let mut hw_builder = NetworkLayerBuilder::default().with_listener(listener);
    55     hw_network.register_server(&poll).unwrap();
    49 
       
    50     #[cfg(feature = "tls-connections")]
       
    51     {
       
    52         let address = format!("0.0.0.0:{}", port + 1).parse().unwrap();
       
    53         hw_builder = hw_builder.with_secure_listener(TcpListener::bind(&address).unwrap());
       
    54     }
       
    55 
       
    56     let mut hw_network = hw_builder.build();
       
    57     hw_network.register(&poll).unwrap();
    56 
    58 
    57     let mut events = Events::with_capacity(1024);
    59     let mut events = Events::with_capacity(1024);
    58 
    60 
    59     loop {
    61     loop {
    60         let timeout = if hw_network.has_pending_operations() {
    62         let timeout = if hw_network.has_pending_operations() {
    65         poll.poll(&mut events, timeout).unwrap();
    67         poll.poll(&mut events, timeout).unwrap();
    66 
    68 
    67         for event in events.iter() {
    69         for event in events.iter() {
    68             if event.readiness() & Ready::readable() == Ready::readable() {
    70             if event.readiness() & Ready::readable() == Ready::readable() {
    69                 match event.token() {
    71                 match event.token() {
    70                     utils::SERVER_TOKEN => hw_network.accept_client(&poll).unwrap(),
    72                     token @ utils::SERVER_TOKEN | token @ utils::SECURE_SERVER_TOKEN => {
       
    73                         hw_network.accept_client(&poll, token).unwrap()
       
    74                     }
    71                     utils::TIMER_TOKEN => hw_network.handle_timeout(&poll).unwrap(),
    75                     utils::TIMER_TOKEN => hw_network.handle_timeout(&poll).unwrap(),
    72                     #[cfg(feature = "official-server")]
    76                     #[cfg(feature = "official-server")]
    73                     utils::IO_TOKEN => hw_network.handle_io_result(),
    77                     utils::IO_TOKEN => hw_network.handle_io_result(),
    74                     Token(tok) => hw_network.client_readable(&poll, tok).unwrap(),
    78                     Token(tok) => hw_network.client_readable(&poll, tok).unwrap(),
    75                 }
    79                 }