mirror of
https://git.alemi.dev/mumble-stats-api.git
synced 2024-11-22 16:04:49 +01:00
feat: simple cli implementation
This commit is contained in:
parent
acbead360a
commit
950b9bd3e5
1 changed files with 68 additions and 0 deletions
68
src/main.rs
Normal file
68
src/main.rs
Normal 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 })
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue