[feat] use clap to pass IP and port
This commit is contained in:
@ -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 }
|
||||
|
@ -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<ControlMessage>,
|
||||
) -> 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::<ControlMessage>();
|
||||
|
||||
// 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 {
|
||||
|
Reference in New Issue
Block a user