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