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