diff --git a/Cargo.lock b/Cargo.lock index 6f88dd5..14807b4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -32,6 +32,55 @@ version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "atomic-waker" version = "1.1.2" @@ -165,6 +214,46 @@ dependencies = [ "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]] name = "cmake" version = "0.1.51" @@ -174,6 +263,12 @@ dependencies = [ "cc", ] +[[package]] +name = "colorchoice" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" + [[package]] name = "crossbeam-utils" version = "0.8.20" @@ -216,7 +311,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba" dependencies = [ "libc", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -375,6 +470,12 @@ dependencies = [ "foldhash", ] +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + [[package]] name = "hermit-abi" version = "0.3.9" @@ -399,7 +500,7 @@ version = "0.5.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3d1354bf6b7235cb4a0576c2619fd4ed18183f689b12b006a0ee7329eeff9a5" dependencies = [ - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -417,6 +518,12 @@ dependencies = [ "memoffset", ] +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + [[package]] name = "itertools" version = "0.12.1" @@ -454,6 +561,7 @@ dependencies = [ name = "lanspread-client" version = "0.1.0" dependencies = [ + "clap", "eyre", "lanspread-db", "lanspread-proto", @@ -491,6 +599,7 @@ name = "lanspread-server" version = "0.1.0" dependencies = [ "bytes", + "clap", "eyre", "itertools 0.13.0", "lanspread-db", @@ -612,7 +721,7 @@ dependencies = [ "hermit-abi", "libc", "wasi 0.11.0+wasi-snapshot-preview1", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -905,7 +1014,7 @@ dependencies = [ "libc", "spin", "untrusted 0.9.0", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -930,7 +1039,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -1244,7 +1353,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ce305eb0b4296696835b71df73eb912e0f1ffd2556a501fcede6e0c50349191c" dependencies = [ "libc", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -1253,6 +1362,12 @@ version = "0.9.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + [[package]] name = "subtle" version = "2.6.1" @@ -1295,7 +1410,7 @@ dependencies = [ "signal-hook-registry", "socket2", "tokio-macros", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -1388,6 +1503,12 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + [[package]] name = "valuable" version = "0.1.0" @@ -1449,6 +1570,15 @@ dependencies = [ "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]] name = "windows-targets" version = "0.52.6" diff --git a/Cargo.toml b/Cargo.toml index a4f9a64..9418bea 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,7 @@ resolver = "2" [workspace.dependencies] bytes = "1.8" -clap = "4.5" +clap = { version = "4.5", features = ["derive"] } eyre = "0.6" itertools = "0.13" s2n-quic = { version = "1.49", features = ["provider-event-tracing"] } diff --git a/client.sh b/client.sh index c7d95e6..9368730 100755 --- a/client.sh +++ b/client.sh @@ -3,5 +3,5 @@ export RUST_LOG=info,lanspread_client=debug,lanspread_proto=debug #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) diff --git a/crates/lanspread-client/Cargo.toml b/crates/lanspread-client/Cargo.toml index 2388eec..3c63275 100644 --- a/crates/lanspread-client/Cargo.toml +++ b/crates/lanspread-client/Cargo.toml @@ -17,6 +17,7 @@ lanspread-db = { path = "../lanspread-db" } lanspread-proto = { path = "../lanspread-proto" } lanspread-utils = { path = "../lanspread-utils" } # external +clap = { workspace = true } eyre = { workspace = true } s2n-quic = { workspace = true } serde_json = { workspace = true } diff --git a/crates/lanspread-client/src/main.rs b/crates/lanspread-client/src/main.rs index 8362b45..d12685c 100644 --- a/crates/lanspread-client/src/main.rs +++ b/crates/lanspread-client/src/main.rs @@ -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_utils::maybe_addr; use s2n_quic::{client::Connect, provider::limits::Limits, Client as QuicClient}; use tokio::{ - io::{AsyncBufReadExt as _, AsyncWriteExt as _}, + io::{AsyncBufReadExt as _, AsyncWriteExt as _, BufReader}, sync::mpsc::UnboundedReceiver, }; use tracing_subscriber::EnvFilter; 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)] enum ControlMessage { ListGames, @@ -24,7 +25,7 @@ struct Client; impl Client { pub(crate) async fn run( - addr: SocketAddr, + remote_addr: SocketAddr, mut rx_control: UnboundedReceiver, ) -> eyre::Result<()> { tracing_subscriber::fmt() @@ -39,7 +40,7 @@ impl Client { .with_limits(limits)? .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?; 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] async fn main() -> eyre::Result<()> { + let cli = Cli::parse(); + let (tx_control, rx_control) = tokio::sync::mpsc::unbounded_channel::(); // Spawn client in a separate task let client_handle = tokio::spawn(async move { - #[allow(clippy::unwrap_used)] - let addr = format!("{SERVER_ADDR}:{SERVER_PORT}").parse().unwrap(); - Client::run(addr, rx_control).await + let remote_addr = SocketAddr::from((cli.ip, cli.port)); + Client::run(remote_addr, rx_control).await }); // 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(); loop { diff --git a/crates/lanspread-server/Cargo.toml b/crates/lanspread-server/Cargo.toml index 56d801d..0cb9031 100644 --- a/crates/lanspread-server/Cargo.toml +++ b/crates/lanspread-server/Cargo.toml @@ -19,6 +19,7 @@ lanspread-utils = { path = "../lanspread-utils" } # external bytes = { workspace = true } +clap = { workspace = true } eyre = { workspace = true } itertools = { workspace = true } s2n-quic = { workspace = true } diff --git a/crates/lanspread-server/src/main.rs b/crates/lanspread-server/src/main.rs index 4361eca..25d9efc 100644 --- a/crates/lanspread-server/src/main.rs +++ b/crates/lanspread-server/src/main.rs @@ -1,9 +1,10 @@ use std::{ - net::SocketAddr, + net::{IpAddr, SocketAddr}, path::{Path, PathBuf}, sync::Arc, }; +use clap::Parser; use lanspread_db::db::GameDB; use lanspread_proto::{Message as _, Request, Response}; 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 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 { db_path: PathBuf, } @@ -162,14 +160,22 @@ impl RequestHandler { 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] async fn main() -> eyre::Result<()> { + let cli = Cli::parse(); + generate_test_db(GAME_DB_PATH); let mut server = Server::new(GAME_DB_PATH); - - #[allow(clippy::unwrap_used)] - server - .run(format!("{SERVER_ADDR}:{SERVER_PORT}").parse().unwrap()) - .await + server.run(SocketAddr::from((cli.ip, cli.port))).await } diff --git a/server.sh b/server.sh index db3a12f..f6a50c5 100755 --- a/server.sh +++ b/server.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash 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