[feat] use clap to pass IP and port

This commit is contained in:
ddidderr 2024-11-08 22:38:11 +01:00
parent 9d8f579a0f
commit 9f63ace39c
Signed by: ddidderr
GPG Key ID: 3841F1C27E6F0E14
8 changed files with 180 additions and 30 deletions

144
Cargo.lock generated
View File

@ -32,6 +32,55 @@ version = "0.2.18"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c6cb57a04249c6480766f7f7cef5467412af1490f8d1e243141daddada3264f" checksum = "5c6cb57a04249c6480766f7f7cef5467412af1490f8d1e243141daddada3264f"
[[package]]
name = "anstream"
version = "0.6.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8acc5369981196006228e28809f761875c0327210a891e941f4c683b3a99529b"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"is_terminal_polyfill",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9"
[[package]]
name = "anstyle-parse"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b2d16507662817a6a20a9ea92df6652ee4f94f914589377d69f3b21bc5798a9"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c"
dependencies = [
"windows-sys 0.59.0",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2109dbce0e72be3ec00bed26e6a7479ca384ad226efdd66db8fa2e3a38c83125"
dependencies = [
"anstyle",
"windows-sys 0.59.0",
]
[[package]] [[package]]
name = "atomic-waker" name = "atomic-waker"
version = "1.1.2" version = "1.1.2"
@ -165,6 +214,46 @@ dependencies = [
"libloading", "libloading",
] ]
[[package]]
name = "clap"
version = "4.5.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b97f376d85a664d5837dbae44bf546e6477a679ff6610010f17276f686d867e8"
dependencies = [
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.5.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19bc80abd44e4bed93ca373a0704ccbd1b710dc5749406201bb018272808dc54"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.5.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ac6a0c7b1a9e9a5186361f67dfa1b88213572f427fb9ab038efb2bd8c582dab"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97"
[[package]] [[package]]
name = "cmake" name = "cmake"
version = "0.1.51" version = "0.1.51"
@ -174,6 +263,12 @@ dependencies = [
"cc", "cc",
] ]
[[package]]
name = "colorchoice"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990"
[[package]] [[package]]
name = "crossbeam-utils" name = "crossbeam-utils"
version = "0.8.20" version = "0.8.20"
@ -216,7 +311,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba" checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba"
dependencies = [ dependencies = [
"libc", "libc",
"windows-sys", "windows-sys 0.52.0",
] ]
[[package]] [[package]]
@ -375,6 +470,12 @@ dependencies = [
"foldhash", "foldhash",
] ]
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]] [[package]]
name = "hermit-abi" name = "hermit-abi"
version = "0.3.9" version = "0.3.9"
@ -399,7 +500,7 @@ version = "0.5.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3d1354bf6b7235cb4a0576c2619fd4ed18183f689b12b006a0ee7329eeff9a5" checksum = "e3d1354bf6b7235cb4a0576c2619fd4ed18183f689b12b006a0ee7329eeff9a5"
dependencies = [ dependencies = [
"windows-sys", "windows-sys 0.52.0",
] ]
[[package]] [[package]]
@ -417,6 +518,12 @@ dependencies = [
"memoffset", "memoffset",
] ]
[[package]]
name = "is_terminal_polyfill"
version = "1.70.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
[[package]] [[package]]
name = "itertools" name = "itertools"
version = "0.12.1" version = "0.12.1"
@ -454,6 +561,7 @@ dependencies = [
name = "lanspread-client" name = "lanspread-client"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"clap",
"eyre", "eyre",
"lanspread-db", "lanspread-db",
"lanspread-proto", "lanspread-proto",
@ -491,6 +599,7 @@ name = "lanspread-server"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"bytes", "bytes",
"clap",
"eyre", "eyre",
"itertools 0.13.0", "itertools 0.13.0",
"lanspread-db", "lanspread-db",
@ -612,7 +721,7 @@ dependencies = [
"hermit-abi", "hermit-abi",
"libc", "libc",
"wasi 0.11.0+wasi-snapshot-preview1", "wasi 0.11.0+wasi-snapshot-preview1",
"windows-sys", "windows-sys 0.52.0",
] ]
[[package]] [[package]]
@ -905,7 +1014,7 @@ dependencies = [
"libc", "libc",
"spin", "spin",
"untrusted 0.9.0", "untrusted 0.9.0",
"windows-sys", "windows-sys 0.52.0",
] ]
[[package]] [[package]]
@ -930,7 +1039,7 @@ dependencies = [
"errno", "errno",
"libc", "libc",
"linux-raw-sys", "linux-raw-sys",
"windows-sys", "windows-sys 0.52.0",
] ]
[[package]] [[package]]
@ -1244,7 +1353,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce305eb0b4296696835b71df73eb912e0f1ffd2556a501fcede6e0c50349191c" checksum = "ce305eb0b4296696835b71df73eb912e0f1ffd2556a501fcede6e0c50349191c"
dependencies = [ dependencies = [
"libc", "libc",
"windows-sys", "windows-sys 0.52.0",
] ]
[[package]] [[package]]
@ -1253,6 +1362,12 @@ version = "0.9.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67"
[[package]]
name = "strsim"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]] [[package]]
name = "subtle" name = "subtle"
version = "2.6.1" version = "2.6.1"
@ -1295,7 +1410,7 @@ dependencies = [
"signal-hook-registry", "signal-hook-registry",
"socket2", "socket2",
"tokio-macros", "tokio-macros",
"windows-sys", "windows-sys 0.52.0",
] ]
[[package]] [[package]]
@ -1388,6 +1503,12 @@ version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1"
[[package]]
name = "utf8parse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]] [[package]]
name = "valuable" name = "valuable"
version = "0.1.0" version = "0.1.0"
@ -1449,6 +1570,15 @@ dependencies = [
"windows-targets", "windows-targets",
] ]
[[package]]
name = "windows-sys"
version = "0.59.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b"
dependencies = [
"windows-targets",
]
[[package]] [[package]]
name = "windows-targets" name = "windows-targets"
version = "0.52.6" version = "0.52.6"

