[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 {
|
||||
|
@ -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 }
|
||||
|
@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user