Start server implementation in rust
authorunc0rr
Sat, 31 Dec 2016 23:56:29 +0300
changeset 12130 858bf4d04c54
parent 12129 ffc7bb9fde01
child 12131 4348997e502b
Start server implementation in rust
gameServer2/Cargo.toml
gameServer2/src/main.rs
gameServer2/src/utils.rs
--- /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 <a.korotaev@hedgewars.org>" ]
+
+[dependencies]
+rand = "0.3"
+mio = "0.6"
+
--- /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!(),
+            }
+        }
+    }
+}
--- /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;