author | alfadur |
Tue, 01 Feb 2022 02:23:35 +0300 | |
changeset 15831 | 7d0f747afcb8 |
parent 15800 | 6af892a0a4b8 |
child 15938 | ce47259d5c86 |
permissions | -rw-r--r-- |
15831 | 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)] |
15831 | 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 |
|
14792
38e66519e585
ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents:
14783
diff
changeset
|
7 |
use getopts::Options; |
14457 | 8 |
use log::*; |
15831 | 9 |
use std::{env, net::SocketAddr, str::FromStr as _}; |
12125 | 10 |
|
15074 | 11 |
mod core; |
15075 | 12 |
mod handlers; |
14457 | 13 |
mod protocol; |
14 |
mod server; |
|
12125 | 15 |
mod utils; |
16 |
||
14830
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14803
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 |
|
14794 | 19 |
const PROGRAM_NAME: &'_ str = "Hedgewars Game Server"; |
20 |
||
15831 | 21 |
#[tokio::main] |
22 |
async fn main() -> tokio::io::Result<()> { |
|
13797 | 23 |
env_logger::init(); |
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12133
diff
changeset
|
24 |
|
14830
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14803
diff
changeset
|
25 |
info!("Hedgewars game server, protocol {}", utils::SERVER_VERSION); |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14803
diff
changeset
|
26 |
|
14792
38e66519e585
ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents:
14783
diff
changeset
|
27 |
let args: Vec<String> = env::args().collect(); |
38e66519e585
ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents:
14783
diff
changeset
|
28 |
let mut opts = Options::new(); |
38e66519e585
ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents:
14783
diff
changeset
|
29 |
|
38e66519e585
ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents:
14783
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:
14783
diff
changeset
|
31 |
opts.optflag("h", "help", "help"); |
38e66519e585
ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents:
14783
diff
changeset
|
32 |
let matches = match opts.parse(&args[1..]) { |
14794 | 33 |
Ok(m) => m, |
34 |
Err(e) => { |
|
35 |
println!("{}\n{}", e, opts.short_usage("")); |
|
15831 | 36 |
return Ok(()); |
14794 | 37 |
} |
14792
38e66519e585
ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents:
14783
diff
changeset
|
38 |
}; |
38e66519e585
ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents:
14783
diff
changeset
|
39 |
if matches.opt_present("h") { |
14794 | 40 |
println!("{}", opts.usage(PROGRAM_NAME)); |
15831 | 41 |
return Ok(()); |
14792
38e66519e585
ugly hacked in argument for port to remove unc0rr's excuse
nemo
parents:
14783
diff
changeset
|
42 |
} |
12125 | 43 |
|
14830
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14803
diff
changeset
|
44 |
let port = matches |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14803
diff
changeset
|
45 |
.opt_str("p") |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14803
diff
changeset
|
46 |
.and_then(|s| u16::from_str(&s).ok()) |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14803
diff
changeset
|
47 |
.unwrap_or(46631); |
15831 | 48 |
let address: SocketAddr = format!("0.0.0.0:{}", port).parse().unwrap(); |
14830
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14803
diff
changeset
|
49 |
|
15831 | 50 |
let server = tokio::net::TcpListener::bind(address).await.unwrap(); |
12125 | 51 |
|
15831 | 52 |
let mut hw_network = NetworkLayerBuilder::default().with_listener(server).build(); |
14916 | 53 |
|
15831 | 54 |
hw_network.run().await; |
55 |
Ok(()) |
|
12125 | 56 |
} |