From ac5d6475f10b53daf31b5951de338bc44f0ebeed Mon Sep 17 00:00:00 2001 From: mars Date: Sun, 20 Nov 2022 11:24:58 -0700 Subject: [PATCH] Add Messenger::send_async() --- apps/magpie/src/protocol.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) 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];