Skip to content

Commit

Permalink
Extract read_packet() from next_packet()
Browse files Browse the repository at this point in the history
Like in the Java version, expose a "static" read_packet() function, used
by next_packet(). This will allow to test packet reading without an
AdbMonitor instance.
  • Loading branch information
rom1v committed Aug 4, 2018
1 parent 7fd0dc3 commit 5737ce6
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions relay-rust/src/adb_monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,26 @@ impl AdbMonitor {
Ok(ok)
}

fn read_packet(buf: &mut ByteBuffer) -> io::Result<Option<String>> {
let packet_length = Self::available_packet_length(buf.peek())?;
if let Some(len) = packet_length {
// retrieve the content and consume the packet
let data = Self::to_string(&buf.peek()[4..len])?;
buf.consume(len);
Ok(Some(data))
} else {
Ok(None)
}
}

fn next_packet(&mut self, stream: &mut TcpStream) -> io::Result<String> {
loop {
let packet_length = Self::available_packet_length(self.buf.peek())?;
match packet_length {
Some(len) => {
// retrieve the content and consume the packet
let data = Self::to_string(&self.buf.peek()[4..len])?;
self.buf.consume(len);
return Ok(data);
}
// need more data
None => self.fill_buffer_from(stream)?,
};
let packet = Self::read_packet(&mut self.buf)?;
if let Some(packet) = packet {
return Ok(packet);
} else {
self.fill_buffer_from(stream)?;
}
}
}

Expand Down

0 comments on commit 5737ce6

Please sign in to comment.