Hello everybody. I'm pretty new to the rust language and have recently begun working with tokio. I'm currently working on a project where I need to communicate with a server. However the Server can send me data and I can send data to the server independently. Therefore I tried to use tokio::split(), however it seems I can't read from the readhalf. If i get a message I get stuck in an infinit loop of reading 0 bytes. Here's the code:
async fn handle_connection(mut reader: ReadHalf<TlsStream<TcpStream>>) {
let mut message = String::new();
loop {
match reader.read_to_string(&mut message).await {
Ok(_len) => {
println!("{}", message);
message = String::new();
},
Err(e) => {
eprintln!("Lost connection to host. Error {}", e);
return;
}
}
}
}
it gets called from the connect function:
async fn connect_to_master(stream: TlsStream<TcpStream>) {
let message = write_netstring(ICINGA_HELLO);
let (reader, mut writer) = tokio::io::split(stream);
writer.write(message.as_bytes()).await;
writer.flush().await;
tokio::spawn(async { handle_connection(reader).await });
loop {
sleep(Duration::from_secs(20));
writer
.write(write_netstring(ICINGA_HEARTBEAT).as_bytes())
.await;
writer.flush().await;
}
}