👕 refactor
parent
6ef12c5859
commit
d93b3036bb
29
src/main.rs
29
src/main.rs
|
@ -1,5 +1,6 @@
|
|||
use std::{
|
||||
fmt,
|
||||
marker::Send,
|
||||
sync::mpsc,
|
||||
thread,
|
||||
time::Duration,
|
||||
|
@ -26,6 +27,19 @@ struct ThreadSystem <'a>
|
|||
send: mpsc::SyncSender <TaskOutput>,
|
||||
}
|
||||
|
||||
impl <'a> ThreadSystem <'a>
|
||||
{
|
||||
fn dispatch <F: FnOnce () -> TaskOutput + Send + 'static> (&self, work: F)
|
||||
{
|
||||
let send = self.send.clone ();
|
||||
self.pool.spawn (move ||
|
||||
{
|
||||
let output = work ();
|
||||
send.send (output).ok ();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
struct Driver <'a>
|
||||
{
|
||||
thread_sys: ThreadSystem <'a>,
|
||||
|
@ -124,6 +138,7 @@ impl <'a> Driver <'_>
|
|||
let dur_encode = Duration::from_millis (20);
|
||||
let dur_transmit = Duration::from_millis (200);
|
||||
|
||||
let thread_sys = &self.thread_sys;
|
||||
let pool = &self.thread_sys.pool;
|
||||
let send = self.thread_sys.send.clone ();
|
||||
|
||||
|
@ -131,15 +146,14 @@ impl <'a> Driver <'_>
|
|||
{
|
||||
TxPipelineEvent::Capture => {
|
||||
let cap = self.capture.start (())?;
|
||||
|
||||
pool.spawn (move ||
|
||||
thread_sys.dispatch (move ||
|
||||
{
|
||||
thread::sleep (dur_capture);
|
||||
let buf_raw = BufRaw
|
||||
{
|
||||
buf: vec![],
|
||||
};
|
||||
send.send (TaskOutput::TxCapture ((cap, buf_raw))).unwrap ();
|
||||
TaskOutput::TxCapture ((cap, buf_raw))
|
||||
});
|
||||
},
|
||||
TxPipelineEvent::PollEncoder =>
|
||||
|
@ -161,20 +175,19 @@ impl <'a> Driver <'_>
|
|||
TxPipelineEvent::Encode (buf_raw) =>
|
||||
{
|
||||
let mut encoder = self.encoder.start (EncoderTaskMetadata {})?;
|
||||
|
||||
pool.spawn (move ||
|
||||
thread_sys.dispatch (move ||
|
||||
{
|
||||
thread::sleep (dur_encode);
|
||||
encoder.try_handle_insert_data (&buf_raw.buf).unwrap ();
|
||||
send.send (TaskOutput::TxEncode (encoder)).unwrap ();
|
||||
TaskOutput::TxEncode (encoder)
|
||||
});
|
||||
},
|
||||
TxPipelineEvent::Transmit (_buf_enc) => {
|
||||
let tx = self.transmitter.start (())?;
|
||||
pool.spawn (move ||
|
||||
thread_sys.dispatch (move ||
|
||||
{
|
||||
thread::sleep (dur_transmit);
|
||||
send.send (TaskOutput::TxTransmit ((tx, false))).unwrap ();
|
||||
TaskOutput::TxTransmit ((tx, false))
|
||||
});
|
||||
},
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue