diff --git a/apps/magpie/src/protocol.rs b/apps/magpie/src/protocol.rs index 133d59f..87c89c6 100644 --- a/apps/magpie/src/protocol.rs +++ b/apps/magpie/src/protocol.rs @@ -214,10 +214,23 @@ impl Messenger { #[cfg(feature = "async")] mod async_messages { use super::*; - use futures_util::AsyncReadExt; + use futures_util::{AsyncReadExt, AsyncWriteExt}; use std::marker::Unpin; - impl Messenger { + impl Messenger { + pub async fn send_async(&mut self, msg: &O) -> std::io::Result<()> { + use byteorder::{LittleEndian, WriteBytesExt}; + let payload = serde_json::to_vec(msg).unwrap(); + let len = payload.len() as u32; + let mut msg = Vec::with_capacity(4 + payload.len()); + msg.write_u32::(len)?; + msg.extend_from_slice(&payload); + self.transport.write_all(&msg).await?; + Ok(()) + } + } + + impl Messenger { pub async fn recv(&mut self) -> std::io::Result { let mut buf = [0u8; 1024];