View File

@ -10,7 +10,7 @@ resolver = "2"
[workspace.dependencies] [workspace.dependencies]
bytes = "1.8" bytes = "1.8"
clap = "4.5" clap = { version = "4.5", features = ["derive"] }
eyre = "0.6" eyre = "0.6"
itertools = "0.13" itertools = "0.13"
s2n-quic = { version = "1.49", features = ["provider-event-tracing"] } s2n-quic = { version = "1.49", features = ["provider-event-tracing"] }

View File

@ -3,5 +3,5 @@
export RUST_LOG=info,lanspread_client=debug,lanspread_proto=debug export RUST_LOG=info,lanspread_client=debug,lanspread_proto=debug
#export RUST_LOG=error #export RUST_LOG=error
exec cargo run -p lanspread-client < <(while sleep 0.1; do echo "list"; sleep 0.1; echo "get 1"; sleep 0.1; echo "get 25"; done) exec cargo run -p lanspread-client -- "$@" < <(while sleep 0.1; do echo "list"; sleep 0.1; echo "get 1"; sleep 0.1; echo "get 25"; done)
#RUST_LOG=info exec cargo run --profile release-lto -p lanspread-client < <(while sleep 0.1; do echo "list"; sleep 0.1; echo "get 1"; sleep 0.1; echo "get 25"; done) #RUST_LOG=info exec cargo run --profile release-lto -p lanspread-client < <(while sleep 0.1; do echo "list"; sleep 0.1; echo "get 1"; sleep 0.1; echo "get 25"; done)

View File

@ -17,6 +17,7 @@ lanspread-db = { path = "../lanspread-db" }
lanspread-proto = { path = "../lanspread-proto" } lanspread-proto = { path = "../lanspread-proto" }
lanspread-utils = { path = "../lanspread-utils" } lanspread-utils = { path = "../lanspread-utils" }
# external # external
clap = { workspace = true }
eyre = { workspace = true } eyre = { workspace = true }
s2n-quic = { workspace = true } s2n-quic = { workspace = true }
serde_json = { workspace = true } serde_json = { workspace = true }

View File

