use std::{ io::{self, Read, Write,}, }; use crate::{ multicast, }; mod error; pub use error::*; pub struct LeakyPipe<'a, T, U> where T: Read, U: Write { oneesan: &'a mut T, imouto: multicast::MulticastStream<&'a mut U> } impl<'a, T, U> LeakyPipe<'a, T, U> where T: Read, U: Write { pub fn new(oneesan: &'a mut T, imouto: multicast::MulticastStream<&'a mut U>) -> Self { Self { oneesan, imouto, } } pub fn pipe(mut self, buf_size: usize, output_to_tty: bool) -> Result<(), Error> { let mut buffer = vec![0u8; buf_size]; let mut bytes_done=0; let stdout = io::stdout(); let mut stdout = stdout.lock(); loop { let sz = self.oneesan.read(&mut buffer[..]).try_oneesan(bytes_done)?; if sz == 0 {return Ok(());} self.imouto.write(&buffer[0..sz]).try_imouto(bytes_done)?; if output_to_tty { stdout.write(&buffer[0..sz])?; } bytes_done+=sz; } } }