Pass data as JSON over UDP.

This commit is contained in:
mattbk
2025-10-05 18:21:16 -05:00
parent 6101932b0d
commit 7957fa7c2c
2 changed files with 16 additions and 6 deletions

View File

@@ -8,6 +8,7 @@ authors = ["Chris, N6CTA <mail@n6cta.com>"]
anyhow = "1.0"
chrono = "0.4"
clap = { version = "4", features = ["derive"] }
serde_json = "1.0.145"
socket2 = "0.5"
[profile.release]

View File

@@ -7,9 +7,10 @@ use std::convert::TryInto;
use std::io::{Read, Write};
use std::net::TcpStream;
use std::thread::sleep;
use std::time::Duration;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use std::net::UdpSocket;
use std::net::Ipv4Addr;
use serde_json::json;
/// Validate that the provided port string can be parsed into a u16 and is nonzero.
fn validate_port(port: &str) -> Result<u16, String> {
@@ -395,11 +396,19 @@ fn handle_frame(frame: &AgwFrame, cli: &Cli, buffers: &mut BufferManager) {
}
// Send UDP
if cli.uport != 55555 {
let uaddr = format!("{}:{}", cli.uip, cli.uport);
let socket = UdpSocket::bind("0.0.0.0:0");
//let message = "Packet received";
let message = &text;
let _ = socket.expect("REASON").send_to(message.as_bytes(), uaddr);
let packet = json!({
"final_destination": &final_destination,
"source": &source,
"summary": &summary,
"text": &text,
"timestamp": SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs(),
});
let _ = socket.expect("REASON").send_to(packet.to_string().as_bytes(), uaddr);
}
// In non-debug mode, print the session line and any additional lines.
if !cli.debug {