small cleanup

This commit is contained in:
Mateusz Faderewski 2024-06-30 12:15:45 +02:00
parent 21db23b22b
commit f669f730da
2 changed files with 8 additions and 11 deletions

View File

@ -325,8 +325,7 @@ impl Wrapper {
} }
fn write_data(&mut self, buffer: &[u8], written: &mut usize) -> std::io::Result<()> { fn write_data(&mut self, buffer: &[u8], written: &mut usize) -> std::io::Result<()> {
*written = 0; let mut transferred = 0;
let mut transferred: i32 = 0;
let result = unsafe { let result = unsafe {
// NOTE: Nasty hack to overcome libftdi1 API limitation. // NOTE: Nasty hack to overcome libftdi1 API limitation.
// Write can partially succeed, but the default ftdi_write_data // Write can partially succeed, but the default ftdi_write_data
@ -340,14 +339,14 @@ impl Wrapper {
100, 100,
) )
}; };
*written += transferred as usize; *written = transferred as usize;
if result < 0 { if result < 0 {
return Err(self.libusb_convert_result(result)); return Err(self.libusb_convert_result(result));
} }
Ok(()) Ok(())
} }
fn try_unclog_pipe(&mut self) -> std::io::Result<()> { fn unclog_pipe(&mut self) -> std::io::Result<()> {
let mut buffer = vec![0u8; self.read_chunksize]; let mut buffer = vec![0u8; self.read_chunksize];
let read = match self.read_data(&mut buffer) { let read = match self.read_data(&mut buffer) {
Ok(read) => read, Ok(read) => read,
@ -368,7 +367,7 @@ impl Wrapper {
self.write_buffer.drain(..written); self.write_buffer.drain(..written);
if let Err(error) = result { if let Err(error) = result {
match error.kind() { match error.kind() {
std::io::ErrorKind::TimedOut => self.try_unclog_pipe()?, std::io::ErrorKind::TimedOut => self.unclog_pipe()?,
_ => return Err(error), _ => return Err(error),
} }
} }
@ -385,16 +384,14 @@ impl Wrapper {
} else if self.unclog_buffer.is_empty() { } else if self.unclog_buffer.is_empty() {
self.read_data(buffer) self.read_data(buffer)
} else { } else {
let mut read = 0; for (index, item) in buffer.iter_mut().enumerate() {
for item in buffer.iter_mut() {
if let Some(byte) = self.unclog_buffer.pop_front() { if let Some(byte) = self.unclog_buffer.pop_front() {
*item = byte; *item = byte;
read += 1;
} else { } else {
break; return Ok(index);
} }
} }
Ok(read) Ok(buffer.len())
} }
} }

View File

@ -419,8 +419,8 @@ fn new_tcp_backend(address: &str) -> Result<TcpBackend, Error> {
let stream = TcpStream::connect(address).map_err(|error| { let stream = TcpStream::connect(address).map_err(|error| {
Error::new(format!("Couldn't connect to [{address}]: {error}").as_str()) Error::new(format!("Couldn't connect to [{address}]: {error}").as_str())
})?; })?;
stream.set_write_timeout(Some(WRITE_TIMEOUT))?;
stream.set_read_timeout(Some(POLL_TIMEOUT))?; stream.set_read_timeout(Some(POLL_TIMEOUT))?;
stream.set_write_timeout(Some(WRITE_TIMEOUT))?;
let reader = BufReader::new(stream.try_clone()?); let reader = BufReader::new(stream.try_clone()?);
let writer = BufWriter::new(stream.try_clone()?); let writer = BufWriter::new(stream.try_clone()?);
Ok(TcpBackend { Ok(TcpBackend {