feat: simple cli implementation

This commit is contained in:
əlemi 2024-02-16 00:40:55 +01:00
parent acbead360a
commit 950b9bd3e5
Signed by: alemi
GPG key ID: A4895B84D311642C

68
src/main.rs Normal file
View file

@ -0,0 +1,68 @@
use std::net::SocketAddr;
use clap::Parser;
use tokio::{io::{AsyncReadExt, AsyncWriteExt}, net::UdpSocket};
#[derive(Parser)]
struct CliArgs {
server: String,
}
#[tokio::main]
async fn main() {
let args = CliArgs::parse();
let dest : SocketAddr = args.server.parse().unwrap();
// by default bind on any interface and request OS to give us a port
let mut addr : SocketAddr = "0.0.0.0:0".parse().unwrap();
let socket = UdpSocket::bind(addr).await.unwrap();
let packet = PingPacket { id: 0, time: chrono::Utc::now().timestamp_micros() as u64 };
let buf = packet.serialize().await.unwrap();
socket.send_to(&buf, dest).await.unwrap();
let mut buf = [0u8; 1024];
socket.recv(&mut buf).await.unwrap();
let pong = PongPacket::deserialize(&buf).await.unwrap();
println!("{pong:#?}");
}
struct PingPacket {
id: i32,
time: u64,
}
impl PingPacket {
async fn serialize(self) -> std::io::Result<Vec<u8>> {
let mut out = Vec::new();
out.write_i32(self.id).await?;
out.write_u64(self.time).await?;
Ok(out)
}
}
#[derive(Debug)]
struct PongPacket {
version: u32,
time: u64,
users: i32,
max_users: i32,
bandwidth: i32,
}
impl PongPacket {
async fn deserialize(mut data: &[u8]) -> std::io::Result<Self> {
let version = data.read_u32().await?;
let time = data.read_u64().await?;
let users = data.read_i32().await?;
let max_users = data.read_i32().await?;
let bandwidth = data.read_i32().await?;
Ok(PongPacket { version, time, users, max_users, bandwidth })
}
}