@ -1,19 +1,20 @@
use std::{net::SocketAddr, time::Duration}; use std::{
net::{IpAddr, SocketAddr},
time::Duration,
};
use clap::Parser;
use lanspread_proto::{Message as _, Request, Response}; use lanspread_proto::{Message as _, Request, Response};
use lanspread_utils::maybe_addr; use lanspread_utils::maybe_addr;
use s2n_quic::{client::Connect, provider::limits::Limits, Client as QuicClient}; use s2n_quic::{client::Connect, provider::limits::Limits, Client as QuicClient};
use tokio::{ use tokio::{
io::{AsyncBufReadExt as _, AsyncWriteExt as _}, io::{AsyncBufReadExt as _, AsyncWriteExt as _, BufReader},
sync::mpsc::UnboundedReceiver, sync::mpsc::UnboundedReceiver,
}; };
use tracing_subscriber::EnvFilter; use tracing_subscriber::EnvFilter;
static CERT_PEM: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../cert.pem")); static CERT_PEM: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../cert.pem"));
const SERVER_ADDR: &str = "127.0.0.1";
const SERVER_PORT: u16 = 13337;
#[derive(Debug)] #[derive(Debug)]
enum ControlMessage { enum ControlMessage {
ListGames, ListGames,
@ -24,7 +25,7 @@ struct Client;
impl Client { impl Client {
pub(crate) async fn run( pub(crate) async fn run(
addr: SocketAddr, remote_addr: SocketAddr,
mut rx_control: UnboundedReceiver<ControlMessage>, mut rx_control: UnboundedReceiver<ControlMessage>,
) -> eyre::Result<()> { ) -> eyre::Result<()> {
tracing_subscriber::fmt() tracing_subscriber::fmt()
@ -39,7 +40,7 @@ impl Client {
.with_limits(limits)? .with_limits(limits)?
.start()?; .start()?;
let conn = Connect::new(addr).with_server_name("localhost"); let conn = Connect::new(remote_addr).with_server_name("localhost");
let mut conn = client.connect(conn).await?; let mut conn = client.connect(conn).await?;
conn.keep_alive(true)?; conn.keep_alive(true)?;
@ -110,19 +111,30 @@ impl Client {
} }
} }
#[derive(Debug, Parser)]
struct Cli {
/// Server IP address.
#[clap(long, default_value = "127.0.0.1")]
ip: IpAddr,
/// Server port.
#[clap(long, default_value = "13337")]
port: u16,
}
#[tokio::main] #[tokio::main]
async fn main() -> eyre::Result<()> { async fn main() -> eyre::Result<()> {
let cli = Cli::parse();
let (tx_control, rx_control) = tokio::sync::mpsc::unbounded_channel::<ControlMessage>(); let (tx_control, rx_control) = tokio::sync::mpsc::unbounded_channel::<ControlMessage>();
// Spawn client in a separate task // Spawn client in a separate task
let client_handle = tokio::spawn(async move { let client_handle = tokio::spawn(async move {
#[allow(clippy::unwrap_used)] let remote_addr = SocketAddr::from((cli.ip, cli.port));
let addr = format!("{SERVER_ADDR}:{SERVER_PORT}").parse().unwrap(); Client::run(remote_addr, rx_control).await
Client::run(addr, rx_control).await
}); });
// Handle stdin commands in the main task // Handle stdin commands in the main task
let mut stdin = tokio::io::BufReader::new(tokio::io::stdin()); let mut stdin = BufReader::new(tokio::io::stdin());
let mut line = String::new(); let mut line = String::new();
loop { loop {

View File

@ -19,6 +19,7 @@ lanspread-utils = { path = "../lanspread-utils" }
# external # external
bytes = { workspace = true } bytes = { workspace = true }
clap = { workspace = true }
eyre = { workspace = true } eyre = { workspace = true }
itertools = { workspace = true } itertools = { workspace = true }
s2n-quic = { workspace = true } s2n-quic = { workspace = true }

View File

@ -1,9 +1,10 @@
use std::{ use std::{
net::SocketAddr, net::{IpAddr, SocketAddr},
path::{Path, PathBuf}, path::{Path, PathBuf},
sync::Arc, sync::Arc,
}; };
use clap::Parser;
use lanspread_db::db::GameDB; use lanspread_db::db::GameDB;
use lanspread_proto::{Message as _, Request, Response}; use lanspread_proto::{Message as _, Request, Response};
use lanspread_utils::maybe_addr; use lanspread_utils::maybe_addr;
@ -17,9 +18,6 @@ mod testing;
static KEY_PEM: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../key.pem")); static KEY_PEM: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../key.pem"));
static CERT_PEM: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../cert.pem")); static CERT_PEM: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../cert.pem"));
const SERVER_ADDR: &str = "0.0.0.0";
const SERVER_PORT: u16 = 13337;
pub(crate) struct Server { pub(crate) struct Server {
db_path: PathBuf, db_path: PathBuf,
} }
@ -162,14 +160,22 @@ impl RequestHandler {
const GAME_DB_PATH: &str = "/home/pfs/shm/game.db"; const GAME_DB_PATH: &str = "/home/pfs/shm/game.db";
#[derive(Debug, Parser)]
struct Cli {
/// The IP address to bind to.
#[clap(long, default_value = "127.0.0.1")]
ip: IpAddr,
/// The listen port.
#[clap(long, default_value = "13337")]
port: u16,
}
#[tokio::main] #[tokio::main]
async fn main() -> eyre::Result<()> { async fn main() -> eyre::Result<()> {
let cli = Cli::parse();
generate_test_db(GAME_DB_PATH); generate_test_db(GAME_DB_PATH);
let mut server = Server::new(GAME_DB_PATH); let mut server = Server::new(GAME_DB_PATH);
server.run(SocketAddr::from((cli.ip, cli.port))).await
#[allow(clippy::unwrap_used)]
server
.run(format!("{SERVER_ADDR}:{SERVER_PORT}").parse().unwrap())
.await
} }

View File

@ -1,6 +1,6 @@
#!/usr/bin/env bash #!/usr/bin/env bash
export RUST_LOG=info,lanspread_server=debug,lanspread_proto=debug export RUST_LOG=info,lanspread_server=debug,lanspread_proto=debug
exec cargo run -p lanspread-server exec cargo run -p lanspread-server -- "$@"
#RUST_LOG=info exec cargo run --profile release-lto -p lanspread-server #RUST_LOG=info exec cargo run --profile release-lto -p lanspread-server