process in multiple threads

This commit is contained in:
Henry Case 2019-07-26 14:02:54 +01:00
parent e62e9262be
commit 9b095a7938
2 changed files with 42 additions and 41 deletions

View File

@ -7,6 +7,4 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
ctrlc = "3.1.3"
thread-control = "0.1.2"
clap = "2.33.0" clap = "2.33.0"

View File

@ -3,8 +3,6 @@ use std::net::*;
use std::time::Duration; use std::time::Duration;
use std::thread; use std::thread;
use std::time; use std::time;
use ctrlc;
use thread_control::*;
use clap::{Arg, App}; use clap::{Arg, App};
static CH_X25519: &'static [u8] = b"\ static CH_X25519: &'static [u8] = b"\
@ -204,15 +202,6 @@ fn sleep_no_comment(millis: u64) {
} }
fn main() -> std::io::Result<()> { fn main() -> std::io::Result<()> {
// Condition variables to control threads
let (flag_t1, control_t1) = make_pair();
ctrlc::set_handler(move || {
println!("Stopping...");
control_t1.stop();
}).expect("Error setting Ctrl-C handler");
let matches = App::new("rawhammer") let matches = App::new("rawhammer")
.version("0.0.1") .version("0.0.1")
.author("Kris Kwiatkowski <kris@amongbytes.com>") .author("Kris Kwiatkowski <kris@amongbytes.com>")
@ -241,6 +230,12 @@ fn main() -> std::io::Result<()> {
.takes_value(true) .takes_value(true)
.required(false) .required(false)
.help("Number of connections sent per second (default 100)")) .help("Number of connections sent per second (default 100)"))
.arg(Arg::with_name("threads")
.short("s")
.long("sending_threads")
.takes_value(true)
.required(false)
.help("Number of sending threads (default 1)"))
.get_matches(); .get_matches();
let sock_addr: SocketAddr = matches let sock_addr: SocketAddr = matches
@ -252,16 +247,20 @@ fn main() -> std::io::Result<()> {
.parse().unwrap(), 0); .parse().unwrap(), 0);
let msg_per_sec: usize = matches.value_of("frequency").unwrap_or("100").parse().unwrap(); let msg_per_sec: usize = matches.value_of("frequency").unwrap_or("100").parse().unwrap();
let ch_case: usize = matches.value_of("case").unwrap().parse().unwrap(); let ch_case: usize = matches.value_of("case").unwrap().parse().unwrap();
let sending_threads: usize = matches.value_of("threads").unwrap_or("1").parse().unwrap();
println!("Sending to {0}, with freq {1} per sec.", sock_addr, msg_per_sec); println!("Sending to {0}, with freq {1} per sec on {2} threads.", sock_addr, msg_per_sec, sending_threads);
// Start a thread which sends CH // Start a thread which sends CH
let _t1 = thread::spawn(move || { let mut threads = Vec::new();
for _ in 0..sending_threads {
// Condition variables to control threads
let t = thread::spawn(move || {
// calculates number of sent client hellos // calculates number of sent client hellos
let mut ch_sent = 0; let mut ch_sent = 0;
// Sent until CTRL+C // Sent until CTRL+C
while flag_t1.is_alive() { loop {
// try connect // try connect
let sock = TcpStream::connect_timeout(&sock_addr, dur); let sock = TcpStream::connect_timeout(&sock_addr, dur);
if sock.is_err() { if sock.is_err() {
@ -284,8 +283,12 @@ fn main() -> std::io::Result<()> {
} }
} }
}); });
threads.push(t);
}
// wait for TX thread to finish // wait for TX thread to finish
_t1.join().unwrap(); for t in threads {
t.join().unwrap();
}
Ok(()) Ok(())
} }