iuna

iuna - experimental devnet protocol
git clone https://iuna.jhx.app/git/iuna.git
Log | Files | Refs | README | LICENSE

writer.rs (832B)


      1 use anyhow::Result;
      2 use tokio::{io::AsyncWriteExt, net::tcp::OwnedWriteHalf};
      3 
      4 use crate::app::GossipEnvelope;
      5 
      6 use super::MAX_GOSSIP_LINE_BYTES;
      7 
      8 pub(super) async fn write_payload(
      9     writer: &mut OwnedWriteHalf,
     10     payload: &[GossipEnvelope],
     11 ) -> Result<()> {
     12     for envelope in payload {
     13         write_envelope(writer, envelope).await?;
     14     }
     15     Ok(())
     16 }
     17 
     18 pub(super) async fn write_envelope(
     19     writer: &mut OwnedWriteHalf,
     20     envelope: &GossipEnvelope,
     21 ) -> Result<()> {
     22     let line = serde_json::to_string(envelope)?;
     23     if line.len() > MAX_GOSSIP_LINE_BYTES {
     24         anyhow::bail!(
     25             "p2p message is {} bytes, exceeding {} byte limit",
     26             line.len(),
     27             MAX_GOSSIP_LINE_BYTES
     28         );
     29     }
     30     writer.write_all(line.as_bytes()).await?;
     31     writer.write_all(b"\n").await?;
     32     Ok(())
     33 }