feat: merge the sender and severity in the DDS payload into the same field to simplify the protocol (#871)

This commit is contained in:
sxyazi 2024-04-04 02:33:58 +08:00
parent 1b030e0f52
commit d04b549f4e
No known key found for this signature in database
6 changed files with 39 additions and 26 deletions

View file

@ -76,7 +76,7 @@ impl<'a> Body<'a> {
} }
#[inline] #[inline]
pub fn with_severity(self, severity: u8) -> Payload<'a> { pub fn with_severity(self, severity: u16) -> Payload<'a> {
Payload::new(self).with_severity(severity) Payload::new(self).with_severity(severity)
} }
} }

View file

@ -9,13 +9,12 @@ use crate::{body::Body, ID};
#[derive(Debug)] #[derive(Debug)]
pub struct Payload<'a> { pub struct Payload<'a> {
pub receiver: u64, pub receiver: u64,
pub severity: u8,
pub sender: u64, pub sender: u64,
pub body: Body<'a>, pub body: Body<'a>,
} }
impl<'a> Payload<'a> { impl<'a> Payload<'a> {
pub(super) fn new(body: Body<'a>) -> Self { Self { receiver: 0, severity: 0, sender: *ID, body } } pub(super) fn new(body: Body<'a>) -> Self { Self { receiver: 0, sender: *ID, body } }
pub(super) fn flush(&self) { writeln!(std::io::stdout(), "{self}").ok(); } pub(super) fn flush(&self) { writeln!(std::io::stdout(), "{self}").ok(); }
@ -38,8 +37,8 @@ impl<'a> Payload<'a> {
self self
} }
pub(super) fn with_severity(mut self, severity: u8) -> Self { pub(super) fn with_severity(mut self, severity: u16) -> Self {
self.severity = severity; self.sender = severity as u64;
self self
} }
} }
@ -55,22 +54,19 @@ impl FromStr for Payload<'_> {
type Err = anyhow::Error; type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut parts = s.splitn(5, ','); let mut parts = s.splitn(4, ',');
let kind = parts.next().ok_or_else(|| anyhow!("empty kind"))?; let kind = parts.next().ok_or_else(|| anyhow!("empty kind"))?;
let receiver = let receiver =
parts.next().and_then(|s| s.parse().ok()).ok_or_else(|| anyhow!("invalid receiver"))?; parts.next().and_then(|s| s.parse().ok()).ok_or_else(|| anyhow!("invalid receiver"))?;
let severity =
parts.next().and_then(|s| s.parse().ok()).ok_or_else(|| anyhow!("invalid severity"))?;
let sender = let sender =
parts.next().and_then(|s| s.parse().ok()).ok_or_else(|| anyhow!("invalid sender"))?; parts.next().and_then(|s| s.parse().ok()).ok_or_else(|| anyhow!("invalid sender"))?;
let body = parts.next().ok_or_else(|| anyhow!("empty body"))?; let body = parts.next().ok_or_else(|| anyhow!("empty body"))?;
Ok(Self { receiver, severity, sender, body: Body::from_str(kind, body)? }) Ok(Self { receiver, sender, body: Body::from_str(kind, body)? })
} }
} }
@ -92,7 +88,7 @@ impl Display for Payload<'_> {
}; };
if let Ok(s) = result { if let Ok(s) = result {
write!(f, "{},{},{},{},{s}", self.body.kind(), self.receiver, self.severity, self.sender) write!(f, "{},{},{},{s}", self.body.kind(), self.receiver, self.sender)
} else { } else {
Err(std::fmt::Error) Err(std::fmt::Error)
} }

View file

