gameServer2/src/main.rs
changeset 12125 858bf4d04c54
child 12126 4348997e502b
equal deleted inserted replaced
12124:ffc7bb9fde01 12125:858bf4d04c54
       
     1 extern crate rand;
       
     2 extern crate mio;
       
     3 
       
     4 use std::io::*;
       
     5 //use rand::Rng;
       
     6 //use std::cmp::Ordering;
       
     7 use mio::tcp::*;
       
     8 use mio::*;
       
     9 
       
    10 mod utils;
       
    11 
       
    12 const SERVER: mio::Token = mio::Token(0);
       
    13 
       
    14 
       
    15 
       
    16 fn main() {
       
    17     println!("Hedgewars game server, protocol {}", utils::PROTOCOL_VERSION);
       
    18 
       
    19     let address = "0.0.0.0:46631".parse().unwrap();
       
    20     let server = TcpListener::bind(&address).unwrap();
       
    21 
       
    22     let poll = Poll::new().unwrap();
       
    23     poll.register(&server, SERVER, Ready::readable(),
       
    24                PollOpt::edge()).unwrap();
       
    25 
       
    26     let mut events = Events::with_capacity(1024);
       
    27 
       
    28     loop {
       
    29         poll.poll(&mut events, None).unwrap();
       
    30 
       
    31         for event in events.iter() {
       
    32             match event.token() {
       
    33                 SERVER => match server.accept() {
       
    34                     Ok((mut client_stream, addr)) => {
       
    35                         println!("Connected: {}", addr);
       
    36                         client_stream.write_all(
       
    37                             format!("CONNECTED\nHedgewars server http://www.hedgewars.org/\n{}\n\n"
       
    38                                     , utils::PROTOCOL_VERSION).as_bytes()
       
    39                         );
       
    40                     },
       
    41                     _ => unreachable!()
       
    42                 },
       
    43 
       
    44                 _ => unreachable!(),
       
    45             }
       
    46         }
       
    47     }
       
    48 }