author | alfadur |
Tue, 28 May 2019 21:28:32 +0300 | |
changeset 15080 | e935b1ad23f3 |
parent 15079 | c5a6e8566425 |
child 15107 | 80ff12edf5e6 |
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)] |
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12858
diff
changeset
|
3 |
|
14797
38e66519e585
ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents:
14788
diff
changeset
|
4 |
use getopts::Options; |
14462 | 5 |
use log::*; |
14835
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14808
diff
changeset
|
6 |
use mio::{net::*, *}; |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14808
diff
changeset
|
7 |
use std::{env, str::FromStr as _, time::Duration}; |
12130 | 8 |
|
15079 | 9 |
mod core; |
15080 | 10 |
mod handlers; |
14462 | 11 |
mod protocol; |
12 |
mod server; |
|
12130 | 13 |
mod utils; |
14 |
||
14835
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14808
diff
changeset
|
15 |
use crate::server::network::{NetworkLayer, NetworkLayerBuilder}; |
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12858
diff
changeset
|
16 |
|
14799 | 17 |
const PROGRAM_NAME: &'_ str = "Hedgewars Game Server"; |
18 |
||
12130 | 19 |
fn main() { |
13802 | 20 |
env_logger::init(); |
12142
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12138
diff
changeset
|
21 |
|
14835
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14808
diff
changeset
|
22 |
info!("Hedgewars game server, protocol {}", utils::SERVER_VERSION); |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14808
diff
changeset
|
23 |
|
14797
38e66519e585
ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents:
14788
diff
changeset
|
24 |
let args: Vec<String> = env::args().collect(); |
38e66519e585
ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents:
14788
diff
changeset
|
25 |
let mut opts = Options::new(); |
38e66519e585
ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents:
14788
diff
changeset
|
26 |
|
38e66519e585
ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents:
14788
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:
14788
diff
changeset
|
28 |
opts.optflag("h", "help", "help"); |
38e66519e585
ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents:
14788
diff
changeset
|
29 |
let matches = match opts.parse(&args[1..]) { |
14799 | 30 |
Ok(m) => m, |
31 |
Err(e) => { |
|
32 |
println!("{}\n{}", e, opts.short_usage("")); |
|
33 |
return; |
|
34 |
} |
|
14797
38e66519e585
ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents:
14788
diff
changeset
|
35 |
}; |
38e66519e585
ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents:
14788
diff
changeset
|
36 |
if matches.opt_present("h") { |
14799 | 37 |
println!("{}", opts.usage(PROGRAM_NAME)); |
14797
38e66519e585
ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents:
14788
diff
changeset
|
38 |
return; |
38e66519e585
ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents:
14788
diff
changeset
|
39 |
} |
12130 | 40 |
|
14835
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14808
diff
changeset
|
41 |
let port = matches |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14808
diff
changeset
|
42 |
.opt_str("p") |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14808
diff
changeset
|
43 |
.and_then(|s| u16::from_str(&s).ok()) |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14808
diff
changeset
|
44 |
.unwrap_or(46631); |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14808
diff
changeset
|
45 |
let address = format!("0.0.0.0:{}", port).parse().unwrap(); |
14797
38e66519e585
ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents:
14788
diff
changeset
|
46 |
|
12131 | 47 |
let listener = TcpListener::bind(&address).unwrap(); |
12130 | 48 |
|
49 |
let poll = Poll::new().unwrap(); |
|
14835
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14808
diff
changeset
|
50 |
let mut hw_builder = NetworkLayerBuilder::default().with_listener(listener); |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14808
diff
changeset
|
51 |
|
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14808
diff
changeset
|
52 |
#[cfg(feature = "tls-connections")] |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14808
diff
changeset
|
53 |
{ |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14808
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:
14808
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:
14808
diff
changeset
|
56 |
} |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14808
diff
changeset
|
57 |
|
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14808
diff
changeset
|
58 |
let mut hw_network = hw_builder.build(); |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14808
diff
changeset
|
59 |
hw_network.register(&poll).unwrap(); |
12130 | 60 |
|
61 |
let mut events = Events::with_capacity(1024); |
|
62 |
||
63 |
loop { |
|
13419 | 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(); |
|
12130 | 70 |
|
71 |
for event in events.iter() { |
|
12858 | 72 |
if event.readiness() & Ready::readable() == Ready::readable() { |
12132 | 73 |
match event.token() { |
14835
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14808
diff
changeset
|
74 |
token @ utils::SERVER_TOKEN | token @ utils::SECURE_SERVER_TOKEN => { |
14921 | 75 |
match hw_network.accept_client(&poll, token) { |
76 |
Ok(()) => (), |
|
77 |
Err(e) => debug!("Error accepting client: {}", e), |
|
78 |
} |
|
14835
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14808
diff
changeset
|
79 |
} |
14921 | 80 |
utils::TIMER_TOKEN => match hw_network.handle_timeout(&poll) { |
81 |
Ok(()) => (), |
|
82 |
Err(e) => debug!("Error in timer event: {}", e), |
|
83 |
}, |
|
14784
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
84 |
#[cfg(feature = "official-server")] |
14921 | 85 |
utils::IO_TOKEN => match hw_network.handle_io_result() { |
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 |
}, |
|
12132 | 93 |
} |
94 |
} |
|
12858 | 95 |
if event.readiness() & Ready::writable() == Ready::writable() { |
12132 | 96 |
match event.token() { |
14921 | 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 |
}, |
|
12132 | 105 |
} |
12130 | 106 |
} |
107 |
} |
|
14921 | 108 |
|
109 |
match hw_network.on_idle(&poll) { |
|
110 |
Ok(()) => (), |
|
111 |
Err(e) => debug!("Error in idle handler: {}", e), |
|
112 |
}; |
|
12130 | 113 |
} |
114 |
} |