Read a length-prefixed frame from the stream. Returns `None` on EOF.
(stream: &mut TcpStream)
| 232 | /// Read a length-prefixed frame from the stream. |
| 233 | /// Returns `None` on EOF. |
| 234 | async fn read_frame(stream: &mut TcpStream) -> Option<Vec<u8>> { |
| 235 | let mut len_buf = [0u8; FRAME_HEADER_LEN]; |
| 236 | match stream.read_exact(&mut len_buf).await { |
| 237 | Ok(_) => {} |
| 238 | Err(e) if e.kind() == std::io::ErrorKind::UnexpectedEof => return None, |
| 239 | Err(e) if e.kind() == std::io::ErrorKind::ConnectionReset => return None, |
| 240 | Err(e) => panic!("read_frame error: {e}"), |
| 241 | } |
| 242 | let payload_len = u32::from_be_bytes(len_buf) as usize; |
| 243 | let mut payload = vec![0u8; payload_len]; |
| 244 | stream.read_exact(&mut payload).await.expect("read payload"); |
| 245 | Some(payload) |
| 246 | } |
| 247 | |
| 248 | // ─── Tests ────────────────────────────────────────────────────────────────── |
| 249 |
no test coverage detected