Moving line reading into its own function
This commit is contained in:
parent
625fb09c8b
commit
eeffe7420f
1 changed files with 16 additions and 21 deletions
|
|
@ -255,38 +255,34 @@ impl<T: Read+Write> Client<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_response(&mut self) -> Result<Vec<String>> {
|
fn read_response(&mut self) -> Result<Vec<String>> {
|
||||||
//Carriage return
|
|
||||||
let cr = 0x0d;
|
|
||||||
//Line Feed
|
|
||||||
let lf = 0x0a;
|
|
||||||
let mut found_tag_line = false;
|
let mut found_tag_line = false;
|
||||||
let start_str = format!("{}{} ", TAG_PREFIX, self.tag);
|
let start_str = format!("{}{} ", TAG_PREFIX, self.tag);
|
||||||
let mut lines: Vec<String> = Vec::new();
|
let mut lines: Vec<String> = Vec::new();
|
||||||
|
|
||||||
while !found_tag_line {
|
while !found_tag_line {
|
||||||
let mut line_buffer: Vec<u8> = Vec::new();
|
match self.readline() {
|
||||||
while line_buffer.len() < 2 || (line_buffer[line_buffer.len()-1] != lf && line_buffer[line_buffer.len()-2] != cr) {
|
Ok(raw_data) => {
|
||||||
let byte_buffer: &mut [u8] = &mut [0];
|
let line = String::from_utf8(raw_data).unwrap();
|
||||||
match self.stream.read(byte_buffer) {
|
|
||||||
Ok(_) => {},
|
|
||||||
Err(_) => return Err(Error::new(ErrorKind::Other, "Failed to read the response")),
|
|
||||||
}
|
|
||||||
line_buffer.push(byte_buffer[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
let line = String::from_utf8(line_buffer).unwrap();
|
|
||||||
|
|
||||||
lines.push(line.clone());
|
lines.push(line.clone());
|
||||||
|
|
||||||
if (&*line).starts_with(&*start_str) {
|
if (&*line).starts_with(&*start_str) {
|
||||||
found_tag_line = true;
|
found_tag_line = true;
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
Err(err) => return Err(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(lines)
|
Ok(lines)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_greeting(&mut self) -> Result<()> {
|
fn read_greeting(&mut self) -> Result<()> {
|
||||||
|
match self.readline() {
|
||||||
|
Ok(_) => Ok(()),
|
||||||
|
Err(err) => Err(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn readline(&mut self) -> Result<Vec<u8>> {
|
||||||
//Carriage return
|
//Carriage return
|
||||||
let cr = 0x0d;
|
let cr = 0x0d;
|
||||||
//Line Feed
|
//Line Feed
|
||||||
|
|
@ -301,8 +297,7 @@ impl<T: Read+Write> Client<T> {
|
||||||
}
|
}
|
||||||
line_buffer.push(byte_buffer[0]);
|
line_buffer.push(byte_buffer[0]);
|
||||||
}
|
}
|
||||||
|
Ok(line_buffer)
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_command(&mut self, command: String) -> String {
|
fn create_command(&mut self, command: String) -> String {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue