gameServer2/src/main.rs
author Wuzzy <Wuzzy2@mail.ru>
Tue, 28 Aug 2018 05:46:33 +0200
changeset 13715 0da36902e5b6
parent 13671 09f4a30e50cc
child 13801 59ea2403f62d
permissions -rw-r--r--
Space Invasion: Continue playing rounds in case the teams are tied at the end Rules in case of a tie: 1) Eliminate all teams not tied for the lead 2) Play another round with the remaining teams 3) Check for the winner again at the end of that round. If there's another tie, repeat the procedure

#![allow(unused_imports)]
#![deny(bare_trait_objects)]
#![feature(rust_2018_preview)]

extern crate rand;
extern crate mio;
extern crate slab;
extern crate netbuf;
extern crate base64;
#[macro_use]
extern crate nom;
#[macro_use]
extern crate log;
extern crate env_logger;
#[macro_use] extern crate proptest;
#[macro_use] extern crate bitflags;
extern crate serde;
extern crate serde_yaml;
#[macro_use] extern crate serde_derive;

//use std::io::*;
//use rand::Rng;
//use std::cmp::Ordering;
use mio::net::*;
use mio::*;

mod utils;
mod server;
mod protocol;

use crate::server::network::NetworkLayer;
use std::time::Duration;

fn main() {
    env_logger::init().unwrap();

    info!("Hedgewars game server, protocol {}", utils::PROTOCOL_VERSION);

    let address = "0.0.0.0:46631".parse().unwrap();
    let listener = TcpListener::bind(&address).unwrap();

    let poll = Poll::new().unwrap();
    let mut hw_network = NetworkLayer::new(listener, 1024, 512);
    hw_network.register_server(&poll).unwrap();

    let mut events = Events::with_capacity(1024);

    loop {
        let timeout = if hw_network.has_pending_operations() {
            Some(Duration::from_millis(1))
        } else {
            None
        };
        poll.poll(&mut events, timeout).unwrap();

        for event in events.iter() {
            if event.readiness() & Ready::readable() == Ready::readable() {
                match event.token() {
                    utils::SERVER => hw_network.accept_client(&poll).unwrap(),
                    Token(tok) => hw_network.client_readable(&poll, tok).unwrap(),
                }
            }
            if event.readiness() & Ready::writable() == Ready::writable() {
                match event.token() {
                    utils::SERVER => unreachable!(),
                    Token(tok) => hw_network.client_writable(&poll, tok).unwrap(),
                }
            }
//            if event.kind().is_hup() || event.kind().is_error() {
//                match event.token() {
//                    utils::SERVER => unreachable!(),
//                    Token(tok) => server.client_error(&poll, tok).unwrap(),
//                }
//            }
        }
        hw_network.on_idle(&poll).unwrap();
    }
}