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