author | Wuzzy <Wuzzy2@mail.ru> |
Sat, 29 Jun 2019 19:06:41 +0200 | |
changeset 15219 | 618f36c56950 |
parent 15123 | 80ff12edf5e6 |
child 15817 | 40929af15167 |
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)] |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
3 |
|
14813
38e66519e585
ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents:
14804
diff
changeset
|
4 |
use getopts::Options; |
14478 | 5 |
use log::*; |
14851
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14824
diff
changeset
|
6 |
use mio::{net::*, *}; |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14824
diff
changeset
|
7 |
use std::{env, str::FromStr as _, time::Duration}; |
12125 | 8 |
|
15095 | 9 |
mod core; |
15096 | 10 |
mod handlers; |
14478 | 11 |
mod protocol; |
12 |
mod server; |
|
12125 | 13 |
mod utils; |
14 |
||
14851
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14824
diff
changeset
|
15 |
use crate::server::network::{NetworkLayer, NetworkLayerBuilder}; |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
16 |
|
14815 | 17 |
const PROGRAM_NAME: &'_ str = "Hedgewars Game Server"; |
18 |
||
12125 | 19 |
fn main() { |
13769 | 20 |
env_logger::init(); |
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12133
diff
changeset
|
21 |
|
14851
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14824
diff
changeset
|
22 |
info!("Hedgewars game server, protocol {}", utils::SERVER_VERSION); |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14824
diff
changeset
|
23 |
|
14813
38e66519e585
ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents:
14804
diff
changeset
|
24 |
let args: Vec<String> = env::args().collect(); |
38e66519e585
ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents:
14804
diff
changeset
|
25 |
let mut opts = Options::new(); |
38e66519e585
ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents:
14804
diff
changeset
|
26 |
|
38e66519e585
ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents:
14804
diff
changeset
|
27 |
opts.optopt("p", "port", "port - defaults to 46631", "PORT"); |
38e66519e585
ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents:
14804
diff
changeset
|
28 |
opts.optflag("h", "help", "help"); |
38e66519e585
ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents:
14804
diff
changeset
|
29 |
let matches = match opts.parse(&args[1..]) { |
14815 | 30 |
Ok(m) => m, |
31 |
Err(e) => { |
|
32 |
println!("{}\n{}", e, opts.short_usage("")); |
|
33 |
return; |
|
34 |
} |
|
14813
38e66519e585
ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents:
14804
diff
changeset
|
35 |
}; |
38e66519e585
ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents:
14804
diff
changeset
|
36 |
if matches.opt_present("h") { |
14815 | 37 |
println!("{}", opts.usage(PROGRAM_NAME)); |
14813
38e66519e585
ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents:
14804
diff
changeset
|
38 |
return; |
38e66519e585
ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents:
14804
diff
changeset
|
39 |
} |
12125 | 40 |
|
14851
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14824
diff
changeset
|
41 |
let port = matches |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14824
diff
changeset
|
42 |
.opt_str("p") |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14824
diff
changeset
|
43 |
.and_then(|s| u16::from_str(&s).ok()) |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14824
diff
changeset
|
44 |
.unwrap_or(46631); |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14824
diff
changeset
|
45 |
let address = format!("0.0.0.0:{}", port).parse().unwrap(); |
14813
38e66519e585
ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents:
14804
diff
changeset
|
46 |
|
12126 | 47 |
let listener = TcpListener::bind(&address).unwrap(); |
12125 | 48 |
|
49 |
let poll = Poll::new().unwrap(); |
|
14851
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14824
diff
changeset
|
50 |
let mut hw_builder = NetworkLayerBuilder::default().with_listener(listener); |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14824
diff
changeset
|
51 |
|
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14824
diff
changeset
|
52 |
#[cfg(feature = "tls-connections")] |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14824
diff
changeset
|
53 |
{ |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14824
diff
changeset
|
54 |
let address = format!("0.0.0.0:{}", port + 1).parse().unwrap(); |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14824
diff
changeset
|
55 |
hw_builder = hw_builder.with_secure_listener(TcpListener::bind(&address).unwrap()); |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14824
diff
changeset
|
56 |
} |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14824
diff
changeset
|
57 |
|
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14824
diff
changeset
|
58 |
let mut hw_network = hw_builder.build(); |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14824
diff
changeset
|
59 |
hw_network.register(&poll).unwrap(); |
12125 | 60 |
|
61 |
let mut events = Events::with_capacity(1024); |
|
62 |
||
63 |
loop { |
|
13414 | 64 |
let timeout = if hw_network.has_pending_operations() { |
65 |
Some(Duration::from_millis(1)) |
|
66 |
} else { |
|
67 |
None |
|
68 |
}; |
|
69 |
poll.poll(&mut events, timeout).unwrap(); |
|
12125 | 70 |
|
71 |
for event in events.iter() { |
|
12853 | 72 |
if event.readiness() & Ready::readable() == Ready::readable() { |
12127 | 73 |
match event.token() { |
14851
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14824
diff
changeset
|
74 |
token @ utils::SERVER_TOKEN | token @ utils::SECURE_SERVER_TOKEN => { |
14937 | 75 |
match hw_network.accept_client(&poll, token) { |
76 |
Ok(()) => (), |
|
77 |
Err(e) => debug!("Error accepting client: {}", e), |
|
78 |
} |
|
14851
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14824
diff
changeset
|
79 |
} |
14937 | 80 |
utils::TIMER_TOKEN => match hw_network.handle_timeout(&poll) { |
81 |
Ok(()) => (), |
|
82 |
Err(e) => debug!("Error in timer event: {}", e), |
|
83 |
}, |
|
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14712
diff
changeset
|
84 |
#[cfg(feature = "official-server")] |
15123 | 85 |
utils::IO_TOKEN => match hw_network.handle_io_result(&poll) { |
14937 | 86 |
Ok(()) => (), |
87 |
Err(e) => debug!("Error in IO task: {}", e), |
|
88 |
}, |
|
89 |
Token(token) => match hw_network.client_readable(&poll, token) { |
|
90 |
Ok(()) => (), |
|
91 |
Err(e) => debug!("Error reading from client socket {}: {}", token, e), |
|
92 |
}, |
|
12127 | 93 |
} |
94 |
} |
|
12853 | 95 |
if event.readiness() & Ready::writable() == Ready::writable() { |
12127 | 96 |
match event.token() { |
14937 | 97 |
utils::SERVER_TOKEN |
98 |
| utils::SECURE_SERVER_TOKEN |
|
99 |
| utils::TIMER_TOKEN |
|
100 |
| utils::IO_TOKEN => unreachable!(), |
|
101 |
Token(token) => match hw_network.client_writable(&poll, token) { |
|
102 |
Ok(()) => (), |
|
103 |
Err(e) => debug!("Error writing to client socket {}: {}", token, e), |
|
104 |
}, |
|
12127 | 105 |
} |
12125 | 106 |
} |
107 |
} |
|
14937 | 108 |
|
109 |
match hw_network.on_idle(&poll) { |
|
110 |
Ok(()) => (), |
|
111 |
Err(e) => debug!("Error in idle handler: {}", e), |
|
112 |
}; |
|
12125 | 113 |
} |
114 |
} |