gameServer2/src/main.rs
branchios-develop
changeset 13413 ba39a1d396c0
parent 13119 1e39b8749072
child 13414 28b314ad566d
equal deleted inserted replaced
13411:6e8b807bda4b 13413:ba39a1d396c0
       
     1 #![allow(unused_imports)]
       
     2 
     1 extern crate rand;
     3 extern crate rand;
     2 extern crate mio;
     4 extern crate mio;
     3 extern crate slab;
     5 extern crate slab;
     4 extern crate netbuf;
     6 extern crate netbuf;
     5 #[macro_use]
     7 #[macro_use]
     6 extern crate nom;
     8 extern crate nom;
     7 #[macro_use]
     9 #[macro_use]
     8 extern crate log;
    10 extern crate log;
     9 extern crate env_logger;
    11 extern crate env_logger;
       
    12 #[macro_use] extern crate proptest;
    10 
    13 
    11 //use std::io::*;
    14 //use std::io::*;
    12 //use rand::Rng;
    15 //use rand::Rng;
    13 //use std::cmp::Ordering;
    16 //use std::cmp::Ordering;
    14 use mio::tcp::*;
    17 use mio::net::*;
    15 use mio::*;
    18 use mio::*;
    16 
    19 
    17 mod utils;
    20 mod utils;
    18 mod server;
    21 mod server;
    19 mod protocol;
    22 mod protocol;
       
    23 
       
    24 use server::network::NetworkLayer;
    20 
    25 
    21 fn main() {
    26 fn main() {
    22     env_logger::init().unwrap();
    27     env_logger::init().unwrap();
    23 
    28 
    24     info!("Hedgewars game server, protocol {}", utils::PROTOCOL_VERSION);
    29     info!("Hedgewars game server, protocol {}", utils::PROTOCOL_VERSION);
    25 
    30 
    26     let address = "0.0.0.0:46631".parse().unwrap();
    31     let address = "0.0.0.0:46631".parse().unwrap();
    27     let listener = TcpListener::bind(&address).unwrap();
    32     let listener = TcpListener::bind(&address).unwrap();
    28     let mut server = server::server::HWServer::new(listener, 1024, 512);
       
    29 
    33 
    30     let poll = Poll::new().unwrap();
    34     let poll = Poll::new().unwrap();
    31     server.register(&poll).unwrap();
    35     let mut hw_network = NetworkLayer::new(listener, 1024, 512);
       
    36     hw_network.register_server(&poll).unwrap();
    32 
    37 
    33     let mut events = Events::with_capacity(1024);
    38     let mut events = Events::with_capacity(1024);
    34 
    39 
    35     loop {
    40     loop {
    36         poll.poll(&mut events, None).unwrap();
    41         poll.poll(&mut events, None).unwrap();
    37 
    42 
    38         for event in events.iter() {
    43         for event in events.iter() {
    39             if event.kind().is_readable() {
    44             if event.readiness() & Ready::readable() == Ready::readable() {
    40                 match event.token() {
    45                 match event.token() {
    41                     utils::SERVER => server.accept(&poll).unwrap(),
    46                     utils::SERVER => hw_network.accept_client(&poll).unwrap(),
    42                     tok => server.client_readable(&poll, tok).unwrap(),
    47                     Token(tok) => hw_network.client_readable(&poll, tok).unwrap(),
    43                 }
    48                 }
    44             }
    49             }
    45             if event.kind().is_writable() {
    50             if event.readiness() & Ready::writable() == Ready::writable() {
    46                 match event.token() {
    51                 match event.token() {
    47                     utils::SERVER => unreachable!(),
    52                     utils::SERVER => unreachable!(),
    48                     tok => server.client_writable(&poll, tok).unwrap(),
    53                     Token(tok) => hw_network.client_writable(&poll, tok).unwrap(),
    49                 }
    54                 }
    50             }
    55             }
    51             if event.kind().is_hup() || event.kind().is_error() {
    56 //            if event.kind().is_hup() || event.kind().is_error() {
    52                 match event.token() {
    57 //                match event.token() {
    53                     utils::SERVER => unreachable!(),
    58 //                    utils::SERVER => unreachable!(),
    54                     tok => server.client_error(&poll, tok).unwrap(),
    59 //                    Token(tok) => server.client_error(&poll, tok).unwrap(),
    55                 }
    60 //                }
    56             }
    61 //            }
    57         }
    62         }
    58     }
    63     }
    59 }
    64 }