author | unC0Rr |
Sat, 28 Sep 2024 22:27:13 +0200 | |
changeset 16037 | 2b4f361e3891 |
parent 15989 | fb389df02e3e |
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 |
|
15989 | 30 |
//todo!("Add options for cert paths"); |
14792
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("")); |
|
15831 | 37 |
return Ok(()); |
14794 | 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)); |
15831 | 42 |
return Ok(()); |
14792
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); |
15831 | 49 |
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
|
50 |
|
15831 | 51 |
let server = tokio::net::TcpListener::bind(address).await.unwrap(); |
12125 | 52 |
|
15831 | 53 |
let mut hw_network = NetworkLayerBuilder::default().with_listener(server).build(); |
14916 | 54 |
|
15831 | 55 |
hw_network.run().await; |
56 |
Ok(()) |
|
12125 | 57 |
} |