Implement direct P2P session scaffolding (#39)
CI / build-and-test (push) Successful in 1m10s
CI / build-and-test (push) Successful in 1m10s
Closes #24 Co-authored-by: Álex Román Núñez <eirexe@eirteam.moe>
This commit was merged in pull request #39.
This commit is contained in:
+45
@@ -1,5 +1,6 @@
|
||||
use bevy::prelude::*;
|
||||
use clap::Parser;
|
||||
use std::net::SocketAddr;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Parser, Debug, Clone, Resource)]
|
||||
@@ -15,6 +16,12 @@ pub struct Args {
|
||||
pub check_distance: usize,
|
||||
#[clap(long, default_value_t = 1)]
|
||||
pub players: usize,
|
||||
/// UDP port to bind for a direct peer-to-peer session.
|
||||
#[clap(long, default_value_t = 6969)]
|
||||
pub local_port: u16,
|
||||
/// Player ownership declarations in the form HANDLE=local or HANDLE=ADDRESS.
|
||||
#[clap(long = "net-player", value_name = "HANDLE=local|ADDRESS")]
|
||||
pub net_players: Vec<String>,
|
||||
/// Record a replay to this path.
|
||||
#[clap(long, conflicts_with = "play_replay")]
|
||||
pub record_replay: Option<PathBuf>,
|
||||
@@ -25,3 +32,41 @@ pub struct Args {
|
||||
#[clap(long, value_enum, default_value_t = crate::replay::ReplayFormat::Binary)]
|
||||
pub replay_format: crate::replay::ReplayFormat,
|
||||
}
|
||||
|
||||
pub fn parse_socket_addr(value: &str) -> Result<SocketAddr, String> {
|
||||
let address = value
|
||||
.parse::<SocketAddr>()
|
||||
.map_err(|_| format!("invalid socket address: {value}"))?;
|
||||
if address.port() == 0 {
|
||||
return Err(format!("socket address must use a nonzero port: {value}"));
|
||||
}
|
||||
Ok(address)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn parses_network_options() {
|
||||
let args = Args::try_parse_from([
|
||||
"game",
|
||||
"--local-port",
|
||||
"7000",
|
||||
"--net-player",
|
||||
"0=local",
|
||||
"--net-player",
|
||||
"1=127.0.0.1:7001",
|
||||
])
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(args.local_port, 7000);
|
||||
assert_eq!(args.net_players, ["0=local", "1=127.0.0.1:7001"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_invalid_socket_addresses() {
|
||||
assert!(parse_socket_addr("127.0.0.1:0").is_err());
|
||||
assert!(parse_socket_addr("not-an-address").is_err());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user