@ -80,7 +80,7 @@ impl Pubsub {
} }
} }
pub fn pub_static(severity: u8, body: Body) { pub fn pub_static(severity: u16, body: Body) {
let (kind, peers) = (body.kind(), PEERS.read()); let (kind, peers) = (body.kind(), PEERS.read());
if peers.values().any(|c| c.able(kind)) { if peers.values().any(|c| c.able(kind)) {
Client::push(body.with_severity(severity)); Client::push(body.with_severity(severity));
@ -100,7 +100,7 @@ impl Pubsub {
Self::pub_(BodyCd::dummy(tab)); Self::pub_(BodyCd::dummy(tab));
} }
if PEERS.read().values().any(|p| p.able("cd")) { if PEERS.read().values().any(|p| p.able("cd")) {
Client::push(BodyCd::borrowed(tab, url).with_severity(10)); Client::push(BodyCd::borrowed(tab, url).with_severity(100));
} }
if BOOT.local_events.contains("cd") { if BOOT.local_events.contains("cd") {
BodyCd::borrowed(tab, url).with_receiver(*ID).flush(); BodyCd::borrowed(tab, url).with_receiver(*ID).flush();
@ -112,7 +112,7 @@ impl Pubsub {
Self::pub_(BodyHover::dummy(tab)); Self::pub_(BodyHover::dummy(tab));
} }
if PEERS.read().values().any(|p| p.able("hover")) { if PEERS.read().values().any(|p| p.able("hover")) {
Client::push(BodyHover::borrowed(tab, url).with_severity(20)); Client::push(BodyHover::borrowed(tab, url).with_severity(200));
} }
if BOOT.local_events.contains("hover") { if BOOT.local_events.contains("hover") {
BodyHover::borrowed(tab, url).with_receiver(*ID).flush(); BodyHover::borrowed(tab, url).with_receiver(*ID).flush();
@ -136,7 +136,7 @@ impl Pubsub {
Self::pub_(BodyYank::dummy()); Self::pub_(BodyYank::dummy());
} }
if PEERS.read().values().any(|p| p.able("yank")) { if PEERS.read().values().any(|p| p.able("yank")) {
Client::push(BodyYank::borrowed(cut, urls).with_severity(30)); Client::push(BodyYank::borrowed(cut, urls).with_severity(300));
} }
if BOOT.local_events.contains("yank") { if BOOT.local_events.contains("yank") {
BodyYank::borrowed(cut, urls).with_receiver(*ID).flush(); BodyYank::borrowed(cut, urls).with_receiver(*ID).flush();

View file

@ -46,7 +46,7 @@ impl Server {
let Some(id) = id else { continue }; let Some(id) = id else { continue };
let Some(kind) = parts.next() else { continue }; let Some(kind) = parts.next() else { continue };
let Some(receiver) = parts.next().and_then(|s| s.parse().ok()) else { continue }; let Some(receiver) = parts.next().and_then(|s| s.parse().ok()) else { continue };
let Some(severity) = parts.next().and_then(|s| s.parse::<u8>().ok()) else { continue }; let Some(sender) = parts.next().and_then(|s| s.parse::<u64>().ok()) else { continue };
let clients = CLIENTS.read(); let clients = CLIENTS.read();
let clients: Vec<_> = if receiver == 0 { let clients: Vec<_> = if receiver == 0 {
@ -61,9 +61,9 @@ impl Server {
continue; continue;
} }
if receiver == 0 && severity > 0 { if receiver == 0 && sender > 0 && sender <= u16::MAX as u64 {
let Some(body) = parts.next() else { continue }; let Some(body) = parts.next() else { continue };
STATE.add(format!("{}_{severity}_{kind}", Body::tab(kind, body)), &line); if !STATE.set(kind, sender as u16, body) { continue }
} }
line.push('\n'); line.push('\n');
@ -95,6 +95,10 @@ impl Server {
let Ok(payload) = Payload::from_str(&s) else { return }; let Ok(payload) = Payload::from_str(&s) else { return };
let Body::Hi(hi) = payload.body else { return }; let Body::Hi(hi) = payload.body else { return };
if payload.sender <= u16::MAX as u64 {
return; // The kind of static messages cannot be "hi"
}
if id.is_none() { if id.is_none() {
if let Some(ref state) = *STATE.read() { if let Some(ref state) = *STATE.read() {
state.values().for_each(|s| _ = tx.send(s.clone())); state.values().for_each(|s| _ = tx.send(s.clone()));

View file

@ -23,11 +23,25 @@ impl Deref for State {
} }
impl State { impl State {
pub fn add(&self, key: String, value: &str) { pub fn set(&self, kind: &str, severity: u16, body: &str) -> bool {
if let Some(ref mut inner) = *self.inner.write() { let Some(inner) = &mut *self.inner.write() else { return false };
inner.insert(key, value.to_owned()); let key = format!("{}_{severity}_{kind}", Body::tab(kind, body));
self.last.store(timestamp_us(), Ordering::Relaxed);
if body == "null" {
return inner
.remove(&key)
.map(|_| self.last.store(timestamp_us(), Ordering::Relaxed))
.is_some();
} }
let value = format!("{kind},0,{severity},{body}\n");
if inner.get(&key).is_some_and(|s| *s == value) {
return false;
}
inner.insert(key, value);
self.last.store(timestamp_us(), Ordering::Relaxed);
true
} }
pub async fn load_or_create(&self) { pub async fn load_or_create(&self) {
@ -69,11 +83,10 @@ impl State {
let mut inner = HashMap::new(); let mut inner = HashMap::new();
while buf.read_line(&mut line).await? > 0 { while buf.read_line(&mut line).await? > 0 {
let mut parts = line.splitn(5, ','); let mut parts = line.splitn(4, ',');
let Some(kind) = parts.next() else { continue }; let Some(kind) = parts.next() else { continue };
let Some(_) = parts.next() else { continue }; let Some(_) = parts.next() else { continue };
let Some(severity) = parts.next().and_then(|s| s.parse::<u8>().ok()) else { continue }; let Some(severity) = parts.next().and_then(|s| s.parse::<u16>().ok()) else { continue };
let Some(_) = parts.next() else { continue };
let Some(body) = parts.next() else { continue }; let Some(body) = parts.next() else { continue };
inner.insert(format!("{}_{severity}_{kind}", Body::tab(kind, body)), mem::take(&mut line)); inner.insert(format!("{}_{severity}_{kind}", Body::tab(kind, body)), mem::take(&mut line));
} }

View file

@ -27,7 +27,7 @@ impl Pubsub {
ps.raw_set( ps.raw_set(
"pub_static", "pub_static",
lua.create_function(|_, (severity, kind, value): (u8, mlua::String, Value)| { lua.create_function(|_, (severity, kind, value): (u16, mlua::String, Value)| {
if severity < 1 { if severity < 1 {
return Err("Severity must be at least 1").into_lua_err(); return Err("Severity must be at least 1").into_lua_err();
} }