gameServer2/src/main.rs
changeset 12125 858bf4d04c54
child 12126 4348997e502b
--- /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!(),
+            }
+        }
+    }
+}