Dijit

how_many_bytes_per_second.rs

Mar 3rd, 2020
751
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use std::net::UdpSocket;
  2. use std::sync::mpsc::channel;
  3. use std::thread;
  4. use std::time::SystemTime;
  5.  
  6.  
  7. fn main() {
  8.     // Create channels for sending and receieving
  9.     let (one_tx, one_rx) = channel();
  10.     let mut buf = [0; 1000000];
  11.  
  12.     // Spawn one second timer to sync the bytecounter to the screen and flush the values
  13.     thread::spawn(move || {
  14.         let mut now = SystemTime::now();
  15.         let mut bytes_recieved = 0;
  16.         loop {
  17.            match now.elapsed() {
  18.                Ok(elapsed) => {
  19.                    if elapsed.as_millis() > 1000 {
  20.                        println!("Cp/s: {}", &bytes_recieved);
  21.                        bytes_recieved = 0;
  22.                        now = SystemTime::now();
  23.                     } else {
  24.                         let _ = one_rx.try_recv().map(|reply| bytes_recieved += reply);
  25.                     }
  26.                }
  27.                Err(e) => {
  28.                    println!("Error: {:?}", e);
  29.                }
  30.            }
  31.         }
  32.     });
  33.  
  34.     loop {
  35.         let socket = UdpSocket::bind("127.0.0.1:34254").expect("Failed to bind UDP port");
  36.         // Receives a single datagram message on the socket. If `buf` is too small to hold
  37.         // the message, it will be cut off.
  38.         // nbr_bytes/source from the buffer:
  39.         let (amt, _) = socket.recv_from(&mut buf).unwrap();
  40.         one_tx.send(amt).unwrap();
  41.     } // once the stream is closed, buffer is empty; we loop and try again
  42. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×