mirror of
				https://gitea.farpn.net/w1cdn/mwtchahrd.git
				synced 2025-11-04 05:46:36 -06:00 
			
		
		
		
	Rough out sending a basic UDP notification.
This commit is contained in:
		
							
								
								
									
										31
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										31
									
								
								src/main.rs
									
									
									
									
									
								
							@@ -8,6 +8,7 @@ use std::io::{Read, Write};
 | 
			
		||||
use std::net::TcpStream;
 | 
			
		||||
use std::thread::sleep;
 | 
			
		||||
use std::time::Duration;
 | 
			
		||||
use std::net::UdpSocket;
 | 
			
		||||
 | 
			
		||||
/// Validate that the provided port string can be parsed into a u16 and is nonzero.
 | 
			
		||||
fn validate_port(port: &str) -> Result<u16, String> {
 | 
			
		||||
@@ -47,6 +48,18 @@ struct Cli {
 | 
			
		||||
    /// Only monitor UI frames
 | 
			
		||||
    #[arg(short = 'u', long, default_value_t = false)]
 | 
			
		||||
    ui_only: bool,
 | 
			
		||||
    
 | 
			
		||||
    // Enable UDP
 | 
			
		||||
    #[arg(short = 'm', long, default_value_t = false)]
 | 
			
		||||
    udp: bool,
 | 
			
		||||
    
 | 
			
		||||
    // Send UDP to what IP address (e.g. 127.0.0.1)
 | 
			
		||||
    #[arg(short = 'b', long)]
 | 
			
		||||
    uip: std::net::IpAddr,
 | 
			
		||||
    
 | 
			
		||||
    // Send UDP to what port (e.g. 8000)
 | 
			
		||||
    #[arg(short = 'k', long, value_parser = validate_port)]
 | 
			
		||||
    uport: u16,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Convert a byte slice into a hex-dump string for debugging purposes.
 | 
			
		||||
@@ -384,6 +397,12 @@ fn handle_frame(frame: &AgwFrame, cli: &Cli, buffers: &mut BufferManager) {
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Send UDP
 | 
			
		||||
    let uaddr = format!("{}:{}", cli.uip, cli.uport);
 | 
			
		||||
    let socket = UdpSocket::bind("0.0.0.0:0");
 | 
			
		||||
    let message = "Packet received";
 | 
			
		||||
    socket.expect("REASON").send_to(message.as_bytes(), uaddr);
 | 
			
		||||
 | 
			
		||||
    // In non-debug mode, print the session line and any additional lines.
 | 
			
		||||
    if !cli.debug {
 | 
			
		||||
        print_session_line(×tamp, &source, &final_destination, &summary);
 | 
			
		||||
@@ -403,7 +422,19 @@ fn handle_frame(frame: &AgwFrame, cli: &Cli, buffers: &mut BufferManager) {
 | 
			
		||||
fn main() -> Result<()> {
 | 
			
		||||
    let cli = Cli::parse();
 | 
			
		||||
    let addr = format!("{}:{}", cli.ip, cli.port);
 | 
			
		||||
    //let uaddr = format!("{}:{}", cli.uip, cli.uport);
 | 
			
		||||
    let reconnect_delay_ms = 5000;
 | 
			
		||||
/*    
 | 
			
		||||
    if cli.udp {
 | 
			
		||||
        // Bind the client socket to any available address and port
 | 
			
		||||
        let socket = UdpSocket::bind("0.0.0.0:0")?;
 | 
			
		||||
        println!("UDP client started");
 | 
			
		||||
        // Send a message to the server
 | 
			
		||||
        let message = "UDP client connected";
 | 
			
		||||
        socket.send_to(message.as_bytes(), uaddr)?;
 | 
			
		||||
        //println!("Sent message to {}: {}", uaddr, message);
 | 
			
		||||
    }
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
    loop {
 | 
			
		||||
        println!("Connecting to AGWPE server at {addr}");
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user