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