author | marmistrz |
Sun, 24 Jun 2018 12:09:31 -0400 | |
changeset 13426 | d1368c776a4f |
parent 13419 | 28b314ad566d |
child 13428 | 87a6cad20c90 |
permissions | -rw-r--r-- |
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12858
diff
changeset
|
1 |
#![allow(unused_imports)] |
13426
d1368c776a4f
Enable all lints from the rust-2018-idioms suite.
marmistrz
parents:
13419
diff
changeset
|
2 |
#![deny(bare_trait_objects)] |
d1368c776a4f
Enable all lints from the rust-2018-idioms suite.
marmistrz
parents:
13419
diff
changeset
|
3 |
#![warn(unreachable_pub)] |
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12858
diff
changeset
|
4 |
|
12130 | 5 |
extern crate rand; |
6 |
extern crate mio; |
|
12131 | 7 |
extern crate slab; |
12132 | 8 |
extern crate netbuf; |
12138 | 9 |
#[macro_use] |
10 |
extern crate nom; |
|
12142
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12138
diff
changeset
|
11 |
#[macro_use] |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12138
diff
changeset
|
12 |
extern crate log; |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12138
diff
changeset
|
13 |
extern crate env_logger; |
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12858
diff
changeset
|
14 |
#[macro_use] extern crate proptest; |
12130 | 15 |
|
12131 | 16 |
//use std::io::*; |
12130 | 17 |
//use rand::Rng; |
18 |
//use std::cmp::Ordering; |
|
12858 | 19 |
use mio::net::*; |
12130 | 20 |
use mio::*; |
21 |
||
22 |
mod utils; |
|
12131 | 23 |
mod server; |
12134 | 24 |
mod protocol; |
12130 | 25 |
|
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12858
diff
changeset
|
26 |
use server::network::NetworkLayer; |
13419 | 27 |
use std::time::Duration; |
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12858
diff
changeset
|
28 |
|
12130 | 29 |
fn main() { |
12142
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12138
diff
changeset
|
30 |
env_logger::init().unwrap(); |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12138
diff
changeset
|
31 |
|
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12138
diff
changeset
|
32 |
info!("Hedgewars game server, protocol {}", utils::PROTOCOL_VERSION); |
12130 | 33 |
|
34 |
let address = "0.0.0.0:46631".parse().unwrap(); |
|
12131 | 35 |
let listener = TcpListener::bind(&address).unwrap(); |
12130 | 36 |
|
37 |
let poll = Poll::new().unwrap(); |
|
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12858
diff
changeset
|
38 |
let mut hw_network = NetworkLayer::new(listener, 1024, 512); |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12858
diff
changeset
|
39 |
hw_network.register_server(&poll).unwrap(); |
12130 | 40 |
|
41 |
let mut events = Events::with_capacity(1024); |
|
42 |
||
43 |
loop { |
|
13419 | 44 |
let timeout = if hw_network.has_pending_operations() { |
45 |
Some(Duration::from_millis(1)) |
|
46 |
} else { |
|
47 |
None |
|
48 |
}; |
|
49 |
poll.poll(&mut events, timeout).unwrap(); |
|
12130 | 50 |
|
51 |
for event in events.iter() { |
|
12858 | 52 |
if event.readiness() & Ready::readable() == Ready::readable() { |
12132 | 53 |
match event.token() { |
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12858
diff
changeset
|
54 |
utils::SERVER => hw_network.accept_client(&poll).unwrap(), |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12858
diff
changeset
|
55 |
Token(tok) => hw_network.client_readable(&poll, tok).unwrap(), |
12132 | 56 |
} |
57 |
} |
|
12858 | 58 |
if event.readiness() & Ready::writable() == Ready::writable() { |
12132 | 59 |
match event.token() { |
60 |
utils::SERVER => unreachable!(), |
|
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12858
diff
changeset
|
61 |
Token(tok) => hw_network.client_writable(&poll, tok).unwrap(), |
12132 | 62 |
} |
12130 | 63 |
} |
12858 | 64 |
// if event.kind().is_hup() || event.kind().is_error() { |
65 |
// match event.token() { |
|
66 |
// utils::SERVER => unreachable!(), |
|
67 |
// Token(tok) => server.client_error(&poll, tok).unwrap(), |
|
68 |
// } |
|
69 |
// } |
|
12130 | 70 |
} |
13419 | 71 |
hw_network.on_idle(&poll).unwrap(); |
12130 | 72 |
} |
73 |
} |