Wire format: [4-byte length (big-endian)][MessagePack payload]
(writer: &mut W, msg: &T)
| 32 | |
| 33 | /// Wire format: [4-byte length (big-endian)][MessagePack payload] |
| 34 | pub async fn write_message<W, T>(writer: &mut W, msg: &T) -> std::io::Result<()> |
| 35 | where |
| 36 | W: AsyncWriteExt + Unpin, |
| 37 | T: Serialize, |
| 38 | { |
| 39 | let payload = rmp_serde::to_vec(msg) |
| 40 | .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?; |
| 41 | |
| 42 | let len = payload.len() as u32; |
| 43 | writer.write_all(&len.to_be_bytes()).await?; |
| 44 | writer.write_all(&payload).await?; |
| 45 | writer.flush().await?; |
| 46 | Ok(()) |
| 47 | } |
| 48 | |
| 49 | pub async fn read_message<R, T>(reader: &mut R) -> std::io::Result<T> |
| 50 | where |