author | S.D. |
Tue, 27 Sep 2022 14:59:03 +0300 | |
changeset 15900 | fc3cb23fd26f |
parent 15853 | 7d0f747afcb8 |
child 15968 | ce47259d5c86 |
permissions | -rw-r--r-- |
15853 | 1 |
#![forbid(unsafe_code)] |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
2 |
#![allow(unused_imports)] |
15853 | 3 |
#![allow(dead_code)] |
4 |
#![allow(unused_variables)] |
|
13421
d1368c776a4f
Enable all lints from the rust-2018-idioms suite.
marmistrz
parents:
13414
diff
changeset
|
5 |
#![deny(bare_trait_objects)] |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
6 |
|
14813
38e66519e585
ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents:
14804
diff
changeset
|
7 |
use getopts::Options; |
14478 | 8 |
use log::*; |
15853 | 9 |
use std::{env, net::SocketAddr, str::FromStr as _}; |
12125 | 10 |
|
15095 | 11 |
mod core; |
15096 | 12 |
mod handlers; |
14478 | 13 |
mod protocol; |
14 |
mod server; |
|
12125 | 15 |
mod utils; |
16 |
||
14851
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14824
diff
changeset
|
17 |
use crate::server::network::{NetworkLayer, NetworkLayerBuilder}; |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
18 |
|
14815 | 19 |
const PROGRAM_NAME: &'_ str = "Hedgewars Game Server"; |
20 |
||
15853 | 21 |
#[tokio::main] |
22 |
async fn main() -> tokio::io::Result<()> { |
|
13769 | 23 |
env_logger::init(); |
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12133
diff
changeset
|
24 |
|
14851
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14824
diff
changeset
|
25 |
info!("Hedgewars game server, protocol {}", utils::SERVER_VERSION); |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14824
diff
changeset
|
26 |
|
14813
38e66519e585
ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents:
14804
diff
changeset
|
27 |
let args: Vec<String> = env::args().collect(); |
38e66519e585
ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents:
14804
diff
changeset
|
28 |
let mut opts = Options::new(); |
38e66519e585
ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents:
14804
diff
changeset
|
29 |
|
38e66519e585
ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents:
14804
diff
changeset
|
30 |
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
|
31 |
opts.optflag("h", "help", "help"); |
38e66519e585
ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents:
14804
diff
changeset
|
32 |
let matches = match opts.parse(&args[1..]) { |
14815 | 33 |
Ok(m) => m, |
34 |
Err(e) => { |
|
35 |
println!("{}\n{}", e, opts.short_usage("")); |
|
15853 | 36 |
return Ok(()); |
14815 | 37 |
} |
14813
38e66519e585
ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents:
14804
diff
changeset
|
38 |
}; |
38e66519e585
ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents:
14804
diff
changeset
|
39 |
if matches.opt_present("h") { |
14815 | 40 |
println!("{}", opts.usage(PROGRAM_NAME)); |
15853 | 41 |
return Ok(()); |
14813
38e66519e585
ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents:
14804
diff
changeset
|
42 |
} |
12125 | 43 |
|
14851
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14824
diff
changeset
|
44 |
let port = matches |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14824
diff
changeset
|
45 |
.opt_str("p") |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14824
diff
changeset
|
46 |
.and_then(|s| u16::from_str(&s).ok()) |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14824
diff
changeset
|
47 |
.unwrap_or(46631); |
15853 | 48 |
let address: SocketAddr = format!("0.0.0.0:{}", port).parse().unwrap(); |
14851
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14824
diff
changeset
|
49 |
|
15853 | 50 |
let server = tokio::net::TcpListener::bind(address).await.unwrap(); |
12125 | 51 |
|
15853 | 52 |
let mut hw_network = NetworkLayerBuilder::default().with_listener(server).build(); |
14937 | 53 |
|
15853 | 54 |
hw_network.run().await; |
55 |
Ok(()) |
|
12125 | 56 |
} |