Skip to main content

Crate udpopt

Crate udpopt 

Source
Expand description

github crates-io docs-rs


A high-performance synchronous and asynchronous UDP testing toolkit for sending and receiving UDP packets with precise timing and bitrate control.

§Details

  • Use udpopt::UdpClient to represent the test client
use std::net::UdpSocket;
use std::sync::mpsc;
use std::thread;
use std::time::Duration;
use udpopt::UdpClient;
use udpopt::ClientCommand;

fn main() {
     
     
    // Connect to server address

     
    let (tx, rx) = mpsc::channel();

    // Create a UDP client instance
    let mut client = UdpClient::new(
        1_000_000.0,          // bitrate in bits per second
        1200,                 // payload size in bytes
        Duration::from_secs(5), // send duration
        rx,                   // control receiver
    );

    // Spawn the client in a separate thread
    let handle = thread::spawn(move || {
        client.run(&mut sock).unwrap();
    });

    // Send start command
    tx.send(ClientCommand::Start).unwrap();

    // Wait for client to finish
    handle.join().unwrap();
}

§

  • After the duration is done the after 5 seconds the otput should be
  Client done. Sent 'Number of packets' packets (+FIN)
  • Use udpopt::UdpServer to represent the test server
use std::net::UdpSocket;
use std::sync::mpsc;
use std::thread;
use std::time::Duration;
use udpopt::UdpServer;
use udpopt::ServerCommand;

fn main()  {
     

    // Create a channel for controlling the server (start/stop)
    let (tx, rx) = mpsc::channel();

    // Create a server that saves results every 5 seconds
    let mut server = UdpServer::new(Duration::from_secs(5), rx);

    // Run the server in a separate thread
    let handle = thread::spawn(move || server.run(&mut sock));

    // Start the test
    tx.send(ServerCommand::Start).unwrap();

    // Simulate running for a while
    thread::sleep(Duration::from_secs(1));

    // Stop the server
    tx.send(ServerCommand::Stop).unwrap();

    // Wait for the server thread to finish
    let result = handle.join().unwrap();

     
}
  • This module defines the TestResult struct, which aggregates multiple IntervalResult measurements that results from UdpServer::run into final performance metrics — total packets, bitrate, jitter, and more. Simulated interval results (normally collected by a UDP server)
use std::time::Duration;
use udpopt::IntervalResult;
use udpopt::TestResult;


// Aggregate the results
let result = TestResult::from_intervals(&intervals);
println!("Total lost: {}", result.total_lost);
println!("Total bytes: {}", result.total_bytes);
println!("Total out of order: {}", result.total_out_of_order);
println!("Total packets: {}", result.total_packets);
println!("Mean bitrate: {:.2} bps", result.mean_bitrate);
println!("Median jitter: {:.2} ms", result.median_jitter);
  • This produces an output similar to:
Total lost: 80
Total bytes: 2450000
Total out of order: 1
Total packets: 1920
Mean bitrate: 9.60e6 bps
Median jitter: 1.00 ms

Modules§

ui

Structs§

AsyncUdpClient
Asynchronous UDP client for high-throughput packet sending.
AsyncUdpServer
Asynchronous UDP Server for high-throughput packet receiving.
IntervalResult
Statistics for a given interval
TestResult
Final aggregated test statistics computed from a list of IntervalResults.
UdpClient
UdpServer

Enums§

ClientCommand
Commands that control the UDP client behavior.
ServerCommand
Commands that control the UDP server behavior.
UdpOptError