# HG changeset patch # User unc0rr # Date 1483217789 -10800 # Node ID 858bf4d04c541c76d5d4bff5a0445af41c404b3a # Parent ffc7bb9fde01f23c0a6a8754b8b200c5589b8200 Start server implementation in rust diff -r ffc7bb9fde01 -r 858bf4d04c54 gameServer2/Cargo.toml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gameServer2/Cargo.toml Sat Dec 31 23:56:29 2016 +0300 @@ -0,0 +1,10 @@ +[package] + +name = "hedgewars-server" +version = "0.0.1" +authors = [ "Andrey Korotaev " ] + +[dependencies] +rand = "0.3" +mio = "0.6" + diff -r ffc7bb9fde01 -r 858bf4d04c54 gameServer2/src/main.rs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gameServer2/src/main.rs Sat Dec 31 23:56:29 2016 +0300 @@ -0,0 +1,48 @@ +extern crate rand; +extern crate mio; + +use std::io::*; +//use rand::Rng; +//use std::cmp::Ordering; +use mio::tcp::*; +use mio::*; + +mod utils; + +const SERVER: mio::Token = mio::Token(0); + + + +fn main() { + println!("Hedgewars game server, protocol {}", utils::PROTOCOL_VERSION); + + let address = "0.0.0.0:46631".parse().unwrap(); + let server = TcpListener::bind(&address).unwrap(); + + let poll = Poll::new().unwrap(); + poll.register(&server, SERVER, Ready::readable(), + PollOpt::edge()).unwrap(); + + let mut events = Events::with_capacity(1024); + + loop { + poll.poll(&mut events, None).unwrap(); + + for event in events.iter() { + match event.token() { + SERVER => match server.accept() { + Ok((mut client_stream, addr)) => { + println!("Connected: {}", addr); + client_stream.write_all( + format!("CONNECTED\nHedgewars server http://www.hedgewars.org/\n{}\n\n" + , utils::PROTOCOL_VERSION).as_bytes() + ); + }, + _ => unreachable!() + }, + + _ => unreachable!(), + } + } + } +} diff -r ffc7bb9fde01 -r 858bf4d04c54 gameServer2/src/utils.rs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gameServer2/src/utils.rs Sat Dec 31 23:56:29 2016 +0300 @@ -0,0 +1,1 @@ +pub const PROTOCOL_VERSION : i32 = 3;