use std::io::{self, BufReader, Read};
use std::thread;
use log::error;
use tokio::sync::mpsc;
pub fn spawn_channel(buffer: usize) -> (thread::JoinHandle<()>, mpsc::Receiver<Vec<u8>>) {
let (tx, rx) = mpsc::channel(1);
let handle = thread::spawn(move || {
let mut stdin = BufReader::new(io::stdin());
let mut buf = vec![0; buffer];
loop {
match stdin.read(&mut buf) {
Ok(0) | Err(_) => break,
Ok(n) => {
if let Err(x) = tx.blocking_send(buf[..n].to_vec()) {
error!("Stdin channel closed: {}", x);
break;
}
thread::yield_now();
}
}
}
});
(handle, rx)
}