read tcp replies from server
This commit is contained in:
parent
4842f18b34
commit
6ce84ddfff
2 changed files with 35 additions and 12 deletions
17
src/main.rs
17
src/main.rs
|
@ -65,10 +65,19 @@ fn main() -> anyhow::Result<()> {
|
|||
println!("+++++++++++++++++++++++++++++++++");
|
||||
let size = mdm.tcp_receive_reply_len()?;
|
||||
println!("{}", size);
|
||||
//println!("+++++++++++++++++++++++++++++++++");
|
||||
//let reply = mdm.tcp_receive(size)?;
|
||||
//println!("{}", reply);
|
||||
//println!("+++++++++++++++++++++++++++++++++");
|
||||
println!("+++++++++++++++++++++++++++++++++");
|
||||
let mut reply = String::new();
|
||||
loop {
|
||||
let s = mdm.tcp_receive_payload(300)?;
|
||||
if &s == "" {
|
||||
break;
|
||||
}
|
||||
else {
|
||||
reply.push_str(&s);
|
||||
}
|
||||
};
|
||||
println!("REPLY({}) = {}", reply.len(), reply);
|
||||
println!("+++++++++++++++++++++++++++++++++");
|
||||
let _ = mdm.tcp_close_connection()?;
|
||||
}
|
||||
|
||||
|
|
30
src/modem.rs
30
src/modem.rs
|
@ -261,23 +261,37 @@ impl<UART: serial::Uart> Modem<UART> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn tcp_parse_payload_size(&mut self, reply_line: &str) -> Result<usize> {
|
||||
reply_line.split(',')
|
||||
.into_iter()
|
||||
.last()
|
||||
.unwrap_or("x")
|
||||
.parse::<usize>()
|
||||
.map_err(|_| ModemError::CommandError(format!("response size should be a number, got {}", reply_line)))
|
||||
}
|
||||
|
||||
pub fn tcp_receive_reply_len(&mut self) -> Result<usize> {
|
||||
let reply = self.send_command(Command::tcp_receive_query_len())?;
|
||||
reply.lines()
|
||||
.filter(|line| line.contains("+CIPRXGET: 4"))
|
||||
.next()
|
||||
.ok_or(ModemError::CommandError("reply not found :/".to_string()))
|
||||
.map(|line| {
|
||||
line.split(',')
|
||||
.into_iter()
|
||||
.last()
|
||||
.unwrap_or("x")
|
||||
.parse::<usize>()
|
||||
.map_err(|_| ModemError::CommandError(format!("response size should be a number, got {}", line)))
|
||||
})
|
||||
.map(|line| self.tcp_parse_payload_size(line))
|
||||
.unwrap_or(Err(ModemError::CommandError(format!("received 0 elements from parsing"))))
|
||||
}
|
||||
|
||||
pub fn tcp_receive_payload(&mut self, size: usize) -> Result<String> {
|
||||
self.send_command(Command::tcp_receive(size))
|
||||
.map(|reply| {
|
||||
reply
|
||||
.split("\r\n")
|
||||
.filter(|line| line.len() > 2 && !line.contains("+CIPRXGET: 2,"))
|
||||
.last()
|
||||
.map(String::from)
|
||||
.unwrap_or("".to_string())
|
||||
})
|
||||
}
|
||||
|
||||
pub fn tcp_close_connection(&mut self) -> Result<String> {
|
||||
self.send_command(Command::tcp_close())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue