use super::{Offered, Topic};
use crate::envelope::Signed;
use crate::{Error, Result, cbor};
use futures_util::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
use serde::{Deserialize, Serialize};
pub const PROTOCOL: &str = "/weirdnet/updates/1";
pub const MAX_FRAME: usize = 64 * 1024;
pub const MAX_TOPICS: usize = 64;
pub const MAX_PUBLISH: usize = 32;
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ToCarrier {
Subscribe {
topics: Vec<Topic>,
#[serde(default)]
all: bool,
},
Publish {
envelopes: Vec<Signed>,
},
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum FromCarrier {
Subscribed {
keepalive_s: u32,
},
Envelope(Signed),
Keepalive,
Published(Vec<std::result::Result<Offered, String>>),
Error(String),
}
pub async fn write<W: AsyncWrite + Unpin, T: Serialize>(w: &mut W, msg: &T) -> Result<()> {
let body = cbor::to_vec(msg)?;
if body.len() > MAX_FRAME {
return Err(Error::invalid("frame", "too large"));
}
let mut b = (body.len() as u32).to_be_bytes().to_vec();
b.extend(body);
w.write_all(&b).await.map_err(io)?;
w.flush().await.map_err(io)
}
pub async fn read<R: AsyncRead + Unpin, T: for<'de> Deserialize<'de>>(r: &mut R) -> Result<Option<T>> {
let mut len = [0u8; 4];
match r.read_exact(&mut len).await {
Ok(()) => {}
Err(e) if e.kind() == std::io::ErrorKind::UnexpectedEof => return Ok(None),
Err(e) => return Err(io(e)),
}
let n = u32::from_be_bytes(len) as usize;
if n == 0 || n > MAX_FRAME {
return Err(Error::Decode("bad frame length".into()));
}
let mut body = vec![0u8; n];
r.read_exact(&mut body).await.map_err(io)?;
cbor::from_slice(&body).map(Some)
}
fn io(e: std::io::Error) -> Error {
Error::Decode(format!("stream: {e}"))
}
pub struct Subscription<S> {
io: S,
pub keepalive_s: u32,
}
impl<S: AsyncRead + AsyncWrite + Unpin> Subscription<S> {
pub async fn start(mut io: S, topics: Vec<Topic>, all: bool) -> Result<Self> {
if topics.len() > MAX_TOPICS || (topics.is_empty() && !all) {
return Err(Error::invalid("subscription", format!("1 to {MAX_TOPICS} topics, or all")));
}
write(&mut io, &ToCarrier::Subscribe { topics, all }).await?;
match read(&mut io).await? {
Some(FromCarrier::Subscribed { keepalive_s }) => Ok(Self { io, keepalive_s }),
Some(FromCarrier::Error(e)) => Err(Error::invalid("subscription", e)),
_ => Err(Error::Decode("unexpected answer to subscribe".into())),
}
}
pub async fn next(&mut self) -> Result<Option<Signed>> {
loop {
match read(&mut self.io).await? {
None => return Ok(None),
Some(FromCarrier::Envelope(e)) => return Ok(Some(e)),
Some(FromCarrier::Keepalive) => continue,
Some(FromCarrier::Error(e)) => return Err(Error::invalid("carrier", e)),
Some(_) => return Err(Error::Decode("unexpected frame".into())),
}
}
}
}
pub async fn publish<S: AsyncRead + AsyncWrite + Unpin>(mut io: S, envelopes: Vec<Signed>) -> Result<Vec<std::result::Result<Offered, String>>> {
if envelopes.is_empty() || envelopes.len() > MAX_PUBLISH {
return Err(Error::invalid("publish", format!("1 to {MAX_PUBLISH} envelopes")));
}
write(&mut io, &ToCarrier::Publish { envelopes }).await?;
let r = read(&mut io).await?;
let _ = io.close().await;
match r {
Some(FromCarrier::Published(v)) => Ok(v),
Some(FromCarrier::Error(e)) => Err(Error::invalid("carrier", e)),
_ => Err(Error::Decode("unexpected answer to publish".into())),
}
}