Bevy+Ratutui powered Monitoring of Pico-Strike devices
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Use async read/write traits

+21 -31
+21 -31
src/net.rs
··· 1 1 use std::{ 2 - io::{Read, Write}, 3 2 net::{SocketAddr, TcpStream}, 4 3 time::Duration, 5 4 }; ··· 9 8 use bevy::{ 10 9 app::{Plugin, Startup}, 11 10 ecs::{resource::Resource, system::Commands}, 12 - tasks::IoTaskPool, 11 + tasks::{ 12 + IoTaskPool, 13 + futures_lite::{AsyncReadExt, AsyncWriteExt}, 14 + }, 13 15 }; 14 16 15 17 use futures_concurrency::future::Race; ··· 126 128 } 127 129 } 128 130 129 - async fn transport_handshake(tcp: &Async<TcpStream>) -> color_eyre::Result<Client> { 131 + async fn transport_handshake(mut tcp: &Async<TcpStream>) -> color_eyre::Result<Client> { 130 132 let (encap, handshake) = ClientBuilder::start(); 131 133 132 134 let msg = encap.serialize(); 133 135 let msg = msg.as_bytes(); 134 136 let msg_len = u16::try_from(msg.len()).unwrap().to_be_bytes(); 135 137 136 - tcp.write_with(|mut tcp| tcp.write_all(&msg_len)).await?; 137 - tcp.write_with(|mut tcp| tcp.write_all(msg)).await?; 138 + tcp.write_all(&msg_len).await?; 139 + tcp.write_all(msg).await?; 138 140 139 141 let mut payload = vec![0u8; 1024]; 140 142 let mut msg_len = [0u8; 2]; 141 143 142 - let msg_len = tcp 143 - .read_with(|mut tcp| { 144 - tcp.read_exact(&mut msg_len)?; 145 - Ok(usize::from(u16::from_be_bytes(msg_len))) 146 - }) 147 - .await?; 144 + tcp.read_exact(&mut msg_len).await?; 145 + 146 + let msg_len = usize::from(u16::from_be_bytes(msg_len)); 148 147 149 - tcp.read_with(|mut tcp| tcp.read_exact(&mut payload[..msg_len])) 150 - .await?; 148 + tcp.read_exact(&mut payload[..msg_len]).await?; 151 149 152 150 let transport = handshake.finish(&payload[..msg_len], &NOISE_PSK)?; 153 151 154 152 Ok(transport) 155 153 } 156 154 157 - async fn recv(stream: &Async<TcpStream>) -> std::io::Result<Vec<u8>> { 155 + async fn recv(mut stream: &Async<TcpStream>) -> std::io::Result<Vec<u8>> { 158 156 let mut msg_len_buf = [0u8; 2]; 159 157 let mut msg = vec![0u8; 4096]; 160 158 161 - stream 162 - .read_with(|mut stream| { 163 - stream.read_exact(&mut msg_len_buf)?; 159 + stream.read_exact(&mut msg_len_buf).await?; 164 160 165 - let msg_len = usize::from(u16::from_be_bytes(msg_len_buf)); 166 - stream.read_exact(&mut msg[..msg_len])?; 161 + let msg_len = usize::from(u16::from_be_bytes(msg_len_buf)); 167 162 168 - msg.truncate(msg_len); 163 + stream.read_exact(&mut msg[..msg_len]).await?; 169 164 170 - Ok(()) 171 - }) 172 - .await?; 165 + msg.truncate(msg_len); 173 166 174 167 Ok(msg) 175 168 } 176 169 177 170 /// Hyper-basic stream transport sender. 16-bit BE size followed by payload. 178 - async fn send(stream: &Async<TcpStream>, buf: &[u8]) -> std::io::Result<()> { 179 - stream 180 - .write_with(|mut stream| { 181 - let len = u16::try_from(buf.len()).expect("message too large"); 182 - stream.write_all(&len.to_be_bytes())?; 183 - stream.write_all(buf) 184 - }) 185 - .await 171 + async fn send(mut stream: &Async<TcpStream>, buf: &[u8]) -> std::io::Result<()> { 172 + let len = u16::try_from(buf.len()).expect("message too large"); 173 + 174 + stream.write_all(&len.to_be_bytes()).await?; 175 + stream.write_all(buf).await 186 176 }