diff options
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 51 |
1 files changed, 41 insertions, 10 deletions
diff --git a/src/main.rs b/src/main.rs index cdd7c17..f213197 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,6 @@ +use clap::Parser; use lightyear::transport::io::TransportConfig; +use rand::Rng; use shared::SERVER_PORT; use std::net::Ipv4Addr; use std::net::SocketAddr; @@ -9,19 +11,48 @@ mod protocol; mod server; mod shared; +#[derive(Parser, Debug)] +struct Cli { + #[arg(long, value_name = "HOST:PORT")] + connect_to: Option<SocketAddr>, + #[arg(long, num_args = 0)] + server: bool, +} + fn main() { - let (from_server_send, from_server_recv) = crossbeam_channel::unbounded(); - let (to_server_send, to_server_recv) = crossbeam_channel::unbounded(); + let cli = Cli::parse(); + + let mut rng = rand::thread_rng(); + let client_id = rng.gen_range(1..=SERVER_PORT); - thread::spawn(|| { + if cli.connect_to.is_some() && cli.server { + panic!("cannot specify both `--connect-to` and `--server`"); + } + + if cli.server { let server_addr = SocketAddr::new(Ipv4Addr::new(0, 0, 0, 0).into(), SERVER_PORT); - server::main(TransportConfig::Channels { - channels: [(server_addr, to_server_recv, from_server_send)].to_vec(), + server::main(TransportConfig::UdpSocket(server_addr)); + } else if let Some(server_addr) = cli.connect_to { + let client_addr = SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), SERVER_PORT + client_id); + client::main(Some(server_addr), client_id as u64, TransportConfig::UdpSocket(client_addr)); + } else { + let (from_server_send, from_server_recv) = crossbeam_channel::unbounded(); + let (to_server_send, to_server_recv) = crossbeam_channel::unbounded(); + + thread::spawn(|| { + let server_addr = SocketAddr::new(Ipv4Addr::new(0, 0, 0, 0).into(), SERVER_PORT); + server::main(TransportConfig::Channels { + channels: [(server_addr, to_server_recv, from_server_send)].to_vec(), + }); }); - }); - client::main(TransportConfig::LocalChannel { - recv: from_server_recv, - send: to_server_send, - }); + client::main( + None, + client_id as u64, + TransportConfig::LocalChannel { + recv: from_server_recv, + send: to_server_send, + }, + ); + } } |