New Protocol Buffers message reading and writing functions
(conn: &mut T)
| 308 | |
| 309 | // New Protocol Buffers message reading and writing functions |
| 310 | pub async fn read_message<T: AsyncRead + Unpin>(conn: &mut T) -> Result<message::Message> { |
| 311 | let mut buf = [0u8; std::mem::size_of::<u32>()]; |
| 312 | conn.read_exact(&mut buf).await?; |
| 313 | let len = u32::from_le_bytes(buf) as usize; |
| 314 | if !(1..=1024 * 1024 * 10).contains(&len) { |
| 315 | bail!("Invalid message length: {}", len); |
| 316 | } |
| 317 | let mut buf = vec![0u8; len]; |
| 318 | conn.read_exact(&mut buf).await?; |
| 319 | |
| 320 | let proto_msg = |
| 321 | Message::decode(buf.as_slice()).context("Failed to decode Protocol Buffers message")?; |
| 322 | |
| 323 | Ok(proto_msg.message.unwrap()) |
| 324 | } |
| 325 | |
| 326 | pub async fn write_message<T: AsyncWrite + Unpin>( |
| 327 | conn: &mut T, |
no outgoing calls
no test coverage detected